Sha256: 0705615ed77b6af6e0f8971506b7074a85ba562b7ba8e0a9bffcf4e19aae1c54

Contents?: true

Size: 1016 Bytes

Versions: 15

Compression:

Stored size: 1016 Bytes

Contents

# frozen_string_literal: true
require 'dry/monads'

module Hyrax
  module Transactions
    module Steps
      ##
      # Adds a work to a parent work
      #
      # @see https://wiki.lyrasis.org/display/samvera/Hydra::Works+Shared+Modeling
      class AddToParent
        include Dry::Monads[:result]

        ##
        # @param [Hyrax::Work] obj
        # @param [#to_s] parent_id
        #
        # @return [Dry::Monads::Result]
        def call(obj, parent_id: nil, user: nil)
          return Success(obj) if parent_id.blank?

          parent = Hyrax.query_service.find_by(id: parent_id)
          parent.member_ids << obj.id
          Hyrax.persister.save(resource: parent)

          user ||= ::User.find_by_user_key(obj.depositor)
          Hyrax.publisher.publish('object.metadata.updated', object: parent, user: user)

          Success(obj)
        rescue Valkyrie::Persistence::ObjectNotFoundError => _err
          Failure[:parent_object_not_found, parent_id]
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
hyrax-5.0.0.rc1 lib/hyrax/transactions/steps/add_to_parent.rb
hyrax-3.6.0 lib/hyrax/transactions/steps/add_to_parent.rb
hyrax-4.0.0 lib/hyrax/transactions/steps/add_to_parent.rb
hyrax-4.0.0.rc3 lib/hyrax/transactions/steps/add_to_parent.rb
hyrax-4.0.0.rc2 lib/hyrax/transactions/steps/add_to_parent.rb
hyrax-4.0.0.rc1 lib/hyrax/transactions/steps/add_to_parent.rb
hyrax-3.5.0 lib/hyrax/transactions/steps/add_to_parent.rb
hyrax-4.0.0.beta2 lib/hyrax/transactions/steps/add_to_parent.rb
hyrax-3.4.2 lib/hyrax/transactions/steps/add_to_parent.rb
hyrax-4.0.0.beta1 lib/hyrax/transactions/steps/add_to_parent.rb
hyrax-3.4.1 lib/hyrax/transactions/steps/add_to_parent.rb
hyrax-3.4.0 lib/hyrax/transactions/steps/add_to_parent.rb
hyrax-3.3.0 lib/hyrax/transactions/steps/add_to_parent.rb
hyrax-3.2.0 lib/hyrax/transactions/steps/add_to_parent.rb
hyrax-3.1.0 lib/hyrax/transactions/steps/add_to_parent.rb