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. - .assign_revision_attributes(record, attributes) ⇒ Object
-
.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.
- .reconstruct_attributes(audits) ⇒ Object
Instance Method Summary collapse
-
#ancestors ⇒ Object
Return all audits older than the current one.
- #associates ⇒ Object
-
#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.
- #user_as_string ⇒ Object (also: #user)
-
#user_as_string=(user) ⇒ Object
(also: #user=)
Allows user to be set to either a string or an ActiveRecord object.
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.
142 143 144 145 146 147 148 |
# File 'lib/audited/audit.rb', line 142 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 |
.assign_revision_attributes(record, attributes) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/audited/audit.rb', line 159 def self.assign_revision_attributes(record, attributes) attributes.each do |attr, val| record = record.dup if record.frozen? if record.respond_to?("#{attr}=") record.attributes.key?(attr.to_s) ? record[attr] = val : record.send("#{attr}=", val) end end record end |
.audited_classes ⇒ Object
Returns the list of classes that are being audited
135 136 137 |
# File 'lib/audited/audit.rb', line 135 def self.audited_classes audited_class_names.map(&:constantize) end |
.collection_cache_key(collection = all) ⇒ Object
use created_at as timestamp cache key
173 174 175 |
# File 'lib/audited/audit.rb', line 173 def self.collection_cache_key(collection = all, *) super(collection, :created_at) end |
.reconstruct_attributes(audits) ⇒ Object
151 152 153 154 155 156 |
# File 'lib/audited/audit.rb', line 151 def self.reconstruct_attributes(audits) audits.each_with_object({}) do |audit, all| all.merge!(audit.new_attributes) all[:audit_version] = audit.version end end |
Instance Method Details
#ancestors ⇒ Object
Return all audits older than the current one.
67 68 69 |
# File 'lib/audited/audit.rb', line 67 def ancestors self.class.ascending.auditable_finder(auditable_id, auditable_type).to_version(version) end |
#associates ⇒ Object
71 72 73 |
# File 'lib/audited/audit.rb', line 71 def associates audit_associates.map(&:associated) end |
#new_attributes ⇒ Object
Returns a hash of the changed attributes with the new values
85 86 87 88 89 |
# File 'lib/audited/audit.rb', line 85 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
92 93 94 95 96 |
# File 'lib/audited/audit.rb', line 92 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.
77 78 79 80 81 82 |
# File 'lib/audited/audit.rb', line 77 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
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/audited/audit.rb', line 99 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 |
#user_as_string ⇒ Object Also known as: user
128 129 130 |
# File 'lib/audited/audit.rb', line 128 def user_as_string user_as_model || username end |
#user_as_string=(user) ⇒ Object Also known as: user=
Allows user to be set to either a string or an ActiveRecord object
117 118 119 120 121 122 123 |
# File 'lib/audited/audit.rb', line 117 def user_as_string=(user) # reset both either way self.user_as_model = self.username = nil user.is_a?(::ActiveRecord::Base) ? self.user_as_model = user : self.username = user end |