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.



190
191
192
193
194
195
# File 'lib/audited/auditor.rb', line 190

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.



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/audited/auditor.rb', line 205

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.



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

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



177
178
179
180
181
# File 'lib/audited/auditor.rb', line 177

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.



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

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


161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/audited/auditor.rb', line 161

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.



140
141
142
# File 'lib/audited/auditor.rb', line 140

def save_with_auditing
  with_auditing { save }
end

#save_without_auditingObject

Temporarily turns off auditing while saving.



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

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


150
151
152
# File 'lib/audited/auditor.rb', line 150

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


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

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