Created
October 25, 2012 23:33
-
-
Save cwsaylor/3956135 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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