Created
June 2, 2011 13:17
-
-
Save krisleech/1004399 to your computer and use it in GitHub Desktop.
Auditable, Problem with class var's
This file contains 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
# 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