Skip to content

Commit

Permalink
Merge pull request #5432 from samvera/collection_deleted_listener
Browse files Browse the repository at this point in the history
Remove collection from index when deleted event published
  • Loading branch information
elrayle authored Feb 3, 2022
2 parents 18d34ae + 9a77ebd commit 5811a8c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/services/hyrax/listeners/metadata_index_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ def on_object_deleted(event)
Hyrax.index_adapter.delete(resource: event[:object])
end

##
# Remove the resource from the index.
#
# Called when 'collection.deleted' event is published
# @param [Dry::Events::Event] event
# @return [void]
def on_collection_deleted(event)
return unless resource?(event.payload[:collection])
Hyrax.index_adapter.delete(resource: event[:collection])
end

private

def resource?(resource)
Expand Down
23 changes: 23 additions & 0 deletions spec/services/hyrax/listeners/metadata_index_listener_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,29 @@
end
end

describe '#on_collection_deleted' do
let(:event_type) { :on_collection_deleted }
let(:resource) { FactoryBot.valkyrie_create(:hyrax_collection) }
let(:data) { { collection: resource } }

it 'reindexes the collection on the configured adapter' do
expect(Hyrax.logger).not_to receive(:info).with(skipping_message)
expect { listener.on_collection_deleted(event) }
.to change { fake_adapter.deleted_resources }
.to contain_exactly(resource)
end

context 'when it gets a non-resource as payload' do
let(:resource) { ActiveFedora::Base.new }

it 'returns as a no-op' do
expect(Hyrax.logger).to receive(:info).with(skipping_message)
expect { listener.on_collection_deleted(event) }
.not_to change { fake_adapter.deleted_resources }
end
end
end

describe '#on_collection_metadata_updated' do
let(:event_type) { :on_collection_metadata_updated }
let(:data) { { collection: resource } }
Expand Down

0 comments on commit 5811a8c

Please sign in to comment.