Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

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  
    1010require 'fixtures/column_name' 
    1111require 'fixtures/subscriber' 
    1212require 'fixtures/keyboard' 
     13require 'fixtures/post' 
    1314 
    1415class Category < ActiveRecord::Base; end 
    1516class Smarts < ActiveRecord::Base; end 
     
    436437    classes.each(&:reset_table_name) 
    437438  end 
    438439   
     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   
    439453  def test_destroy_all 
    440454    assert_equal 2, Topic.count 
    441455 
  • test/fixtures/post.rb

    old new  
    5555 
    5656class SubStiPost < StiPost 
    5757end 
     58 
     59class Post::NestedStiPost < Post 
     60end 
  • lib/active_record/base.rb

    old new  
    11651165 
    11661166        def type_condition 
    11671167          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}' " 
    11701170          end 
    11711171 
    11721172          " (#{type_condition}) " 
     
    18111811      # Message class in that example. 
    18121812      def ensure_proper_type 
    18131813        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
    18151815        end 
    18161816      end 
    18171817 
  • CHANGELOG

    old new  
    11*SVN* 
    22 
     3* Make STI work better with nested model classes. Closes #6519. [Jonathan Viney] 
     4 
    35* Make add_column use the options hash with the Sqlite Adapter. Closes #6464 [obrie] 
    46 
    57* Document other options available to migration's add_column. #6419 [grg]