Skip to content

Instantly share code, notes, and snippets.

@cwsaylor
Created October 25, 2012 23:33
Show Gist options
  • Save cwsaylor/3956135 to your computer and use it in GitHub Desktop.
Save cwsaylor/3956135 to your computer and use it in GitHub Desktop.
# To Audit Foo model, create a model called Foo::BarLog with a foreign key of bar_id and include this concern
module Auditable
extend ActiveSupport::Concern
included do
after_save :audit
has_many :audits, class_name: "#{self}Log"
end
module InstanceMethods
def audit
foreign_key = self.class.name.foreign_key
"#{self.class.name}Log".constantize.create self.serializable_hash.except("id").merge({"#{foreign_key}" => self.id})
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment