paper_trail 2.3.2 → 2.3.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -42,10 +42,11 @@ When you declare `has_paper_trail` in your model, you get these methods:
42
42
  has_paper_trail # you can pass various options here
43
43
  end
44
44
 
45
- # Returns this widget's versions.
45
+ # Returns this widget's versions. You can customise the name of the association.
46
46
  widget.versions
47
47
 
48
48
  # Return the version this widget was reified from, or nil if it is live.
49
+ # You can customise the name of the method.
49
50
  widget.version
50
51
 
51
52
  # Returns true if this widget is the current, live one; or false if it is from a previous version.
@@ -649,6 +650,7 @@ Many thanks to:
649
650
  * [nir0](https://github.com/nir0)
650
651
  * [Eduard Tsech](https://github.com/edtsech)
651
652
  * [Mathieu Arnold](https://github.com/mat813)
653
+ * [Nicholas Thrower](https://github.com/throwern)
652
654
 
653
655
 
654
656
  ## Inspirations
@@ -21,13 +21,18 @@ module PaperTrail
21
21
  # trail). See `PaperTrail::Controller.info_for_paper_trail` for how to store data from
22
22
  # the controller.
23
23
  # :versions the name to use for the versions association. Default is `:versions`.
24
+ # :version_name the name to use for the method which returns the version the instance was reified from.
25
+ # Default is `:version`.
24
26
  def has_paper_trail(options = {})
25
27
  # Lazily include the instance methods so we don't clutter up
26
28
  # any more ActiveRecord models than we have to.
27
29
  send :include, InstanceMethods
28
30
 
31
+ class_attribute :version_name
32
+ self.version_name = options[:version_name] || 'version'
33
+
29
34
  # The version this instance was reified from.
30
- attr_accessor :version
35
+ attr_accessor self.version_name
31
36
 
32
37
  class_attribute :version_class_name
33
38
  self.version_class_name = options[:class_name] || 'Version'
@@ -74,7 +79,7 @@ module PaperTrail
74
79
  # Returns true if this instance is the current, live one;
75
80
  # returns false if this instance came from a previous version.
76
81
  def live?
77
- version.nil?
82
+ source_version.nil?
78
83
  end
79
84
 
80
85
  # Returns who put the object into its current state.
@@ -86,13 +91,13 @@ module PaperTrail
86
91
  def version_at(timestamp, reify_options={})
87
92
  # Because a version stores how its object looked *before* the change,
88
93
  # we need to look for the first version created *after* the timestamp.
89
- version = send(self.class.versions_association_name).after(timestamp).first
90
- version ? version.reify(reify_options) : self
94
+ v = send(self.class.versions_association_name).after(timestamp).first
95
+ v ? v.reify(reify_options) : self
91
96
  end
92
97
 
93
98
  # Returns the object (not a Version) as it was most recently.
94
99
  def previous_version
95
- preceding_version = version ? version.previous : send(self.class.versions_association_name).last
100
+ preceding_version = source_version ? source_version.previous : send(self.class.versions_association_name).last
96
101
  preceding_version.try :reify
97
102
  end
98
103
 
@@ -100,7 +105,7 @@ module PaperTrail
100
105
  def next_version
101
106
  # NOTE: if self (the item) was not reified from a version, i.e. it is the
102
107
  # "live" item, we return nil. Perhaps we should return self instead?
103
- subsequent_version = version ? version.next : nil
108
+ subsequent_version = source_version ? source_version.next : nil
104
109
  subsequent_version.reify if subsequent_version
105
110
  end
106
111
 
@@ -119,6 +124,10 @@ module PaperTrail
119
124
  version_class_name.constantize
120
125
  end
121
126
 
127
+ def source_version
128
+ send self.class.version_name
129
+ end
130
+
122
131
  def record_create
123
132
  if switched_on?
124
133
  send(self.class.versions_association_name).create merge_metadata(:event => 'create', :whodunnit => PaperTrail.whodunnit)
@@ -68,7 +68,7 @@ class Version < ActiveRecord::Base
68
68
  end
69
69
  end
70
70
 
71
- model.version = self
71
+ model.send "#{model.class.version_name}=", self
72
72
 
73
73
  unless options[:has_one] == false
74
74
  reify_has_ones model, options[:has_one]
@@ -1,3 +1,3 @@
1
1
  module PaperTrail
2
- VERSION = '2.3.2'
2
+ VERSION = '2.3.3'
3
3
  end
@@ -0,0 +1,3 @@
1
+ class LegacyWidget < ActiveRecord::Base
2
+ has_paper_trail :version_name => 'custom_version'
3
+ end
@@ -97,6 +97,12 @@ class SetUpTestTables < ActiveRecord::Migration
97
97
  create_table :documents, :force => true do |t|
98
98
  t.string :name
99
99
  end
100
+
101
+ create_table :legacy_widgets, :force => true do |t|
102
+ t.string :name
103
+ t.integer :version
104
+ end
105
+
100
106
  end
101
107
 
102
108
  def self.down
@@ -115,5 +121,6 @@ class SetUpTestTables < ActiveRecord::Migration
115
121
  drop_table :versions
116
122
  drop_table :widgets
117
123
  drop_table :documents
124
+ drop_table :legacy_widgets
118
125
  end
119
126
  end
Binary file
@@ -912,6 +912,42 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
912
912
  end
913
913
  end
914
914
 
915
+ context 'A model with column version and custom version_method' do
916
+ setup do
917
+ @legacy_widget = LegacyWidget.create(:name => "foo", :version => 2)
918
+ end
919
+
920
+ should 'set version on create' do
921
+ assert_equal 2, @legacy_widget.version
922
+ end
923
+
924
+ should 'allow version updates' do
925
+ @legacy_widget.update_attributes :version => 3
926
+ assert_equal 3, @legacy_widget.version
927
+ end
928
+
929
+ should 'create a new version record' do
930
+ assert_equal 1, @legacy_widget.versions.size
931
+ end
932
+ end
933
+
934
+ context 'A reified item with a column -version- and custom version_method' do
935
+ setup do
936
+ widget = LegacyWidget.create(:name => "foo", :version => 2)
937
+ %w( bar baz ).each { |name| widget.update_attributes :name => name }
938
+ @version = widget.versions.last
939
+ @widget = @version.reify
940
+ end
941
+
942
+ should 'know which version it came from' do
943
+ assert_equal @version, @widget.custom_version
944
+ end
945
+
946
+ should 'return its previous self' do
947
+ assert_equal @widget.versions[-2].reify, @widget.previous_version
948
+ end
949
+ end
950
+
915
951
  private
916
952
 
917
953
  # Updates `model`'s last version so it looks like the version was
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: 7
4
+ hash: 5
5
5
  prerelease: false
6
6
  segments:
7
7
  - 2
8
8
  - 3
9
- - 2
10
- version: 2.3.2
9
+ - 3
10
+ version: 2.3.3
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-31 00:00:00 +02:00
18
+ date: 2011-09-14 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -134,6 +134,7 @@ files:
134
134
  - test/dummy/app/models/elephant.rb
135
135
  - test/dummy/app/models/fluxor.rb
136
136
  - test/dummy/app/models/foo_widget.rb
137
+ - test/dummy/app/models/legacy_widget.rb
137
138
  - test/dummy/app/models/person.rb
138
139
  - test/dummy/app/models/post.rb
139
140
  - test/dummy/app/models/song.rb
@@ -230,6 +231,7 @@ test_files:
230
231
  - test/dummy/app/models/elephant.rb
231
232
  - test/dummy/app/models/fluxor.rb
232
233
  - test/dummy/app/models/foo_widget.rb
234
+ - test/dummy/app/models/legacy_widget.rb
233
235
  - test/dummy/app/models/person.rb
234
236
  - test/dummy/app/models/post.rb
235
237
  - test/dummy/app/models/song.rb