Skip to content

Commit

Permalink
Fix batch add new works doesn't add new works (#6266)
Browse files Browse the repository at this point in the history
* Fix batch add new works doesn't add new works

* Refactor and fix specs

---------

Co-authored-by: Eliot Jordan <[email protected]>
  • Loading branch information
VivianChu and eliotjordan authored Sep 12, 2023
1 parent ae9b1bf commit 2d2f11a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .koppie/config/features.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
batch_upload:
enabled: "true"
40 changes: 36 additions & 4 deletions app/jobs/create_work_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,47 @@ class CreateWorkJob < Hyrax::ApplicationJob
def perform(user, model, attributes, operation)
operation.performing!
work = model.constantize.new
current_ability = Ability.new(user)
env = Hyrax::Actors::Environment.new(work, current_ability, attributes)
status = work_actor.create(env)
if model.constantize < ActiveFedora::Base
status = batch_create_af_work(work, attributes, user)
errors = work.errors
else
result = batch_create_valkyrie_work(work, attributes, user)
status = result.success?
errors = result.failure&.last
end

return operation.success! if status
operation.fail!(work.errors.full_messages.join(' '))
operation.fail!(errors.full_messages.join(' '))
end

private

def batch_create_af_work(work, attributes, user)
current_ability = Ability.new(user)
env = Hyrax::Actors::Environment.new(work, current_ability, attributes)
work_actor.create(env)
end

def batch_create_valkyrie_work(work, attributes, user)
uploaded_file_ids = attributes.delete(:uploaded_files)
files = Hyrax::UploadedFile.find(uploaded_file_ids)
permissions_params = attributes.delete(:permissions_attributes)
form = Hyrax::FormFactory.new.build(work, nil, nil)
form.validate(attributes)

transactions['change_set.create_work']
.with_step_args(
'work_resource.add_file_sets' => { uploaded_files: files },
'change_set.set_user_as_depositor' => { user: user },
'work_resource.save_acl' => { permissions_params: permissions_params }
)
.call(form)
end

def transactions
Hyrax::Transactions::Container
end

def work_actor
Hyrax::CurationConcern.actor
end
Expand Down
1 change: 1 addition & 0 deletions spec/features/batch_create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
before do
allow(CharacterizeJob).to receive(:perform_later).and_return(true)
allow(CreateDerivativesJob).to receive(:perform_later).and_return(true)
allow(Hyrax.config.characterization_service).to receive(:run).and_return(true)

ProxyDepositRights.create!(grantor: second_user, grantee: user)
sign_in user
Expand Down
81 changes: 57 additions & 24 deletions spec/jobs/create_work_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,76 @@
let(:upload1) { Hyrax::UploadedFile.create(user: user, file: file1) }
let(:metadata) do
{ keyword: [],
"permissions_attributes" => [{ "type" => "group", "name" => "public", "access" => "read" }],
"visibility" => 'open',
permissions_attributes: [{ "type" => "group", "name" => "public", "access" => "read" }],
visibility: 'open',
uploaded_files: [upload1.id],
title: ['File One'],
creator: ['Last, First'],
resource_type: ['Article'] }
end
let(:errors) { double(full_messages: ["It's broke!"]) }
let(:work) { double(errors: errors) }
let(:actor) { double(curation_concern: work) }

before do
allow(Hyrax::CurationConcern).to receive(:actor).and_return(actor)
allow(GenericWork).to receive(:new).and_return(work)
end
context "with an ActiveFedora model", skip: !(GenericWork < ActiveFedora::Base) do
before do
allow(Hyrax::CurationConcern).to receive(:actor).and_return(actor)
allow(GenericWork).to receive(:new).and_return(work)
end

context "when the update is successful" do
it "logs the success" do
expect(actor).to receive(:create).with(Hyrax::Actors::Environment) do |env|
expect(env.attributes).to eq("keyword" => [],
"title" => ['File One'],
"resource_type" => ["Article"],
"permissions_attributes" =>
[{ "type" => "group", "name" => "public", "access" => "read" }],
"visibility" => "open",
"uploaded_files" => [upload1.id])
end.and_return(true)
described_class.perform_later(user, 'GenericWork', metadata, log)
expect(log.reload.status).to eq 'success'
context "when the update is successful" do
it "logs the success" do
expect(actor).to receive(:create).with(Hyrax::Actors::Environment) do |env|
expect(env.attributes).to eq("keyword" => [],
"title" => ['File One'],
"creator" => ['Last, First'],
"resource_type" => ["Article"],
"permissions_attributes" =>
[{ "type" => "group", "name" => "public", "access" => "read" }],
"visibility" => "open",
"uploaded_files" => [upload1.id])
end.and_return(true)
described_class.perform_later(user, 'GenericWork', metadata, log)
expect(log.reload.status).to eq 'success'
end
end

context "when the actor does not create the work" do
it "logs the failure" do
expect(actor).to receive(:create).and_return(false)
described_class.perform_later(user, 'GenericWork', metadata, log)
expect(log.reload.status).to eq 'failure'
expect(log.message).to eq "It's broke!"
end
end
end

context "when the actor does not create the work" do
it "logs the failure" do
expect(actor).to receive(:create).and_return(false)
context "with a Valkyrie model", skip: GenericWork < ActiveFedora::Base do
context "when there is a validation error" do
let(:metadata) do
{ keyword: [],
permissions_attributes: [{ "type" => "group", "name" => "public", "access" => "read" }],
visibility: 'open',
uploaded_files: [upload1.id],
resource_type: ['Article'] }
end

it "logs the failure" do
described_class.perform_later(user, 'GenericWork', metadata, log)
expect(log.reload.status).to eq 'failure'
expect(log.message).to eq "Title can't be blank Creator can't be blank"
end
end

it "it creates a work and a file_set" do
described_class.perform_later(user, 'GenericWork', metadata, log)
expect(log.reload.status).to eq 'failure'
expect(log.message).to eq "It's broke!"
work_id = Hyrax.custom_queries.find_ids_by_model(model: GenericWork).first
work = Hyrax.query_service.find_by(id: work_id)
expect(work.title).to eq ['File One']
expect(work.creator).to eq ['Last, First']
expect(work.depositor).to eq user.to_s
expect(work.member_ids.count).to eq 1
expect(log.reload.status).to eq 'success'
end
end
end
Expand Down

0 comments on commit 2d2f11a

Please sign in to comment.