From 4bdd8330b7f73147aec736776378f9f828eb7c6e Mon Sep 17 00:00:00 2001 From: Joseph Palermo Date: Wed, 2 Mar 2011 17:32:58 +0800 Subject: [PATCH] respect store_full_sti_class for polymorphic type columns --- .../lib/active_record/associations/association.rb | 2 +- .../belongs_to_polymorphic_association.rb | 2 +- .../join_dependency/join_association.rb | 4 ++-- .../associations/preloader/association.rb | 2 +- .../associations/through_association.rb | 2 +- activerecord/lib/active_record/base.rb | 4 ++++ .../eager_load_includes_full_sti_class_test.rb | 17 +++++++++++++---- 7 files changed, 23 insertions(+), 10 deletions(-) diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb index 86904ea..710a92f 100644 --- a/activerecord/lib/active_record/associations/association.rb +++ b/activerecord/lib/active_record/associations/association.rb @@ -192,7 +192,7 @@ module ActiveRecord attributes[reflection.foreign_key] = owner[reflection.active_record_primary_key] if options[:as] - attributes["#{options[:as]}_type"] = owner.class.base_class.name + attributes["#{options[:as]}_type"] = owner.class.polymorphic_name end end attributes diff --git a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb index 1ca4482..af376be 100644 --- a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb @@ -6,7 +6,7 @@ module ActiveRecord def replace_keys(record) super - owner[reflection.foreign_type] = record && record.class.base_class.name + owner[reflection.foreign_type] = record && record.class.polymorphic_name end def different_target?(record) diff --git a/activerecord/lib/active_record/associations/join_dependency/join_association.rb b/activerecord/lib/active_record/associations/join_dependency/join_association.rb index ebe39c3..4abbc9c 100644 --- a/activerecord/lib/active_record/associations/join_dependency/join_association.rb +++ b/activerecord/lib/active_record/associations/join_dependency/join_association.rb @@ -205,7 +205,7 @@ module ActiveRecord if through_reflection.options[:as] # has_many :through against a polymorphic join jt_conditions << join_table["#{through_reflection.options[:as]}_type"]. - eq(parent.active_record.base_class.name) + eq(parent.active_record.polymorphic_name) end end @@ -260,7 +260,7 @@ module ActiveRecord target_table["#{reflection.options[:as]}_id"]. eq(parent_table[parent.primary_key]).and( target_table["#{reflection.options[:as]}_type"]. - eq(parent.active_record.base_class.name)) + eq(parent.active_record.polymorphic_name)) ) end diff --git a/activerecord/lib/active_record/associations/preloader/association.rb b/activerecord/lib/active_record/associations/preloader/association.rb index 7256dd5..107c579 100644 --- a/activerecord/lib/active_record/associations/preloader/association.rb +++ b/activerecord/lib/active_record/associations/preloader/association.rb @@ -103,7 +103,7 @@ module ActiveRecord if options[:as] scope = scope.where( klass.table_name => { - reflection.type => model.base_class.sti_name + reflection.type => model.polymorphic_name } ) end diff --git a/activerecord/lib/active_record/associations/through_association.rb b/activerecord/lib/active_record/associations/through_association.rb index e1d60cc..801616d 100644 --- a/activerecord/lib/active_record/associations/through_association.rb +++ b/activerecord/lib/active_record/associations/through_association.rb @@ -102,7 +102,7 @@ module ActiveRecord if options[:source_type] join_attributes[source_reflection.foreign_type] = - records.map { |record| record.class.base_class.name } + records.map { |record| record.class.polymorphic_name } end if records.count == 1 diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index b3204b2..0b764f8 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -852,6 +852,10 @@ module ActiveRecord #:nodoc: store_full_sti_class ? name : name.demodulize end + def polymorphic_name + base_class.sti_name + end + def arel_table Arel::Table.new(table_name, arel_engine) end diff --git a/activerecord/test/cases/associations/eager_load_includes_full_sti_class_test.rb b/activerecord/test/cases/associations/eager_load_includes_full_sti_class_test.rb index d75791c..48f74fe 100644 --- a/activerecord/test/cases/associations/eager_load_includes_full_sti_class_test.rb +++ b/activerecord/test/cases/associations/eager_load_includes_full_sti_class_test.rb @@ -11,16 +11,13 @@ end class EagerLoadIncludeFullStiClassNamesTest < ActiveRecord::TestCase - def setup - generate_test_objects - end - def generate_test_objects post = Namespaced::Post.create( :title => 'Great stuff', :body => 'This is not', :author_id => 1 ) Tagging.create( :taggable => post ) end def test_class_names + generate_test_objects old = ActiveRecord::Base.store_full_sti_class ActiveRecord::Base.store_full_sti_class = false @@ -34,4 +31,16 @@ class EagerLoadIncludeFullStiClassNamesTest < ActiveRecord::TestCase ensure ActiveRecord::Base.store_full_sti_class = old end + + def test_eager_load_works_when_not_storing_full_sti_class + old = ActiveRecord::Base.store_full_sti_class + + ActiveRecord::Base.store_full_sti_class = false + generate_test_objects + + post = Namespaced::Post.find_by_title( 'Great stuff', :include => :tagging ) + assert_instance_of Tagging, post.tagging + ensure + ActiveRecord::Base.store_full_sti_class = old + end end -- 1.7.1.1