@@ -44,9 +44,11 @@ def set_has_many(assoc, raw_value)
4444
4545 def set_belongs_to ( assoc , raw_value )
4646 set_common ( assoc . attribute , raw_value ) do |value , attr |
47- update_has_many_through_associations assoc , value
48- update_current_inverse_attribute assoc , @attributes [ assoc . attribute ]
49- update_new_inverse_attribute assoc , value
47+ current_value = @attributes [ assoc . attribute ]
48+ update_has_many_through_associations assoc , current_value , :remove_member
49+ update_has_many_through_associations assoc , value , :add_member
50+ remove_current_inverse_attribute assoc , current_value
51+ add_new_inverse_attribute assoc , value
5052 update_belongs_to attr , value . itself
5153 end
5254 end
@@ -134,7 +136,7 @@ def change_status_and_notify_helper(attr, changed)
134136 # change from has_one to has_many. So we first deal with the current value, then
135137 # update the new value which uses the push_onto_collection helper
136138
137- def update_current_inverse_attribute ( association , model )
139+ def remove_current_inverse_attribute ( association , model )
138140 return if model . nil?
139141 inverse_association = association . inverse ( model )
140142 if inverse_association . collection?
@@ -146,7 +148,7 @@ def update_current_inverse_attribute(association, model)
146148 end
147149 end
148150
149- def update_new_inverse_attribute ( association , model )
151+ def add_new_inverse_attribute ( association , model )
150152 return if model . nil?
151153 inverse_association = association . inverse ( model )
152154 if inverse_association . collection?
@@ -191,36 +193,28 @@ def push_onto_collection(model, association, ar_instance)
191193 # and we have to then find any inverse has_many_through association (i.e. group or projects.uzers) and delete the
192194 # current value from those collections and push the new value on
193195
194- def update_has_many_through_associations ( assoc , value )
195- # note that through and source_associations returns an empty set of
196- # the provided ar_instance does not belong to a has_many_through association
196+ def update_has_many_through_associations ( assoc , value , method )
197+ return if value . nil?
197198 assoc . through_associations ( value ) . each do |ta |
198199 source_value = @attributes [ ta . source ]
199- current_value = @attributes [ assoc . attribute ]
200200 # skip if source value is nil or if type of the association does not match type of source
201201 next unless source_value . class . to_s == ta . source_type
202- update_through_association ( ta , source_value , current_value , value )
202+ ta . send method , source_value , value
203203 ta . source_associations ( source_value ) . each do |sa |
204- update_source_association ( sa , source_value , current_value , value )
204+ sa . send method , value , source_value
205205 end
206206 end
207207 end
208208
209- def update_through_association ( ta , source_value , current_value , new_value )
210- unless current_value . nil? || current_value . attributes [ ta . attribute ] . nil?
211- current_value . attributes [ ta . attribute ] . delete ( source_value )
212- end
213- return unless new_value
214- new_value . attributes [ ta . attribute ] ||= Collection . new ( ta . owner_class , new_value , ta )
215- new_value . attributes [ ta . attribute ] << source_value
216- end
209+ # def remove_src_assoc(sa, source_value, current_value)
210+ # source_inverse_collection = source_value.attributes[sa.attribute]
211+ # source_inverse_collection.delete(current_value) if source_inverse_collection
212+ # end
213+ #
214+ # def add_src_assoc(sa, source_value, new_value)
215+ # source_value.attributes[sa.attribute] ||= Collection.new(sa.owner_class, source_value, sa)
216+ # source_value.attributes[sa.attribute] << new_value
217+ # end
217218
218- def update_source_association ( sa , source_value , current_value , new_value )
219- source_inverse_collection = source_value . attributes [ sa . attribute ]
220- source_inverse_collection . delete ( current_value ) if source_inverse_collection
221- return unless new_value
222- source_value . attributes [ sa . attribute ] ||= Collection . new ( sa . owner_class , source_value , sa )
223- source_value . attributes [ sa . attribute ] << new_value
224- end
225219 end
226220end
0 commit comments