Class: Hyrax::Transactions::Steps::AddToParent

Inherits:
Object
  • Object
show all
Defined in:
lib/hyrax/transactions/steps/add_to_parent.rb

Overview

Adds a work to a parent work

Instance Method Summary collapse

Instance Method Details

#call(obj, parent_id: nil, user: nil) ⇒ Dry::Monads::Result

Parameters:

  • obj (Hyrax::Work)
  • parent_id (#to_s) (defaults to: nil)

Returns:

  • (Dry::Monads::Result)

Since:

  • 2.4.0



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hyrax/transactions/steps/add_to_parent.rb', line 19

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