paper_trail 2.3.1 → 2.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -187,7 +187,7 @@ You can ignore changes to certain attributes like this:
187
187
  has_paper_trail :ignore => [:title, :rating]
188
188
  end
189
189
 
190
- This means that changes to just the `title` or `rating` will not store another version of the article. It does not mean that the `title` and `rating` attributes will be ignored if some other change causes a new `Version` to be crated. For example:
190
+ This means that changes to just the `title` or `rating` will not store another version of the article. It does not mean that the `title` and `rating` attributes will be ignored if some other change causes a new `Version` to be created. For example:
191
191
 
192
192
  >> a = Article.create
193
193
  >> a.versions.length # 1
@@ -563,6 +563,17 @@ And on again like this:
563
563
 
564
564
  >> Widget.paper_trail_on
565
565
 
566
+ ### Per method call
567
+
568
+ You can call a method without creating a new version using `without_versioning`. It takes either a method name as a symbol:
569
+
570
+ @widget.without_versioning :destroy
571
+
572
+ Or a block:
573
+
574
+ @widget.without_versioning do
575
+ @widget.update_attributes :name => 'Ford'
576
+ end
566
577
 
567
578
 
568
579
  ## Deleting Old Versions
@@ -29,22 +29,22 @@ module PaperTrail
29
29
  # The version this instance was reified from.
30
30
  attr_accessor :version
31
31
 
32
- cattr_accessor :version_class_name
32
+ class_attribute :version_class_name
33
33
  self.version_class_name = options[:class_name] || 'Version'
34
34
 
35
- cattr_accessor :ignore
35
+ class_attribute :ignore
36
36
  self.ignore = ([options[:ignore]].flatten.compact || []).map &:to_s
37
37
 
38
- cattr_accessor :only
38
+ class_attribute :only
39
39
  self.only = ([options[:only]].flatten.compact || []).map &:to_s
40
40
 
41
- cattr_accessor :meta
41
+ class_attribute :meta
42
42
  self.meta = options[:meta] || {}
43
43
 
44
- cattr_accessor :paper_trail_enabled_for_model
44
+ class_attribute :paper_trail_enabled_for_model
45
45
  self.paper_trail_enabled_for_model = true
46
46
 
47
- cattr_accessor :versions_association_name
47
+ class_attribute :versions_association_name
48
48
  self.versions_association_name = options[:versions] || :versions
49
49
 
50
50
  has_many self.versions_association_name,
@@ -104,6 +104,15 @@ module PaperTrail
104
104
  subsequent_version.reify if subsequent_version
105
105
  end
106
106
 
107
+ # Executes the given method or block without creating a new version.
108
+ def without_versioning(method = nil)
109
+ paper_trail_was_enabled = self.paper_trail_enabled_for_model
110
+ self.class.paper_trail_off
111
+ method ? method.to_proc.call(self) : yield
112
+ ensure
113
+ self.class.paper_trail_on if paper_trail_was_enabled
114
+ end
115
+
107
116
  private
108
117
 
109
118
  def version_class
@@ -84,7 +84,7 @@ class Version < ActiveRecord::Base
84
84
  def changeset
85
85
  if self.class.column_names.include? 'object_changes'
86
86
  if changes = object_changes
87
- YAML::load(changes)
87
+ HashWithIndifferentAccess[YAML::load(changes)]
88
88
  else
89
89
  {}
90
90
  end
@@ -1,3 +1,3 @@
1
1
  module PaperTrail
2
- VERSION = '2.3.1'
2
+ VERSION = '2.3.2'
3
3
  end
@@ -0,0 +1,3 @@
1
+ class Elephant < Animal
2
+ paper_trail_off
3
+ end
@@ -64,7 +64,7 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
64
64
  assert @widget.live?
65
65
  end
66
66
 
67
- should 'should not have changes' do
67
+ should 'not have changes' do
68
68
  assert_equal Hash.new, @widget.versions.last.changeset
69
69
  end
70
70
 
@@ -109,6 +109,11 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
109
109
  assert_equal ({'name' => ['Henry', 'Harry']}), @widget.versions.last.changeset
110
110
  end
111
111
 
112
+ should 'return changes with indifferent access' do
113
+ assert_equal ['Henry', 'Harry'], @widget.versions.last.changeset[:name]
114
+ assert_equal ['Henry', 'Harry'], @widget.versions.last.changeset['name']
115
+ end
116
+
112
117
  if defined?(ActiveRecord::IdentityMap) && ActiveRecord::IdentityMap.respond_to?(:without)
113
118
  should 'not clobber the IdentityMap when reifying' do
114
119
  module ActiveRecord::IdentityMap
@@ -191,7 +196,7 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
191
196
  assert_equal 1, @reified_widget.fluxors.length
192
197
  end
193
198
 
194
- should 'should not have changes' do
199
+ should 'not have changes' do
195
200
  assert_equal Hash.new, @widget.versions.last.changeset
196
201
  end
197
202
  end
@@ -329,6 +334,13 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
329
334
  end
330
335
  end
331
336
 
337
+ context 'when destroyed "without versioning"' do
338
+ should 'leave paper trail off after call' do
339
+ @widget.without_versioning :destroy
340
+ assert !Widget.paper_trail_enabled_for_model
341
+ end
342
+ end
343
+
332
344
  context 'and then its paper trail turned on' do
333
345
  setup { Widget.paper_trail_on }
334
346
 
@@ -339,6 +351,22 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
339
351
  assert_equal @count + 1, @widget.versions.length
340
352
  end
341
353
  end
354
+
355
+ context 'when updated "without versioning"' do
356
+ setup do
357
+ @widget.without_versioning do
358
+ @widget.update_attributes :name => 'Ford'
359
+ end
360
+ end
361
+
362
+ should 'not create new version' do
363
+ assert_equal 1, @widget.versions.length
364
+ end
365
+
366
+ should 'enable paper trail after call' do
367
+ assert Widget.paper_trail_enabled_for_model
368
+ end
369
+ end
342
370
  end
343
371
  end
344
372
  end
@@ -772,7 +800,7 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
772
800
  end
773
801
  end
774
802
 
775
- should 'should have versions of the custom class' do
803
+ should 'have versions of the custom class' do
776
804
  assert_equal "PostVersion", @post.versions.first.class.name
777
805
  end
778
806
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paper_trail
3
3
  version: !ruby/object:Gem::Version
4
- hash: 1
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 2
8
8
  - 3
9
- - 1
10
- version: 2.3.1
9
+ - 2
10
+ version: 2.3.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andy Stewart
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-08-22 00:00:00 +02:00
18
+ date: 2011-08-31 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -131,6 +131,7 @@ files:
131
131
  - test/dummy/app/models/cat.rb
132
132
  - test/dummy/app/models/document.rb
133
133
  - test/dummy/app/models/dog.rb
134
+ - test/dummy/app/models/elephant.rb
134
135
  - test/dummy/app/models/fluxor.rb
135
136
  - test/dummy/app/models/foo_widget.rb
136
137
  - test/dummy/app/models/person.rb
@@ -226,6 +227,7 @@ test_files:
226
227
  - test/dummy/app/models/cat.rb
227
228
  - test/dummy/app/models/document.rb
228
229
  - test/dummy/app/models/dog.rb
230
+ - test/dummy/app/models/elephant.rb
229
231
  - test/dummy/app/models/fluxor.rb
230
232
  - test/dummy/app/models/foo_widget.rb
231
233
  - test/dummy/app/models/person.rb