Class: Hyrax::Transactions::Steps::Save

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

Overview

Saves a given work from a change_set, returning a `Dry::Monads::Result` (`Success`|`Failure`).

If the save is successful, publishes an `object.metadata.updated` event for the affected resource.

Instance Method Summary collapse

Constructor Details

#initialize(persister: Hyrax.persister) ⇒ Save

Returns a new instance of Save.

Since:

  • 2.4.0


20
21
22
# File 'lib/hyrax/transactions/steps/save.rb', line 20

def initialize(persister: Hyrax.persister)
  @persister = persister
end

Instance Method Details

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

Returns `Success(work)` if the change_set is applied and the resource is saved; `Failure([#to_s, change_set.resource])`, otherwise.

Parameters:

Returns:

  • (Dry::Monads::Result)

    `Success(work)` if the change_set is applied and the resource is saved; `Failure([#to_s, change_set.resource])`, otherwise.

Since:

  • 2.4.0


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hyrax/transactions/steps/save.rb', line 31

def call(change_set, user: nil)
  unsaved = change_set.sync
  saved = @persister.save(resource: unsaved)

  # if we have a permission manager, it's acting as a local cache of another resource.
  # we want to resync changes that we had in progress so we can persist them later.
  saved.permission_manager.acl.permissions = unsaved.permission_manager.acl.permissions if
    unsaved.respond_to?(:permission_manager)

  user ||= ::User.find_by_user_key(saved.depositor)
  Hyrax.publisher.publish('object.deposited', object: saved, user: user) if unsaved.new_record
  Hyrax.publisher.publish('object.metadata.updated', object: saved, user: user)

  Success(saved)
rescue StandardError => err
  Failure([err.message, change_set.resource])
end