decisiv-acts_as_versioned 0.6.1
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/CHANGELOG +102 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +24 -0
- data/RUNNING_UNIT_TESTS +21 -0
- data/Rakefile +24 -0
- data/lib/acts_as_versioned.rb +492 -0
- data/test/fixtures/authors.yml +6 -0
- data/test/fixtures/landmark_versions.yml +7 -0
- data/test/fixtures/landmarks.yml +7 -0
- data/test/fixtures/locked_pages.yml +10 -0
- data/test/fixtures/locked_pages_revisions.yml +27 -0
- data/test/fixtures/page_versions.yml +16 -0
- data/test/fixtures/pages.yml +8 -0
- data/test/fixtures/widgets.yml +2 -0
- data/test/helper.rb +27 -0
- data/test/lib/boot.rb +68 -0
- data/test/lib/database.yml +26 -0
- data/test/lib/schema.rb +91 -0
- data/test/migration_test.rb +43 -0
- data/test/migrations/1_add_versioned_tables.rb +19 -0
- data/test/models/author.rb +3 -0
- data/test/models/landmark.rb +3 -0
- data/test/models/page.rb +38 -0
- data/test/models/thing.rb +7 -0
- data/test/models/widget.rb +6 -0
- data/test/versioned_test.rb +336 -0
- metadata +80 -0
@@ -0,0 +1,336 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class VersionedTest < AAVTestCase
|
4
|
+
|
5
|
+
|
6
|
+
test "saves versioned copy" do
|
7
|
+
p = Page.create! :title => 'first title', :body => 'first body'
|
8
|
+
assert !p.new_record?
|
9
|
+
assert_equal 1, p.versions.size
|
10
|
+
assert_equal 1, p.version
|
11
|
+
assert_instance_of Page.versioned_class, p.versions.first
|
12
|
+
end
|
13
|
+
|
14
|
+
test "version has unique created on" do
|
15
|
+
p = pages(:welcome)
|
16
|
+
p.title = 'update me'
|
17
|
+
p.save!
|
18
|
+
assert_not_equal p.created_on, p.versions.latest.created_on
|
19
|
+
end
|
20
|
+
|
21
|
+
test "saves without revision" do
|
22
|
+
p = pages(:welcome)
|
23
|
+
old_versions = p.versions.count
|
24
|
+
p.save_without_revision
|
25
|
+
p.without_revision do
|
26
|
+
p.update_attributes :title => 'changed'
|
27
|
+
end
|
28
|
+
assert_equal old_versions, p.versions.count
|
29
|
+
end
|
30
|
+
|
31
|
+
test "rollback with version number" do
|
32
|
+
p = pages(:welcome)
|
33
|
+
assert_equal 24, p.version
|
34
|
+
assert_equal 'Welcome to the weblog', p.title
|
35
|
+
assert p.revert_to!(23), "Couldn't revert to 23"
|
36
|
+
assert_equal 23, p.version
|
37
|
+
assert_equal 'Welcome to the weblg', p.title
|
38
|
+
end
|
39
|
+
|
40
|
+
test "versioned class name" do
|
41
|
+
assert_equal 'Version', Page.versioned_class_name
|
42
|
+
assert_equal 'LockedPageRevision', LockedPage.versioned_class_name
|
43
|
+
end
|
44
|
+
|
45
|
+
test "versioned class" do
|
46
|
+
assert_equal Page::Version, Page.versioned_class
|
47
|
+
assert_equal LockedPage::LockedPageRevision, LockedPage.versioned_class
|
48
|
+
end
|
49
|
+
|
50
|
+
test "special methods" do
|
51
|
+
assert_nothing_raised { pages(:welcome).feeling_good? }
|
52
|
+
assert_nothing_raised { pages(:welcome).versions.first.feeling_good? }
|
53
|
+
assert_nothing_raised { locked_pages(:welcome).hello_world }
|
54
|
+
assert_nothing_raised { locked_pages(:welcome).versions.first.hello_world }
|
55
|
+
end
|
56
|
+
|
57
|
+
test "version objects are ordered by the version columns" do
|
58
|
+
p = pages(:welcome)
|
59
|
+
assert_sql(/ORDER BY version ASC/) { p.versions(true) }
|
60
|
+
assert_equal [23,24], p.versions.map(&:version)
|
61
|
+
end
|
62
|
+
|
63
|
+
test "rollback with version class" do
|
64
|
+
p = pages(:welcome)
|
65
|
+
assert_equal 24, p.version
|
66
|
+
assert_equal 'Welcome to the weblog', p.title
|
67
|
+
assert p.revert_to!(p.versions.find_by_version(23)), "Couldn't revert to 23"
|
68
|
+
assert_equal 23, p.version
|
69
|
+
assert_equal 'Welcome to the weblg', p.title
|
70
|
+
end
|
71
|
+
|
72
|
+
test "rollback fails with invalid revision" do
|
73
|
+
p = locked_pages(:welcome)
|
74
|
+
assert !p.revert_to!(locked_pages(:thinking))
|
75
|
+
end
|
76
|
+
|
77
|
+
test "saves versioned copy with options" do
|
78
|
+
p = LockedPage.create! :title => 'first title'
|
79
|
+
assert !p.new_record?
|
80
|
+
assert_equal 1, p.versions.size
|
81
|
+
assert_instance_of LockedPage.versioned_class, p.versions.first
|
82
|
+
end
|
83
|
+
|
84
|
+
test "rollback with version number with options" do
|
85
|
+
p = locked_pages(:welcome)
|
86
|
+
assert_equal 'Welcome to the weblog', p.title
|
87
|
+
assert_equal 'LockedPage', p.versions.first.version_type
|
88
|
+
assert p.revert_to!(p.versions.first.lock_version), "Couldn't revert to 23"
|
89
|
+
assert_equal 'Welcome to the weblg', p.title
|
90
|
+
assert_equal 'LockedPage', p.versions.first.version_type
|
91
|
+
end
|
92
|
+
|
93
|
+
test "rollback with version class with options" do
|
94
|
+
p = locked_pages(:welcome)
|
95
|
+
assert_equal 'Welcome to the weblog', p.title
|
96
|
+
assert_equal 'LockedPage', p.versions.first.version_type
|
97
|
+
assert p.revert_to!(p.versions.first), "Couldn't revert to 1"
|
98
|
+
assert_equal 'Welcome to the weblg', p.title
|
99
|
+
assert_equal 'LockedPage', p.versions.first.version_type
|
100
|
+
end
|
101
|
+
|
102
|
+
test "saves versioned copy with sti" do
|
103
|
+
p = SpecialLockedPage.create! :title => 'first title'
|
104
|
+
assert !p.new_record?
|
105
|
+
assert_equal 1, p.versions.size
|
106
|
+
assert_instance_of LockedPage.versioned_class, p.versions.first
|
107
|
+
assert_equal 'SpecialLockedPage', p.versions.first.version_type
|
108
|
+
end
|
109
|
+
|
110
|
+
test "rollback with version number with sti" do
|
111
|
+
p = locked_pages(:thinking)
|
112
|
+
assert_equal 'So I was thinking', p.title
|
113
|
+
|
114
|
+
assert p.revert_to!(p.versions.first.lock_version), "Couldn't revert to 1"
|
115
|
+
assert_equal 'So I was thinking!!!', p.title
|
116
|
+
assert_equal 'SpecialLockedPage', p.versions.first.version_type
|
117
|
+
end
|
118
|
+
|
119
|
+
test "lock version works with versioning" do
|
120
|
+
p = locked_pages(:thinking)
|
121
|
+
p2 = LockedPage.find(p.id)
|
122
|
+
p.title = 'fresh title'
|
123
|
+
p.save
|
124
|
+
assert_equal 2, p.versions.size # limit!
|
125
|
+
assert_raises(ActiveRecord::StaleObjectError) do
|
126
|
+
p2.title = 'stale title'
|
127
|
+
p2.save
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
test "version if condition" do
|
132
|
+
p = Page.create! :title => "title"
|
133
|
+
assert_equal 1, p.version
|
134
|
+
Page.feeling_good = false
|
135
|
+
p.save
|
136
|
+
assert_equal 1, p.version
|
137
|
+
Page.feeling_good = true
|
138
|
+
end
|
139
|
+
|
140
|
+
test "version if condition2" do
|
141
|
+
# set new if condition
|
142
|
+
Page.class_eval do
|
143
|
+
def new_feeling_good() title[0..0] == 'a'; end
|
144
|
+
alias_method :old_feeling_good, :feeling_good?
|
145
|
+
alias_method :feeling_good?, :new_feeling_good
|
146
|
+
end
|
147
|
+
p = Page.create! :title => "title"
|
148
|
+
assert_equal 1, p.version # version does not increment
|
149
|
+
assert_equal 1, p.versions.count
|
150
|
+
p.update_attributes(:title => 'new title')
|
151
|
+
assert_equal 1, p.version # version does not increment
|
152
|
+
assert_equal 1, p.versions.count
|
153
|
+
p.update_attributes(:title => 'a title')
|
154
|
+
assert_equal 2, p.version
|
155
|
+
assert_equal 2, p.versions.count
|
156
|
+
# reset original if condition
|
157
|
+
Page.class_eval { alias_method :feeling_good?, :old_feeling_good }
|
158
|
+
end
|
159
|
+
|
160
|
+
test "version if condition with block" do
|
161
|
+
# set new if condition
|
162
|
+
old_condition = Page.version_condition
|
163
|
+
Page.version_condition = Proc.new { |page| page.title[0..0] == 'b' }
|
164
|
+
p = Page.create! :title => "title"
|
165
|
+
assert_equal 1, p.version # version does not increment
|
166
|
+
assert_equal 1, p.versions.count
|
167
|
+
p.update_attributes(:title => 'a title')
|
168
|
+
assert_equal 1, p.version # version does not increment
|
169
|
+
assert_equal 1, p.versions.count
|
170
|
+
p.update_attributes(:title => 'b title')
|
171
|
+
assert_equal 2, p.version
|
172
|
+
assert_equal 2, p.versions.count
|
173
|
+
# reset original if condition
|
174
|
+
Page.version_condition = old_condition
|
175
|
+
end
|
176
|
+
|
177
|
+
test "version no limit" do
|
178
|
+
p = Page.create! :title => "title", :body => 'first body'
|
179
|
+
p.save
|
180
|
+
p.save
|
181
|
+
5.times do |i|
|
182
|
+
p.title = "title#{i}"
|
183
|
+
p.save
|
184
|
+
assert_equal "title#{i}", p.title
|
185
|
+
assert_equal (i+2), p.version
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
test "version max limit" do
|
190
|
+
p = LockedPage.create! :title => "title"
|
191
|
+
p.update_attributes(:title => "title1")
|
192
|
+
p.update_attributes(:title => "title2")
|
193
|
+
5.times do |i|
|
194
|
+
p.title = "title#{i}"
|
195
|
+
p.save
|
196
|
+
assert_equal "title#{i}", p.title
|
197
|
+
assert_equal (i+4), p.lock_version
|
198
|
+
assert p.versions(true).size <= 2, "locked version can only store 2 versions"
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
test "track altered attributes default value" do
|
203
|
+
assert !Page.track_altered_attributes
|
204
|
+
assert LockedPage.track_altered_attributes
|
205
|
+
assert SpecialLockedPage.track_altered_attributes
|
206
|
+
end
|
207
|
+
|
208
|
+
test "track altered attributes" do
|
209
|
+
p = LockedPage.create! :title => "title"
|
210
|
+
assert_equal 1, p.lock_version
|
211
|
+
assert_equal 1, p.versions(true).size
|
212
|
+
p.body = 'whoa'
|
213
|
+
assert !p.save_version?
|
214
|
+
p.save
|
215
|
+
assert_equal 2, p.lock_version # still increments version because of optimistic locking
|
216
|
+
assert_equal 1, p.versions(true).size
|
217
|
+
p.title = 'updated title'
|
218
|
+
assert p.save_version?
|
219
|
+
p.save
|
220
|
+
assert_equal 3, p.lock_version
|
221
|
+
assert_equal 1, p.versions(true).size # version 1 deleted
|
222
|
+
p.title = 'updated title!'
|
223
|
+
assert p.save_version?
|
224
|
+
p.save
|
225
|
+
assert_equal 4, p.lock_version
|
226
|
+
assert_equal 2, p.versions(true).size # version 1 deleted
|
227
|
+
end
|
228
|
+
|
229
|
+
test "find versions" do
|
230
|
+
assert_equal 1, locked_pages(:welcome).versions.find(:all, :conditions => ['title LIKE ?', '%weblog%']).size
|
231
|
+
end
|
232
|
+
|
233
|
+
test "find version" do
|
234
|
+
assert_equal page_versions(:welcome_1), pages(:welcome).versions.find_by_version(23)
|
235
|
+
end
|
236
|
+
|
237
|
+
test "with sequence" do
|
238
|
+
assert_equal 'widgets_seq', Widget.versioned_class.sequence_name
|
239
|
+
3.times { Widget.create! :name => 'new widget' }
|
240
|
+
assert_equal 3, Widget.count
|
241
|
+
assert_equal 3, Widget.versioned_class.count
|
242
|
+
end
|
243
|
+
|
244
|
+
test "has many through" do
|
245
|
+
assert_equal [authors(:caged), authors(:mly)], pages(:welcome).authors
|
246
|
+
end
|
247
|
+
|
248
|
+
test "has many through with custom association" do
|
249
|
+
assert_equal [authors(:caged), authors(:mly)], pages(:welcome).revisors
|
250
|
+
end
|
251
|
+
|
252
|
+
test "referential integrity" do
|
253
|
+
pages(:welcome).destroy
|
254
|
+
assert_equal 0, Page.count
|
255
|
+
assert_equal 0, Page::Version.count
|
256
|
+
end
|
257
|
+
|
258
|
+
test "association options" do
|
259
|
+
association = Page.reflect_on_association(:versions)
|
260
|
+
options = association.options
|
261
|
+
assert_equal :delete_all, options[:dependent]
|
262
|
+
association = Widget.reflect_on_association(:versions)
|
263
|
+
options = association.options
|
264
|
+
assert_equal :nullify, options[:dependent]
|
265
|
+
assert_equal 'version desc', options[:order]
|
266
|
+
assert_equal 'widget_id', options[:foreign_key]
|
267
|
+
widget = Widget.create! :name => 'new widget'
|
268
|
+
assert_equal 1, Widget.count
|
269
|
+
assert_equal 1, Widget.versioned_class.count
|
270
|
+
widget.destroy
|
271
|
+
assert_equal 0, Widget.count
|
272
|
+
assert_equal 1, Widget.versioned_class.count
|
273
|
+
end
|
274
|
+
|
275
|
+
test "versioned records should belong to parent" do
|
276
|
+
page = pages(:welcome)
|
277
|
+
page_version = page.versions.last
|
278
|
+
assert_equal page, page_version.page
|
279
|
+
end
|
280
|
+
|
281
|
+
test "unaltered attributes" do
|
282
|
+
landmarks(:washington).attributes = landmarks(:washington).attributes.except("id")
|
283
|
+
assert !landmarks(:washington).changed?
|
284
|
+
end
|
285
|
+
|
286
|
+
test "unchanged string attributes" do
|
287
|
+
landmarks(:washington).attributes = landmarks(:washington).attributes.except("id").inject({}) { |params, (key, value)| params.update(key => value.to_s) }
|
288
|
+
assert !landmarks(:washington).changed?
|
289
|
+
end
|
290
|
+
|
291
|
+
test "should find earliest version" do
|
292
|
+
assert_equal page_versions(:welcome_1), pages(:welcome).versions.earliest
|
293
|
+
end
|
294
|
+
|
295
|
+
test "should find latest version" do
|
296
|
+
assert_equal page_versions(:welcome_2), pages(:welcome).versions.latest
|
297
|
+
end
|
298
|
+
|
299
|
+
test "should find previous version" do
|
300
|
+
assert_equal page_versions(:welcome_1), page_versions(:welcome_2).previous
|
301
|
+
assert_equal page_versions(:welcome_1), pages(:welcome).versions.before(page_versions(:welcome_2))
|
302
|
+
end
|
303
|
+
|
304
|
+
test "should find next version" do
|
305
|
+
assert_equal page_versions(:welcome_2), page_versions(:welcome_1).next
|
306
|
+
assert_equal page_versions(:welcome_2), pages(:welcome).versions.after(page_versions(:welcome_1))
|
307
|
+
end
|
308
|
+
|
309
|
+
test "should find version count" do
|
310
|
+
assert_equal 2, pages(:welcome).versions.size
|
311
|
+
end
|
312
|
+
|
313
|
+
test "if changed creates version if a listed column is changed" do
|
314
|
+
landmarks(:washington).name = "Washington"
|
315
|
+
assert landmarks(:washington).changed?
|
316
|
+
assert landmarks(:washington).altered?
|
317
|
+
end
|
318
|
+
|
319
|
+
test "if changed creates version if all listed columns are changed" do
|
320
|
+
landmarks(:washington).name = "Washington"
|
321
|
+
landmarks(:washington).latitude = 1.0
|
322
|
+
landmarks(:washington).longitude = 1.0
|
323
|
+
assert landmarks(:washington).changed?
|
324
|
+
assert landmarks(:washington).altered?
|
325
|
+
end
|
326
|
+
|
327
|
+
test "if changed does not create new version if unlisted column is changed" do
|
328
|
+
landmarks(:washington).doesnt_trigger_version = "This should not trigger version"
|
329
|
+
assert landmarks(:washington).changed?
|
330
|
+
assert !landmarks(:washington).altered?
|
331
|
+
end
|
332
|
+
|
333
|
+
|
334
|
+
end
|
335
|
+
|
336
|
+
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: decisiv-acts_as_versioned
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.6.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rick Olson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-01-05 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: ActiveRecord plugin for versioning your models.
|
17
|
+
email: ken@metaskills.net
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
- CHANGELOG
|
25
|
+
- MIT-LICENSE
|
26
|
+
files:
|
27
|
+
- CHANGELOG
|
28
|
+
- MIT-LICENSE
|
29
|
+
- Rakefile
|
30
|
+
- README.rdoc
|
31
|
+
- RUNNING_UNIT_TESTS
|
32
|
+
- lib/acts_as_versioned.rb
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://github.com/metaskills/acts_as_versioned/
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options:
|
37
|
+
- --main
|
38
|
+
- README.rdoc
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
requirements: []
|
54
|
+
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.2.0
|
57
|
+
signing_key:
|
58
|
+
specification_version: 2
|
59
|
+
summary: ActiveRecord plugin for versioning your models.
|
60
|
+
test_files:
|
61
|
+
- test/fixtures/authors.yml
|
62
|
+
- test/fixtures/landmark_versions.yml
|
63
|
+
- test/fixtures/landmarks.yml
|
64
|
+
- test/fixtures/locked_pages_revisions.yml
|
65
|
+
- test/fixtures/locked_pages.yml
|
66
|
+
- test/fixtures/page_versions.yml
|
67
|
+
- test/fixtures/pages.yml
|
68
|
+
- test/fixtures/widgets.yml
|
69
|
+
- test/helper.rb
|
70
|
+
- test/lib/boot.rb
|
71
|
+
- test/lib/database.yml
|
72
|
+
- test/lib/schema.rb
|
73
|
+
- test/migration_test.rb
|
74
|
+
- test/migrations/1_add_versioned_tables.rb
|
75
|
+
- test/models/author.rb
|
76
|
+
- test/models/landmark.rb
|
77
|
+
- test/models/page.rb
|
78
|
+
- test/models/thing.rb
|
79
|
+
- test/models/widget.rb
|
80
|
+
- test/versioned_test.rb
|