Class: Audit
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Audit
- Defined in:
- lib/acts_as_audited_rails3/audit.rb
Overview
Audit saves the changes to ActiveRecord models. It has the following attributes:
-
auditable
: the ActiveRecord model that was changed -
user
: the user that performed the change; a string or an ActiveRecord model -
action
: one of create, update, or delete -
audit_changes
: a serialized hash of all the changes -
created_at
: Time that the change was performed
Class Method Summary collapse
-
.as_user(user, &block) ⇒ Object
All audits made during the block called will be recorded as made by
user
. - .assign_revision_attributes(record, attributes) ⇒ Object
- .audited_classes ⇒ Object
- .reconstruct_attributes(audits) ⇒ Object
Instance Method Summary collapse
- #ancestors ⇒ 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
-
#user_as_string ⇒ Object
(also: #user)
:nodoc:.
-
#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, &block) ⇒ 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.
29 30 31 32 33 34 35 |
# File 'lib/acts_as_audited_rails3/audit.rb', line 29 def self.as_user(user, &block) Thread.current[:acts_as_audited_user] = user yield Thread.current[:acts_as_audited_user] = nil end |
.assign_revision_attributes(record, attributes) ⇒ Object
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/acts_as_audited_rails3/audit.rb', line 92 def self.assign_revision_attributes(record, attributes) attributes.each do |attr, val| if record.respond_to?("#{attr}=") record.attributes.has_key?(attr.to_s) ? record[attr] = val : record.send("#{attr}=", val) end end record end |
.audited_classes ⇒ Object
22 23 24 |
# File 'lib/acts_as_audited_rails3/audit.rb', line 22 def self.audited_classes self.audited_class_names.map(&:constantize) end |
.reconstruct_attributes(audits) ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/acts_as_audited_rails3/audit.rb', line 83 def self.reconstruct_attributes(audits) attributes = {} result = audits.collect do |audit| attributes.merge!(audit.new_attributes).merge!(:version => audit.version) yield attributes if block_given? end block_given? ? result : attributes end |
Instance Method Details
#ancestors ⇒ Object
61 62 63 64 65 |
# File 'lib/acts_as_audited_rails3/audit.rb', line 61 def ancestors self.class.find(:all, :order => 'version', :conditions => ['auditable_id = ? and auditable_type = ? and version <= ?', auditable_id, auditable_type, version]) end |
#new_attributes ⇒ Object
Returns a hash of the changed attributes with the new values
68 69 70 71 72 73 |
# File 'lib/acts_as_audited_rails3/audit.rb', line 68 def new_attributes (audit_changes || {}).inject({}.with_indifferent_access) do |attrs,(attr,values)| attrs[attr] = values.is_a?(Array) ? values.last : values attrs end end |
#old_attributes ⇒ Object
Returns a hash of the changed attributes with the old values
76 77 78 79 80 81 |
# File 'lib/acts_as_audited_rails3/audit.rb', line 76 def old_attributes (audit_changes || {}).inject({}.with_indifferent_access) do |attrs,(attr,values)| attrs[attr] = Array(values).first attrs end end |
#revision ⇒ Object
54 55 56 57 58 59 |
# File 'lib/acts_as_audited_rails3/audit.rb', line 54 def revision clazz = auditable_type.constantize returning clazz.find_by_id(auditable_id) || clazz.new do |m| Audit.assign_revision_attributes(m, self.class.reconstruct_attributes(ancestors).merge({:version => version})) end end |
#user_as_string ⇒ Object Also known as: user
:nodoc:
48 49 50 |
# File 'lib/acts_as_audited_rails3/audit.rb', line 48 def user_as_string #:nodoc: self.user_as_model || self.username end |
#user_as_string=(user) ⇒ Object Also known as: user=
Allows user to be set to either a string or an ActiveRecord object
38 39 40 41 42 43 44 |
# File 'lib/acts_as_audited_rails3/audit.rb', line 38 def user_as_string=(user) #:nodoc: # 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 |