From 7e4b2abeeeae10a734793f284cfec9f9ca4b166e Mon Sep 17 00:00:00 2001 From: yui-knk Date: Sun, 31 Jan 2016 22:51:36 +0900 Subject: Each concrete classes have responsibility to return `association_class` --- activerecord/lib/active_record/reflection.rb | 47 +++++++++++++++------------- 1 file changed, 25 insertions(+), 22 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 320ced5afa..cbb5b99a05 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -483,28 +483,7 @@ module ActiveRecord # Returns +true+ if +self+ is a +has_one+ reflection. def has_one?; false; end - def association_class - case macro - when :belongs_to - if polymorphic? - Associations::BelongsToPolymorphicAssociation - else - Associations::BelongsToAssociation - end - when :has_many - if options[:through] - Associations::HasManyThroughAssociation - else - Associations::HasManyAssociation - end - when :has_one - if options[:through] - Associations::HasOneThroughAssociation - else - Associations::HasOneAssociation - end - end - end + def association_class; raise NotImplementedError; end def polymorphic? options[:polymorphic] @@ -629,6 +608,14 @@ module ActiveRecord def macro; :has_many; end def collection?; true; end + + def association_class + if options[:through] + Associations::HasManyThroughAssociation + else + Associations::HasManyAssociation + end + end end class HasOneReflection < AssociationReflection # :nodoc: @@ -639,6 +626,14 @@ module ActiveRecord def macro; :has_one; end def has_one?; true; end + + def association_class + if options[:through] + Associations::HasOneThroughAssociation + else + Associations::HasOneAssociation + end + end end class BelongsToReflection < AssociationReflection # :nodoc: @@ -650,6 +645,14 @@ module ActiveRecord def belongs_to?; true; end + def association_class + if polymorphic? + Associations::BelongsToPolymorphicAssociation + else + Associations::BelongsToAssociation + end + end + def join_keys(association_klass) key = polymorphic? ? association_primary_key(association_klass) : association_primary_key JoinKeys.new(key, foreign_key) -- cgit v1.2.3