Skip to content

Instantly share code, notes, and snippets.

@laserlemon
Created November 14, 2017 16:28
Show Gist options
  • Save laserlemon/6bd187bae1ab73d3aea50e21ca77a8a4 to your computer and use it in GitHub Desktop.
Save laserlemon/6bd187bae1ab73d3aea50e21ca77a8a4 to your computer and use it in GitHub Desktop.
Updating a record with rollback to original attributes
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