-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Valkyrie index when notified of metadata updates
When an object's metadata is updated, update the Valkyrie-driven index via the configured indexing adapter. Note that this is currently a no-op, since we use a null indexing adapter by default.
- Loading branch information
Tom Johnson
committed
Dec 26, 2019
1 parent
8d8c4d2
commit 8696c09
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
module Hyrax | ||
module Listeners | ||
## | ||
# Reindexes resources when their metadata is updated. | ||
# | ||
# @note This listener makes no attempt to avoid reindexing when no metadata | ||
# has actually changed, or when real metadata changes won't impact the | ||
# indexed data. We trust that published metadata update events represent | ||
# actual changes to object metadata, and that the indexing adapter | ||
# optimizes reasonably for actual index document contents. | ||
class MetadataIndexListener | ||
## | ||
# Re-index the resource. | ||
# | ||
# @param event [Dry::Event] | ||
def on_object_metadata_updated(event) | ||
Hyrax.index_adapter.save(resource: event[:object]) | ||
end | ||
end | ||
end | ||
end |
24 changes: 24 additions & 0 deletions
24
spec/services/hyrax/listeners/metadata_index_listener_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Hyrax::Listeners::MetadataIndexListener do | ||
subject(:listener) { described_class.new } | ||
let(:data) { { object: resource } } | ||
let(:event) { Dry::Events::Event.new(event_type, data) } | ||
let(:fake_adapter) { FakeIndexingAdapter.new } | ||
let(:resource) { FactoryBot.valkyrie_create(:hyrax_resource) } | ||
|
||
# the listener should always use the currently configured Hyrax Index Adapter | ||
before do | ||
allow(Hyrax).to receive(:index_adapter).and_return(fake_adapter) | ||
end | ||
|
||
describe '#on_object_metadata_updated' do | ||
let(:event_type) { :on_object_metadata_updated } | ||
|
||
it 'reindexes the object on the configured adapter' do | ||
expect { listener.on_object_metadata_updated(event) } | ||
.to change { fake_adapter.saved_resources } | ||
.to contain_exactly(resource) | ||
end | ||
end | ||
end |