Class: Audited::Audit

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.as_user(user) ⇒ 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.

[View source]

140
141
142
143
144
145
146
# File 'lib/audited/audit.rb', line 140

def self.as_user(user)
  last_audited_user = ::Audited.store[:audited_user]
  ::Audited.store[:audited_user] = user
  yield
ensure
  ::Audited.store[:audited_user] = last_audited_user
end

.assign_revision_attributes(record, attributes) ⇒ Object

[View source]

157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/audited/audit.rb', line 157

def self.assign_revision_attributes(record, attributes)
  attributes.each do |attr, val|
    record = record.dup if record.frozen?

    if record.respond_to?("#{attr}=")
      record.attributes.key?(attr.to_s) ?
        record[attr] = val :
        record.send("#{attr}=", val)
    end
  end
  record
end

.audited_classesObject

Returns the list of classes that are being audited

[View source]

133
134
135
# File 'lib/audited/audit.rb', line 133

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

.collection_cache_key(collection = all) ⇒ Object

use created_at as timestamp cache key

[View source]

171
172
173
# File 'lib/audited/audit.rb', line 171

def self.collection_cache_key(collection = all, *)
  super(collection, :created_at)
end

.reconstruct_attributes(audits) ⇒ Object

[View source]

149
150
151
152
153
154
# File 'lib/audited/audit.rb', line 149

def self.reconstruct_attributes(audits)
  audits.each_with_object({}) do |audit, all|
    all.merge!(audit.new_attributes)
    all[:audit_version] = audit.version
  end
end

Instance Method Details

#ancestorsObject

Return all audits older than the current one.

[View source]

69
70
71
# File 'lib/audited/audit.rb', line 69

def ancestors
  self.class.ascending.auditable_finder(auditable_id, auditable_type).to_version(version)
end

#new_attributesObject

Returns a hash of the changed attributes with the new values

[View source]

83
84
85
86
87
# File 'lib/audited/audit.rb', line 83

def new_attributes
  (audited_changes || {}).each_with_object({}.with_indifferent_access) do |(attr, values), attrs|
    attrs[attr] = (action == "update") ? values.last : values
  end
end

#old_attributesObject

Returns a hash of the changed attributes with the old values

[View source]

90
91
92
93
94
# File 'lib/audited/audit.rb', line 90

def old_attributes
  (audited_changes || {}).each_with_object({}.with_indifferent_access) do |(attr, values), attrs|
    attrs[attr] = (action == "update") ? values.first : values
  end
end

#revisionObject

Return an instance of what the object looked like at this revision. If the object has been destroyed, this will be a new record.

[View source]

75
76
77
78
79
80
# File 'lib/audited/audit.rb', line 75

def revision
  clazz = auditable_type.constantize
  (clazz.find_by_id(auditable_id) || clazz.new).tap do |m|
    self.class.assign_revision_attributes(m, self.class.reconstruct_attributes(ancestors).merge(audit_version: version))
  end
end

#undoObject

Allows user to undo changes

[View source]

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/audited/audit.rb', line 97

def undo
  case action
  when "create"
    # destroys a newly created record
    auditable.destroy!
  when "destroy"
    # creates a new record with the destroyed record attributes
    auditable_type.constantize.create!(audited_changes)
  when "update"
    # changes back attributes
    auditable.update!(audited_changes.transform_values(&:first))
  else
    raise StandardError, "invalid action given #{action}"
  end
end

#user_as_stringObject Also known as: user

[View source]

126
127
128
# File 'lib/audited/audit.rb', line 126

def user_as_string
  user_as_model || username
end

#user_as_string=(user) ⇒ Object Also known as: user=

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

[View source]

115
116
117
118
119
120
121
# File 'lib/audited/audit.rb', line 115

def user_as_string=(user)
  # 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