@@ -2,6 +2,10 @@ module PolymorphicIntegerType
2
2
3
3
module Extensions
4
4
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 )
5
9
6
10
def belongs_to ( name , scope = nil , **options )
7
11
options = scope if scope . kind_of? Hash
@@ -58,16 +62,16 @@ def remove_type_and_establish_mapping(name, options, scope)
58
62
59
63
options [ :foreign_key ] ||= "#{ poly_type } _id"
60
64
foreign_type = options . delete ( :foreign_type ) || "#{ poly_type } _type"
61
- options [ :foreign_integer_type ] = foreign_type
62
- options [ :integer_type ] = klass_mapping . to_i
63
65
64
66
options [ :scope ] ||= -> {
65
67
condition = where ( foreign_type => klass_mapping . to_i )
66
68
condition = instance_exec ( &scope ) . merge ( condition ) if scope . is_a? ( Proc )
67
69
condition
68
70
}
71
+ return foreign_type , klass_mapping . to_i
69
72
else
70
73
options [ :scope ] ||= scope
74
+ return nil , nil
71
75
end
72
76
end
73
77
@@ -88,9 +92,9 @@ def has_many(name, scope = nil, **options, &extension)
88
92
scope = nil
89
93
end
90
94
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 ] )
94
98
end
95
99
end
96
100
@@ -100,27 +104,25 @@ def has_one(name, scope = nil, **options)
100
104
scope = nil
101
105
end
102
106
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 ] )
106
110
end
107
111
end
108
112
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 ]
112
116
is_polymorphic_integer = foreign_integer_type && integer_type
113
117
114
118
if is_polymorphic_integer
115
- klass . attr_accessor ( :foreign_integer_type )
116
- klass . attr_accessor ( :integer_type )
117
119
reflection . foreign_integer_type = foreign_integer_type
118
120
reflection . integer_type = integer_type
119
121
120
122
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 )
122
124
else
123
- ActiveRecord ::Associations ::ForeignAssociation . prepend ( ActiveRecord :: Associations ::PolymorphicForeignAssociationExtension )
125
+ ActiveRecord ::Associations ::ForeignAssociation . prepend ( PolymorphicIntegerType ::PolymorphicForeignAssociationExtension )
124
126
end
125
127
end
126
128
end
0 commit comments