Skip to content

Commit e011440

Browse files
committed
wip - has-many-through completely broken head back to previous commit perhaps
1 parent d45a2b8 commit e011440

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

ruby/hyper-model/lib/reactive_record/active_record/reactive_record/collection.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -414,18 +414,20 @@ def set_belongs_to(child)
414414

415415
def update_child(item)
416416
backing_record = item.backing_record
417-
if backing_record && @owner && @association && !@association.through_association? && item.attributes[@association.inverse_of] != @owner
417+
if backing_record && @owner && @association && item.attributes[@association.inverse_of] != @owner #&& [email protected]_association?
418418
inverse_of = @association.inverse_of
419419
current_association_value = item.attributes[inverse_of]
420420
backing_record.virgin = false unless backing_record.data_loading?
421-
backing_record.update_belongs_to(inverse_of, @owner)
422-
unless current_association_value.nil? # might be a dummy value which responds to nil
423-
current_association = @association.inverse.inverse(current_association_value)
424-
current_association_attribute = current_association.attribute
425-
if current_association.collection? && current_association_value.attributes[current_association_attribute]
426-
current_association.attributes[current_association_attribute].delete(item)
427-
end
428-
end
421+
#backing_record.update_belongs_to(inverse_of, @owner)
422+
backing_record.set_belongs_to_via_has_many(@association, @owner)
423+
# following is handled by update_belongs_to and is redundant
424+
# unless current_association_value.nil? # might be a dummy value which responds to nil
425+
# current_association = @association.inverse.inverse(current_association_value)
426+
# current_association_attribute = current_association.attribute
427+
# if current_association.collection? && current_association_value.attributes[current_association_attribute]
428+
# current_association.attributes[current_association_attribute].delete(item)
429+
# end
430+
# end
429431
@owner.backing_record.sync_has_many(@association.attribute)
430432
end
431433
end

ruby/hyper-model/lib/reactive_record/active_record/reactive_record/setters.rb

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,28 @@ def set_has_many(assoc, raw_value)
4545
def set_belongs_to(assoc, raw_value)
4646
set_common(assoc.attribute, raw_value) do |value, attr|
4747
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
48+
update_has_many_through_associations assoc, nil, current_value, :remove_member
49+
update_has_many_through_associations assoc, nil, value, :add_member
50+
remove_current_inverse_attribute assoc, nil, current_value
51+
add_new_inverse_attribute assoc, nil, value
5252
update_belongs_to attr, value.itself
5353
end
5454
end
5555

56+
def set_belongs_to_via_has_many(orig, value)
57+
puts "set_belongs_to_via_has_many(#{orig.inspect}, #{value.inspect})"
58+
assoc = orig.inverse
59+
attr = assoc.attribute
60+
current_value = @attributes[attr]
61+
puts "set_belongs_to_via_has_many(#{orig.attribute}, #{attr}, #{current_value.inspect}, #{value.inspect})"
62+
63+
update_has_many_through_associations assoc, orig, current_value, :remove_member
64+
update_has_many_through_associations assoc, orig, value, :add_member
65+
remove_current_inverse_attribute assoc, orig, current_value
66+
add_new_inverse_attribute assoc, orig, value
67+
update_belongs_to attr, value.itself
68+
end
69+
5670
def sync_has_many(attr)
5771
set_change_status_and_notify_only attr, @attributes[attr] != @synced_attributes[attr]
5872
end
@@ -136,9 +150,10 @@ def change_status_and_notify_helper(attr, changed)
136150
# change from has_one to has_many. So we first deal with the current value, then
137151
# update the new value which uses the push_onto_collection helper
138152

139-
def remove_current_inverse_attribute(association, model)
153+
def remove_current_inverse_attribute(association, orig, model)
140154
return if model.nil?
141155
inverse_association = association.inverse(model)
156+
return if inverse_association == orig
142157
if inverse_association.collection?
143158
# note we don't have to check if the collection exists, since it must
144159
# exist as at this ar_instance is already part of it.
@@ -148,9 +163,10 @@ def remove_current_inverse_attribute(association, model)
148163
end
149164
end
150165

151-
def add_new_inverse_attribute(association, model)
166+
def add_new_inverse_attribute(association, orig, model)
152167
return if model.nil?
153168
inverse_association = association.inverse(model)
169+
return if inverse_association == orig
154170
if inverse_association.collection?
155171
model.backing_record.push_onto_collection(@model, inverse_association, @ar_instance)
156172
else
@@ -193,9 +209,10 @@ def push_onto_collection(model, association, ar_instance)
193209
# and we have to then find any inverse has_many_through association (i.e. group or projects.uzers) and delete the
194210
# current value from those collections and push the new value on
195211

196-
def update_has_many_through_associations(assoc, value, method)
212+
def update_has_many_through_associations(assoc, orig, value, method)
197213
return if value.nil?
198214
assoc.through_associations(value).each do |ta|
215+
next if orig == ta
199216
source_value = @attributes[ta.source]
200217
# skip if source value is nil or if type of the association does not match type of source
201218
next unless source_value.class.to_s == ta.source_type

ruby/hyper-model/spec/batch7/poly_assoc_spec.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,8 @@ def compare_to_server(model, expression, expected_result, load=true)
273273
@uzer2 = Uzer.create(name: 'uzer2', uzer_data: 'uzer data2')
274274
@uzer3 = Uzer.create(name: 'uzer3', uzer_data: 'uzer data3')
275275
@group1 = Group.create(name: 'group1', group_data: 'group data1')
276-
@group1 = Group.create(name: 'group2', group_data: 'group data2')
277-
@group1 = Group.create(name: 'group3', group_data: 'group data3')
276+
@group2 = Group.create(name: 'group2', group_data: 'group data2')
277+
@group3 = Group.create(name: 'group3', group_data: 'group data3')
278278
@project1 = Project.create(name: 'project1', project_data: 'project data1')
279279
@project2 = Project.create(name: 'project2', project_data: 'project data2')
280280
@project3 = Project.create(name: 'project3', project_data: 'project data3')
@@ -303,10 +303,13 @@ def compare_to_server(model, expression, expected_result, load=true)
303303
evaluate_promise do
304304
uzer = Uzer.find(1)
305305
group = Group.find(1)
306-
uzer.groups << group # client side
307-
group.save # needed for client side semantics
306+
project = Membership.new(uzer: uzer, memerable: group)
307+
#uzer.groups << group # client side
308+
#group.save # needed for client side semantics
309+
project.save
308310
end
309311
binding.pry
312+
@group1.reload
310313
compare_to_server @group1, 'uzers.collect(&:id)', [@uzer1.id] # server side
311314
end
312315

0 commit comments

Comments
 (0)