Ticket #11575: 0001-Add-ActiveRecord-option-to-store-the-full-class-name.patch
| File 0001-Add-ActiveRecord-option-to-store-the-full-class-name.patch, 5.5 kB (added by divoxx, 3 months ago) |
|---|
-
a/activerecord/lib/active_record/base.rb
old new 436 436 cattr_accessor :schema_format , :instance_writer => false 437 437 @@schema_format = :ruby 438 438 439 # Determine whether to store the full constant name including namespace when using STI 440 superclass_delegating_accessor :store_full_sti_class 441 self.store_full_sti_class = false 442 439 443 class << self # Class methods 440 444 # Find operates with four different retrieval approaches: 441 445 # … … 1548 1552 1549 1553 def type_condition 1550 1554 quoted_inheritance_column = connection.quote_column_name(inheritance_column) 1551 type_condition = subclasses.inject("#{quoted_table_name}.#{quoted_inheritance_column} = '#{ name.demodulize}' ") do |condition, subclass|1552 condition << "OR #{quoted_table_name}.#{quoted_inheritance_column} = '#{s ubclass.name.demodulize}' "1555 type_condition = subclasses.inject("#{quoted_table_name}.#{quoted_inheritance_column} = '#{store_full_sti_class ? name : name.demodulize}' ") do |condition, subclass| 1556 condition << "OR #{quoted_table_name}.#{quoted_inheritance_column} = '#{store_full_sti_class ? subclass.name : subclass.name.demodulize}' " 1553 1557 end 1554 1558 1555 1559 " (#{type_condition}) " … … 2468 2472 # Message class in that example. 2469 2473 def ensure_proper_type 2470 2474 unless self.class.descends_from_active_record? 2471 write_attribute(self.class.inheritance_column, Inflector.demodulize(self.class.name))2475 write_attribute(self.class.inheritance_column, store_full_sti_class ? self.class.name : Inflector.demodulize(self.class.name)) 2472 2476 end 2473 2477 end 2474 2478 -
a/activerecord/test/cases/inheritance_test.rb
old new 2 2 require 'models/company' 3 3 require 'models/project' 4 4 require 'models/subscriber' 5 require 'models/collection_item' 5 6 6 7 class InheritanceTest < ActiveRecord::TestCase 7 8 fixtures :companies, :projects, :subscribers, :accounts 9 10 def test_should_store_demodulized_class_name_with_store_full_sti_class_option_disabled 11 item = ComicCollection::Item.new 12 assert_equal 'Item', item[:type] 13 end 14 15 def test_should_store_full_class_name_with_store_full_sti_class_option_enabled 16 ActiveRecord::Base.store_full_sti_class = true 17 item = ComicCollection::Item.new 18 assert_equal 'ComicCollection::Item', item[:type] 19 ActiveRecord::Base.store_full_sti_class = false 20 end 21 22 def test_different_namespace_subclass_should_load_correctly_with_store_full_sti_class_option 23 ActiveRecord::Base.store_full_sti_class = true 24 item = ComicCollection::Item.create :name => "Wolverine 2" 25 assert_not_nil CollectionItem.find(item.id) 26 assert_not_nil ComicCollection::Item.find(item.id) 27 ActiveRecord::Base.store_full_sti_class = false 28 end 8 29 9 30 def test_company_descends_from_active_record 10 31 assert_raise(NoMethodError) { ActiveRecord::Base.descends_from_active_record? } -
/dev/null
old new 1 class CollectionItem < ActiveRecord::Base 2 end 3 4 module ComicCollection 5 class Item < CollectionItem 6 end 7 end -
a/activerecord/test/schema/schema.rb
old new 424 424 t.integer :sponsorable_id 425 425 t.string :sponsorable_type 426 426 end 427 428 429 create_table :collection_items, :force => true do |t| 430 t.string :name 431 t.string :type 432 end 427 433 end -
a/railties/configs/initializers/new_rails_defaults.rb
old new 7 7 # Include ActiveRecord class name as root for JSON serialized output. 8 8 ActiveRecord::Base.include_root_in_json = true 9 9 10 # Store the full class name (including module namespace) in STI type column 11 ActiveRecord::Base.store_full_sti_class = true 12 10 13 # Use ISO 8601 format for JSON serialized times and dates 11 14 ActiveSupport.use_standard_json_time_format = true 12 15