Class: Hyrax::Transactions::Steps::Save
- Inherits:
-
Object
- Object
- Hyrax::Transactions::Steps::Save
- 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
-
#call(change_set, user: nil) ⇒ Dry::Monads::Result
‘Success(work)` if the change_set is applied and the resource is saved; `Failure([#to_s, change_set.resource])`, otherwise.
-
#initialize(persister: Hyrax.persister, publisher: Hyrax.publisher) ⇒ Save
constructor
A new instance of Save.
Constructor Details
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.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/hyrax/transactions/steps/save.rb', line 32 def call(change_set, user: nil) begin new_collections = changed_collection_membership(change_set) unsaved = change_set.sync saved = @persister.save(resource: unsaved) rescue StandardError => err return Failure(["Failed save on #{change_set}\n\t#{err.}", change_set.resource]) 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..acl. = unsaved..acl. 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 |