Module: ActsAsAudited::Auditor::ClassMethods

Defined in:
lib/acts_as_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, acts_as_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
      acts_as_audited :except => :password
    end
    
  • protect - If your model uses attr_protected, set this to false to prevent Rails from raising an error. If you declare attr_accessible before calling acts_as_audited, it will automatically default to false. You only need to explicitly set this if you are calling attr_accessible after.

  • require_comment - Ensures that audit_comment is supplied before any create, update or destroy operation.

    class User < ActiveRecord::Base
      acts_as_audited :protect => false
      attr_accessible :name
    end
    


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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/acts_as_audited/auditor.rb', line 49

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

  options = {:protect => accessible_attributes.empty?}.merge(options)

  class_attribute :non_audited_columns, :instance_writer => false
  class_attribute :auditing_enabled, :instance_writer => false
  class_attribute :audit_associated_with, :instance_writer => false

  if options[:only]
    except = self.column_names - options[:only].flatten.map(&:to_s)
  else
    except = [self.primary_key, inheritance_column] + ActsAsAudited.ignored_attributes
    except |= Array(options[:except]).collect(&:to_s) if options[:except]
  end
  self.non_audited_columns = except
  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
  unless accessible_attributes.empty? || options[:protect]
    attr_accessible :audit_comment
  end

  has_many :audits, :as => :auditable
  attr_protected :audit_ids if options[:protect]
  Audit.audited_class_names << self.to_s

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

  attr_accessor :version

  extend ActsAsAudited::Auditor::SingletonMethods
  include ActsAsAudited::Auditor::InstanceMethods

  self.auditing_enabled = true
end

#has_associated_auditsObject



94
95
96
# File 'lib/acts_as_audited/auditor.rb', line 94

def has_associated_audits
  has_many :associated_audits, :as => :associated, :class_name => "Audit"
end