paper_trail 1.4.5 → 1.4.6

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.4.4
1
+ 1.4.6
data/lib/paper_trail.rb CHANGED
@@ -1,19 +1,12 @@
1
1
  require 'yaml'
2
2
  require 'paper_trail/config'
3
+ require 'paper_trail/controller'
3
4
  require 'paper_trail/has_paper_trail'
4
5
  require 'paper_trail/version'
5
6
 
7
+ # PaperTrail's module methods can be called in both models and controllers.
6
8
  module PaperTrail
7
9
 
8
- def self.included(base)
9
- base.before_filter :set_whodunnit
10
- end
11
-
12
- # Returns PaperTrail's configuration object.
13
- def self.config
14
- @@config ||= PaperTrail::Config.instance
15
- end
16
-
17
10
  # Switches PaperTrail on or off.
18
11
  def self.enabled=(value)
19
12
  PaperTrail.config.enabled = value
@@ -25,6 +18,11 @@ module PaperTrail
25
18
  !!PaperTrail.config.enabled
26
19
  end
27
20
 
21
+ # Returns PaperTrail's configuration object.
22
+ def self.config
23
+ @@config ||= PaperTrail::Config.instance
24
+ end
25
+
28
26
  # Returns who is reponsible for any changes that occur.
29
27
  def self.whodunnit
30
28
  Thread.current[:whodunnit]
@@ -38,13 +36,4 @@ module PaperTrail
38
36
  Thread.current[:whodunnit] = value
39
37
  end
40
38
 
41
- protected
42
-
43
- # Sets who is responsible for any changes that occur: the controller's
44
- # `current_user`.
45
- def set_whodunnit
46
- Thread.current[:whodunnit] = self.send :current_user rescue nil
47
- end
48
39
  end
49
-
50
- ActionController::Base.send :include, PaperTrail
@@ -0,0 +1,19 @@
1
+ module PaperTrail
2
+ module Controller
3
+
4
+ def self.included(base)
5
+ base.before_filter :set_whodunnit
6
+ end
7
+
8
+ protected
9
+
10
+ # Sets who is responsible for any changes that occur: the controller's
11
+ # `current_user`.
12
+ def set_whodunnit
13
+ ::PaperTrail.whodunnit = self.send :current_user rescue nil
14
+ end
15
+
16
+ end
17
+ end
18
+
19
+ ActionController::Base.send :include, PaperTrail::Controller
@@ -1,117 +1,119 @@
1
1
  module PaperTrail
2
+ module Model
2
3
 
3
- def self.included(base)
4
- base.send :extend, ClassMethods
5
- end
4
+ def self.included(base)
5
+ base.send :extend, ClassMethods
6
+ end
6
7
 
7
8
 
8
- module ClassMethods
9
- # Options:
10
- # :ignore an array of attributes for which a new +Version+ will not be created if only they change.
11
- # :meta a hash of extra data to store. You must add a column to the versions table for each key.
12
- # Values are objects or procs (which are called with +self+, i.e. the model with the paper
13
- # trail).
14
- def has_paper_trail(options = {})
15
- send :include, InstanceMethods
9
+ module ClassMethods
10
+ # Options:
11
+ # :ignore an array of attributes for which a new +Version+ will not be created if only they change.
12
+ # :meta a hash of extra data to store. You must add a column to the versions table for each key.
13
+ # Values are objects or procs (which are called with +self+, i.e. the model with the paper
14
+ # trail).
15
+ def has_paper_trail(options = {})
16
+ send :include, InstanceMethods
16
17
 
17
- cattr_accessor :ignore
18
- self.ignore = (options[:ignore] || []).map &:to_s
18
+ cattr_accessor :ignore
19
+ self.ignore = (options[:ignore] || []).map &:to_s
19
20
 
20
- cattr_accessor :meta
21
- self.meta = options[:meta] || {}
21
+ cattr_accessor :meta
22
+ self.meta = options[:meta] || {}
22
23
 
23
- # Indicates whether or not PaperTrail is active for this class.
24
- # This is independent of whether PaperTrail is globally enabled or disabled.
25
- cattr_accessor :paper_trail_active
26
- self.paper_trail_active = true
24
+ # Indicates whether or not PaperTrail is active for this class.
25
+ # This is independent of whether PaperTrail is globally enabled or disabled.
26
+ cattr_accessor :paper_trail_active
27
+ self.paper_trail_active = true
27
28
 
28
- has_many :versions, :as => :item, :order => 'created_at ASC, id ASC'
29
+ has_many :versions, :as => :item, :order => 'created_at ASC, id ASC'
29
30
 
30
- after_create :record_create
31
- before_update :record_update
32
- after_destroy :record_destroy
33
- end
31
+ after_create :record_create
32
+ before_update :record_update
33
+ after_destroy :record_destroy
34
+ end
34
35
 
35
- # Switches PaperTrail off for this class.
36
- def paper_trail_off
37
- self.paper_trail_active = false
38
- end
36
+ # Switches PaperTrail off for this class.
37
+ def paper_trail_off
38
+ self.paper_trail_active = false
39
+ end
39
40
 
40
- # Switches PaperTrail on for this class.
41
- def paper_trail_on
42
- self.paper_trail_active = true
41
+ # Switches PaperTrail on for this class.
42
+ def paper_trail_on
43
+ self.paper_trail_active = true
44
+ end
43
45
  end
44
- end
45
46
 
46
47
 
47
- module InstanceMethods
48
- def record_create
49
- if switched_on?
50
- versions.create merge_metadata(:event => 'create', :whodunnit => PaperTrail.whodunnit)
48
+ module InstanceMethods
49
+ def record_create
50
+ if switched_on?
51
+ versions.create merge_metadata(:event => 'create', :whodunnit => PaperTrail.whodunnit)
52
+ end
51
53
  end
52
- end
53
54
 
54
- def record_update
55
- if switched_on? && changed_and_we_care?
56
- versions.build merge_metadata(:event => 'update',
57
- :object => object_to_string(previous_version),
58
- :whodunnit => PaperTrail.whodunnit)
55
+ def record_update
56
+ if switched_on? && changed_and_we_care?
57
+ versions.build merge_metadata(:event => 'update',
58
+ :object => object_to_string(previous_version),
59
+ :whodunnit => PaperTrail.whodunnit)
60
+ end
59
61
  end
60
- end
61
62
 
62
- def record_destroy
63
- if switched_on?
64
- versions.create merge_metadata(:event => 'destroy',
65
- :object => object_to_string(previous_version),
66
- :whodunnit => PaperTrail.whodunnit)
63
+ def record_destroy
64
+ if switched_on?
65
+ versions.create merge_metadata(:event => 'destroy',
66
+ :object => object_to_string(previous_version),
67
+ :whodunnit => PaperTrail.whodunnit)
68
+ end
67
69
  end
68
- end
69
70
 
70
- # Returns the object (not a Version) as it was at the given timestamp.
71
- def version_at(timestamp)
72
- # Short-circuit if the current state is applicable.
73
- return self if self.updated_at <= timestamp
74
- # Look for the first version created after, rather than before, the
75
- # timestamp because a version stores how the object looked before the
76
- # change.
77
- version = versions.first :conditions => ['created_at > ?', timestamp],
78
- :order => 'created_at ASC'
79
- version.reify if version
80
- end
71
+ # Returns the object (not a Version) as it was at the given timestamp.
72
+ def version_at(timestamp)
73
+ # Short-circuit if the current state is applicable.
74
+ return self if self.updated_at <= timestamp
75
+ # Look for the first version created after, rather than before, the
76
+ # timestamp because a version stores how the object looked before the
77
+ # change.
78
+ version = versions.first :conditions => ['created_at > ?', timestamp],
79
+ :order => 'created_at ASC'
80
+ version.reify if version
81
+ end
81
82
 
82
- private
83
+ private
83
84
 
84
- def merge_metadata(data)
85
- meta.each do |k,v|
86
- data[k] = v.respond_to?(:call) ? v.call(self) : v
85
+ def merge_metadata(data)
86
+ meta.each do |k,v|
87
+ data[k] = v.respond_to?(:call) ? v.call(self) : v
88
+ end
89
+ data
87
90
  end
88
- data
89
- end
90
91
 
91
- def previous_version
92
- previous = self.clone
93
- previous.id = id
94
- changes.each do |attr, ary|
95
- previous.send "#{attr}=", ary.first
92
+ def previous_version
93
+ previous = self.clone
94
+ previous.id = id
95
+ changes.each do |attr, ary|
96
+ previous.send "#{attr}=", ary.first
97
+ end
98
+ previous
96
99
  end
97
- previous
98
- end
99
100
 
100
- def object_to_string(object)
101
- object.attributes.to_yaml
102
- end
101
+ def object_to_string(object)
102
+ object.attributes.to_yaml
103
+ end
103
104
 
104
- def changed_and_we_care?
105
- changed? and !(changed - self.class.ignore).empty?
106
- end
105
+ def changed_and_we_care?
106
+ changed? and !(changed - self.class.ignore).empty?
107
+ end
107
108
 
108
- # Returns `true` if PaperTrail is globally enabled and active for this class,
109
- # `false` otherwise.
110
- def switched_on?
111
- PaperTrail.enabled? && self.class.paper_trail_active
109
+ # Returns `true` if PaperTrail is globally enabled and active for this class,
110
+ # `false` otherwise.
111
+ def switched_on?
112
+ PaperTrail.enabled? && self.class.paper_trail_active
113
+ end
112
114
  end
113
- end
114
115
 
116
+ end
115
117
  end
116
118
 
117
- ActiveRecord::Base.send :include, PaperTrail
119
+ ActiveRecord::Base.send :include, PaperTrail::Model
data/paper_trail.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{paper_trail}
8
- s.version = "1.4.5"
8
+ s.version = "1.4.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Andy Stewart"]
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
27
27
  "install.rb",
28
28
  "lib/paper_trail.rb",
29
29
  "lib/paper_trail/config.rb",
30
+ "lib/paper_trail/controller.rb",
30
31
  "lib/paper_trail/has_paper_trail.rb",
31
32
  "lib/paper_trail/version.rb",
32
33
  "paper_trail.gemspec",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paper_trail
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.5
4
+ version: 1.4.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Stewart
@@ -34,6 +34,7 @@ files:
34
34
  - install.rb
35
35
  - lib/paper_trail.rb
36
36
  - lib/paper_trail/config.rb
37
+ - lib/paper_trail/controller.rb
37
38
  - lib/paper_trail/has_paper_trail.rb
38
39
  - lib/paper_trail/version.rb
39
40
  - paper_trail.gemspec