Skip to content

Commit

Permalink
Update Valkyrie index when notified of metadata updates
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
23 changes: 23 additions & 0 deletions app/services/hyrax/listeners/metadata_index_listener.rb
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 spec/services/hyrax/listeners/metadata_index_listener_spec.rb
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

0 comments on commit 8696c09

Please sign in to comment.