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

🎁 Change set_child_flag transaction to listener #331

Merged
merged 1 commit into from
Feb 6, 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
16 changes: 16 additions & 0 deletions app/listeners/iiif_print/listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,21 @@ def on_file_set_attached(event, service: IiifPrint::SplitPdfs::ChildWorkCreation

service.conditionally_enqueue(file_set: file_set, work: work, file: file, user: user)
end

##
# Responsible for setting the is_child flag on the work when a child work is created.
#
# @param event [#[]] a hash like construct with :object key
def on_object_membership_updated(event)
object = event[:object]
return unless object.respond_to?(:iiif_print_config?) && object.iiif_print_config?

Hyrax.custom_queries.find_child_works(resource: object).each do |child_work|
unless child_work.is_child
child_work.is_child = true
Hyrax.persister.save(resource: child_work)
jeremyf marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,6 @@ class IiifPrintContainerDecorator
)
end
end

namespace 'change_set' do |ops|
ops.register 'update_work' do
steps = Hyrax::Transactions::WorkUpdate::DEFAULT_STEPS.dup
# NOTE: applications that don't want the file splitting of IIIF Print will want to remove
# this step from their transactions.
steps.insert(steps.index('work_resource.update_work_members') + 1, 'work_resource.set_child_flag')
Hyrax::Transactions::WorkUpdate.new(steps: steps)
end
end

if defined?(Bulkrax::Transactions::Container)
namespace 'work_resource' do |ops|
ops.register Bulkrax::Transactions::Container::UPDATE_WITH_BULK_BEHAVIOR do
steps = Bulkrax::Transactions::Container::UPDATE_WITH_BULK_BEHAVIOR_STEPS.dup
# NOTE: applications that don't want the file splitting of IIIF Print will want to remove
# this step from their transactions.
steps.insert(steps.index('work_resource.update_work_members') + 1, 'work_resource.set_child_flag')
Hyrax::Transactions::WorkUpdate.new(steps: steps)
end
end
end

namespace 'work_resource' do |ops|
ops.register 'set_child_flag' do
Steps::SetChildFlag.new
end
end
end
end
end
Expand Down
32 changes: 0 additions & 32 deletions app/transactions/hyrax/transactions/steps/set_child_flag.rb

This file was deleted.

34 changes: 34 additions & 0 deletions spec/listeners/iiif_print/listener_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,38 @@
end
end
end

describe '#on_object_membership_updated' do
class Work < Hyrax::Work
include Hyrax::Schema(:child_works_from_pdf_splitting)

def iiif_print_config?
true
end
end

subject { described_class.new.on_object_membership_updated(event) }

let(:event) { { object: parent_work } }
let(:parent_work) do
parent_work = Work.new
parent_work.title = ['Parent Work']
Hyrax.persister.save(resource: parent_work)
end
let(:child_work) do
child_work = Work.new
child_work.title = ['Child Work']
Hyrax.persister.save(resource: child_work)
end

before do
parent_work.member_ids << child_work.id
Hyrax.persister.save(resource: parent_work)
Hyrax.index_adapter.save(resource: parent_work)
end

it 'sets the is_child flag on the child work' do
expect { subject }.to change { Hyrax.query_service.find_by(id: child_work.id).is_child }.to(true)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,7 @@
expect(transaction_step.steps).to match_array(["change_set.apply",
"work_resource.save_acl",
"work_resource.add_file_sets",
"work_resource.update_work_members",
"work_resource.set_child_flag"])
"work_resource.update_work_members"])
end
end

describe 'work_resource.set_child_flag' do
subject(:transaction_step) { described_class['work_resource.set_child_flag'] }
it { is_expected.to be_a Hyrax::Transactions::Steps::SetChildFlag }
end
end
37 changes: 0 additions & 37 deletions spec/transactions/hyrax/transactions/steps/set_child_flag_spec.rb

This file was deleted.

Loading