Skip to content

Instantly share code, notes, and snippets.

@krisleech
Created June 2, 2011 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krisleech/1004399 to your computer and use it in GitHub Desktop.
Save krisleech/1004399 to your computer and use it in GitHub Desktop.
Auditable, Problem with class var's
# Fixed
module Auditable
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def auditable(options = {})
options[:only] ||= [:create, :update, :destroy]
options[:if] ||= lambda { |record| true }
options[:only_once] ||= false
class_eval do
cattr_accessor :only_once
self.only_once = options[:only_once]
has_one :audit_item, :as => :auditable
after_create :audit_create, :if => options[:if] if options[:only].include?(:create)
after_destroy :audit_destroy, :if => options[:if] if options[:only].include?(:destroy)
after_update :audit_update, :if => options[:if] if options[:only].include?(:update)
end
end
private
def audit(action)
return if self.class.only_once && self.audit_item
AuditItem.create(:auditable => self, :tag => "#{self.class.to_s}_#{action}".downcase, :user => self.student)
end
def audit_create
audit(:create)
end
def audit_destroy
audit(:destroy)
end
def audit_update
audit(:update) if self.changed? && self.changed != ['updated_at']
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment