paper_trail 2.2.4 → 2.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -12,7 +12,7 @@ There's an excellent [Railscast on implementing Undo with Paper Trail](http://ra
12
12
  * Allows you to get at every version, including the original, even once destroyed.
13
13
  * Allows you to get at every version even if the schema has since changed.
14
14
  * Allows you to get at the version as of a particular time.
15
- * Automatically restores the `has_one` associations as they were at the time.
15
+ * Option to automatically restore `has_one` associations as they were at the time.
16
16
  * Automatically records who was responsible via your controller. PaperTrail calls `current_user` by default, if it exists, but you can have it call any method you like.
17
17
  * Allows you to set who is responsible at model-level (useful for migrations).
18
18
  * Allows you to store arbitrary model-level metadata with each version (useful for filtering versions).
@@ -337,7 +337,7 @@ If you can think of a good way to achieve this, please let me know.
337
337
 
338
338
  ## Has-One Associations
339
339
 
340
- PaperTrail automatically restores `:has_one` associations as they were at (actually, 3 seconds before) the time.
340
+ PaperTrail can restore `:has_one` associations as they were at (actually, 3 seconds before) the time.
341
341
 
342
342
  class Treasure < ActiveRecord::Base
343
343
  has_one :location
@@ -349,21 +349,17 @@ PaperTrail automatically restores `:has_one` associations as they were at (actua
349
349
  >> treasure.update_attributes :amount => 153
350
350
  >> treasure.location.update_attributes :latitude => 54.321
351
351
 
352
- >> t = treasure.versions.last.reify
352
+ >> t = treasure.versions.last.reify(:has_one => true)
353
353
  >> t.amount # 100
354
354
  >> t.location.latitude # 12.345
355
355
 
356
356
  The implementation is complicated by the edge case where the parent and child are updated in one go, e.g. in one web request or database transaction. PaperTrail doesn't know about different models being updated "together", so you can't ask it definitively to get the child as it was before the joint parent-and-child update.
357
357
 
358
- The correct solution is to make PaperTrail aware of requests or transactions (c.f. [Efficiency's transaction ID middleware](http://github.com/efficiency20/ops_middleware/blob/master/lib/e20/ops/middleware/transaction_id_middleware.rb)). In the meantime we work around the problem by finding the child as it was a few seconds before the parent was updated. By default we go 3 seconds before but you can change this by passing the `:has_one` option to `reify`:
358
+ The correct solution is to make PaperTrail aware of requests or transactions (c.f. [Efficiency's transaction ID middleware](http://github.com/efficiency20/ops_middleware/blob/master/lib/e20/ops/middleware/transaction_id_middleware.rb)). In the meantime we work around the problem by finding the child as it was a few seconds before the parent was updated. By default we go 3 seconds before but you can change this by passing the desired number of seconds to the `:has_one` option:
359
359
 
360
360
  >> t = treasure.versions.last.reify(:has_one => 1) # look back 1 second instead of 3
361
361
 
362
- If you are shuddering, take solace from knowing you can opt out of these shenanigans:
363
-
364
- >> t = treasure.versions.last.reify(:has_one => false) # I say no to "workarounds"!
365
-
366
- Opting out means your `:has_one` associated objects will be the live ones, not the ones the user saw at the time. Since PaperTrail doesn't auto-restore `:has_many` associations (I can't get it to work) or `:belongs_to` (I ran out of time looking at `:has_many`), this at least makes your associations wrong consistently ;)
362
+ If you are shuddering, take solace from knowing PaperTrail opts out of these shenanigans by default. This means your `:has_one` associated objects will be the live ones, not the ones the user saw at the time. Since PaperTrail doesn't auto-restore `:has_many` associations (I can't get it to work) or `:belongs_to` (I ran out of time looking at `:has_many`), this at least makes your associations wrong consistently ;)
367
363
 
368
364
 
369
365
 
@@ -606,6 +602,7 @@ Many thanks to:
606
602
  * [thinkcast](https://github.com/thinkcast)
607
603
  * [Dominik Sander](https://github.com/dsander)
608
604
  * [Burke Libbey](https://github.com/burke)
605
+ * [6twenty](https://github.com/6twenty)
609
606
 
610
607
 
611
608
  ## Inspirations
@@ -32,7 +32,8 @@ class Version < ActiveRecord::Base
32
32
  # sub-second datetimes if you want them).
33
33
  def reify(options = {})
34
34
  without_identity_map do
35
- options.reverse_merge! :has_one => 3
35
+ options[:has_one] = 3 if options[:has_one] == true
36
+ options.reverse_merge! :has_one => false
36
37
 
37
38
  unless object.nil?
38
39
  attrs = YAML::load object
@@ -1,3 +1,3 @@
1
1
  module PaperTrail
2
- VERSION = '2.2.4'
2
+ VERSION = '2.2.5'
3
3
  end
@@ -117,11 +117,17 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
117
117
  context 'and has one associated object' do
118
118
  setup do
119
119
  @wotsit = @widget.create_wotsit :name => 'John'
120
- @reified_widget = @widget.versions.last.reify
120
+ end
121
+
122
+ should 'not copy the has_one association by default when reifying' do
123
+ reified_widget = @widget.versions.last.reify
124
+ assert_equal @wotsit, reified_widget.wotsit # association hasn't been affected by reifying
125
+ assert_equal @wotsit, @widget.wotsit # confirm that the association is correct
121
126
  end
122
127
 
123
- should 'copy the has_one association when reifying' do
124
- assert_nil @reified_widget.wotsit # wotsit wasn't there at the last version
128
+ should 'copy the has_one association when reifying with :has_one => true' do
129
+ reified_widget = @widget.versions.last.reify(:has_one => true)
130
+ assert_nil reified_widget.wotsit # wotsit wasn't there at the last version
125
131
  assert_equal @wotsit, @widget.wotsit # wotsit came into being on the live object
126
132
  end
127
133
  end
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: 15
4
+ hash: 13
5
5
  prerelease: false
6
6
  segments:
7
7
  - 2
8
8
  - 2
9
- - 4
10
- version: 2.2.4
9
+ - 5
10
+ version: 2.2.5
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-05-16 00:00:00 +01:00
18
+ date: 2011-06-21 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency