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.



254
255
256
257
258
259
# File 'lib/audited/auditor.rb', line 254

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.



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/audited/auditor.rb', line 271

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.



262
263
264
265
266
267
268
# File 'lib/audited/auditor.rb', line 262

def own_and_associated_audits
  Audited.audit_class.unscoped
    .includes(:audit_associates)
    .where(auditable: self)
    .or(Audited.audit_class.unscoped.includes(:audit_associates).where(audit_associates: { 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



241
242
243
244
245
# File 'lib/audited/auditor.rb', line 241

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.



248
249
250
251
# File 'lib/audited/auditor.rb', line 248

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


225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/audited/auditor.rb', line 225

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.



204
205
206
# File 'lib/audited/auditor.rb', line 204

def save_with_auditing
  with_auditing { save }
end

#save_without_auditingObject

Temporarily turns off auditing while saving.



189
190
191
# File 'lib/audited/auditor.rb', line 189

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


214
215
216
# File 'lib/audited/auditor.rb', line 214

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


199
200
201
# File 'lib/audited/auditor.rb', line 199

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