Skip to content

Instantly share code, notes, and snippets.

@r38y
Created December 30, 2008 18:45
Show Gist options
  • Save r38y/41704 to your computer and use it in GitHub Desktop.
Save r38y/41704 to your computer and use it in GitHub Desktop.
module AuditingExtensions
def self.included( recipient )
recipient.extend( ModelClassMethods )
recipient.class_eval do
include ModelInstanceMethods
end
end
module ModelClassMethods
def audit_me
audit :when => :before_update,
:if => lambda{|m| m.changed?},
:with_message => :audit_message
end
end # class methods
module ModelInstanceMethods
def audit_message
columns = changed - ['updated_at']
columns.map{|a|
was = self.send("#{a}_was")
is = self.send(a)
if is != was
"*#{a}*: #{was} => #{is}"
else
nil
end
}.compact.join(",\n ")
end
end # instance methods
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment