Created
November 14, 2017 16:28
-
-
Save laserlemon/6bd187bae1ab73d3aea50e21ca77a8a4 to your computer and use it in GitHub Desktop.
Updating a record with rollback to original attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UpdateUser | |
include Interactor | |
# Receives: user - A User record to update. | |
# new_attributes - A hash of attributes to be saved to the user. | |
# Updates: user | |
# Provides: old_attributes - A hash of only those user attributes that | |
# changed and their original values. | |
def call | |
context.user.attributes = context.new_attributes | |
context.old_attributes = context.user.changed_attributes | |
unless context.user.save | |
context.fail!(errors: context.user.errors) | |
end | |
end | |
def rollback | |
context.user.update(context.old_attributes) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment