Class: Audit

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/acts_as_audited/audit.rb

Overview

Audit saves the changes to ActiveRecord models. It has the following attributes:

  • auditable: the ActiveRecord model that was changed

  • user: the user that performed the change; a string or an ActiveRecord model

  • action: one of create, update, or delete

  • changes: a serialized hash of all the changes

  • created_at: Time that the change was performed

Class Method Summary collapse

Instance Method Summary collapse

Class 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.



29
30
31
32
33
# File 'lib/acts_as_audited/audit.rb', line 29

def as_user(user, &block)
  Thread.current[:acts_as_audited_user] = user
  yield
  Thread.current[:acts_as_audited_user] = nil
end

.assign_revision_attributes(record, attributes) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/acts_as_audited/audit.rb', line 108

def self.assign_revision_attributes(record, attributes)
  attributes.each do |attr, val|
    if record.respond_to?("#{attr}=")
      record.attributes.has_key?(attr.to_s) ?
        record[attr] = val :
        record.send("#{attr}=", val)
    end
  end
  record
end

.audited_classesObject



22
23
24
# File 'lib/acts_as_audited/audit.rb', line 22

def audited_classes
  self.audited_class_names.map(&:constantize)
end

.manual_audit(user, action, on_behalf_of = nil, auditable = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/acts_as_audited/audit.rb', line 35

def manual_audit(user, action, on_behalf_of = nil, auditable = nil)
  attribs = { :action => action }

  case user
  when ActiveRecord::Base
    attribs[CollectiveIdea::Acts::Audited.human_model] = user
  when String
    attribs[:username] = user
  end

  case auditable
  when ActiveRecord::Base
    attribs[:auditable] = auditable
  when String
    attribs[:auditable_type] = auditable
  end

  attribs[:on_behalf_of] = on_behalf_of if on_behalf_of
  Audit.create attribs
end

.reconstruct_attributes(audits) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/acts_as_audited/audit.rb', line 99

def self.reconstruct_attributes(audits)
  attributes = {}
  result = audits.collect do |audit|
    attributes.merge!(audit.new_attributes).merge!(:version => audit.version)
    yield attributes if block_given?
  end
  block_given? ? result : attributes
end

Instance Method Details

#ancestorsObject



77
78
79
80
81
# File 'lib/acts_as_audited/audit.rb', line 77

def ancestors
  self.class.find(:all, :order => 'version',
    :conditions => ['auditable_id = ? and auditable_type = ? and version <= ?',
    auditable_id, auditable_type, version])
end

#new_attributesObject

Returns a hash of the changed attributes with the new values



84
85
86
87
88
89
# File 'lib/acts_as_audited/audit.rb', line 84

def new_attributes
  (changes || {}).inject({}.with_indifferent_access) do |attrs,(attr,values)|
    attrs[attr] = Array(values).last
    attrs
  end
end

#old_attributesObject

Returns a hash of the changed attributes with the old values



92
93
94
95
96
97
# File 'lib/acts_as_audited/audit.rb', line 92

def old_attributes
  (changes || {}).inject({}.with_indifferent_access) do |attrs,(attr,values)|
    attrs[attr] = Array(values).first
    attrs
  end
end

#revisionObject



70
71
72
73
74
75
# File 'lib/acts_as_audited/audit.rb', line 70

def revision
  clazz = auditable_type.constantize
  returning clazz.find_by_id(auditable_id) || clazz.new do |m|
    Audit.assign_revision_attributes(m, self.class.reconstruct_attributes(ancestors).merge({:version => version}))
  end
end

#user_as_stringObject

:nodoc:



66
67
68
# File 'lib/acts_as_audited/audit.rb', line 66

def user_as_string #:nodoc:
  self.user_as_model || self.username
end

#user_as_string=(user) ⇒ Object

Allows user to be set to either a string or an ActiveRecord object



58
59
60
61
62
63
64
# File 'lib/acts_as_audited/audit.rb', line 58

def user_as_string=(user) #:nodoc:
  # reset both either way
  self.user_as_model = self.username = nil
  user.is_a?(ActiveRecord::Base) ?
    self.user_as_model = user :
    self.username = user
end