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, publisher: Hyrax.publisher) ⇒ Save

Returns a new instance of Save.

Since:

  • 2.4.0



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

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

Instance Method Details

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

rubocop:disable Metrics/MethodLength, Metrics/AbcSize

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hyrax/transactions/steps/save.rb', line 33

def call(change_set, user: nil)
  begin
    valid_future_date?(change_set.lease, 'lease_expiration_date') if change_set.respond_to?(:lease) && change_set.lease
    valid_future_date?(change_set.embargo, 'embargo_release_date') if change_set.respond_to?(:embargo) && change_set.embargo
    new_collections = changed_collection_membership(change_set)

    unsaved = change_set.sync
    save_lease_or_embargo(unsaved)
    saved = @persister.save(resource: unsaved)
  rescue StandardError => err
    return Failure.new(["Failed save on #{change_set}\n\t#{err.message}", change_set.resource], err.backtrace.first)
  end

  # 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)

  publish_changes(resource: saved, user: user, new: unsaved.new_record, new_collections: new_collections)
  Success(saved)
end