diff --git a/app/actors/curation_concerns/actors/actor_stack.rb b/app/actors/curation_concerns/actors/actor_stack.rb index d668c97be..50cd2b7c3 100644 --- a/app/actors/curation_concerns/actors/actor_stack.rb +++ b/app/actors/curation_concerns/actors/actor_stack.rb @@ -24,6 +24,15 @@ def create(attributes) def update(attributes) actor.update(attributes.with_indifferent_access) end + + def destroy + curation_concern.in_collection_ids.each do |id| + destination_collection = ::Collection.find(id) + destination_collection.members.delete(curation_concern) + destination_collection.update_index + end + curation_concern.destroy + end end end end diff --git a/app/controllers/concerns/curation_concerns/curation_concern_controller.rb b/app/controllers/concerns/curation_concerns/curation_concern_controller.rb index 41483dda3..346c102c8 100644 --- a/app/controllers/concerns/curation_concerns/curation_concern_controller.rb +++ b/app/controllers/concerns/curation_concerns/curation_concern_controller.rb @@ -95,9 +95,10 @@ def update def destroy title = curation_concern.to_s - curation_concern.destroy - CurationConcerns.config.callback.run(:after_destroy, curation_concern.id, current_user) - after_destroy_response(title) + if actor.destroy + CurationConcerns.config.callback.run(:after_destroy, curation_concern.id, current_user) + after_destroy_response(title) + end end def file_manager diff --git a/spec/controllers/curation_concerns/generic_works_controller_spec.rb b/spec/controllers/curation_concerns/generic_works_controller_spec.rb index 9d2372582..d02f0d2b6 100644 --- a/spec/controllers/curation_concerns/generic_works_controller_spec.rb +++ b/spec/controllers/curation_concerns/generic_works_controller_spec.rb @@ -235,6 +235,7 @@ describe '#destroy' do let(:work_to_be_deleted) { create(:private_generic_work, user: user) } + let(:parent_collection) { create(:collection) } it 'deletes the work' do delete :destroy, id: work_to_be_deleted @@ -242,6 +243,19 @@ expect(GenericWork).not_to exist(work_to_be_deleted.id) end + context "when work is a member of a collection" do + before do + parent_collection.members = [work_to_be_deleted] + parent_collection.save! + end + it 'deletes the work and updates the parent collection' do + delete :destroy, id: work_to_be_deleted + expect(GenericWork).not_to exist(work_to_be_deleted.id) + expect(response).to redirect_to main_app.search_catalog_path + expect(parent_collection.reload.members).to eq [] + end + end + it "invokes the after_destroy callback" do expect(CurationConcerns.config.callback).to receive(:run) .with(:after_destroy, work_to_be_deleted.id, user)