Class: Audited::Audit
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Audited::Audit
- Defined in:
- lib/audited/audit.rb
Class Method Summary collapse
-
.as_user(user) ⇒ Object
All audits made during the block called will be recorded as made by
user
. -
.audited_classes ⇒ Object
Returns the list of classes that are being audited.
-
.collection_cache_key(collection = all) ⇒ Object
use created_at as timestamp cache key.
Instance Method Summary collapse
-
#ancestors ⇒ Object
Return all audits older than the current one.
-
#new_attributes ⇒ Object
Returns a hash of the changed attributes with the new values.
-
#old_attributes ⇒ Object
Returns a hash of the changed attributes with the old values.
-
#revision ⇒ Object
Return an instance of what the object looked like at this revision.
-
#undo ⇒ Object
Allows user to undo changes.
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.
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 |
.audited_classes ⇒ Object
Returns the list of classes that are being audited
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
171 172 173 |
# File 'lib/audited/audit.rb', line 171 def self.collection_cache_key(collection = all, *) super(collection, :created_at) end |
Instance Method Details
#ancestors ⇒ Object
Return all audits older than the current one.
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_attributes ⇒ Object
Returns a hash of the changed attributes with the new values
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_attributes ⇒ Object
Returns a hash of the changed attributes with the old values
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 |
#revision ⇒ Object
Return an instance of what the object looked like at this revision. If the object has been destroyed, this will be a new record.
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 |
#undo ⇒ Object
Allows user to undo changes
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 |