Skip to content

Commit

Permalink
Clean up membership associations on delete.
Browse files Browse the repository at this point in the history
Closes #85.
  • Loading branch information
tpendragon committed Aug 8, 2017
1 parent 34628a0 commit fa8f7be
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/change_set_persisters/plum_change_set_persister.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ def after_save(change_set:, updated_resource:)

def before_delete(change_set:)
clean_up_collection_associations(change_set: change_set) if change_set.resource.is_a?(Collection)
clean_up_membership(change_set: change_set)
end

def clean_up_membership(change_set:)
parents = query_service.find_parents(resource: change_set.resource)
parents.each do |parent|
parent.member_ids -= [change_set.id]
persister.save(resource: parent)
end
end

def append(append_id:, updated_resource:)
Expand Down
15 changes: 15 additions & 0 deletions spec/change_set_persisters/plum_change_set_persister_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,21 @@
end
end

describe "deleting child SRs" do
context "when a child is deleted" do
it "cleans up associations" do
child = FactoryGirl.create_for_repository(:scanned_resource)
parent = FactoryGirl.create_for_repository(:scanned_resource, member_ids: child.id)
change_set = ScannedResourceChangeSet.new(child)

change_set_persister.delete(change_set: change_set)
reloaded = query_service.find_by(id: parent.id)

expect(reloaded.member_ids).to eq []
end
end
end

describe "setting visibility" do
context "when setting to public" do
it "adds the public read_group" do
Expand Down

0 comments on commit fa8f7be

Please sign in to comment.