paper_trail 1.4.3 → 1.4.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +36 -5
- data/VERSION +1 -1
- data/lib/paper_trail.rb +18 -0
- data/lib/paper_trail/config.rb +11 -0
- data/lib/paper_trail/has_paper_trail.rb +13 -3
- data/paper_trail.gemspec +2 -1
- data/test/paper_trail_model_test.rb +27 -12
- metadata +2 -1
data/README.md
CHANGED
@@ -14,7 +14,8 @@ PaperTrail lets you track changes to your models' data. It's good for auditing
|
|
14
14
|
* Automatically records who was responsible if your controller has a `current_user` method.
|
15
15
|
* Allows you to set who is responsible at model-level (useful for migrations).
|
16
16
|
* Allows you to store arbitrary metadata with each version (useful for filtering versions).
|
17
|
-
* Can be turned off/on (useful for migrations).
|
17
|
+
* Can be turned off/on per class (useful for migrations).
|
18
|
+
* Can be turned off/on globally (useful for testing).
|
18
19
|
* No configuration necessary.
|
19
20
|
* Stores everything in a single database table (generates migration for you).
|
20
21
|
* Thoroughly tested.
|
@@ -171,11 +172,9 @@ Why would you do this? In this example, `author_id` is an attribute of `Article
|
|
171
172
|
|
172
173
|
## Turning PaperTrail Off/On
|
173
174
|
|
174
|
-
Sometimes you don't want to store changes. Perhaps you are only interested in changes made
|
175
|
-
by your users and don't need to store changes you make yourself in, say, a migration.
|
175
|
+
Sometimes you don't want to store changes. Perhaps you are only interested in changes made by your users and don't need to store changes you make yourself in, say, a migration -- or when testing your application.
|
176
176
|
|
177
|
-
If you are about change some widgets and you don't want a paper trail of your changes, you can
|
178
|
-
turn PaperTrail off like this:
|
177
|
+
If you are about change some widgets and you don't want a paper trail of your changes, you can turn PaperTrail off like this:
|
179
178
|
|
180
179
|
>> Widget.paper_trail_off
|
181
180
|
|
@@ -183,6 +182,38 @@ And on again like this:
|
|
183
182
|
|
184
183
|
>> Widget.paper_trail_on
|
185
184
|
|
185
|
+
You can also disable PaperTrail for all models:
|
186
|
+
|
187
|
+
>> PaperTrail.enabled = false
|
188
|
+
|
189
|
+
For example, you might want to disable PaperTrail in your Rails application's test environment to speed up your tests. This will do it:
|
190
|
+
|
191
|
+
# in config/environments/test.rb
|
192
|
+
config.after_initialize do
|
193
|
+
PaperTrail.enabled = false
|
194
|
+
end
|
195
|
+
|
196
|
+
If you disable PaperTrail in your test environment but want to enable it for specific tests, you can add a helper like this to your test helper:
|
197
|
+
|
198
|
+
# in test/test_helper.rb
|
199
|
+
def with_versioning
|
200
|
+
was_enabled = PaperTrail.enabled?
|
201
|
+
PaperTrail.enabled = true
|
202
|
+
begin
|
203
|
+
yield
|
204
|
+
ensure
|
205
|
+
PaperTrail.enabled = was_enabled
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
And then use it in your tests like this:
|
210
|
+
|
211
|
+
test "something that needs versioning" do
|
212
|
+
with_versioning do
|
213
|
+
# your test
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
186
217
|
|
187
218
|
## Installation
|
188
219
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.4.
|
1
|
+
1.4.4
|
data/lib/paper_trail.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'yaml'
|
2
|
+
require 'paper_trail/config'
|
2
3
|
require 'paper_trail/has_paper_trail'
|
3
4
|
require 'paper_trail/version'
|
4
5
|
|
@@ -8,6 +9,23 @@ module PaperTrail
|
|
8
9
|
base.before_filter :set_whodunnit
|
9
10
|
end
|
10
11
|
|
12
|
+
# Returns PaperTrail's configuration object.
|
13
|
+
def self.config
|
14
|
+
@@config ||= PaperTrail::Config.instance
|
15
|
+
end
|
16
|
+
|
17
|
+
# Switches PaperTrail on or off.
|
18
|
+
def self.enabled=(value)
|
19
|
+
PaperTrail.config.enabled = value
|
20
|
+
end
|
21
|
+
|
22
|
+
# Returns `true` if PaperTrail is on, `false` otherwise.
|
23
|
+
# PaperTrail is enabled by default.
|
24
|
+
def self.enabled?
|
25
|
+
!!PaperTrail.config.enabled
|
26
|
+
end
|
27
|
+
|
28
|
+
# Returns who is reponsible for any changes that occur.
|
11
29
|
def self.whodunnit
|
12
30
|
Thread.current[:whodunnit]
|
13
31
|
end
|
@@ -20,6 +20,8 @@ module PaperTrail
|
|
20
20
|
cattr_accessor :meta
|
21
21
|
self.meta = options[:meta] || {}
|
22
22
|
|
23
|
+
# Indicates whether or not PaperTrail is active for this class.
|
24
|
+
# This is independent of whether PaperTrail is globally enabled or disabled.
|
23
25
|
cattr_accessor :paper_trail_active
|
24
26
|
self.paper_trail_active = true
|
25
27
|
|
@@ -30,10 +32,12 @@ module PaperTrail
|
|
30
32
|
after_destroy :record_destroy
|
31
33
|
end
|
32
34
|
|
35
|
+
# Switches PaperTrail off for this class.
|
33
36
|
def paper_trail_off
|
34
37
|
self.paper_trail_active = false
|
35
38
|
end
|
36
39
|
|
40
|
+
# Switches PaperTrail on for this class.
|
37
41
|
def paper_trail_on
|
38
42
|
self.paper_trail_active = true
|
39
43
|
end
|
@@ -42,13 +46,13 @@ module PaperTrail
|
|
42
46
|
|
43
47
|
module InstanceMethods
|
44
48
|
def record_create
|
45
|
-
if
|
49
|
+
if switched_on?
|
46
50
|
versions.create merge_metadata(:event => 'create', :whodunnit => PaperTrail.whodunnit)
|
47
51
|
end
|
48
52
|
end
|
49
53
|
|
50
54
|
def record_update
|
51
|
-
if
|
55
|
+
if switched_on? && changed_and_we_care?
|
52
56
|
versions.build merge_metadata(:event => 'update',
|
53
57
|
:object => object_to_string(previous_version),
|
54
58
|
:whodunnit => PaperTrail.whodunnit)
|
@@ -56,7 +60,7 @@ module PaperTrail
|
|
56
60
|
end
|
57
61
|
|
58
62
|
def record_destroy
|
59
|
-
if
|
63
|
+
if switched_on?
|
60
64
|
versions.create merge_metadata(:event => 'destroy',
|
61
65
|
:object => object_to_string(previous_version),
|
62
66
|
:whodunnit => PaperTrail.whodunnit)
|
@@ -100,6 +104,12 @@ module PaperTrail
|
|
100
104
|
def changed_and_we_care?
|
101
105
|
changed? and !(changed - self.class.ignore).empty?
|
102
106
|
end
|
107
|
+
|
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
|
112
|
+
end
|
103
113
|
end
|
104
114
|
|
105
115
|
end
|
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.
|
8
|
+
s.version = "1.4.4"
|
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"]
|
@@ -26,6 +26,7 @@ Gem::Specification.new do |s|
|
|
26
26
|
"init.rb",
|
27
27
|
"install.rb",
|
28
28
|
"lib/paper_trail.rb",
|
29
|
+
"lib/paper_trail/config.rb",
|
29
30
|
"lib/paper_trail/has_paper_trail.rb",
|
30
31
|
"lib/paper_trail/version.rb",
|
31
32
|
"paper_trail.gemspec",
|
@@ -214,9 +214,7 @@ class HasPaperTrailModelTest < Test::Unit::TestCase
|
|
214
214
|
end
|
215
215
|
|
216
216
|
should 'handle datetimes' do
|
217
|
-
|
218
|
-
format = '%a, %d %b %Y %H:%M:%S %z' # :rfc822
|
219
|
-
assert_equal @date_time.utc.strftime(format), @previous.a_datetime.utc.strftime(format)
|
217
|
+
assert_equal @date_time.to_time.utc, @previous.a_datetime.to_time.utc
|
220
218
|
end
|
221
219
|
|
222
220
|
should 'handle times' do
|
@@ -245,15 +243,14 @@ class HasPaperTrailModelTest < Test::Unit::TestCase
|
|
245
243
|
end
|
246
244
|
|
247
245
|
should 'restore all forward-compatible attributes' do
|
248
|
-
|
249
|
-
assert_equal '
|
250
|
-
assert_equal
|
251
|
-
|
252
|
-
assert_in_delta
|
253
|
-
|
254
|
-
assert_equal @
|
255
|
-
assert_equal @
|
256
|
-
assert_equal @date, @last.reify.a_date
|
246
|
+
assert_equal 'Warble', @last.reify.name
|
247
|
+
assert_equal 'The quick brown fox', @last.reify.a_text
|
248
|
+
assert_equal 42, @last.reify.an_integer
|
249
|
+
assert_in_delta 153.01, @last.reify.a_float, 0.001
|
250
|
+
assert_in_delta 2.71828, @last.reify.a_decimal, 0.00001
|
251
|
+
assert_equal @date_time.to_time.utc, @last.reify.a_datetime.to_time.utc
|
252
|
+
assert_equal @time, @last.reify.a_time
|
253
|
+
assert_equal @date, @last.reify.a_date
|
257
254
|
assert @last.reify.a_boolean
|
258
255
|
end
|
259
256
|
end
|
@@ -263,6 +260,23 @@ class HasPaperTrailModelTest < Test::Unit::TestCase
|
|
263
260
|
context 'A record' do
|
264
261
|
setup { @widget = Widget.create :name => 'Zaphod' }
|
265
262
|
|
263
|
+
context 'with PaperTrail globally disabled' do
|
264
|
+
setup do
|
265
|
+
PaperTrail.enabled = false
|
266
|
+
@count = @widget.versions.length
|
267
|
+
end
|
268
|
+
|
269
|
+
teardown { PaperTrail.enabled = true }
|
270
|
+
|
271
|
+
context 'when updated' do
|
272
|
+
setup { @widget.update_attributes :name => 'Beeblebrox' }
|
273
|
+
|
274
|
+
should 'not add to its trail' do
|
275
|
+
assert_equal @count, @widget.versions.length
|
276
|
+
end
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
266
280
|
context 'with its paper trail turned off' do
|
267
281
|
setup do
|
268
282
|
Widget.paper_trail_off
|
@@ -486,4 +500,5 @@ class HasPaperTrailModelTest < Test::Unit::TestCase
|
|
486
500
|
end
|
487
501
|
end
|
488
502
|
|
503
|
+
|
489
504
|
end
|
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.
|
4
|
+
version: 1.4.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andy Stewart
|
@@ -33,6 +33,7 @@ files:
|
|
33
33
|
- init.rb
|
34
34
|
- install.rb
|
35
35
|
- lib/paper_trail.rb
|
36
|
+
- lib/paper_trail/config.rb
|
36
37
|
- lib/paper_trail/has_paper_trail.rb
|
37
38
|
- lib/paper_trail/version.rb
|
38
39
|
- paper_trail.gemspec
|