Skip to content

Commit

Permalink
🧹 Move container & steps
Browse files Browse the repository at this point in the history
Match Hyrax convention by using bulkrax/transactions.
  • Loading branch information
LaRita Robinson committed Jan 24, 2024
1 parent ed2ffb3 commit f573eb0
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 96 deletions.
10 changes: 5 additions & 5 deletions app/factories/bulkrax/valkyrie_object_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def create
result = transaction
.with_step_args(
# "work_resource.add_to_parent" => {parent_id: @related_parents_parsed_mapping, user: @user},
"work_resource.#{Bulkrax::Container::ADD_BULKRAX_FILES}" => { files: get_s3_files(remote_files: attributes["remote_files"]), user: @user },
"work_resource.#{Bulkrax::Transactions::Container::ADD_BULKRAX_FILES}" => { files: get_s3_files(remote_files: attributes["remote_files"]), user: @user },
"change_set.set_user_as_depositor" => { user: @user },
"work_resource.change_depositor" => { user: @user },
'work_resource.save_acl' => { permissions_params: [attrs.try('visibility') || 'open'].compact }
Expand Down Expand Up @@ -83,7 +83,7 @@ def update

result = update_transaction
.with_step_args(
"work_resource.#{Bulkrax::Container::ADD_BULKRAX_FILES}" => { files: get_s3_files(remote_files: attributes["remote_files"]), user: @user }
"work_resource.#{Bulkrax::Transactions::Container::ADD_BULKRAX_FILES}" => { files: get_s3_files(remote_files: attributes["remote_files"]), user: @user }

# TODO: uncomment when we upgrade Hyrax 4.x
# 'work_resource.save_acl' => { permissions_params: [attrs.try('visibility') || 'open'].compact }
Expand Down Expand Up @@ -164,14 +164,14 @@ def destroy_existing_files

private

# TODO Rename to create_transaction
# TODO: Rename to create_transaction
def transaction
Hyrax::Transactions::Container["work_resource.#{Bulkrax::Container::CREATE_WITH_BULK_BEHAVIOR}"]
Hyrax::Transactions::Container["work_resource.#{Bulkrax::Transactions::Container::CREATE_WITH_BULK_BEHAVIOR}"]
end

# Customize Hyrax::Transactions::WorkUpdate transaction with bulkrax
def update_transaction
Hyrax::Transactions::Container["work_resource.#{Bulkrax::Container::UPDATE_WITH_BULK_BEHAVIOR}"]
Hyrax::Transactions::Container["work_resource.#{Bulkrax::Transactions::Container::UPDATE_WITH_BULK_BEHAVIOR}"]
end

# Query child FileSet in the resource/object
Expand Down
42 changes: 0 additions & 42 deletions app/transactions/bulkrax/container.rb

This file was deleted.

47 changes: 0 additions & 47 deletions app/transactions/bulkrax/steps/add_files.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/transactions/bulkrax/transactions.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true
require 'bulkrax/container'
require 'bulkrax/transactions/container'

module Bulkrax
##
Expand Down
44 changes: 44 additions & 0 deletions app/transactions/bulkrax/transactions/container.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true
require 'dry/container'

module Bulkrax
module Transactions
class Container
extend Dry::Container::Mixin

ADD_BULKRAX_FILES = 'add_bulkrax_files'
CREATE_WITH_BULK_BEHAVIOR = 'create_with_bulk_behavior'
CREATE_WITH_BULK_BEHAVIOR_STEPS = begin
steps = Hyrax::Transactions::WorkCreate::DEFAULT_STEPS.dup
steps[steps.index("work_resource.add_file_sets")] = "work_resource.#{Bulkrax::Transactions::Container::ADD_BULKRAX_FILES}"
steps
end.freeze
UPDATE_WITH_BULK_BEHAVIOR = 'update_with_bulk_behavior'
UPDATE_WITH_BULK_BEHAVIOR_STEPS = begin
steps = Hyrax::Transactions::WorkUpdate::DEFAULT_STEPS.dup
steps[steps.index("work_resource.add_file_sets")] = "work_resource.#{Bulkrax::Transactions::Container::ADD_BULKRAX_FILES}"
steps
end.freeze

namespace "work_resource" do |ops|
ops.register CREATE_WITH_BULK_BEHAVIOR do
Hyrax::Transactions::WorkCreate.new(steps: CREATE_WITH_BULK_BEHAVIOR_STEPS)
end

ops.register UPDATE_WITH_BULK_BEHAVIOR do
Hyrax::Transactions::WorkUpdate.new(steps: UPDATE_WITH_BULK_BEHAVIOR_STEPS)
end

# TODO: uninitialized constant Bulkrax::Transactions::Container::InlineUploadHandler
# ops.register "add_file_sets" do
# Hyrax::Transactions::Steps::AddFileSets.new(handler: InlineUploadHandler)
# end

ops.register ADD_BULKRAX_FILES do
Bulkrax::Transactions::Steps::AddFiles.new
end
end
end
end
end
Hyrax::Transactions::Container.merge(Bulkrax::Transactions::Container)
49 changes: 49 additions & 0 deletions app/transactions/bulkrax/transactions/steps/add_files.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

require "dry/monads"

module Bulkrax
module Transactions
module Steps
class AddFiles
include Dry::Monads[:result]

##
# @param [Class] handler
def initialize(handler: Hyrax::WorkUploadsHandler)
@handler = handler
end

##
# @param [Hyrax::Work] obj
# @param [Array<Fog::AWS::Storage::File>] file
# @param [User] user
#
# @return [Dry::Monads::Result]
def call(obj, files:, user:)
if files && user
begin
files.each do |file|
FileIngest.upload(
content_type: file.content_type,
file_body: StringIO.new(file.body),
filename: Pathname.new(file.key).basename,
last_modified: file.last_modified,
permissions: Hyrax::AccessControlList.new(resource: obj),
size: file.content_length,
user: user,
work: obj
)
end
rescue => e
Hyrax.logger.error(e)
return Failure[:failed_to_attach_file_sets, files]
end
end

Success(obj)
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/bulkrax/transactions/container_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
describe 'work_resource.add_bulkrax_files' do
subject(:transaction_step) { described_class['work_resource.add_bulkrax_files'] }

it { is_expected.to be_a Bulkrax::Steps::AddFiles }
it { is_expected.to be_a Bulkrax::Transactions::Steps::AddFiles }
end
end

0 comments on commit f573eb0

Please sign in to comment.