paper_trail 2.5.2 → 2.6.0

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/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
1
  test/debug.log
2
2
  test/paper_trail_plugin.sqlite3.db
3
+ test/dummy/db/*.sqlite3
3
4
  test/dummy/log/*
4
5
  test/dummy/tmp/*
5
6
  coverage
data/README.md CHANGED
@@ -179,6 +179,16 @@ You can choose which events to track with the `on` option. For example, to igno
179
179
  end
180
180
 
181
181
 
182
+ ## Choosing When To Save New Versions
183
+
184
+ You can choose the conditions when to add new versions with the `if` and `unless` options. For example, to save versions only for US non-draft translations:
185
+
186
+ class Translation < ActiveRecord::Base
187
+ has_paper_trail :if => Proc.new { |t| t.language_code == 'US' },
188
+ :unless => Proc.new { |t| t.type == 'DRAFT' }
189
+ end
190
+
191
+
182
192
 
183
193
  ## Choosing Attributes To Monitor
184
194
 
@@ -694,6 +704,7 @@ Many thanks to:
694
704
  * [Benjamin Curtis](https://github.com/stympy)
695
705
  * [Peter Harkins](https://github.com/pushcx)
696
706
  * [Mohd Amree](https://github.com/amree)
707
+ * [Nikita Cernovs](https://github.com/nikitachernov)
697
708
 
698
709
 
699
710
  ## Inspirations
@@ -15,6 +15,7 @@ module PaperTrail
15
15
  # `:create`, `:update`, `:destroy` as desired.
16
16
  # :class_name the name of a custom Version class. This class should inherit from Version.
17
17
  # :ignore an array of attributes for which a new `Version` will not be created if only they change.
18
+ # :if, :unless Procs that allow to specify conditions when to save versions for an object
18
19
  # :only inverse of `ignore` - a new `Version` will be created only for these attributes if supplied
19
20
  # :skip fields to ignore completely. As with `ignore`, updates to these fields will not create
20
21
  # a new `Version`. In addition, these fields will not be included in the serialized versions
@@ -43,6 +44,12 @@ module PaperTrail
43
44
  class_attribute :ignore
44
45
  self.ignore = ([options[:ignore]].flatten.compact || []).map &:to_s
45
46
 
47
+ class_attribute :if_condition
48
+ self.if_condition = options[:if]
49
+
50
+ class_attribute :unless_condition
51
+ self.unless_condition = options[:unless]
52
+
46
53
  class_attribute :skip
47
54
  self.skip = ([options[:skip]].flatten.compact || []).map &:to_s
48
55
 
@@ -63,8 +70,8 @@ module PaperTrail
63
70
  :as => :item,
64
71
  :order => "created_at ASC, #{self.version_class_name.constantize.primary_key} ASC"
65
72
 
66
- after_create :record_create if !options[:on] || options[:on].include?(:create)
67
- before_update :record_update if !options[:on] || options[:on].include?(:update)
73
+ after_create :record_create, :if => :save_version? if !options[:on] || options[:on].include?(:create)
74
+ before_update :record_update, :if => :save_version? if !options[:on] || options[:on].include?(:update)
68
75
  after_destroy :record_destroy if !options[:on] || options[:on].include?(:destroy)
69
76
  end
70
77
 
@@ -215,7 +222,10 @@ module PaperTrail
215
222
  def switched_on?
216
223
  PaperTrail.enabled? && PaperTrail.enabled_for_controller? && self.class.paper_trail_enabled_for_model
217
224
  end
218
- end
219
225
 
226
+ def save_version?
227
+ (if_condition.blank? || if_condition.call(self)) && !unless_condition.try(:call, self)
228
+ end
229
+ end
220
230
  end
221
231
  end
@@ -1,3 +1,3 @@
1
1
  module PaperTrail
2
- VERSION = '2.5.2'
2
+ VERSION = '2.6.0'
3
3
  end
@@ -0,0 +1,4 @@
1
+ class Translation < ActiveRecord::Base
2
+ has_paper_trail :if => Proc.new { |t| t.language_code == 'US' },
3
+ :unless => Proc.new { |t| t.type == 'DRAFT' }
4
+ end
@@ -103,7 +103,13 @@ class SetUpTestTables < ActiveRecord::Migration
103
103
  t.string :name
104
104
  t.integer :version
105
105
  end
106
-
106
+
107
+ create_table :translations, :force => true do |t|
108
+ t.string :headline
109
+ t.string :content
110
+ t.string :language_code
111
+ t.string :type
112
+ end
107
113
  end
108
114
 
109
115
  def self.down
@@ -123,5 +129,6 @@ class SetUpTestTables < ActiveRecord::Migration
123
129
  drop_table :widgets
124
130
  drop_table :documents
125
131
  drop_table :legacy_widgets
132
+ drop_table :translations
126
133
  end
127
134
  end
@@ -68,6 +68,48 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
68
68
  end
69
69
  end
70
70
 
71
+ context 'A record with defined "if" and "unless" attributes' do
72
+ setup { @translation = Translation.new :headline => 'Headline' }
73
+
74
+ context 'for non-US translations' do
75
+ setup { @translation.save }
76
+ should_not_change('the number of versions') { Version.count }
77
+
78
+ context 'after update' do
79
+ setup { @translation.update_attributes :content => 'Content' }
80
+ should_not_change('the number of versions') { Version.count }
81
+ end
82
+ end
83
+
84
+ context 'for US translations' do
85
+ setup { @translation.language_code = "US" }
86
+
87
+ context 'that are drafts' do
88
+ setup do
89
+ @translation.type = 'DRAFT'
90
+ @translation.save
91
+ end
92
+
93
+ should_not_change('the number of versions') { Version.count }
94
+
95
+ context 'after update' do
96
+ setup { @translation.update_attributes :content => 'Content' }
97
+ should_not_change('the number of versions') { Version.count }
98
+ end
99
+ end
100
+
101
+ context 'that are not drafts' do
102
+ setup { @translation.save }
103
+
104
+ should_change('the number of versions', :by => 1) { Version.count }
105
+
106
+ context 'after update' do
107
+ setup { @translation.update_attributes :content => 'Content' }
108
+ should_change('the number of versions', :by => 1) { Version.count }
109
+ end
110
+ end
111
+ end
112
+ end
71
113
 
72
114
  context 'A new record' do
73
115
  setup { @widget = Widget.new }
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: 31
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 2
8
- - 5
9
- - 2
10
- version: 2.5.2
8
+ - 6
9
+ - 0
10
+ version: 2.6.0
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: 2012-01-16 00:00:00 +01:00
18
+ date: 2012-01-18 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -138,6 +138,7 @@ files:
138
138
  - test/dummy/app/models/person.rb
139
139
  - test/dummy/app/models/post.rb
140
140
  - test/dummy/app/models/song.rb
141
+ - test/dummy/app/models/translation.rb
141
142
  - test/dummy/app/models/widget.rb
142
143
  - test/dummy/app/models/wotsit.rb
143
144
  - test/dummy/app/versions/post_version.rb
@@ -157,10 +158,8 @@ files:
157
158
  - test/dummy/config/initializers/session_store.rb
158
159
  - test/dummy/config/locales/en.yml
159
160
  - test/dummy/config/routes.rb
160
- - test/dummy/db/development.sqlite3
161
161
  - test/dummy/db/migrate/20110208155312_set_up_test_tables.rb
162
162
  - test/dummy/db/schema.rb
163
- - test/dummy/db/test.sqlite3
164
163
  - test/dummy/public/404.html
165
164
  - test/dummy/public/422.html
166
165
  - test/dummy/public/500.html
@@ -235,6 +234,7 @@ test_files:
235
234
  - test/dummy/app/models/person.rb
236
235
  - test/dummy/app/models/post.rb
237
236
  - test/dummy/app/models/song.rb
237
+ - test/dummy/app/models/translation.rb
238
238
  - test/dummy/app/models/widget.rb
239
239
  - test/dummy/app/models/wotsit.rb
240
240
  - test/dummy/app/versions/post_version.rb
@@ -254,10 +254,8 @@ test_files:
254
254
  - test/dummy/config/initializers/session_store.rb
255
255
  - test/dummy/config/locales/en.yml
256
256
  - test/dummy/config/routes.rb
257
- - test/dummy/db/development.sqlite3
258
257
  - test/dummy/db/migrate/20110208155312_set_up_test_tables.rb
259
258
  - test/dummy/db/schema.rb
260
- - test/dummy/db/test.sqlite3
261
259
  - test/dummy/public/404.html
262
260
  - test/dummy/public/422.html
263
261
  - test/dummy/public/500.html
Binary file
Binary file