modified_acts_as_versioned 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/.gitignore +1 -0
  2. data/CHANGELOG +82 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README +28 -0
  5. data/RUNNING_UNIT_TESTS +41 -0
  6. data/Rakefile +50 -0
  7. data/VERSION.yml +4 -0
  8. data/acts_as_versioned.gemspec +29 -0
  9. data/init.rb +1 -0
  10. data/lib/acts_as_versioned.rb +488 -0
  11. data/rdoc/classes/ActiveRecord/Acts/Versioned/ActMethods/ClassMethods.html +336 -0
  12. data/rdoc/classes/ActiveRecord/Acts/Versioned/ActMethods.html +581 -0
  13. data/rdoc/classes/ActiveRecord/Acts/Versioned/ClassMethods.html +506 -0
  14. data/rdoc/classes/ActiveRecord/Acts/Versioned.html +187 -0
  15. data/rdoc/created.rid +1 -0
  16. data/rdoc/files/CHANGELOG.html +288 -0
  17. data/rdoc/files/README.html +158 -0
  18. data/rdoc/files/RUNNING_UNIT_TESTS.html +158 -0
  19. data/rdoc/files/lib/acts_as_versioned_rb.html +129 -0
  20. data/rdoc/fr_class_index.html +30 -0
  21. data/rdoc/fr_file_index.html +30 -0
  22. data/rdoc/fr_method_index.html +54 -0
  23. data/rdoc/index.html +24 -0
  24. data/rdoc/rdoc-style.css +208 -0
  25. data/test/abstract_unit.rb +60 -0
  26. data/test/database.yml +18 -0
  27. data/test/fixtures/authors.yml +6 -0
  28. data/test/fixtures/landmark.rb +3 -0
  29. data/test/fixtures/landmark_versions.yml +7 -0
  30. data/test/fixtures/landmarks.yml +7 -0
  31. data/test/fixtures/locked_pages.yml +10 -0
  32. data/test/fixtures/locked_pages_revisions.yml +27 -0
  33. data/test/fixtures/migrations/1_add_versioned_tables.rb +15 -0
  34. data/test/fixtures/page.rb +48 -0
  35. data/test/fixtures/page_versions.yml +16 -0
  36. data/test/fixtures/pages.yml +8 -0
  37. data/test/fixtures/widget.rb +6 -0
  38. data/test/migration_test.rb +47 -0
  39. data/test/schema.rb +82 -0
  40. data/test/versioned_test.rb +379 -0
  41. metadata +114 -0
@@ -0,0 +1,379 @@
1
+ require File.join(File.dirname(__FILE__), 'abstract_unit')
2
+ require File.join(File.dirname(__FILE__), 'fixtures/page')
3
+ require File.join(File.dirname(__FILE__), 'fixtures/widget')
4
+
5
+ #class VersionedTest < Test::Unit::TestCase
6
+ class VersionedTest < ActiveSupport::TestCase
7
+ fixtures :pages, :page_versions, :locked_pages, :locked_pages_revisions, :authors, :landmarks, :landmark_versions
8
+ set_fixture_class :page_versions => Page::Version
9
+
10
+ def test_saves_versioned_copy
11
+ p = Page.create! :title => 'first title', :body => 'first body'
12
+ assert !p.new_record?
13
+ assert_equal 1, p.versions.size
14
+ assert_equal 1, p.version
15
+ assert_instance_of Page.versioned_class, p.versions.first
16
+ end
17
+
18
+ def test_saves_without_revision
19
+ p = pages(:welcome)
20
+ old_versions = p.versions.count
21
+
22
+ p.save_without_revision
23
+
24
+ p.without_revision do
25
+ p.update_attributes :title => 'changed'
26
+ end
27
+
28
+ assert_equal old_versions, p.versions.count
29
+ end
30
+
31
+ def test_rollback_with_version_number
32
+ p = pages(:welcome)
33
+ assert_equal 24, p.version
34
+ assert_equal 'Welcome to the weblog', p.title
35
+
36
+ assert p.revert_to!(23), "Couldn't revert to 23"
37
+ assert_equal 23, p.version
38
+ assert_equal 'Welcome to the weblg', p.title
39
+ end
40
+
41
+ def test_versioned_class_name
42
+ assert_equal 'Version', Page.versioned_class_name
43
+ assert_equal 'LockedPageRevision', LockedPage.versioned_class_name
44
+ end
45
+
46
+ def test_versioned_class
47
+ assert_equal Page::Version, Page.versioned_class
48
+ assert_equal LockedPage::LockedPageRevision, LockedPage.versioned_class
49
+ end
50
+
51
+ def test_special_methods
52
+ assert_nothing_raised { pages(:welcome).feeling_good? }
53
+ assert_nothing_raised { pages(:welcome).versions.first.feeling_good? }
54
+ assert_nothing_raised { locked_pages(:welcome).hello_world }
55
+ assert_nothing_raised { locked_pages(:welcome).versions.first.hello_world }
56
+ end
57
+
58
+ def test_rollback_with_version_class
59
+ p = pages(:welcome)
60
+ assert_equal 24, p.version
61
+ assert_equal 'Welcome to the weblog', p.title
62
+
63
+ assert p.revert_to!(p.versions.find_by_version(23)), "Couldn't revert to 23"
64
+ assert_equal 23, p.version
65
+ assert_equal 'Welcome to the weblg', p.title
66
+ end
67
+
68
+ def test_rollback_fails_with_invalid_revision
69
+ p = locked_pages(:welcome)
70
+ assert !p.revert_to!(locked_pages(:thinking))
71
+ end
72
+
73
+ def test_saves_versioned_copy_with_options
74
+ p = LockedPage.create! :title => 'first title'
75
+ assert !p.new_record?
76
+ assert_equal 1, p.versions.size
77
+ assert_instance_of LockedPage.versioned_class, p.versions.first
78
+ end
79
+
80
+ def test_rollback_with_version_number_with_options
81
+ p = locked_pages(:welcome)
82
+ assert_equal 'Welcome to the weblog', p.title
83
+ assert_equal 'LockedPage', p.versions.first.version_type
84
+
85
+ assert p.revert_to!(p.versions.first.lock_version), "Couldn't revert to 23"
86
+ assert_equal 'Welcome to the weblg', p.title
87
+ assert_equal 'LockedPage', p.versions.first.version_type
88
+ end
89
+
90
+ def test_rollback_with_version_class_with_options
91
+ p = locked_pages(:welcome)
92
+ assert_equal 'Welcome to the weblog', p.title
93
+ assert_equal 'LockedPage', p.versions.first.version_type
94
+
95
+ assert p.revert_to!(p.versions.first), "Couldn't revert to 1"
96
+ assert_equal 'Welcome to the weblg', p.title
97
+ assert_equal 'LockedPage', p.versions.first.version_type
98
+ end
99
+
100
+ def test_saves_versioned_copy_with_sti
101
+ p = SpecialLockedPage.create! :title => 'first title'
102
+ assert !p.new_record?
103
+ assert_equal 1, p.versions.size
104
+ assert_instance_of LockedPage.versioned_class, p.versions.first
105
+ assert_equal 'SpecialLockedPage', p.versions.first.version_type
106
+ end
107
+
108
+ def test_rollback_with_version_number_with_sti
109
+ p = locked_pages(:thinking)
110
+ assert_equal 'So I was thinking', p.title
111
+
112
+ assert p.revert_to!(p.versions.first.lock_version), "Couldn't revert to 1"
113
+ assert_equal 'So I was thinking!!!', p.title
114
+ assert_equal 'SpecialLockedPage', p.versions.first.version_type
115
+ end
116
+
117
+ def test_lock_version_works_with_versioning
118
+ p = locked_pages(:thinking)
119
+ p2 = LockedPage.find(p.id)
120
+
121
+ p.title = 'fresh title'
122
+ p.save
123
+ assert_equal 2, p.versions.size # limit!
124
+
125
+ assert_raises(ActiveRecord::StaleObjectError) do
126
+ p2.title = 'stale title'
127
+ p2.save
128
+ end
129
+ end
130
+
131
+ def test_version_if_condition
132
+ p = Page.create! :title => "title"
133
+ assert_equal 1, p.version
134
+
135
+ Page.feeling_good = false
136
+ p.save
137
+ assert_equal 1, p.version
138
+ Page.feeling_good = true
139
+ end
140
+
141
+ def test_version_if_condition2
142
+ # set new if condition
143
+ Page.class_eval do
144
+ def new_feeling_good() title[0..0] == 'a'; end
145
+ alias_method :old_feeling_good, :feeling_good?
146
+ alias_method :feeling_good?, :new_feeling_good
147
+ end
148
+
149
+ p = Page.create! :title => "title"
150
+ assert_equal 1, p.version # version does not increment
151
+ assert_equal 1, p.versions.count
152
+
153
+ p.update_attributes(:title => 'new title')
154
+ assert_equal 1, p.version # version does not increment
155
+ assert_equal 1, p.versions.count
156
+
157
+ p.update_attributes(:title => 'a title')
158
+ assert_equal 2, p.version
159
+ assert_equal 2, p.versions.count
160
+
161
+ # reset original if condition
162
+ Page.class_eval { alias_method :feeling_good?, :old_feeling_good }
163
+ end
164
+
165
+ def test_version_if_condition_with_block
166
+ # set new if condition
167
+ old_condition = Page.version_condition
168
+ Page.version_condition = Proc.new { |page| page.title[0..0] == 'b' }
169
+
170
+ p = Page.create! :title => "title"
171
+ assert_equal 1, p.version # version does not increment
172
+ assert_equal 1, p.versions.count
173
+
174
+ p.update_attributes(:title => 'a title')
175
+ assert_equal 1, p.version # version does not increment
176
+ assert_equal 1, p.versions.count
177
+
178
+ p.update_attributes(:title => 'b title')
179
+ assert_equal 2, p.version
180
+ assert_equal 2, p.versions.count
181
+
182
+ # reset original if condition
183
+ Page.version_condition = old_condition
184
+ end
185
+
186
+ def test_version_no_limit
187
+ p = Page.create! :title => "title", :body => 'first body'
188
+ p.save
189
+ p.save
190
+ 5.times do |i|
191
+ p.title = "title#{i}"
192
+ p.save
193
+ assert_equal "title#{i}", p.title
194
+ assert_equal (i+2), p.version
195
+ end
196
+ end
197
+
198
+ def test_version_max_limit
199
+ p = LockedPage.create! :title => "title"
200
+ p.update_attributes(:title => "title1")
201
+ p.update_attributes(:title => "title2")
202
+ 5.times do |i|
203
+ p.title = "title#{i}"
204
+ p.save
205
+ assert_equal "title#{i}", p.title
206
+ assert_equal (i+4), p.lock_version
207
+ assert p.versions(true).size <= 2, "locked version can only store 2 versions"
208
+ end
209
+ end
210
+
211
+ def test_track_altered_attributes_default_value
212
+ assert !Page.track_altered_attributes
213
+ assert LockedPage.track_altered_attributes
214
+ assert SpecialLockedPage.track_altered_attributes
215
+ end
216
+
217
+ def test_track_altered_attributes
218
+ p = LockedPage.create! :title => "title"
219
+ assert_equal 1, p.lock_version
220
+ assert_equal 1, p.versions(true).size
221
+
222
+ p.body = 'whoa'
223
+ assert !p.save_version?
224
+ p.save
225
+ assert_equal 2, p.lock_version # still increments version because of optimistic locking
226
+ assert_equal 1, p.versions(true).size
227
+
228
+ p.title = 'updated title'
229
+ assert p.save_version?
230
+ p.save
231
+ assert_equal 3, p.lock_version
232
+ assert_equal 1, p.versions(true).size # version 1 deleted
233
+
234
+ p.title = 'updated title!'
235
+ assert p.save_version?
236
+ p.save
237
+ assert_equal 4, p.lock_version
238
+ assert_equal 2, p.versions(true).size # version 1 deleted
239
+ end
240
+
241
+ def test_find_versions
242
+ assert_equal 1, locked_pages(:welcome).versions.find(:all, :conditions => ['title LIKE ?', '%weblog%']).size
243
+ end
244
+
245
+ def test_find_version
246
+ assert_equal page_versions(:welcome_1), pages(:welcome).versions.find_by_version(23)
247
+ end
248
+
249
+ def test_with_sequence
250
+ assert_equal 'widgets_seq', Widget.versioned_class.sequence_name
251
+ 3.times { Widget.create! :name => 'new widget' }
252
+ assert_equal 3, Widget.count
253
+ assert_equal 3, Widget.versioned_class.count
254
+ end
255
+
256
+ def test_has_many_through
257
+ assert_equal [authors(:caged), authors(:mly)], pages(:welcome).authors
258
+ end
259
+
260
+ def test_has_many_through_with_custom_association
261
+ assert_equal [authors(:caged), authors(:mly)], pages(:welcome).revisors
262
+ end
263
+
264
+ def test_referential_integrity
265
+ pages(:welcome).destroy
266
+ assert_equal 0, Page.count
267
+ assert_equal 0, Page::Version.count
268
+ end
269
+
270
+ def test_association_options
271
+ association = Page.reflect_on_association(:versions)
272
+ options = association.options
273
+ assert_equal :delete_all, options[:dependent]
274
+
275
+ association = Widget.reflect_on_association(:versions)
276
+ options = association.options
277
+ assert_equal :nullify, options[:dependent]
278
+ assert_equal 'version desc', options[:order]
279
+ assert_equal 'widget_id', options[:foreign_key]
280
+
281
+ widget = Widget.create! :name => 'new widget'
282
+ assert_equal 1, Widget.count
283
+ assert_equal 1, Widget.versioned_class.count
284
+ widget.destroy
285
+ assert_equal 0, Widget.count
286
+ assert_equal 1, Widget.versioned_class.count
287
+ end
288
+
289
+ def test_versioned_records_should_belong_to_parent
290
+ page = pages(:welcome)
291
+ page_version = page.versions.last
292
+ assert_equal page, page_version.page
293
+ end
294
+
295
+ def test_unaltered_attributes
296
+ landmarks(:washington).attributes = landmarks(:washington).attributes.except("id")
297
+ assert !landmarks(:washington).changed?
298
+ end
299
+
300
+ def test_unchanged_string_attributes
301
+ landmarks(:washington).attributes = landmarks(:washington).attributes.except("id").inject({}) { |params, (key, value)| params.update(key => value.to_s) }
302
+ assert !landmarks(:washington).changed?
303
+ end
304
+
305
+ def test_should_find_earliest_version
306
+ assert_equal page_versions(:welcome_1), pages(:welcome).versions.earliest
307
+ end
308
+
309
+ def test_should_find_latest_version
310
+ assert_equal page_versions(:welcome_2), pages(:welcome).versions.latest
311
+ end
312
+
313
+ def test_should_find_previous_version
314
+ assert_equal page_versions(:welcome_1), page_versions(:welcome_2).previous
315
+ assert_equal page_versions(:welcome_1), pages(:welcome).versions.before(page_versions(:welcome_2))
316
+ end
317
+
318
+ def test_should_find_next_version
319
+ assert_equal page_versions(:welcome_2), page_versions(:welcome_1).next
320
+ assert_equal page_versions(:welcome_2), pages(:welcome).versions.after(page_versions(:welcome_1))
321
+ end
322
+
323
+ def test_should_find_version_count
324
+ assert_equal 2, pages(:welcome).versions.size
325
+ end
326
+
327
+ def test_if_changed_creates_version_if_a_listed_column_is_changed
328
+ landmarks(:washington).name = "Washington"
329
+ assert landmarks(:washington).changed?
330
+ assert landmarks(:washington).altered?
331
+ end
332
+
333
+ def test_if_changed_creates_version_if_all_listed_columns_are_changed
334
+ landmarks(:washington).name = "Washington"
335
+ landmarks(:washington).latitude = 1.0
336
+ landmarks(:washington).longitude = 1.0
337
+ assert landmarks(:washington).changed?
338
+ assert landmarks(:washington).altered?
339
+ end
340
+
341
+ def test_if_changed_does_not_create_new_version_if_unlisted_column_is_changed
342
+ landmarks(:washington).doesnt_trigger_version = "This should not trigger version"
343
+ assert landmarks(:washington).changed?
344
+ assert !landmarks(:washington).altered?
345
+ end
346
+
347
+ def test_without_locking_temporarily_disables_optimistic_locking
348
+ enabled1 = false
349
+ block_called = false
350
+
351
+ ActiveRecord::Base.lock_optimistically = true
352
+ LockedPage.without_locking do
353
+ enabled1 = ActiveRecord::Base.lock_optimistically
354
+ block_called = true
355
+ end
356
+ enabled2 = ActiveRecord::Base.lock_optimistically
357
+
358
+ assert block_called
359
+ assert !enabled1
360
+ assert enabled2
361
+ end
362
+
363
+ def test_without_locking_reverts_optimistic_locking_settings_if_block_raises_exception
364
+ assert_raises(RuntimeError) do
365
+ LockedPage.without_locking do
366
+ raise RuntimeError, "oh noes"
367
+ end
368
+ end
369
+ assert ActiveRecord::Base.lock_optimistically
370
+ end
371
+
372
+ def test_using_alternative_versions_name
373
+ p = AlternativeVersionsNamePage.create! :title => 'first title', :body => 'first body'
374
+ assert !p.new_record?
375
+ assert_equal 1, p.happy_versions.size
376
+ assert_equal 1, p.version
377
+ assert_instance_of AlternativeVersionsNamePage.versioned_class, p.happy_versions.first
378
+ end
379
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: modified_acts_as_versioned
3
+ version: !ruby/object:Gem::Version
4
+ hash: 15
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 5
9
+ - 2
10
+ version: 0.5.2
11
+ platform: ruby
12
+ authors:
13
+ - David Henry
14
+ - Thomas Brand
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-09-29 00:00:00 +01:00
20
+ default_executable:
21
+ dependencies: []
22
+
23
+ description: Small added functionality to technoweenie original gen for Rails 2 - Simple versioning with active record models
24
+ email: dw_henry@yahoo.com.au
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files:
30
+ - README
31
+ files:
32
+ - .gitignore
33
+ - CHANGELOG
34
+ - MIT-LICENSE
35
+ - README
36
+ - RUNNING_UNIT_TESTS
37
+ - Rakefile
38
+ - VERSION.yml
39
+ - acts_as_versioned.gemspec
40
+ - init.rb
41
+ - lib/acts_as_versioned.rb
42
+ - rdoc/classes/ActiveRecord/Acts/Versioned.html
43
+ - rdoc/classes/ActiveRecord/Acts/Versioned/ActMethods.html
44
+ - rdoc/classes/ActiveRecord/Acts/Versioned/ActMethods/ClassMethods.html
45
+ - rdoc/classes/ActiveRecord/Acts/Versioned/ClassMethods.html
46
+ - rdoc/created.rid
47
+ - rdoc/files/CHANGELOG.html
48
+ - rdoc/files/README.html
49
+ - rdoc/files/RUNNING_UNIT_TESTS.html
50
+ - rdoc/files/lib/acts_as_versioned_rb.html
51
+ - rdoc/fr_class_index.html
52
+ - rdoc/fr_file_index.html
53
+ - rdoc/fr_method_index.html
54
+ - rdoc/index.html
55
+ - rdoc/rdoc-style.css
56
+ - test/abstract_unit.rb
57
+ - test/database.yml
58
+ - test/fixtures/authors.yml
59
+ - test/fixtures/landmark.rb
60
+ - test/fixtures/landmark_versions.yml
61
+ - test/fixtures/landmarks.yml
62
+ - test/fixtures/locked_pages.yml
63
+ - test/fixtures/locked_pages_revisions.yml
64
+ - test/fixtures/migrations/1_add_versioned_tables.rb
65
+ - test/fixtures/page.rb
66
+ - test/fixtures/page_versions.yml
67
+ - test/fixtures/pages.yml
68
+ - test/fixtures/widget.rb
69
+ - test/migration_test.rb
70
+ - test/schema.rb
71
+ - test/versioned_test.rb
72
+ has_rdoc: true
73
+ homepage: http://github.com/tom025/acts_as_versioned
74
+ licenses: []
75
+
76
+ post_install_message:
77
+ rdoc_options:
78
+ - --charset=UTF-8
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ hash: 3
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ requirements: []
100
+
101
+ rubyforge_project:
102
+ rubygems_version: 1.3.7
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Small added functionality to technoweenie original gen for Rails 2 - Simple versioning with active record models
106
+ test_files:
107
+ - test/abstract_unit.rb
108
+ - test/fixtures/landmark.rb
109
+ - test/fixtures/migrations/1_add_versioned_tables.rb
110
+ - test/fixtures/page.rb
111
+ - test/fixtures/widget.rb
112
+ - test/migration_test.rb
113
+ - test/schema.rb
114
+ - test/versioned_test.rb