Skip to content

Instantly share code, notes, and snippets.

@tomas-stefano
Created October 21, 2010 19:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomas-stefano/639115 to your computer and use it in GitHub Desktop.
Save tomas-stefano/639115 to your computer and use it in GitHub Desktop.
before_changes
module BeforeChanges
def before_changes
before_change_object = self.clone
changes.each do |changed_field, change|
before_change_object.send("#{changed_field}=", change[0])
end
before_change_object
end
end
module ActiveRecord
class Base
include BeforeChanges
end
end
class Post < ActiveRecord::Base
end
>> post = Post.first
#<Post:0x3434> {
:title => 'Joker'
}
>> post.title = 'Joker owned Batman'
'Joker owned Batman'
>> post.before_changes
#<Post:0x5678> {
:title => 'Joker'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment