Module: Audited::Auditor::ClassMethods

Defined in:
lib/audited/auditor.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_audited(options = {}) ⇒ Object

Configuration options

  • only - Only audit the given attributes

  • except - Excludes fields from being saved in the audit log. By default, Audited will audit all but these fields:

    [self.primary_key, inheritance_column, 'lock_version', 'created_at', 'updated_at']
    

    You can add to those by passing one or an array of fields to skip.

    class User < ActiveRecord::Base
      audited except: :password
    end
    
  • require_comment - Ensures that audit_comment is supplied before any create, update or destroy operation.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/audited/auditor.rb', line 37

def acts_as_audited(options = {})
  # don't allow multiple calls
  return if included_modules.include?(Audited::Auditor::AuditedInstanceMethods)

  class_attribute :audit_associated_with,   instance_writer: false
  class_attribute :audited_options,       instance_writer: false

  self.audited_options = options
  self.audit_associated_with = options[:associated_with]

  if options[:comment_required]
    validates_presence_of :audit_comment, if: :auditing_enabled
    before_destroy :require_comment
  end

  attr_accessor :audit_comment

  has_many :audits, -> { order(version: :asc) }, as: :auditable, class_name: Audit.name
  Audit.audited_class_names << to_s

  on = Array(options[:on])
  after_create :audit_create    if on.empty? || on.include?(:create)
  before_update :audit_update   if on.empty? || on.include?(:update)
  before_destroy :audit_destroy if on.empty? || on.include?(:destroy)

  # Define and set after_audit and around_audit callbacks. This might be useful if you want
  # to notify a party after the audit has been created or if you want to access the newly-created
  # audit.
  define_callbacks :audit
  set_callback :audit, :after, :after_audit, if: lambda { respond_to?(:after_audit, true) }
  set_callback :audit, :around, :around_audit, if: lambda { respond_to?(:around_audit, true) }

  attr_accessor :version

  extend Audited::Auditor::AuditedClassMethods
  include Audited::Auditor::AuditedInstanceMethods

  self.auditing_enabled = true
end

#default_ignored_attributesObject



81
82
83
# File 'lib/audited/auditor.rb', line 81

def default_ignored_attributes
  [primary_key, inheritance_column]
end

#has_associated_auditsObject



77
78
79
# File 'lib/audited/auditor.rb', line 77

def has_associated_audits
  has_many :associated_audits, as: :associated, class_name: Audit.name
end