Feature Request for ActiveModel::Dirty - a "model_was" and a "revert_changes" methods

Hi, I’ve encountered a need to see the model’s state before all the changes that were made to it.

I fantasized on something like this:

“model.was”

or

“model.before_changes”

Also, I would like to have an option to revert all changes without reloading from DB. Something like:

“model.revert_changes”

I want to make a pull request with the following solution:

def was

result = self.dup.tap { |model| model.id = self.id }

self.changes.each {|attr, values| result[attr] = values[0]}

result

end

def revert_changes

self.changes.each {|attr, values| self[attr] = values[0]}

end

Any thoughts on the subject?

  • Regards,

– Adam Klein

500tech.com

Another option I thought about - revert_changes will return the model before the changes but without changing the object

revert_changes! will revert the changes on the object itself and return it