Skip to content

Commit

Permalink
Delete PermissionTemplate when destroying a Hyrax::AdministrativeSet …
Browse files Browse the repository at this point in the history
…or Hyrax collection (#6917)

Co-authored-by: David Moles <dmoles@dmoles.net>
  • Loading branch information
dmolesUC and dmoles authored Oct 14, 2024
1 parent be73c6f commit 3e8c633
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 10 deletions.
3 changes: 2 additions & 1 deletion lib/hyrax/transactions/admin_set_destroy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class AdminSetDestroy < Transaction
DEFAULT_STEPS = ['admin_set_resource.check_default',
'admin_set_resource.check_empty',
'admin_set_resource.delete',
'admin_set_resource.delete_acl'].freeze
'admin_set_resource.delete_acl',
'admin_set_resource.delete_permission_template'].freeze

##
# @see Hyrax::Transactions::Transaction
Expand Down
3 changes: 2 additions & 1 deletion lib/hyrax/transactions/collection_destroy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class CollectionDestroy < Transaction
# TODO: Add step that checks if collection is empty for collections of types that require it
DEFAULT_STEPS = ['collection_resource.delete_acl',
'collection_resource.remove_from_membership',
'collection_resource.delete'].freeze
'collection_resource.delete',
'collection_resource.delete_permission_template'].freeze.freeze

##
# @see Hyrax::Transactions::Transaction
Expand Down
9 changes: 9 additions & 0 deletions lib/hyrax/transactions/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Container # rubocop:disable Metrics/ClassLength
require 'hyrax/transactions/steps/delete_access_control'
require 'hyrax/transactions/steps/delete_all_file_metadata'
require 'hyrax/transactions/steps/delete_all_file_sets'
require 'hyrax/transactions/steps/delete_permission_template'
require 'hyrax/transactions/steps/delete_resource'
require 'hyrax/transactions/steps/ensure_admin_set'
require 'hyrax/transactions/steps/file_metadata_delete'
Expand Down Expand Up @@ -202,6 +203,10 @@ class Container # rubocop:disable Metrics/ClassLength
ops.register 'save_acl' do
Steps::SaveAccessControl.new
end

ops.register 'delete_permission_template' do
Steps::DeletePermissionTemplate.new
end
end

namespace 'collection_resource' do |ops| # Hyrax::PcdmCollection resource
Expand Down Expand Up @@ -229,6 +234,10 @@ class Container # rubocop:disable Metrics/ClassLength
Steps::SaveAccessControl.new
end

ops.register 'delete_permission_template' do
Steps::DeletePermissionTemplate.new
end

ops.register 'save_collection_banner' do
Steps::SaveCollectionBanner.new
end
Expand Down
30 changes: 30 additions & 0 deletions lib/hyrax/transactions/steps/delete_permission_template.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true
require 'dry/monads'

module Hyrax
module Transactions
module Steps
##
# Deletes the Hyrax::PermissionTemplate for a resource.
# If no PermissionTemplate associated with that resource is found, succeeds.
#
# @see https://dry-rb.org/gems/dry-monads/1.0/result/
class DeletePermissionTemplate
include Dry::Monads[:result]

##
# @param [Valkyrie::Resource] obj
#
# @return [Dry::Monads::Result]
def call(obj)
permission_template = Hyrax::PermissionTemplate.find_by(source_id: obj.id)
return Success(obj) if permission_template.nil?

permission_template.destroy || (return Failure[:failed_to_delete_permission_template, permission_template])

Success(obj)
end
end
end
end
end
32 changes: 31 additions & 1 deletion spec/hyrax/transactions/admin_set_destroy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'hyrax/transactions'

RSpec.describe Hyrax::Transactions::AdminSetDestroy, valkyrie_adapter: :test_adapter do
subject(:tx) { described_class.new }
subject(:tx) { described_class.new }
let(:resource) { FactoryBot.valkyrie_create(:hyrax_admin_set) }

describe '#call' do
Expand All @@ -27,5 +27,35 @@
.to include(contain_exactly(member_work))
end
end

context 'with the default admin set' do
it 'is a failure' do
default_admin_set = Hyrax.config.default_admin_set
expect(tx.call(default_admin_set)).to be_failure
end
end

context 'with a permission template' do
let(:resource_with_pt) { FactoryBot.valkyrie_create(:hyrax_admin_set, :with_permission_template) }

describe '#call' do
it 'is a success' do
expect(tx.call(resource_with_pt)).to be_success
end

it 'will destroy the associated permission template' do
# NOTE: We don't just check the PermissionTemplate count, because there are too many
# possible PermissionTemplate-creating side effects to depend the "before" value
tx.call(resource_with_pt)
expect(Hyrax::PermissionTemplate.where(source_id: resource_with_pt.id)).not_to exist
end

it 'succeeds if the associated permission template has already been destroyed' do
permission_template = Hyrax::PermissionTemplate.find_by!(source_id: resource_with_pt.id)
permission_template.destroy!
expect(tx.call(resource_with_pt)).to be_success
end
end
end
end
end
39 changes: 32 additions & 7 deletions spec/hyrax/transactions/collection_destroy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,43 @@
require 'hyrax/transactions'

RSpec.describe Hyrax::Transactions::CollectionDestroy, valkyrie_adapter: :test_adapter do
subject(:tx) { described_class.new }
let(:resource) { FactoryBot.valkyrie_create(:hyrax_collection) }
let(:user) { FactoryBot.create(:user) }

subject(:tx) do
described_class.new
.with_step_args(
'collection_resource.delete' => { user: user },
'collection_resource.remove_from_membership' => { user: user }
)
end

describe '#call' do
it 'is a success' do
expect(
tx
.with_step_args('collection_resource.delete' => { user: user },
'collection_resource.remove_from_membership' => { user: user })
.call(resource)
).to be_success
expect(tx.call(resource)).to be_success
end

context 'with a permission template' do
let(:resource_with_pt) { FactoryBot.valkyrie_create(:hyrax_collection, :with_permission_template) }

describe '#call' do
it 'is a success' do
expect(tx.call(resource_with_pt)).to be_success
end

it 'will destroy the associated permission template' do
# NOTE: We don't just check the PermissionTemplate count, because there are too many
# possible PermissionTemplate-creating side effects to depend the "before" value
tx.call(resource_with_pt)
expect(Hyrax::PermissionTemplate.where(source_id: resource_with_pt.id)).not_to exist
end

it 'succeeds if the associated permission template has already been destroyed' do
permission_template = Hyrax::PermissionTemplate.find_by!(source_id: resource_with_pt.id)
permission_template.destroy!
expect(tx.call(resource_with_pt)).to be_success
end
end
end
end
end

0 comments on commit 3e8c633

Please sign in to comment.