Sha256: 37ca84445919e25f50f57baec5f98ecf231733b88cbfa7123cb3e89c8ea5c8aa

Contents?: true

Size: 1.42 KB

Versions: 9

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true
require 'dry/monads'

module Hyrax
  module Transactions
    module Steps
      ##
      # removes the file set from all its parents, returning a
      # `Dry::Monads::Result` (`Success`|`Failure`).
      #
      # there should normally be only one parent for a FileSet, but in the case
      # that there are multiple, this step will remove the file set from all
      # parents.
      #
      # if no user is provided to attribute the removal to, the step fails
      # immediately.
      #
      # @see https://dry-rb.org/gems/dry-monads/1.0/result/
      class RemoveFileSetFromWork
        include Dry::Monads[:result]

        ##
        # @param [Valkyrie::QueryService] query_service
        def initialize(query_service: Hyrax.query_service, persister: Hyrax.persister)
          @persister     = persister
          @query_service = query_service
        end

        ##
        # @param [Hyrax::FileSet] file_set
        #
        # @return [Dry::Monads::Result]
        def call(file_set, user: nil)
          return Failure('No user provided.') if user.nil?

          @query_service.find_parents(resource: file_set).each do |parent|
            parent.member_ids -= [file_set.id]
            saved = @persister.save(resource: parent)
            Hyrax.publisher.publish('object.metadata.updated', object: saved, user: user)
          end

          Success(file_set)
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
hyrax-3.6.0 lib/hyrax/transactions/steps/remove_file_set_from_work.rb
hyrax-3.5.0 lib/hyrax/transactions/steps/remove_file_set_from_work.rb
hyrax-3.4.2 lib/hyrax/transactions/steps/remove_file_set_from_work.rb
hyrax-4.0.0.beta1 lib/hyrax/transactions/steps/remove_file_set_from_work.rb
hyrax-3.4.1 lib/hyrax/transactions/steps/remove_file_set_from_work.rb
hyrax-3.4.0 lib/hyrax/transactions/steps/remove_file_set_from_work.rb
hyrax-3.3.0 lib/hyrax/transactions/steps/remove_file_set_from_work.rb
hyrax-3.2.0 lib/hyrax/transactions/steps/remove_file_set_from_work.rb
hyrax-3.1.0 lib/hyrax/transactions/steps/remove_file_set_from_work.rb