Skip to content

Commit

Permalink
Simplify the undo logic (collectiveidea#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
krainboltgreene authored and tbrisker committed Apr 8, 2018
1 parent 4116a78 commit ed4e2ec
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions lib/audited/audit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,18 @@ def old_attributes

# Allows user to undo changes
def undo
model = self.auditable_type.constantize
if action == 'create'
case action
when 'create'
# destroys a newly created record
model.find(auditable_id).destroy!
elsif action == 'destroy'
auditable.destroy!
when 'destroy'
# creates a new record with the destroyed record attributes
model.create(audited_changes)
else
auditable_type.constantize.create!(audited_changes)
when 'update'
# changes back attributes
audited_object = model.find(auditable_id)
self.audited_changes.each do |k, v|
audited_object[k] = v[0]
end
audited_object.save
auditable.update_attributes!(audited_changes.transform_values(&:first))
else
raise StandardError, "invalid action given #{action}"
end
end

Expand Down

0 comments on commit ed4e2ec

Please sign in to comment.