-
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.
Browse files
Browse the repository at this point in the history
* Fixes the deletion of collections 500 (#6362). * Reworks the logic into a Transaction::Step. * rubocop appeasement. * Transfers listener rspec to step rspec. * rspec tweak * rubocop tweak
- Loading branch information
Showing
9 changed files
with
111 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
require 'dry/monads' | ||
|
||
module Hyrax | ||
module Transactions | ||
module Steps | ||
## | ||
# Removes a collection from its members, returning a `Dry::Monads::Result` | ||
# (`Success`|`Failure`). | ||
# | ||
# @see https://dry-rb.org/gems/dry-monads/1.0/result/ | ||
class RemoveFromMembership | ||
include Dry::Monads[:result] | ||
|
||
## | ||
# @params [#save] persister | ||
def initialize(query_service: Hyrax.custom_queries, 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(collection, user: nil) | ||
return Failure(:resource_not_persisted) unless collection.persisted? | ||
return Failure(:user_not_present) if user.blank? | ||
|
||
@query_service.find_members_of(collection: collection).each do |member| | ||
member.member_of_collection_ids -= [collection.id] | ||
@persister.save(resource: member) | ||
@publisher.publish('collection.membership.updated', collection: collection, user: user) | ||
rescue StandardError | ||
nil | ||
end | ||
|
||
Success(collection) | ||
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
41 changes: 41 additions & 0 deletions
41
spec/hyrax/transactions/steps/remove_from_membership_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,41 @@ | ||
# frozen_string_literal: true | ||
require 'spec_helper' | ||
require 'hyrax/transactions' | ||
require 'hyrax/specs/spy_listener' | ||
|
||
RSpec.describe Hyrax::Transactions::Steps::RemoveFromMembership, valkyrie_adapter: :test_adapter do | ||
subject(:step) { described_class.new } | ||
let(:user) { FactoryBot.create(:user) } | ||
let(:collection) { FactoryBot.valkyrie_create(:hyrax_collection) } | ||
let(:work) { FactoryBot.valkyrie_create(:monograph, member_of_collection_ids: [collection.id]) } | ||
let(:spy_listener) { Hyrax::Specs::SpyListener.new } | ||
|
||
describe '#call' do | ||
before do | ||
work | ||
Hyrax.publisher.subscribe(spy_listener) | ||
end | ||
after { Hyrax.publisher.unsubscribe(spy_listener) } | ||
|
||
it 'fails without a user' do | ||
expect(step.call(collection)).to be_failure | ||
end | ||
|
||
it 'gives success' do | ||
expect(step.call(collection, user: user)).to be_success | ||
end | ||
|
||
it 'removes collection references from member objects' do | ||
expect { step.call(collection, user: user) } | ||
.to change { Hyrax.custom_queries.find_members_of(collection: collection).size } | ||
.from(1) | ||
.to(0) | ||
end | ||
|
||
it 'publishes events' do | ||
expect { step.call(collection, user: user) } | ||
.to change { spy_listener.collection_membership_updated&.payload } | ||
.to match(collection: collection, user: user) | ||
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