Module: Audit::Tracking
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/audit/tracking.rb
Overview
Methods for tracking changes to your models by creating audit records for every atomic change. Including this module adds callbacks which create audit records every time a model object is changed and saved.
Instance Method Summary collapse
-
#audit ⇒ Object
Creates a new audit record for this model object using data returned by ActiveRecord::Base#changes.
-
#audit_bucket ⇒ Object
Generates the bucket name for the model class.
-
#audit_metadata(metadata = {}) ⇒ Object
Public: Store audit metadata for the next write.
-
#audits ⇒ Object
Public: fetch audit records for a model object.
-
#clear_skip ⇒ Object
Public: Write audit metadata for the next write.
-
#revert(changesets) ⇒ Object
Public: Restore a model’s attributes by reversing a series of changesets.
-
#revert_to(changeset) ⇒ Object
Public: Restore the model’s attributes to the state recorded by a changeset.
-
#skip_audit ⇒ Object
Public: Skip writing audit meatadata for the next write.
-
#skip_audit? ⇒ Boolean
Public: Flag indicating whether metadata is logged on the next write.
Instance Method Details
#audit ⇒ Object
Creates a new audit record for this model object using data returned by ActiveRecord::Base#changes.
Returns nothing.
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/audit/tracking.rb', line 30 def audit if skip_audit? clear_skip return end data = {"changes" => changes, "metadata" => } = Time.now.utc.iso8601(3) Audit::Tracking.log.record(audit_bucket, self.id, , data) = {} end |
#audit_bucket ⇒ Object
Generates the bucket name for the model class.
Returns a Symbol-ified and pluralized version of the model’s name.
45 46 47 |
# File 'lib/audit/tracking.rb', line 45 def audit_bucket self.class.name.pluralize.to_sym end |
#audit_metadata(metadata = {}) ⇒ Object
Public: Store audit metadata for the next write.
metadata - a Hash of data that is written alongside the change data
Returns nothing.
54 55 56 |
# File 'lib/audit/tracking.rb', line 54 def (={}) = .try(:update, ) || end |
#audits ⇒ Object
Public: fetch audit records for a model object.
Returns an Array of Changeset objects.
22 23 24 |
# File 'lib/audit/tracking.rb', line 22 def audits Audit::Tracking.log.audits(audit_bucket, self.id) end |
#clear_skip ⇒ Object
Public: Write audit metadata for the next write.
Returns: nothing.
68 69 70 |
# File 'lib/audit/tracking.rb', line 68 def clear_skip @skip = false end |
#revert(changesets) ⇒ Object
Public: Restore a model’s attributes by reversing a series of changesets.
changesets - the changesets to undo
Returns nothing.
97 98 99 100 101 102 |
# File 'lib/audit/tracking.rb', line 97 def revert(changesets) changesets.each do |changeset| revert_to(changeset) end nil end |
#revert_to(changeset) ⇒ Object
Public: Restore the model’s attributes to the state recorded by a changeset.
changeset - the changes to undo
Returns nothing.
85 86 87 88 89 90 |
# File 'lib/audit/tracking.rb', line 85 def revert_to(changeset) changeset.changes.each do |change| write_attribute(change.attribute, change.old_value) end nil end |
#skip_audit ⇒ Object
Public: Skip writing audit meatadata for the next write.
Returns: nothing.
61 62 63 |
# File 'lib/audit/tracking.rb', line 61 def skip_audit @skip = true end |
#skip_audit? ⇒ Boolean
Public: Flag indicating whether metadata is logged on the next write.
Returns: nothing.
75 76 77 |
# File 'lib/audit/tracking.rb', line 75 def skip_audit? @skip ||= false end |