Module: Espinita::AuditorBehavior::ClassMethods

Defined in:
lib/espinita/auditor_behavior.rb

Constant Summary collapse

@@default_excluded =
%w(lock_version created_at updated_at created_on updated_on)

Instance Method Summary collapse

Instance Method Details

#as_user(user, &block) ⇒ Object

All audits made during the block called will be recorded as made by user. This method is hopefully threadsafe, making it ideal for background operations that require audit information.



50
51
52
53
54
55
# File 'lib/espinita/auditor_behavior.rb', line 50

def as_user(user, &block)
  RequestStore.store[:audited_user] = user
  yield
ensure
  RequestStore.store[:audited_user] = nil
end

#auditable(options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/espinita/auditor_behavior.rb', line 15

def auditable(options = {})

  self.audit_callbacks = []
  self.audit_callbacks << options[:on] unless options[:on].blank?
  self.audit_callbacks.flatten!

  after_create   :audit_create  if self.audit_callbacks.blank? || self.audit_callbacks.include?(:create)
  before_update  :audit_update  if self.audit_callbacks.blank? || self.audit_callbacks.include?(:update)
  before_destroy :audit_destroy if self.audit_callbacks.blank? || self.audit_callbacks.include?(:destroy)

  self.excluded_cols   = (@@default_excluded)

  if options[:only]
    options[:only] = [options[:only]].flatten.map { |x| x.to_s }
    self.excluded_cols = (self.column_names - options[:only] )
  end

  if options[:except]
    options[:except] = [options[:except]].flatten.map { |x| x.to_s }
    self.excluded_cols = (@@default_excluded) + options[:except]
  end

  has_many :audits, :as => :auditable, :class_name => Espinita::Audit.name
  #attr_accessor :audited_user, :audited_ip
  accepts_nested_attributes_for :audits

end

#permited_columnsObject



43
44
45
# File 'lib/espinita/auditor_behavior.rb', line 43

def permited_columns
  self.column_names - self.excluded_cols.to_a
end