mongo_mapper_acts_as_versioned 0.0.10 → 0.0.11
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/lib/acts_as_versioned.rb +21 -10
- data/spec/acts_as_versioned_spec.rb +17 -0
- metadata +3 -3
data/lib/acts_as_versioned.rb
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
module MongoMapper
|
2
2
|
module Acts
|
3
3
|
module Versioned
|
4
|
-
VERSION = '0.0.
|
5
|
-
CALLBACKS = [:save_version, :save_version
|
4
|
+
VERSION = '0.0.11'
|
5
|
+
CALLBACKS = [:save_version, :save_version?, :clear_old_versions]
|
6
6
|
|
7
7
|
def self.configure(model)
|
8
8
|
model.class_eval do
|
9
|
-
cattr_accessor :versioned_class_name, :non_versioned_keys
|
9
|
+
cattr_accessor :versioned_class_name, :non_versioned_keys, :max_version_limit
|
10
10
|
|
11
11
|
self.versioned_class_name = :Version
|
12
|
-
self.
|
12
|
+
self.max_version_limit = 0
|
13
|
+
self.non_versioned_keys = %w(
|
13
14
|
_id _type created_at updated_at creator_id updater_id version
|
14
15
|
)
|
15
16
|
|
@@ -21,27 +22,39 @@ module MongoMapper
|
|
21
22
|
end
|
22
23
|
|
23
24
|
many :versions, :class => "#{self}::#{versioned_class_name}".constantize do
|
24
|
-
def [](
|
25
|
-
detect {|
|
25
|
+
def [](version)
|
26
|
+
detect { |doc| doc.version.to_s == version.to_s }
|
26
27
|
end
|
27
28
|
end
|
28
29
|
end
|
29
30
|
|
30
31
|
model.key :version, Integer
|
31
32
|
model.before_save :save_version
|
33
|
+
model.before_save :clear_old_versions
|
32
34
|
end
|
33
35
|
|
34
36
|
module InstanceMethods
|
35
37
|
def save_version
|
36
38
|
if new_record? || save_version?
|
37
39
|
self.version = next_version
|
40
|
+
|
38
41
|
rev = self.class.versioned_class.new
|
39
42
|
clone_attributes(self, rev)
|
40
43
|
rev.version = version
|
44
|
+
|
41
45
|
self.versions << rev
|
42
46
|
end
|
43
47
|
end
|
44
48
|
|
49
|
+
def clear_old_versions
|
50
|
+
return if self.class.max_version_limit == 0
|
51
|
+
excess_bagage = version.to_i - self.class.max_version_limit
|
52
|
+
|
53
|
+
if excess_bagage > 0
|
54
|
+
versions.reject! { |v| v.version.to_i <= excess_bagage }
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
45
58
|
def revert_to(version)
|
46
59
|
if version.is_a?(self.class.versioned_class)
|
47
60
|
return false if version.new_record?
|
@@ -56,7 +69,7 @@ module MongoMapper
|
|
56
69
|
end
|
57
70
|
|
58
71
|
def revert_to!(version)
|
59
|
-
revert_to(version)
|
72
|
+
revert_to(version) and save_without_revision or false
|
60
73
|
end
|
61
74
|
|
62
75
|
def save_without_revision
|
@@ -67,9 +80,7 @@ module MongoMapper
|
|
67
80
|
end
|
68
81
|
|
69
82
|
def save_without_revision!
|
70
|
-
without_revision
|
71
|
-
save!
|
72
|
-
end
|
83
|
+
without_revision { save! }
|
73
84
|
end
|
74
85
|
|
75
86
|
def clone_attributes(orig_model, new_model)
|
@@ -33,6 +33,23 @@ describe MongoMapper::Acts::Versioned do
|
|
33
33
|
l.versions.first.should be_a(Landmark.versioned_class)
|
34
34
|
end
|
35
35
|
|
36
|
+
it 'should clear old versions when a limit is set' do
|
37
|
+
Landmark.max_version_limit = 3
|
38
|
+
|
39
|
+
l = Landmark.create(:title => 'title')
|
40
|
+
(2..10).each do |i|
|
41
|
+
l = Landmark.first
|
42
|
+
l.update_attributes(:title => "title#{i}")
|
43
|
+
end
|
44
|
+
|
45
|
+
l = l.reload
|
46
|
+
l.versions.size.should == 3
|
47
|
+
l.versions.first.version.should == 8
|
48
|
+
l.versions.last.version.should == 10
|
49
|
+
|
50
|
+
Landmark.max_version_limit = 0
|
51
|
+
end
|
52
|
+
|
36
53
|
it 'should save without revision' do
|
37
54
|
l = Landmark.create(:title => 'title')
|
38
55
|
l.version.should == 1
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 11
|
9
|
+
version: 0.0.11
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Gigamo
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-09-
|
17
|
+
date: 2010-09-22 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|