Sha256: cb4b86ebeb1806c3183638c4afac330840c868ae7d7daa2c8c4c840ec937dcd1

Contents?: true

Size: 1.53 KB

Versions: 22

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true
require 'dry/monads'

module Hyrax
  module Transactions
    module Steps
      ##
      # Add a resource to collections via `ChangeSet`.
      #
      # Accepts `:collection_ids` and never retrieves the actual collections
      # from the persisted data. We trust that the identifiers passed refer to
      # actual, extant collections, and that the permissions to add to
      # collection membership have been established, or else that they'll be
      # validated prior to save.
      #
      # @since 3.0.0
      class AddToCollections
        include Dry::Monads[:result]

        ##
        # Add to collections by inserting collections to
        # `ChangeSet#member_of_collection_ids`.
        #
        # @param [Hyrax::ChangeSet] change_set
        # @param [Array<#to_s>] collection_ids
        #
        # @return [Dry::Monads::Result]
        def call(change_set, collection_ids: [])
          multi_membership_messages = check_multi_membership(change_set, collection_ids)
          return Failure(multi_membership_messages) if multi_membership_messages.present?

          change_set.member_of_collection_ids += collection_ids
          Success(change_set)
        end

        private

        def check_multi_membership(change_set, collection_ids)
          return if change_set.is_a? Hyrax::Forms::PcdmCollectionForm

          Hyrax::MultipleMembershipChecker
            .new(item: change_set)
            .check(collection_ids: [collection_ids], include_current_members: true)
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
hyrax-5.1.0.pre.beta1 lib/hyrax/transactions/steps/add_to_collections.rb
hyrax-5.0.4 lib/hyrax/transactions/steps/add_to_collections.rb
hyrax-5.0.3 lib/hyrax/transactions/steps/add_to_collections.rb
hyrax-5.0.2 lib/hyrax/transactions/steps/add_to_collections.rb
hyrax-5.0.1 lib/hyrax/transactions/steps/add_to_collections.rb
hyrax-5.0.0 lib/hyrax/transactions/steps/add_to_collections.rb
hyrax-5.0.0.rc3 lib/hyrax/transactions/steps/add_to_collections.rb
hyrax-5.0.0.rc2 lib/hyrax/transactions/steps/add_to_collections.rb
hyrax-5.0.0.rc1 lib/hyrax/transactions/steps/add_to_collections.rb
hyrax-3.6.0 lib/hyrax/transactions/steps/add_to_collections.rb
hyrax-4.0.0 lib/hyrax/transactions/steps/add_to_collections.rb
hyrax-4.0.0.rc3 lib/hyrax/transactions/steps/add_to_collections.rb
hyrax-4.0.0.rc2 lib/hyrax/transactions/steps/add_to_collections.rb
hyrax-4.0.0.rc1 lib/hyrax/transactions/steps/add_to_collections.rb
hyrax-3.5.0 lib/hyrax/transactions/steps/add_to_collections.rb
hyrax-4.0.0.beta2 lib/hyrax/transactions/steps/add_to_collections.rb
hyrax-3.4.2 lib/hyrax/transactions/steps/add_to_collections.rb
hyrax-4.0.0.beta1 lib/hyrax/transactions/steps/add_to_collections.rb
hyrax-3.4.1 lib/hyrax/transactions/steps/add_to_collections.rb
hyrax-3.4.0 lib/hyrax/transactions/steps/add_to_collections.rb