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 transaction for set_child_flag #321

Merged
merged 1 commit into from
Jan 24, 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
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Hyrax
module Transactions
##
Expand Down Expand Up @@ -25,6 +27,20 @@ class IiifPrintContainerDecorator
)
end
end

namespace 'change_set' do |ops|
ops.register 'update_work' do
steps = Hyrax::Transactions::WorkUpdate::DEFAULT_STEPS.dup
steps.insert(steps.index('work_resource.update_work_members') + 1, 'work_resource.set_child_flag')
Hyrax::Transactions::WorkUpdate.new(steps: steps)
end
end

namespace 'work_resource' do |ops|
ops.register 'set_child_flag' do
Steps::SetChildFlag.new
end
end
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions app/transactions/hyrax/transactions/steps/set_child_flag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Hyrax
module Transactions
module Steps
class SetChildFlag
include Dry::Monads[:result]

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|
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
1 change: 0 additions & 1 deletion lib/iiif_print/persistence_layer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ module IiifPrint
module PersistenceLayer
# @abstract
class AbstractAdapter

##
# @param object [Object]
# @return [Array<Object>]
Expand Down
1 change: 0 additions & 1 deletion lib/iiif_print/persistence_layer/active_fedora_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module IiifPrint
module PersistenceLayer
class ActiveFedoraAdapter < AbstractAdapter

##
# @param object [ActiveFedora::Base]
# @return [Array<SolrDocument>]
Expand Down
3 changes: 1 addition & 2 deletions lib/iiif_print/persistence_layer/valkyrie_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module IiifPrint
module PersistenceLayer
class ValkyrieAdapter < AbstractAdapter

##
# @param object [Valkyrie::Resource]
# @return [Array<Valkyrie::Resource>]
Expand All @@ -21,7 +20,7 @@ def self.object_ordered_works(object)
def self.decorate_with_adapter_logic(work_type:)
work_type.send(:include, Hyrax::Schema(:child_works_from_pdf_splitting)) unless work_type.included_modules.include?(Hyrax::Schema(:child_works_from_pdf_splitting))
# TODO: Use `Hyrax::ValkyrieIndexer.indexer_class_for` once changes are merged.
indexer = "#{work_type.to_s}Indexer".constantize
indexer = "#{work_type}Indexer".constantize
indexer.send(:include, Hyrax::Indexer(:child_works_from_pdf_splitting)) unless indexer.included_modules.include?(Hyrax::Indexer(:child_works_from_pdf_splitting))
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
subject(:instance) { described_class.new }

context '#config_search_paths' do
subject { instance.config_search_paths }
subject { instance.config_search_paths }

# Would it make sense for IiifPrint::Engine to come before Hyrax::Engine? As IiifPrint depends
# on Hyrax.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,25 @@
}
end
end

describe 'file_set.iiif_print_conditionally_destroy_spawned_children' do
subject(:transaction_step) { described_class['file_set.iiif_print_conditionally_destroy_spawned_children'] }
it { is_expected.to be_a Hyrax::Transactions::Steps::ConditionallyDestroyChildrenFromSplit }
end

describe 'change_set.update_work' do
subject(:transaction_step) { described_class['change_set.update_work'] }
it 'has the correct steps' do
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"])
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: 37 additions & 0 deletions spec/transactions/hyrax/transactions/steps/set_child_flag_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
subject
expect { subject }.to change { Hyrax.query_service.find_by(id: child_work.id).is_child }.to(true)

# 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
Loading