Class: VelocityAudited::Audit
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- VelocityAudited::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.
136 137 138 139 140 141 142 |
# File 'lib/audited/audit.rb', line 136 def self.as_user(user) last_audited_user = ::VelocityAudited.store[:audited_user] ::VelocityAudited.store[:audited_user] = user yield ensure ::VelocityAudited.store[:audited_user] = last_audited_user end |
.audited_classes ⇒ Object
Returns the list of classes that are being audited
129 130 131 |
# File 'lib/audited/audit.rb', line 129 def self.audited_classes audited_class_names.map(&:constantize) end |
.collection_cache_key(collection = all) ⇒ Object
use created_at as timestamp cache key
167 168 169 |
# File 'lib/audited/audit.rb', line 167 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.
65 66 67 |
# File 'lib/audited/audit.rb', line 65 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
79 80 81 82 83 |
# File 'lib/audited/audit.rb', line 79 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
86 87 88 89 90 |
# File 'lib/audited/audit.rb', line 86 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.
71 72 73 74 75 76 |
# File 'lib/audited/audit.rb', line 71 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
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/audited/audit.rb', line 93 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 |