Sha256: 96d7e1e868dd7eb4eff2c28495fda29036d124e1cc6908b37a67e029be2781b5

Contents?: true

Size: 1.08 KB

Versions: 4

Compression:

Stored size: 1.08 KB

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)
          Hyrax.publisher.publish('object.membership.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

4 entries across 4 versions & 1 rubygems

Version Path
hyrax-5.0.2 lib/hyrax/transactions/steps/add_to_parent.rb
hyrax-5.0.1 lib/hyrax/transactions/steps/add_to_parent.rb
hyrax-5.0.0 lib/hyrax/transactions/steps/add_to_parent.rb
hyrax-5.0.0.rc3 lib/hyrax/transactions/steps/add_to_parent.rb