jeremyw-paper_trail 1.2.2 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -49,6 +49,45 @@ module PaperTrail
49
49
  :whodunnit => PaperTrail.whodunnit) if self.class.paper_trail_active
50
50
  end
51
51
 
52
+ # Walk the versions to construct an audit trail of the edits made
53
+ # over time, and by whom.
54
+ def audit_trail options={}
55
+ options[:attributes_to_ignore] ||= %w(updated_at)
56
+
57
+ audit_trail = []
58
+
59
+ versions_desc = versions_including_current_in_descending_order
60
+
61
+ versions_desc.each_with_index do |version, index|
62
+ previous_version = versions_desc[index + 1]
63
+ break if previous_version.nil?
64
+
65
+ attributes_after = yaml_to_hash(version.object)
66
+ attributes_before = yaml_to_hash(previous_version.object)
67
+
68
+ # remove some attributes that we don't need to report
69
+ [attributes_before, attributes_after].each do |hash|
70
+ hash.reject! { |k,v| k.in? Array(options[:attributes_to_ignore]) }
71
+ end
72
+
73
+ audit_trail << {
74
+ :event => previous_version.event,
75
+ :changed_by => transform_whodunnit(previous_version.whodunnit),
76
+ :changed_at => previous_version.created_at,
77
+ :changes => differences(attributes_before, attributes_after)
78
+ }
79
+ end
80
+
81
+ audit_trail
82
+ end
83
+
84
+ protected
85
+
86
+ def transform_whodunnit(whodunnit)
87
+ whodunnit
88
+ end
89
+
90
+
52
91
  private
53
92
 
54
93
  def previous_version
@@ -63,6 +102,28 @@ module PaperTrail
63
102
  def object_to_string(object)
64
103
  object.attributes.to_yaml
65
104
  end
105
+
106
+ def yaml_to_hash(yaml)
107
+ return {} if yaml.nil?
108
+ YAML::load(yaml).to_hash
109
+ end
110
+
111
+ # Returns an array of hashes, where each hash specifies the +:attribute+,
112
+ # value +:before+ the change, and value +:after+ the change.
113
+ def differences(before, after)
114
+ before.diff(after).keys.sort.inject([]) do |diffs, k|
115
+ diff = { :attribute => k, :before => before[k], :after => after[k] }
116
+ diffs << diff; diffs
117
+ end
118
+ end
119
+
120
+ def versions_including_current_in_descending_order
121
+ v = self.versions.dup
122
+ v << Version.new(:event => 'update',
123
+ :object => object_to_string(self),
124
+ :created_at => self.updated_at)
125
+ v.reverse # newest first
126
+ end
66
127
  end
67
128
 
68
129
  end
data/lib/paper_trail.rb CHANGED
@@ -16,44 +16,6 @@ module PaperTrail
16
16
  def self.whodunnit=(value)
17
17
  @@whodunnit = value
18
18
  end
19
-
20
- # Walk the versions to construct an audit trail of the edits made
21
- # over time, and by whom.
22
- def audit_trail options={}
23
- options[:attributes_to_ignore] ||= %w(updated_at)
24
-
25
- audit_trail = []
26
-
27
- versions_desc = versions_including_current_in_descending_order
28
-
29
- versions_desc.each_with_index do |version, index|
30
- previous_version = versions_desc[index + 1]
31
- break if previous_version.nil?
32
-
33
- attributes_after = yaml_to_hash(version.object)
34
- attributes_before = yaml_to_hash(previous_version.object)
35
-
36
- # remove some attributes that we don't need to report
37
- [attributes_before, attributes_after].each do |hash|
38
- hash.reject! { |k,v| k.in? Array(options[:attributes_to_ignore]) }
39
- end
40
-
41
- audit_trail << {
42
- :event => previous_version.event,
43
- :changed_by => transform_whodunnit(previous_version.whodunnit),
44
- :changed_at => previous_version.created_at,
45
- :changes => differences(attributes_before, attributes_after)
46
- }
47
- end
48
-
49
- audit_trail
50
- end
51
-
52
- protected
53
-
54
- def transform_whodunnit(whodunnit)
55
- whodunnit
56
- end
57
19
 
58
20
  private
59
21
 
@@ -63,27 +25,6 @@ module PaperTrail
63
25
  }
64
26
  end
65
27
 
66
- def yaml_to_hash(yaml)
67
- return {} if yaml.nil?
68
- YAML::load(yaml).to_hash
69
- end
70
-
71
- # Returns an array of hashes, where each hash specifies the +:attribute+,
72
- # value +:before+ the change, and value +:after+ the change.
73
- def differences(before, after)
74
- before.diff(after).keys.sort.inject([]) do |diffs, k|
75
- diff = { :attribute => k, :before => before[k], :after => after[k] }
76
- diffs << diff; diffs
77
- end
78
- end
79
-
80
- def versions_including_current_in_descending_order
81
- v = self.versions.dup
82
- v << Version.new(:event => 'update',
83
- :object => object_to_string(self),
84
- :created_at => self.updated_at)
85
- v.reverse # newest first
86
- end
87
28
  end
88
29
 
89
30
  ActionController::Base.send :include, PaperTrail
data/paper_trail.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{paper_trail}
5
- s.version = "1.2.2"
5
+ s.version = "1.2.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Andy Stewart", "Jeremy Weiskotten", "Joe Lind"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jeremyw-paper_trail
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Stewart