cure_acts_as_versioned 0.6.1 → 0.6.2
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/Gemfile +3 -3
- data/Rakefile +1 -1
- data/acts_as_versioned.gemspec +5 -5
- data/lib/acts_as_versioned.rb +109 -107
- data/test/fixtures/migrations/{1_add_versioned_tables.rb → 2_add_versioned_tables.rb} +0 -0
- data/test/migration_test.rb +0 -1
- metadata +12 -13
data/Gemfile
CHANGED
data/Rakefile
CHANGED
data/acts_as_versioned.gemspec
CHANGED
@@ -13,8 +13,8 @@ Gem::Specification.new do |s|
|
|
13
13
|
## If your rubyforge_project name is different, then edit it and comment out
|
14
14
|
## the sub! line in the Rakefile
|
15
15
|
s.name = 'cure_acts_as_versioned'
|
16
|
-
s.version = '0.6.
|
17
|
-
s.date = '
|
16
|
+
s.version = '0.6.2'
|
17
|
+
s.date = '2014-01-26'
|
18
18
|
s.rubyforge_project = 'acts_as_versioned'
|
19
19
|
|
20
20
|
## Make sure your summary is short. The description may be as long
|
@@ -46,11 +46,11 @@ Gem::Specification.new do |s|
|
|
46
46
|
|
47
47
|
## List your runtime dependencies here. Runtime dependencies are those
|
48
48
|
## that are needed for an end user to actually USE your code.
|
49
|
-
s.add_dependency('activerecord', ["
|
49
|
+
s.add_dependency('activerecord', [">= 3.0.9"])
|
50
50
|
|
51
51
|
## List your development dependencies here. Development dependencies are
|
52
52
|
## those that are only needed during development
|
53
|
-
s.add_development_dependency('sqlite3-ruby', ["
|
53
|
+
s.add_development_dependency('sqlite3-ruby', [">= 1.3.1"])
|
54
54
|
|
55
55
|
## Leave this section as-is. It will be automatically generated from the
|
56
56
|
## contents of your Git repository via the gemspec task. DO NOT REMOVE
|
@@ -74,7 +74,7 @@ Gem::Specification.new do |s|
|
|
74
74
|
test/fixtures/landmarks.yml
|
75
75
|
test/fixtures/locked_pages.yml
|
76
76
|
test/fixtures/locked_pages_revisions.yml
|
77
|
-
test/fixtures/migrations/
|
77
|
+
test/fixtures/migrations/2_add_versioned_tables.rb
|
78
78
|
test/fixtures/page.rb
|
79
79
|
test/fixtures/page_versions.yml
|
80
80
|
test/fixtures/pages.yml
|
data/lib/acts_as_versioned.rb
CHANGED
@@ -249,12 +249,12 @@ module ActiveRecord #:nodoc:
|
|
249
249
|
|
250
250
|
versioned_class.cattr_accessor :original_class
|
251
251
|
versioned_class.original_class = self
|
252
|
-
versioned_class.
|
252
|
+
versioned_class.table_name = versioned_table_name
|
253
253
|
versioned_class.belongs_to self.to_s.demodulize.underscore.to_sym,
|
254
254
|
:class_name => "::#{self.to_s}",
|
255
255
|
:foreign_key => versioned_foreign_key
|
256
256
|
versioned_class.send :include, options[:extend] if options[:extend].is_a?(Module)
|
257
|
-
versioned_class.
|
257
|
+
versioned_class.sequence_name = version_sequence_name if version_sequence_name
|
258
258
|
end
|
259
259
|
|
260
260
|
module Behaviors
|
@@ -268,134 +268,136 @@ module ActiveRecord #:nodoc:
|
|
268
268
|
after_save :clear_old_versions
|
269
269
|
end
|
270
270
|
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
rev.save
|
281
|
-
end
|
271
|
+
# Saves a version of the model in the versioned table. This is called in the after_save callback by default
|
272
|
+
def save_version
|
273
|
+
if @saving_version
|
274
|
+
@saving_version = nil
|
275
|
+
rev = self.class.versioned_class.new
|
276
|
+
clone_versioned_model(self, rev)
|
277
|
+
rev.send("#{self.class.version_column}=", send(self.class.version_column))
|
278
|
+
rev.send("#{self.class.versioned_foreign_key}=", id)
|
279
|
+
rev.save
|
282
280
|
end
|
281
|
+
end
|
283
282
|
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
end
|
283
|
+
# Clears old revisions if a limit is set with the :limit option in <tt>acts_as_versioned</tt>.
|
284
|
+
# Override this method to set your own criteria for clearing old versions.
|
285
|
+
def clear_old_versions
|
286
|
+
return if self.class.max_version_limit == 0
|
287
|
+
excess_baggage = send(self.class.version_column).to_i - self.class.max_version_limit
|
288
|
+
if excess_baggage > 0
|
289
|
+
self.class.versioned_class.delete_all ["#{self.class.version_column} <= ? and #{self.class.versioned_foreign_key} = ?", excess_baggage, id]
|
292
290
|
end
|
291
|
+
end
|
293
292
|
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
end
|
301
|
-
self.clone_versioned_model(version, self)
|
302
|
-
send("#{self.class.version_column}=", version.send(self.class.version_column))
|
303
|
-
true
|
293
|
+
# Reverts a model to a given version. Takes either a version number or an instance of the versioned model
|
294
|
+
def revert_to(version)
|
295
|
+
if version.is_a?(self.class.versioned_class)
|
296
|
+
return false unless version.send(self.class.versioned_foreign_key) == id and !version.new_record?
|
297
|
+
else
|
298
|
+
return false unless version = versions.where(self.class.version_column => version).first
|
304
299
|
end
|
300
|
+
self.clone_versioned_model(version, self)
|
301
|
+
send("#{self.class.version_column}=", version.send(self.class.version_column))
|
302
|
+
true
|
303
|
+
end
|
305
304
|
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
305
|
+
# Reverts a model to a given version and saves the model.
|
306
|
+
# Takes either a version number or an instance of the versioned model
|
307
|
+
def revert_to!(version)
|
308
|
+
revert_to(version) ? save_without_revision : false
|
309
|
+
end
|
311
310
|
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
311
|
+
# Temporarily turns off Optimistic Locking while saving. Used when reverting so that a new version is not created.
|
312
|
+
def save_without_revision
|
313
|
+
save_without_revision!
|
314
|
+
true
|
315
|
+
rescue
|
316
|
+
false
|
317
|
+
end
|
319
318
|
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
end
|
319
|
+
def save_without_revision!
|
320
|
+
without_locking do
|
321
|
+
without_revision do
|
322
|
+
save!
|
325
323
|
end
|
326
324
|
end
|
325
|
+
end
|
327
326
|
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
# Clones a model. Used when saving a new version or reverting a model's version.
|
333
|
-
def clone_versioned_model(orig_model, new_model)
|
334
|
-
self.class.versioned_columns.each do |col|
|
335
|
-
new_model[col.name] = orig_model.send(col.name) if orig_model.has_attribute?(col.name)
|
336
|
-
end
|
327
|
+
def altered?
|
328
|
+
track_altered_attributes ? (version_if_changed - changed).length < version_if_changed.length : changed?
|
329
|
+
end
|
337
330
|
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
end
|
331
|
+
# Clones a model. Used when saving a new version or reverting a model's version.
|
332
|
+
def clone_versioned_model(orig_model, new_model)
|
333
|
+
self.class.versioned_columns.each do |col|
|
334
|
+
new_model[col.name] = orig_model.send(col.name) if orig_model.has_attribute?(col.name)
|
343
335
|
end
|
344
336
|
|
345
|
-
|
346
|
-
|
347
|
-
|
337
|
+
clone_inheritance_column(orig_model, new_model)
|
338
|
+
end
|
339
|
+
|
340
|
+
def clone_inheritance_column(orig_model, new_model)
|
341
|
+
if orig_model.is_a?(self.class.versioned_class) && new_model.class.column_names.include?(new_model.class.inheritance_column.to_s)
|
342
|
+
new_model[new_model.class.inheritance_column] = orig_model[self.class.versioned_inheritance_column]
|
343
|
+
elsif new_model.is_a?(self.class.versioned_class) && new_model.class.column_names.include?(self.class.versioned_inheritance_column.to_s)
|
344
|
+
new_model[self.class.versioned_inheritance_column] = orig_model[orig_model.class.inheritance_column]
|
348
345
|
end
|
346
|
+
end
|
349
347
|
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
when version_condition.is_a?(Symbol)
|
355
|
-
send(version_condition)
|
356
|
-
when version_condition.respond_to?(:call) && (version_condition.arity == 1 || version_condition.arity == -1)
|
357
|
-
version_condition.call(self)
|
358
|
-
else
|
359
|
-
version_condition
|
360
|
-
end
|
361
|
-
end
|
348
|
+
# Checks whether a new version shall be saved or not. Calls <tt>version_condition_met?</tt> and <tt>changed?</tt>.
|
349
|
+
def save_version?
|
350
|
+
version_condition_met? && altered?
|
351
|
+
end
|
362
352
|
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
353
|
+
# Checks condition set in the :if option to check whether a revision should be created or not. Override this for
|
354
|
+
# custom version condition checking.
|
355
|
+
def version_condition_met?
|
356
|
+
case
|
357
|
+
when version_condition.is_a?(Symbol)
|
358
|
+
send(version_condition)
|
359
|
+
when version_condition.respond_to?(:call) && (version_condition.arity == 1 || version_condition.arity == -1)
|
360
|
+
version_condition.call(self)
|
361
|
+
else
|
362
|
+
version_condition
|
371
363
|
end
|
364
|
+
end
|
372
365
|
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
366
|
+
# Executes the block with the versioning callbacks disabled.
|
367
|
+
#
|
368
|
+
# @foo.without_revision do
|
369
|
+
# @foo.save
|
370
|
+
# end
|
371
|
+
#
|
372
|
+
def without_revision(&block)
|
373
|
+
self.class.without_revision(&block)
|
374
|
+
end
|
382
375
|
|
383
|
-
|
384
|
-
|
376
|
+
# Turns off optimistic locking for the duration of the block
|
377
|
+
#
|
378
|
+
# @foo.without_locking do
|
379
|
+
# @foo.save
|
380
|
+
# end
|
381
|
+
#
|
382
|
+
def without_locking(&block)
|
383
|
+
self.class.without_locking(&block)
|
384
|
+
end
|
385
385
|
|
386
|
-
|
386
|
+
def empty_callback()
|
387
|
+
end
|
387
388
|
|
388
|
-
|
389
|
-
# sets the new version before saving, unless you're using optimistic locking. In that case, let it take care of the version.
|
390
|
-
def set_new_version
|
391
|
-
@saving_version = new_record? || save_version?
|
392
|
-
self.send("#{self.class.version_column}=", next_version) if new_record? || (!locking_enabled? && save_version?)
|
393
|
-
end
|
389
|
+
#:nodoc:
|
394
390
|
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
391
|
+
protected
|
392
|
+
# sets the new version before saving, unless you're using optimistic locking. In that case, let it take care of the version.
|
393
|
+
def set_new_version
|
394
|
+
@saving_version = new_record? || save_version?
|
395
|
+
self.send("#{self.class.version_column}=", next_version) if new_record? || (!locking_enabled? && save_version?)
|
396
|
+
end
|
397
|
+
|
398
|
+
# Gets the next available version for the current record, or 1 for a new record
|
399
|
+
def next_version
|
400
|
+
(new_record? ? 0 : versions.calculate(:maximum, version_column).to_i) + 1
|
399
401
|
end
|
400
402
|
|
401
403
|
module ClassMethods
|
File without changes
|
data/test/migration_test.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cure_acts_as_versioned
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 3
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 6
|
9
|
-
-
|
10
|
-
version: 0.6.
|
9
|
+
- 2
|
10
|
+
version: 0.6.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Ward Vandewege
|
@@ -15,8 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
19
|
-
default_executable:
|
18
|
+
date: 2014-01-26 00:00:00 Z
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
22
21
|
name: activerecord
|
@@ -24,13 +23,14 @@ dependencies:
|
|
24
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
24
|
none: false
|
26
25
|
requirements:
|
27
|
-
- -
|
26
|
+
- - ">="
|
28
27
|
- !ruby/object:Gem::Version
|
29
|
-
hash:
|
28
|
+
hash: 21
|
30
29
|
segments:
|
31
30
|
- 3
|
32
|
-
-
|
33
|
-
|
31
|
+
- 0
|
32
|
+
- 9
|
33
|
+
version: 3.0.9
|
34
34
|
type: :runtime
|
35
35
|
version_requirements: *id001
|
36
36
|
- !ruby/object:Gem::Dependency
|
@@ -39,7 +39,7 @@ dependencies:
|
|
39
39
|
requirement: &id002 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
|
-
- -
|
42
|
+
- - ">="
|
43
43
|
- !ruby/object:Gem::Version
|
44
44
|
hash: 25
|
45
45
|
segments:
|
@@ -77,7 +77,7 @@ files:
|
|
77
77
|
- test/fixtures/landmarks.yml
|
78
78
|
- test/fixtures/locked_pages.yml
|
79
79
|
- test/fixtures/locked_pages_revisions.yml
|
80
|
-
- test/fixtures/migrations/
|
80
|
+
- test/fixtures/migrations/2_add_versioned_tables.rb
|
81
81
|
- test/fixtures/page.rb
|
82
82
|
- test/fixtures/page_versions.yml
|
83
83
|
- test/fixtures/pages.yml
|
@@ -85,7 +85,6 @@ files:
|
|
85
85
|
- test/migration_test.rb
|
86
86
|
- test/schema.rb
|
87
87
|
- test/versioned_test.rb
|
88
|
-
has_rdoc: true
|
89
88
|
homepage: http://github.com/cure/acts_as_versioned
|
90
89
|
licenses: []
|
91
90
|
|
@@ -115,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
115
114
|
requirements: []
|
116
115
|
|
117
116
|
rubyforge_project: acts_as_versioned
|
118
|
-
rubygems_version: 1.
|
117
|
+
rubygems_version: 1.8.24
|
119
118
|
signing_key:
|
120
119
|
specification_version: 2
|
121
120
|
summary: Add simple versioning to ActiveRecord models.
|