Ticket #6519: nested_sti.patch
| File nested_sti.patch, 2.8 kB (added by jonathan_viney, 2 years ago) |
|---|
-
test/base_test.rb
old new 10 10 require 'fixtures/column_name' 11 11 require 'fixtures/subscriber' 12 12 require 'fixtures/keyboard' 13 require 'fixtures/post' 13 14 14 15 class Category < ActiveRecord::Base; end 15 16 class Smarts < ActiveRecord::Base; end … … 436 437 classes.each(&:reset_table_name) 437 438 end 438 439 440 def test_nested_sti_inheritance_column_value 441 assert_equal "Post::NestedStiPost", Post::NestedStiPost.new[:type] 442 end 443 444 def test_find_nested_sti_from_base_class 445 assert_equal Post::NestedStiPost, Post.find(Post::NestedStiPost.create!.id).class 446 end 447 448 def test_find_nested_sti_from_sti_class 449 nested_sti_post = Post::NestedStiPost.create! 450 assert_equal nested_sti_post, Post::NestedStiPost.find(nested_sti_post.id) 451 end 452 439 453 def test_destroy_all 440 454 assert_equal 2, Topic.count 441 455 -
test/fixtures/post.rb
old new 55 55 56 56 class SubStiPost < StiPost 57 57 end 58 59 class Post::NestedStiPost < Post 60 end -
lib/active_record/base.rb
old new 1165 1165 1166 1166 def type_condition 1167 1167 quoted_inheritance_column = connection.quote_column_name(inheritance_column) 1168 type_condition = subclasses.inject("#{table_name}.#{quoted_inheritance_column} = '#{name .demodulize}' ") do |condition, subclass|1169 condition << "OR #{table_name}.#{quoted_inheritance_column} = '#{subclass.name .demodulize}' "1168 type_condition = subclasses.inject("#{table_name}.#{quoted_inheritance_column} = '#{name}' ") do |condition, subclass| 1169 condition << "OR #{table_name}.#{quoted_inheritance_column} = '#{subclass.name}' " 1170 1170 end 1171 1171 1172 1172 " (#{type_condition}) " … … 1811 1811 # Message class in that example. 1812 1812 def ensure_proper_type 1813 1813 unless self.class.descends_from_active_record? 1814 write_attribute(self.class.inheritance_column, Inflector.demodulize(self.class.name))1814 write_attribute(self.class.inheritance_column, self.class.name) 1815 1815 end 1816 1816 end 1817 1817 -
CHANGELOG
old new 1 1 *SVN* 2 2 3 * Make STI work better with nested model classes. Closes #6519. [Jonathan Viney] 4 3 5 * Make add_column use the options hash with the Sqlite Adapter. Closes #6464 [obrie] 4 6 5 7 * Document other options available to migration's add_column. #6419 [grg]