Skip to content

Commit

Permalink
Propagate child changes through association manipulation
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisandreae committed Jul 5, 2019
1 parent 36864e1 commit 44cb3f9
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions lib/view_model/active_record/association_manipulation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,26 @@ def append_associated(association_name, subtree_hash_or_hashes, references: {},
child_context = self.context_for_child(association_name, context: deserialize_context)
updated_viewmodels = update_context.run!(deserialize_context: child_context)

# Propagate changes and finalize the parent
updated_viewmodels.each do |child|
child_changes = child.previous_changes

if association_data.nested?
nested_children_changed! if child_changes.changed_nested_tree?
referenced_children_changed! if child_changes.changed_referenced_children?
elsif association_data.owned?
referenced_children_changed! if child_changes.changed_owned_tree?
end
end

final_changes = self.clear_changes!

if association_data.through?
updated_viewmodels.map! do |direct_vm|
direct_vm._read_association(association_data.indirect_reflection.name)
end
end

# Finalize the parent
final_changes = self.clear_changes!

# Could happen if hooks attempted to change the parent, which aren't
# valid since we're only editing children here.
unless final_changes.contained_to?(associations: [association_name.to_s])
Expand Down Expand Up @@ -268,7 +279,11 @@ def delete_associated(association_name, associated_id, type: nil, deserialize_co
association.delete(child_vm.model)
end

self.referenced_children_changed! if association_data.owned?
if association_data.nested?
nested_children_changed!
elsif association_data.owned?
referenced_children_changed!
end

final_changes = self.clear_changes!

Expand All @@ -286,7 +301,7 @@ def delete_associated(association_name, associated_id, type: nil, deserialize_co

private

def construct_direct_append_updates(association_data, subtree_hashes, references)
def construct_direct_append_updates(_association_data, subtree_hashes, references)
ViewModel::ActiveRecord::UpdateData.parse_hashes(subtree_hashes, references)
end

Expand Down

0 comments on commit 44cb3f9

Please sign in to comment.