Class: Auditable::Base
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Auditable::Base
- Defined in:
- lib/auditable/base.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#diff(other_audit) ⇒ Object
Diffing two audits’ modifications.
-
#diff_since(time) ⇒ Object
Diff this audit with the latest audit created before the ‘time` variable passed.
-
#diff_since_version(version) ⇒ Object
Diff this audit with the latest audit created before this version.
-
#latest_diff(options = {}) ⇒ Object
Diff this audit with the one created immediately before it.
- #relevant_attributes ⇒ Object
- #same_audited_content?(other_audit) ⇒ Boolean
Instance Method Details
#diff(other_audit) ⇒ Object
Diffing two audits’ modifications
Returns a hash containing arrays of the form
{
:key_1 => [<value_in_other_audit>, <value_in_this_audit>],
:key_2 => [<value_in_other_audit>, <value_in_this_audit>],
:other_audit_own_key => [<value_in_other_audit>, nil],
:this_audio_own_key => [nil, <value_in_this_audit>]
}
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/auditable/base.rb', line 22 def diff(other_audit) other_modifications = other_audit ? other_audit.modifications : {} {}.tap do |d| # find keys present only in this audit (self.modifications.keys - other_modifications.keys).each do |k| d[k] = [nil, self.modifications[k]] if self.modifications[k] end # find keys present only in other audit (other_modifications.keys - self.modifications.keys).each do |k| d[k] = [other_modifications[k], nil] if other_modifications[k] end # find common keys and diff values self.modifications.keys.each do |k| if self.modifications[k] != other_modifications[k] d[k] = [other_modifications[k], self.modifications[k]] end end end end |
#diff_since(time) ⇒ Object
Diff this audit with the latest audit created before the ‘time` variable passed
72 73 74 75 76 |
# File 'lib/auditable/base.rb', line 72 def diff_since(time) other_audit = auditable.audits.where("created_at <= ? AND id != ?", time, id).order("id DESC").limit(1).first diff(other_audit) end |
#diff_since_version(version) ⇒ Object
Diff this audit with the latest audit created before this version
79 80 81 82 83 |
# File 'lib/auditable/base.rb', line 79 def diff_since_version(version) other_audit = auditable.audits.where("version <= ? AND id != ?", version, id).order("version DESC").limit(1).first diff(other_audit) end |
#latest_diff(options = {}) ⇒ Object
Diff this audit with the one created immediately before it
See #diff for more details
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/auditable/base.rb', line 48 def latest_diff( = {}) if .present? scoped = auditable.class.audited_version ? auditable.audits.order("version DESC") : auditable.audits.order("id DESC") if tag = .delete(:tag) scoped = scoped.where(:tag => tag) end if changed_by = .delete(:changed_by) scoped = scoped.where(:user_id => changed_by.id, :user_type => changed_by.class.name) end if audit_tag = .delete(:audit_tag) scoped = scoped.where(:tag => audit_tag) end diff scoped.first else if auditable.class.audited_version diff_since_version(version) else diff_since(created_at) end end end |
#relevant_attributes ⇒ Object
94 95 96 |
# File 'lib/auditable/base.rb', line 94 def relevant_attributes attributes.slice("modifications", "tag", "user").reject {|k,v| v.blank? } end |
#same_audited_content?(other_audit) ⇒ Boolean
90 91 92 |
# File 'lib/auditable/base.rb', line 90 def same_audited_content?(other_audit) other_audit and relevant_attributes == other_audit.relevant_attributes end |