Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎁 Add listener for handling file set attachment #317

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions app/listeners/iiif_print/listener.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
##
# @see https://github.com/samvera/hyrax/wiki/Hyrax's-Event-Bus-(Hyrax::Publisher)
# @see https://www.rubydoc.info/gems/hyrax/Hyrax/Publisher
# @see https://dry-rb.org/gems/dry-events
module IiifPrint
class Listener
##
# Responsible for conditionally enqueuing the creation of child works from a PDF.
#
# @param event [#[]] a hash like construct with keys :user and :file_set
# @param service [#conditionally_enqueue]
#
# @see Hyrax::WorkUploadsHandler
def on_file_set_attached(event, service: IiifPrint::SplitPdfs::ChildWorkCreationFromPdfService)
user = event[:user]
file_set = event[:file_set]

return false unless file_set
return false unless file_set.file_set?

work = IiifPrint.parent_for(file_set)

# A short-circuit to avoid fetching the underlying file.
return false unless work

# TODO: Verify that this is the correct thing to be sending off for conditional enquing. That
# will require a more involved integration test.
file = file_set.original_file

service.conditionally_enqueue(file_set: file_set, work: work, file: file, user: user)
end
end
end
2 changes: 2 additions & 0 deletions lib/iiif_print/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class Engine < ::Rails::Engine
IiifPrint::PluggableDerivativeService
)

Hyrax.publisher.subscribe(IiifPrint::Listener.new)

Hyrax::IiifManifestPresenter.prepend(IiifPrint::IiifManifestPresenterBehavior)
Hyrax::IiifManifestPresenter::Factory.prepend(IiifPrint::IiifManifestPresenterFactoryBehavior)
Hyrax::ManifestBuilderService.prepend(IiifPrint::ManifestBuilderServiceBehavior)
Expand Down
33 changes: 33 additions & 0 deletions spec/listeners/iiif_print/listener_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe IiifPrint::Listener do
describe '#on_file_set_attached' do
subject { described_class.new.on_file_set_attached(event) }
let(:file_set) { double(Hyrax::FileSet, file_set?: true) }
let(:user) { double(User) }
let(:event) { { user: user, file_set: file_set } }

before { allow(IiifPrint).to receive(:parent_for).with(file_set).and_return(parent) }

context 'without a parent work' do
let(:parent) { nil }

it "does not call the service's #conditionally_enqueue method" do
expect(IiifPrint::SplitPdfs::ChildWorkCreationFromPdfService).not_to receive(:conditionally_enqueue)
subject
end
end

context 'with a parent work' do
let(:parent) { double(Valkyrie::Resource) }

it "calls the service's #conditionally_enqueue method" do
expect(IiifPrint::SplitPdfs::ChildWorkCreationFromPdfService).to receive(:conditionally_enqueue)
expect(file_set).to receive(:original_file).and_return(double)
subject
end
end
end
end
Loading