@@ -2,6 +2,10 @@ module PolymorphicIntegerType
22
33 module Extensions
44 module ClassMethods
5+ ActiveRecord ::Reflection ::HasManyReflection . attr_accessor ( :foreign_integer_type )
6+ ActiveRecord ::Reflection ::HasManyReflection . attr_accessor ( :integer_type )
7+ ActiveRecord ::Reflection ::HasOneReflection . attr_accessor ( :foreign_integer_type )
8+ ActiveRecord ::Reflection ::HasOneReflection . attr_accessor ( :integer_type )
59
610 def belongs_to ( name , scope = nil , **options )
711 options = scope if scope . kind_of? Hash
@@ -58,16 +62,16 @@ def remove_type_and_establish_mapping(name, options, scope)
5862
5963 options [ :foreign_key ] ||= "#{ poly_type } _id"
6064 foreign_type = options . delete ( :foreign_type ) || "#{ poly_type } _type"
61- options [ :foreign_integer_type ] = foreign_type
62- options [ :integer_type ] = klass_mapping . to_i
6365
6466 options [ :scope ] ||= -> {
6567 condition = where ( foreign_type => klass_mapping . to_i )
6668 condition = instance_exec ( &scope ) . merge ( condition ) if scope . is_a? ( Proc )
6769 condition
6870 }
71+ return foreign_type , klass_mapping . to_i
6972 else
7073 options [ :scope ] ||= scope
74+ return nil , nil
7175 end
7276 end
7377
@@ -88,9 +92,9 @@ def has_many(name, scope = nil, **options, &extension)
8892 scope = nil
8993 end
9094
91- remove_type_and_establish_mapping ( name , options , scope )
92- super ( name , options . delete ( :scope ) , **options . except ( :foreign_integer_type , :integer_type ) , &extension ) . tap do | _ |
93- remove_integer_type_and_set_attributes_and_extension ( options , ActiveRecord :: Reflection :: HasManyReflection , reflections [ name . to_s ] )
95+ integer_type_values = remove_type_and_establish_mapping ( name , options , scope )
96+ super ( name , options . delete ( :scope ) , **options , &extension ) . tap do
97+ remove_integer_type_and_set_attributes_and_extension ( integer_type_values , reflections [ name . to_s ] )
9498 end
9599 end
96100
@@ -100,27 +104,25 @@ def has_one(name, scope = nil, **options)
100104 scope = nil
101105 end
102106
103- remove_type_and_establish_mapping ( name , options , scope )
104- super ( name , options . delete ( :scope ) , **options . except ( :foreign_integer_type , :integer_type ) ) . tap do | _ |
105- remove_integer_type_and_set_attributes_and_extension ( options , ActiveRecord :: Reflection :: HasOneReflection , reflections [ name . to_s ] )
107+ integer_type_values = remove_type_and_establish_mapping ( name , options , scope )
108+ super ( name , options . delete ( :scope ) , **options ) . tap do
109+ remove_integer_type_and_set_attributes_and_extension ( integer_type_values , reflections [ name . to_s ] )
106110 end
107111 end
108112
109- def remove_integer_type_and_set_attributes_and_extension ( options , klass , reflection )
110- foreign_integer_type = options . delete :foreign_integer_type
111- integer_type = options . delete :integer_type
113+ def remove_integer_type_and_set_attributes_and_extension ( integer_type_values , reflection )
114+ foreign_integer_type = integer_type_values [ 0 ]
115+ integer_type = integer_type_values [ 1 ]
112116 is_polymorphic_integer = foreign_integer_type && integer_type
113117
114118 if is_polymorphic_integer
115- klass . attr_accessor ( :foreign_integer_type )
116- klass . attr_accessor ( :integer_type )
117119 reflection . foreign_integer_type = foreign_integer_type
118120 reflection . integer_type = integer_type
119121
120122 if Gem ::Version . new ( ActiveRecord ::VERSION ::STRING ) < Gem ::Version . new ( "6.1" )
121- ActiveRecord ::Associations ::Association . prepend ( ActiveRecord :: Associations ::PolymorphicForeignAssociationExtension )
123+ ActiveRecord ::Associations ::Association . prepend ( PolymorphicIntegerType ::PolymorphicForeignAssociationExtension )
122124 else
123- ActiveRecord ::Associations ::ForeignAssociation . prepend ( ActiveRecord :: Associations ::PolymorphicForeignAssociationExtension )
125+ ActiveRecord ::Associations ::ForeignAssociation . prepend ( PolymorphicIntegerType ::PolymorphicForeignAssociationExtension )
124126 end
125127 end
126128 end
0 commit comments