Module: Audited::Auditor::AuditedInstanceMethods

Defined in:
lib/audited/auditor.rb

Constant Summary collapse

REDACTED =
"[REDACTED]"

Instance Method Summary collapse

Instance Method Details

#audited_attributesObject

List of attributes that are audited.



174
175
176
177
178
179
# File 'lib/audited/auditor.rb', line 174

def audited_attributes
  audited_attributes = attributes.except(*self.class.non_audited_columns)
  audited_attributes = redact_values(audited_attributes)
  audited_attributes = filter_encrypted_attrs(audited_attributes)
  normalize_enum_changes(audited_attributes)
end

#combine_audits(audits_to_combine) ⇒ Object

Combine multiple audits into one.



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/audited/auditor.rb', line 189

def combine_audits(audits_to_combine)
  combine_target = audits_to_combine.last
  combine_target.audited_changes = audits_to_combine.pluck(:audited_changes).reduce(&:merge)
  combine_target.comment = "#{combine_target.comment}\nThis audit is the result of multiple audits being combined."

  transaction do
    begin
      combine_target.save!
      audits_to_combine.unscope(:limit).where("version < ?", combine_target.version).delete_all
    rescue ActiveRecord::Deadlocked
      # Ignore Deadlocks, if the same record is getting its old audits combined more than once at the same time then
      # both combining operations will be the same. Ignoring this error allows one of the combines to go through successfully.
    end
  end
end

#own_and_associated_auditsObject

Returns a list combined of record audits and associated audits.



182
183
184
185
186
# File 'lib/audited/auditor.rb', line 182

def own_and_associated_audits
  Audited.audit_class.unscoped.where(auditable: self)
    .or(Audited.audit_class.unscoped.where(associated: self))
    .order(created_at: :desc)
end

#revision(version) ⇒ Object

Get a specific revision specified by the version number, or :previous Returns nil for versions greater than revisions count



161
162
163
164
165
# File 'lib/audited/auditor.rb', line 161

def revision(version)
  if version == :previous || audits.last.version >= version
    revision_with Audited.audit_class.reconstruct_attributes(audits_to(version))
  end
end

#revision_at(date_or_time) ⇒ Object

Find the oldest revision recorded prior to the date/time provided.



168
169
170
171
# File 'lib/audited/auditor.rb', line 168

def revision_at(date_or_time)
  audits = self.audits.up_until(date_or_time)
  revision_with Audited.audit_class.reconstruct_attributes(audits) unless audits.empty?
end

#revisions(from_version = 1) ⇒ Object

Gets an array of the revisions available

user.revisions.each do |revision|
  user.name
  user.version
end


145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/audited/auditor.rb', line 145

def revisions(from_version = 1)
  return [] unless audits.from_version(from_version).exists?

  all_audits = audits.select([:audited_changes, :version, :action]).to_a
  targeted_audits = all_audits.select { |audit| audit.version >= from_version }

  previous_attributes = reconstruct_attributes(all_audits - targeted_audits)

  targeted_audits.map do |audit|
    previous_attributes.merge!(audit.new_attributes)
    revision_with(previous_attributes.merge!(version: audit.version))
  end
end

#save_with_auditingObject

Temporarily turns on auditing while saving.



124
125
126
# File 'lib/audited/auditor.rb', line 124

def save_with_auditing
  with_auditing { save }
end

#save_without_auditingObject

Temporarily turns off auditing while saving.



109
110
111
# File 'lib/audited/auditor.rb', line 109

def save_without_auditing
  without_auditing { save }
end

#with_auditing(&block) ⇒ Object

Executes the block with the auditing callbacks enabled.

@foo.with_auditing do
  @foo.save
end


134
135
136
# File 'lib/audited/auditor.rb', line 134

def with_auditing(&block)
  self.class.with_auditing(&block)
end

#without_auditing(&block) ⇒ Object

Executes the block with the auditing callbacks disabled.

@foo.without_auditing do
  @foo.save
end


119
120
121
# File 'lib/audited/auditor.rb', line 119

def without_auditing(&block)
  self.class.without_auditing(&block)
end