-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6357 from samvera/remove_file_set_relatives_work_…
…del_6334 Removes FileSet related objects when work deleted (#6334).
- Loading branch information
Showing
17 changed files
with
236 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: true | ||
require 'dry/monads' | ||
|
||
module Hyrax | ||
module Transactions | ||
module Steps | ||
## | ||
# Deletes a resource's member FileSets from the persister, returning a `Dry::Monads::Result` | ||
# (`Success`|`Failure`). | ||
# | ||
# @see https://dry-rb.org/gems/dry-monads/1.0/result/ | ||
class DeleteAllFileSets | ||
include Dry::Monads[:result] | ||
|
||
## | ||
# @params [#save] persister | ||
def initialize(query_service: Hyrax.query_service, persister: Hyrax.persister, publisher: Hyrax.publisher) | ||
@persister = persister | ||
@query_service = query_service | ||
@publisher = publisher | ||
end | ||
|
||
## | ||
# @param [Valkyrie::Resource] resource | ||
# @param [::User] the user resposible for the delete action | ||
# | ||
# @return [Dry::Monads::Result] | ||
def call(resource, user: nil) | ||
return Failure(:resource_not_persisted) unless resource.persisted? | ||
|
||
@query_service.custom_queries.find_child_file_sets(resource: resource).each do |file_set| | ||
return Failure[:failed_to_delete_file_set, file_set] unless | ||
Hyrax::Transactions::Container['file_set.destroy'] | ||
.with_step_args('file_set.remove_from_work' => { user: user }, | ||
'file_set.delete' => { user: user }) | ||
.call(file_set).success? | ||
rescue ::Ldp::Gone | ||
nil | ||
end | ||
|
||
Success(resource) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
spec/hyrax/transactions/steps/delete_all_file_metadata_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
require 'spec_helper' | ||
require 'hyrax/transactions' | ||
|
||
RSpec.describe Hyrax::Transactions::Steps::DeleteAllFileMetadata, valkyrie_adapter: :test_adapter, storage_adapter: :test_disk do | ||
subject(:step) { described_class.new } | ||
let(:file_set) { FactoryBot.valkyrie_create(:hyrax_file_set, :with_files) } | ||
|
||
describe '#call' do | ||
it 'gives success' do | ||
expect(step.call(file_set)).to be_success | ||
end | ||
|
||
it 'destroys each file_set' do | ||
file_ids = file_set.file_ids | ||
step.call(file_set) | ||
file_ids.each do |id| | ||
expect { Hyrax.query_service.find_by(id: id) }.to raise_error Valkyrie::Persistence::ObjectNotFoundError | ||
end | ||
end | ||
|
||
context 'with a resource that is not saved' do | ||
let(:file_set) { FactoryBot.build(:hyrax_file_set) } | ||
|
||
it 'is a failure' do | ||
expect(step.call(file_set)).to be_failure | ||
end | ||
end | ||
end | ||
end |
35 changes: 35 additions & 0 deletions
35
spec/hyrax/transactions/steps/delete_all_file_sets_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
require 'spec_helper' | ||
require 'hyrax/transactions' | ||
|
||
RSpec.describe Hyrax::Transactions::Steps::DeleteAllFileSets, valkyrie_adapter: :test_adapter do | ||
subject(:step) { described_class.new } | ||
let(:user) { FactoryBot.create(:user) } | ||
let(:work) { FactoryBot.valkyrie_create(:hyrax_work, :with_member_file_sets) } | ||
|
||
describe '#call' do | ||
it 'fails without a user' do | ||
expect(step.call(work)).to be_failure | ||
end | ||
|
||
it 'gives success' do | ||
expect(step.call(work, user: user)).to be_success | ||
end | ||
|
||
it 'destroys each file_set' do | ||
member_ids = work.member_ids | ||
step.call(work, user: user) | ||
member_ids.each do |id| | ||
expect { Hyrax.query_service.find_by(id: id) }.to raise_error Valkyrie::Persistence::ObjectNotFoundError | ||
end | ||
end | ||
|
||
context 'with a resource that is not saved' do | ||
let(:work) { FactoryBot.build(:hyrax_work) } | ||
|
||
it 'is a failure' do | ||
expect(step.call(work)).to be_failure | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# frozen_string_literal: true | ||
require 'spec_helper' | ||
require 'hyrax/transactions' | ||
|
||
RSpec.describe Hyrax::Transactions::WorkDestroy do | ||
subject(:transaction) { described_class.new } | ||
let(:work) { FactoryBot.valkyrie_create(:hyrax_work, read_users: [user], members: [file_set]) } | ||
let(:file_set) { FactoryBot.valkyrie_create(:hyrax_file_set) } | ||
|
||
describe '#call' do | ||
let(:user) { FactoryBot.create(:user) } | ||
|
||
context 'without a user' do | ||
it 'is a failure' do | ||
expect(transaction.call(work)).to be_failure | ||
end | ||
end | ||
|
||
it 'succeeds' do | ||
expect(transaction.with_step_args('work_resource.delete_all_file_sets' => { user: user }).call(work)) | ||
.to be_success | ||
end | ||
|
||
it 'deletes the work' do | ||
transaction.with_step_args('work_resource.delete_all_file_sets' => { user: user }).call(work) | ||
|
||
expect { Hyrax.query_service.find_by(id: work.id) } | ||
.to raise_error Valkyrie::Persistence::ObjectNotFoundError | ||
end | ||
|
||
it 'deletes the file set' do | ||
transaction.with_step_args('work_resource.delete_all_file_sets' => { user: user }).call(work) | ||
|
||
expect { Hyrax.query_service.find_by(id: file_set.id) } | ||
.to raise_error Valkyrie::Persistence::ObjectNotFoundError | ||
end | ||
|
||
it 'deletes the access control resource' do | ||
expect { transaction.with_step_args('work_resource.delete_all_file_sets' => { user: user }).call(work) } | ||
.to change { Hyrax::AccessControl.for(resource: work).persisted? } | ||
.from(true).to(false) | ||
end | ||
|
||
context "with attached files" do | ||
let(:work) { FactoryBot.valkyrie_create(:hyrax_work, uploaded_files: [FactoryBot.create(:uploaded_file)], edit_users: [user]) } | ||
let(:file_set) { query_service.find_members(resource: work).first } | ||
let(:file_metadata) { query_service.custom_queries.find_file_metadata_by(id: file_set.file_ids.first) } | ||
let(:uploaded) { storage_adapter.find_by(id: file_metadata.file_identifier) } | ||
let(:storage_adapter) { Hyrax.storage_adapter } | ||
let(:query_service) { Hyrax.query_service } | ||
it "deletes them" do | ||
file_metadata | ||
transaction.with_step_args('work_resource.delete_all_file_sets' => { user: user }).call(work) | ||
|
||
expect { Hyrax.query_service.find_by(id: file_metadata.id) }.to raise_error Valkyrie::Persistence::ObjectNotFoundError | ||
expect { storage_adapter.find_by(id: uploaded.id) }.to raise_error Valkyrie::StorageAdapter::FileNotFound | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.