Sha256: b064d05860d2c9c65f8b9f2b226a0683d8939a1b5afadb6e504abb967fc16980

Contents?: true

Size: 1.39 KB

Versions: 4

Compression:

Stored size: 1.39 KB

Contents

# 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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hyrax-5.0.1 lib/hyrax/transactions/steps/remove_from_membership.rb
hyrax-5.0.0 lib/hyrax/transactions/steps/remove_from_membership.rb
hyrax-5.0.0.rc3 lib/hyrax/transactions/steps/remove_from_membership.rb
hyrax-5.0.0.rc2 lib/hyrax/transactions/steps/remove_from_membership.rb