From 98128fdc55a97ab5851bfbaff8306fcddaf709ad Mon Sep 17 00:00:00 2001 From: Kirk Wang Date: Mon, 5 Feb 2024 15:55:47 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=81=20Change=20set=5Fchild=5Fflag=20tr?= =?UTF-8?q?ansaction=20to=20listener?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit will remove the set_child_flag transaction and replace it with a listener. Ref: - https://github.com/scientist-softserv/iiif_print/issues/330 --- app/listeners/iiif_print/listener.rb | 16 ++++++++ .../iiif_print_container_decorator.rb | 28 -------------- .../transactions/steps/set_child_flag.rb | 32 ---------------- spec/listeners/iiif_print/listener_spec.rb | 31 ++++++++++++++++ .../iiif_print_container_decorator_spec.rb | 8 +--- .../transactions/steps/set_child_flag_spec.rb | 37 ------------------- 6 files changed, 48 insertions(+), 104 deletions(-) delete mode 100644 app/transactions/hyrax/transactions/steps/set_child_flag.rb delete mode 100644 spec/transactions/hyrax/transactions/steps/set_child_flag_spec.rb diff --git a/app/listeners/iiif_print/listener.rb b/app/listeners/iiif_print/listener.rb index 376e21d6..924e096d 100644 --- a/app/listeners/iiif_print/listener.rb +++ b/app/listeners/iiif_print/listener.rb @@ -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 Hyrax.config.curation_concerns.include?(object.class) + + 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) + end + end + end end end diff --git a/app/transactions/hyrax/transactions/iiif_print_container_decorator.rb b/app/transactions/hyrax/transactions/iiif_print_container_decorator.rb index 09ff253e..161cb5ac 100644 --- a/app/transactions/hyrax/transactions/iiif_print_container_decorator.rb +++ b/app/transactions/hyrax/transactions/iiif_print_container_decorator.rb @@ -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 diff --git a/app/transactions/hyrax/transactions/steps/set_child_flag.rb b/app/transactions/hyrax/transactions/steps/set_child_flag.rb deleted file mode 100644 index 89d85272..00000000 --- a/app/transactions/hyrax/transactions/steps/set_child_flag.rb +++ /dev/null @@ -1,32 +0,0 @@ -# frozen_string_literal: true - -module Hyrax - module Transactions - module Steps - class SetChildFlag - include Dry::Monads[:result] - - # @see IiifPrint.model_configuration - def call(resource) - return Failure(:resource_not_persisted) unless resource.persisted? - - user = ::User.find_by_user_key(resource.depositor) - - Hyrax.custom_queries.find_child_works(resource: resource).each do |child_work| - # not all child works might have the is_child property that we define when we configure - # the a model for iiif_print. see IiifPrint.model_configuration - # - # Put another the existence of the is_child property is optional. - next unless child_work.respond_to?(:is_child) - next if child_work.is_child - child_work.is_child = true - Hyrax.persister.save(resource: child_work) - Hyrax.publisher.publish('object.metadata.updated', object: child_work, user: user) - end - - Success(resource) - end - end - end - end -end diff --git a/spec/listeners/iiif_print/listener_spec.rb b/spec/listeners/iiif_print/listener_spec.rb index 3477cbbd..73881c8d 100644 --- a/spec/listeners/iiif_print/listener_spec.rb +++ b/spec/listeners/iiif_print/listener_spec.rb @@ -30,4 +30,35 @@ end end end + + describe '#on_object_membership_updated' do + class Work < Hyrax::Work + include Hyrax::Schema(:child_works_from_pdf_splitting) + 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 + allow(Hyrax.config).to receive(:curation_concerns).and_return([parent_work.class]) + 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 diff --git a/spec/transactions/hyrax/transactions/iiif_print_container_decorator_spec.rb b/spec/transactions/hyrax/transactions/iiif_print_container_decorator_spec.rb index 26bf875e..a438484c 100644 --- a/spec/transactions/hyrax/transactions/iiif_print_container_decorator_spec.rb +++ b/spec/transactions/hyrax/transactions/iiif_print_container_decorator_spec.rb @@ -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 diff --git a/spec/transactions/hyrax/transactions/steps/set_child_flag_spec.rb b/spec/transactions/hyrax/transactions/steps/set_child_flag_spec.rb deleted file mode 100644 index 8a3e810f..00000000 --- a/spec/transactions/hyrax/transactions/steps/set_child_flag_spec.rb +++ /dev/null @@ -1,37 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Hyrax::Transactions::Steps::SetChildFlag do - class Work < Hyrax::Work - include Hyrax::Schema(:child_works_from_pdf_splitting) - end - 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 - - describe '#call' do - subject { described_class.new.call(parent_work) } - - it 'sets the is_child flag on the child work' do - allow(::User).to receive(:find_by_user_key).and_return('user') - expect(child_work.is_child).to be nil - subject - # gets a reloaded version of the child work - expect(Hyrax.query_service.find_by(id: child_work.id).is_child).to be true - end - end -end