mongoid_versioning 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/Gemfile.lock +9 -9
- data/README.md +7 -1
- data/lib/mongoid_versioning/version.rb +1 -1
- data/lib/mongoid_versioning/versioned.rb +43 -52
- data/test/mongoid_versioning/versioned_test.rb +185 -112
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 87547bda9b49e1db52669360fa1c652b81797bbc
|
4
|
+
data.tar.gz: 4970ab0963f35517304e732b5779fb2fa928f62c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 25ae935bcacf3284bd31d7af9b03203b0f9b45801f3cfacd959114c8ce3b53bf47a69b6760400de0d493d3df48f9632e60c15466ba3a66ffceac00088f02d643
|
7
|
+
data.tar.gz: 736f7cf63d417a05d4d48c12ad593722cd5d3877c85a79831b864cb10e481b1b6f05bfafeaa05a9695930f1c52633959245ca4fc763e9b4317c8a591027a7576
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
## 0.0.2
|
2
|
+
|
3
|
+
* Calling `#revise` on new document simply creates it (`:revise` callbacks are not triggered).
|
4
|
+
* Added `#revise!` method raising Mongoid exceptions.
|
5
|
+
* Added tests for conflicting updates.
|
6
|
+
* Added thread-safe version check and a loop to resolve version discrepancies in case of failure.
|
7
|
+
|
8
|
+
## 0.0.1
|
9
|
+
|
10
|
+
Initial version
|
data/Gemfile.lock
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
mongoid_versioning (0.0.
|
4
|
+
mongoid_versioning (0.0.2)
|
5
5
|
mongoid (~> 4.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activemodel (4.
|
11
|
-
activesupport (= 4.
|
10
|
+
activemodel (4.2.0)
|
11
|
+
activesupport (= 4.2.0)
|
12
12
|
builder (~> 3.1)
|
13
|
-
activesupport (4.
|
14
|
-
i18n (~> 0.
|
13
|
+
activesupport (4.2.0)
|
14
|
+
i18n (~> 0.7)
|
15
15
|
json (~> 1.7, >= 1.7.7)
|
16
16
|
minitest (~> 5.1)
|
17
|
-
thread_safe (~> 0.
|
17
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
18
18
|
tzinfo (~> 1.1)
|
19
19
|
bson (2.3.0)
|
20
20
|
builder (3.2.2)
|
21
21
|
celluloid (0.16.0)
|
22
22
|
timers (~> 4.0.0)
|
23
23
|
coderay (1.1.0)
|
24
|
-
connection_pool (2.
|
24
|
+
connection_pool (2.1.0)
|
25
25
|
coveralls (0.7.1)
|
26
26
|
multi_json (~> 1.3)
|
27
27
|
rest-client
|
@@ -42,7 +42,7 @@ GEM
|
|
42
42
|
guard (~> 2.0)
|
43
43
|
minitest (>= 3.0)
|
44
44
|
hitimes (1.2.2)
|
45
|
-
i18n (0.
|
45
|
+
i18n (0.7.0)
|
46
46
|
json (1.8.1)
|
47
47
|
listen (2.7.11)
|
48
48
|
celluloid (>= 0.15.2)
|
@@ -57,7 +57,7 @@ GEM
|
|
57
57
|
moped (~> 2.0.0)
|
58
58
|
origin (~> 2.1)
|
59
59
|
tzinfo (>= 0.3.37)
|
60
|
-
moped (2.0.
|
60
|
+
moped (2.0.2)
|
61
61
|
bson (~> 2.2)
|
62
62
|
connection_pool (~> 2.0)
|
63
63
|
optionable (~> 0.2.0)
|
data/README.md
CHANGED
@@ -2,6 +2,10 @@
|
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/tomasc/mongoid_versioning.svg)](https://travis-ci.org/tomasc/mongoid_versioning) [![Gem Version](https://badge.fury.io/rb/mongoid_versioning.svg)](http://badge.fury.io/rb/mongoid_versioning) [![Coverage Status](https://img.shields.io/coveralls/tomasc/mongoid_versioning.svg)](https://coveralls.io/r/tomasc/mongoid_versioning)
|
4
4
|
|
5
|
+
Versioning of Mongoid documents. Past versions are stored in separate collection.
|
6
|
+
|
7
|
+
This gem is intended for versioning of whole documents. However, it might be possible to extend its support to embedded documents as well. Pull requests are welcome.
|
8
|
+
|
5
9
|
## Installation
|
6
10
|
|
7
11
|
Add this line to your application's Gemfile:
|
@@ -51,7 +55,9 @@ doc._version # => 1
|
|
51
55
|
doc._based_on_version # => nil
|
52
56
|
```
|
53
57
|
|
54
|
-
The `#revise` method validates the document and runs `:revise`, `:save` and `:update` callbacks (
|
58
|
+
The `#revise` method validates the document and runs `:revise`, `:save` and `:update` callbacks. (Please note that running `#revise` on new document will resort to standard `#save`.)
|
59
|
+
|
60
|
+
A `#revise!` method, raising `Mongoid::Errors::Validations` and `Mongoid::Errors::Callbacks` exceptions, is also available.
|
55
61
|
|
56
62
|
### Retrieving versions
|
57
63
|
|
@@ -9,10 +9,11 @@ module MongoidVersioning
|
|
9
9
|
field :_version, type: Integer
|
10
10
|
field :_based_on_version, type: Integer
|
11
11
|
|
12
|
-
|
12
|
+
versions_collection.indexes.create(
|
13
13
|
{ _orig_id: 1, _version: 1 }, { unique: true }
|
14
14
|
)
|
15
15
|
|
16
|
+
before_create :set_initial_version
|
16
17
|
after_initialize :revert_id
|
17
18
|
end
|
18
19
|
end
|
@@ -23,16 +24,36 @@ module MongoidVersioning
|
|
23
24
|
def versions_collection_name
|
24
25
|
[collection.name, 'versions'].join('.')
|
25
26
|
end
|
27
|
+
|
28
|
+
def versions_collection
|
29
|
+
collection.database[versions_collection_name]
|
30
|
+
end
|
26
31
|
end
|
27
32
|
|
28
33
|
# =====================================================================
|
29
34
|
|
30
35
|
def revise options={}
|
31
|
-
if new_record?
|
32
|
-
|
33
|
-
|
34
|
-
|
36
|
+
return save if new_record?
|
37
|
+
|
38
|
+
return false if performing_validations?(options) && invalid?(:update)
|
39
|
+
process_flagged_destroys
|
40
|
+
result = run_callbacks(:revise) do
|
41
|
+
run_callbacks(:save) do
|
42
|
+
run_callbacks(:update) do
|
43
|
+
_revise
|
44
|
+
true
|
45
|
+
end
|
46
|
+
end
|
35
47
|
end
|
48
|
+
post_process_persist(result, options) and self
|
49
|
+
end
|
50
|
+
|
51
|
+
def revise! options={}
|
52
|
+
unless revise(options)
|
53
|
+
fail_due_to_validation! unless errors.empty?
|
54
|
+
fail_due_to_callback!(:revise!)
|
55
|
+
end
|
56
|
+
true
|
36
57
|
end
|
37
58
|
|
38
59
|
# ---------------------------------------------------------------------
|
@@ -59,65 +80,35 @@ module MongoidVersioning
|
|
59
80
|
|
60
81
|
private # =============================================================
|
61
82
|
|
83
|
+
def set_initial_version
|
84
|
+
self._version ||= 1
|
85
|
+
end
|
86
|
+
|
62
87
|
def revert_id
|
63
88
|
return unless self['_orig_id']
|
64
89
|
self._id = self['_orig_id']
|
65
90
|
end
|
66
91
|
|
67
|
-
# these mirror the #create and #save methods from Mongoid
|
68
|
-
def _create_revised options={}
|
69
|
-
return self if performing_validations?(options) && invalid?(:create)
|
70
|
-
result = run_callbacks(:revise) do
|
71
|
-
run_callbacks(:save) do
|
72
|
-
run_callbacks(:create) do
|
73
|
-
_revise
|
74
|
-
true
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
post_process_persist(result, options) and self
|
79
|
-
end
|
80
|
-
|
81
|
-
def _update_revised options={}
|
82
|
-
return false if performing_validations?(options) && invalid?(:update)
|
83
|
-
process_flagged_destroys
|
84
|
-
result = run_callbacks(:revise) do
|
85
|
-
run_callbacks(:save) do
|
86
|
-
run_callbacks(:update) do
|
87
|
-
_revise
|
88
|
-
true
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
92
|
-
post_process_persist(result, options) and self
|
93
|
-
end
|
94
|
-
|
95
92
|
def _revise
|
96
|
-
|
97
|
-
|
98
|
-
# 1. get the latest version as stored in the db
|
99
|
-
if previous_doc = self.class.collection.find({ _id: id }).first
|
100
|
-
|
101
|
-
previous_version = previous_doc["_version"] || 1
|
93
|
+
loop do
|
94
|
+
previous_doc = latest_version
|
102
95
|
|
103
96
|
previous_doc['_orig_id'] = previous_doc['_id']
|
104
97
|
previous_doc['_id'] = BSON::ObjectId.new
|
105
|
-
previous_doc['_version'] = previous_version
|
106
98
|
|
107
|
-
|
108
|
-
|
109
|
-
|
99
|
+
current_version = previous_doc._version
|
100
|
+
|
101
|
+
res1 = self.class.versions_collection.where(_orig_id: previous_doc['_orig_id'], _version: previous_doc._version).upsert(previous_doc.as_document)
|
110
102
|
|
111
|
-
|
112
|
-
|
113
|
-
self._version = previous_version.to_i+1
|
103
|
+
self._based_on_version = _version || current_version
|
104
|
+
self._version = current_version+1
|
114
105
|
|
115
|
-
|
106
|
+
res2 = self.class.collection.where(_id: id, _version: current_version).update(self.as_document)
|
116
107
|
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
# }
|
108
|
+
# replay flow if someone else updated the document before us
|
109
|
+
break unless res2['n'] != 1
|
110
|
+
end
|
121
111
|
end
|
112
|
+
|
122
113
|
end
|
123
|
-
end
|
114
|
+
end
|
@@ -32,12 +32,19 @@ module MongoidVersioning
|
|
32
32
|
# =====================================================================
|
33
33
|
|
34
34
|
describe 'fields' do
|
35
|
-
|
36
|
-
subject.must_respond_to :_version
|
35
|
+
describe '_version' do
|
36
|
+
it { subject.must_respond_to :_version }
|
37
|
+
it { subject._version.must_be_nil }
|
38
|
+
|
39
|
+
describe 'on create' do
|
40
|
+
before { subject.save }
|
41
|
+
it { subject._version.must_equal 1 }
|
42
|
+
end
|
37
43
|
end
|
38
44
|
|
39
|
-
|
40
|
-
subject.must_respond_to :_based_on_version
|
45
|
+
describe '_based_on_version' do
|
46
|
+
it { subject.must_respond_to :_based_on_version }
|
47
|
+
it { subject._based_on_version.must_be_nil }
|
41
48
|
end
|
42
49
|
end
|
43
50
|
|
@@ -56,141 +63,193 @@ module MongoidVersioning
|
|
56
63
|
describe 'instance methods' do
|
57
64
|
describe '#revise' do
|
58
65
|
|
59
|
-
describe '
|
60
|
-
let(:
|
66
|
+
describe 'on new document' do
|
67
|
+
let(:new_document) { TestDocument.new(name: 'New') }
|
68
|
+
|
69
|
+
before do
|
70
|
+
new_document.name = 'Foo'
|
71
|
+
new_document.revise
|
61
72
|
|
62
|
-
|
63
|
-
|
73
|
+
@versions = TestDocument.versions_collection.where(_orig_id: new_document.id).sort(_version: -1)
|
74
|
+
@current = TestDocument.collection.where(_id: new_document.id).first
|
64
75
|
end
|
65
|
-
end
|
66
76
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
77
|
+
it 'sets _version' do
|
78
|
+
new_document._version.must_equal 1
|
79
|
+
end
|
80
|
+
it 'sets _based_on_version' do
|
81
|
+
new_document._based_on_version.must_be_nil
|
82
|
+
end
|
71
83
|
|
72
|
-
|
73
|
-
|
84
|
+
describe 'when invalid' do
|
85
|
+
before { new_document.name = nil }
|
86
|
+
|
87
|
+
it 'returns false' do
|
88
|
+
new_document.revise.must_equal false
|
89
|
+
end
|
74
90
|
end
|
75
91
|
|
76
92
|
describe 'callbacks' do
|
77
|
-
it '
|
78
|
-
|
93
|
+
it 'does not run :revise' do
|
94
|
+
new_document.callbacks.wont_include 'before_revise'
|
95
|
+
new_document.callbacks.wont_include 'after_revise'
|
79
96
|
end
|
80
97
|
end
|
81
98
|
|
82
|
-
describe '
|
83
|
-
it '
|
84
|
-
@
|
85
|
-
end
|
86
|
-
it '_version to 1' do
|
87
|
-
@current_docs.first['_version'].must_equal 1
|
88
|
-
end
|
89
|
-
it '_based_on_version at nil' do
|
90
|
-
@current_docs.first['_based_on_version'].must_be_nil
|
91
|
-
end
|
92
|
-
it 'maintains only one current doc' do
|
93
|
-
@current_docs.count.must_equal 1
|
99
|
+
describe 'versions' do
|
100
|
+
it 'does not store anything' do
|
101
|
+
@versions.count.must_equal 0
|
94
102
|
end
|
95
103
|
end
|
96
104
|
|
97
|
-
describe '
|
98
|
-
it '
|
99
|
-
@
|
105
|
+
describe 'current' do
|
106
|
+
it 'stores updated doc into current collection' do
|
107
|
+
@current["_version"].must_equal 1
|
108
|
+
@current["_based_on_version"].must_be_nil
|
109
|
+
@current["name"].must_equal 'Foo'
|
100
110
|
end
|
101
111
|
end
|
102
112
|
end
|
103
113
|
|
104
114
|
# ---------------------------------------------------------------------
|
105
115
|
|
106
|
-
describe 'existing
|
107
|
-
let(:existing_document) { TestDocument.create(name: '
|
116
|
+
describe 'on existing document' do
|
117
|
+
let(:existing_document) { TestDocument.create(name: 'Existing') }
|
108
118
|
|
109
119
|
before do
|
110
120
|
existing_document.callbacks = []
|
111
121
|
existing_document.name = 'Foo'
|
112
122
|
existing_document.revise
|
113
123
|
|
114
|
-
@
|
115
|
-
@
|
124
|
+
@versions = TestDocument.versions_collection.where(_orig_id: existing_document.id).sort(_version: -1)
|
125
|
+
@current = TestDocument.collection.where(_id: existing_document.id).first
|
126
|
+
end
|
127
|
+
|
128
|
+
it 'sets _version' do
|
129
|
+
existing_document._version.must_equal 2
|
130
|
+
end
|
131
|
+
it 'sets _based_on_version' do
|
132
|
+
existing_document._based_on_version.must_equal 1
|
133
|
+
end
|
134
|
+
|
135
|
+
describe 'when invalid' do
|
136
|
+
before { existing_document.name = nil }
|
137
|
+
|
138
|
+
it 'returns false' do
|
139
|
+
existing_document.revise.must_equal false
|
140
|
+
end
|
116
141
|
end
|
117
142
|
|
118
143
|
describe 'callbacks' do
|
119
|
-
it 'runs :revise, :save, :
|
144
|
+
it 'runs :revise, :save, :create' do
|
120
145
|
existing_document.callbacks.must_equal %w(before_revise before_save before_update after_update after_save after_revise)
|
121
146
|
end
|
122
147
|
end
|
123
148
|
|
124
|
-
describe '
|
125
|
-
it '
|
126
|
-
@
|
127
|
-
|
128
|
-
|
129
|
-
@
|
130
|
-
end
|
131
|
-
it 'sets the _based_on_version to 1' do
|
132
|
-
@current_docs.first['_based_on_version'].must_equal 1
|
149
|
+
describe 'versions' do
|
150
|
+
it 'stores previous doc' do
|
151
|
+
@versions.count.must_equal 1
|
152
|
+
@versions.first["_version"].must_equal 1
|
153
|
+
@versions.first["_based_on_version"].must_be_nil
|
154
|
+
@versions.first["name"].must_equal 'Existing'
|
133
155
|
end
|
134
|
-
|
135
|
-
|
156
|
+
end
|
157
|
+
|
158
|
+
describe 'current' do
|
159
|
+
it 'stores updated doc into current collection' do
|
160
|
+
@current["_version"].must_equal 2
|
161
|
+
@current["_based_on_version"].must_equal 1
|
162
|
+
@current["name"].must_equal 'Foo'
|
136
163
|
end
|
137
164
|
end
|
138
165
|
|
139
|
-
describe '
|
140
|
-
|
141
|
-
|
166
|
+
describe 'subsequent revise' do
|
167
|
+
before do
|
168
|
+
existing_document.callbacks = []
|
169
|
+
existing_document.name = 'Bar'
|
170
|
+
existing_document.revise
|
171
|
+
|
172
|
+
@versions = TestDocument.versions_collection.where(_orig_id: existing_document.id).sort(_version: -1)
|
173
|
+
@current = TestDocument.collection.where(_id: existing_document.id).first
|
142
174
|
end
|
143
175
|
|
144
|
-
it '_version
|
145
|
-
|
176
|
+
it 'sets _version' do
|
177
|
+
existing_document._version.must_equal 3
|
146
178
|
end
|
147
|
-
it '
|
148
|
-
|
179
|
+
it 'sets _based_on_version' do
|
180
|
+
existing_document._based_on_version.must_equal 2
|
149
181
|
end
|
150
|
-
|
151
|
-
|
182
|
+
|
183
|
+
describe 'versions' do
|
184
|
+
it 'stores previous doc' do
|
185
|
+
@versions.count.must_equal 2
|
186
|
+
@versions.map{ |v| v['_version'] }.must_equal [2, 1]
|
187
|
+
@versions.map{ |v| v['_based_on_version'] }.must_equal [1, nil]
|
188
|
+
@versions.first["_version"].must_equal 2
|
189
|
+
@versions.first["_based_on_version"].must_equal 1
|
190
|
+
@versions.first["name"].must_equal 'Foo'
|
191
|
+
end
|
152
192
|
end
|
153
|
-
|
154
|
-
|
193
|
+
|
194
|
+
describe 'current' do
|
195
|
+
it 'stores updated doc into current collection' do
|
196
|
+
@current["_version"].must_equal 3
|
197
|
+
@current["_based_on_version"].must_equal 2
|
198
|
+
@current["name"].must_equal 'Bar'
|
199
|
+
end
|
155
200
|
end
|
156
201
|
end
|
157
202
|
end
|
158
203
|
|
159
204
|
# ---------------------------------------------------------------------
|
160
205
|
|
161
|
-
describe '
|
206
|
+
describe 'on concurrent updates' do
|
162
207
|
before do
|
163
|
-
|
164
|
-
subject.revise
|
165
|
-
subject.name = 'v2'
|
166
|
-
subject.revise
|
167
|
-
subject.name = 'v3'
|
168
|
-
subject.revise
|
208
|
+
@doc = TestDocument.create(name: 'Doc')
|
169
209
|
|
170
|
-
@
|
171
|
-
@
|
210
|
+
@user1 = TestDocument.where(_id: @doc.id).first
|
211
|
+
@user2 = TestDocument.where(_id: @doc.id).first
|
212
|
+
|
213
|
+
@user2.name = 'Doc2'
|
214
|
+
@user1.name = 'Doc1'
|
215
|
+
|
216
|
+
@user2.revise
|
217
|
+
@user1.revise
|
218
|
+
|
219
|
+
@result = TestDocument.where(_id: @doc.id).first
|
220
|
+
@versions = TestDocument.versions_collection.where(_orig_id: @doc.id).sort(_version: -1)
|
221
|
+
@current = TestDocument.collection.where(_id: @doc.id).first
|
172
222
|
end
|
173
223
|
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
@current_docs.first['_based_on_version'].must_equal 2
|
179
|
-
@current_docs.count.must_equal 1
|
180
|
-
end
|
224
|
+
it 'will correctly set values' do
|
225
|
+
@result._version.must_equal 3
|
226
|
+
@result._based_on_version.must_equal 1
|
227
|
+
@result.name.must_equal 'Doc1'
|
181
228
|
end
|
182
229
|
|
183
230
|
describe 'versions' do
|
184
|
-
it '
|
185
|
-
@
|
186
|
-
@
|
187
|
-
@
|
231
|
+
it 'stores previous doc' do
|
232
|
+
@versions.count.must_equal 2
|
233
|
+
@versions.map{ |v| v['_version'] }.must_equal [2, 1]
|
234
|
+
@versions.map{ |v| v['_based_on_version'] }.must_equal [1, nil]
|
235
|
+
@versions.map{ |v| v['name'] }.must_equal ['Doc2', 'Doc']
|
236
|
+
@versions.first["_version"].must_equal 2
|
237
|
+
@versions.first["_based_on_version"].must_equal 1
|
238
|
+
@versions.first["name"].must_equal 'Doc2'
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
describe 'current' do
|
243
|
+
it 'stores updated doc into current collection' do
|
244
|
+
@current["_version"].must_equal 3
|
245
|
+
@current["_based_on_version"].must_equal 1
|
246
|
+
@current["name"].must_equal 'Doc1'
|
188
247
|
end
|
189
248
|
end
|
190
249
|
end
|
191
250
|
|
192
251
|
# ---------------------------------------------------------------------
|
193
|
-
|
252
|
+
|
194
253
|
describe 'revise on previous version' do
|
195
254
|
before do
|
196
255
|
subject.name = 'v1'
|
@@ -216,6 +275,24 @@ module MongoidVersioning
|
|
216
275
|
end
|
217
276
|
end
|
218
277
|
|
278
|
+
# =====================================================================
|
279
|
+
|
280
|
+
describe '#revise!' do
|
281
|
+
let(:doc) { TestDocument.create(name: 'revise!') }
|
282
|
+
|
283
|
+
it 'raises error when invalid' do
|
284
|
+
doc.name = nil
|
285
|
+
proc { doc.revise! }.must_raise Mongoid::Errors::Validations
|
286
|
+
end
|
287
|
+
|
288
|
+
it 'raises error when the :revise callbacks returns false' do
|
289
|
+
skip 'not sure how to test this'
|
290
|
+
TestDocument.stub(:before_revise, false) do
|
291
|
+
proc { doc.revise! }.must_raise Mongoid::Errors::Callback
|
292
|
+
end
|
293
|
+
end
|
294
|
+
end
|
295
|
+
|
219
296
|
# =====================================================================
|
220
297
|
|
221
298
|
describe 'versions' do
|
@@ -229,70 +306,66 @@ module MongoidVersioning
|
|
229
306
|
subject.name = 'Foo'
|
230
307
|
end
|
231
308
|
|
232
|
-
it 'returns an Array' do
|
233
|
-
subject.versions.must_be_kind_of Array
|
234
|
-
end
|
235
|
-
|
236
309
|
# ---------------------------------------------------------------------
|
237
310
|
|
238
311
|
describe '#previous_versions' do
|
239
312
|
it 'returns everything but the latest' do
|
240
313
|
subject.previous_versions.map(&:_version).must_equal [2,1]
|
241
314
|
end
|
242
|
-
it '
|
315
|
+
it 'reverts document _ids' do
|
243
316
|
subject.previous_versions.map(&:id).uniq.must_equal [subject.id]
|
244
317
|
end
|
245
318
|
end
|
246
319
|
|
247
|
-
# ---------------------------------------------------------------------
|
248
|
-
|
249
320
|
describe '#latest_version' do
|
250
321
|
it 'includes the latest version as in the database' do
|
251
322
|
subject.latest_version.name.wont_equal 'Foo'
|
252
323
|
end
|
253
324
|
end
|
254
325
|
|
255
|
-
# ---------------------------------------------------------------------
|
256
|
-
|
257
326
|
describe '#versions' do
|
327
|
+
it 'returns an Array' do
|
328
|
+
subject.versions.must_be_kind_of Array
|
329
|
+
end
|
258
330
|
it 'returns all versions including the latest one' do
|
259
331
|
subject.versions.map(&:_version).must_equal [3,2,1]
|
260
332
|
end
|
261
333
|
end
|
334
|
+
end
|
262
335
|
|
263
336
|
|
264
|
-
|
337
|
+
# ---------------------------------------------------------------------
|
265
338
|
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
339
|
+
describe '#version' do
|
340
|
+
before do
|
341
|
+
subject.name = 'v1'
|
342
|
+
subject.revise
|
343
|
+
subject.name = 'v2'
|
344
|
+
subject.revise
|
345
|
+
subject.name = 'v3'
|
346
|
+
subject.revise
|
347
|
+
subject.name = 'Foo'
|
348
|
+
end
|
276
349
|
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
end
|
350
|
+
describe 'when latest version' do
|
351
|
+
it 'returns the version from db' do
|
352
|
+
subject.version(3)._version.must_equal 3
|
281
353
|
end
|
354
|
+
end
|
282
355
|
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
end
|
356
|
+
describe 'when previous version' do
|
357
|
+
it 'returns the version from db' do
|
358
|
+
subject.version(1)._version.must_equal 1
|
287
359
|
end
|
360
|
+
end
|
288
361
|
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
end
|
362
|
+
describe 'when version does not exist' do
|
363
|
+
it 'returns nil' do
|
364
|
+
subject.version(10).must_be_nil
|
293
365
|
end
|
294
366
|
end
|
295
367
|
end
|
368
|
+
|
296
369
|
end
|
297
370
|
|
298
371
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_versioning
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomas Celizna
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|
@@ -131,6 +131,7 @@ extra_rdoc_files: []
|
|
131
131
|
files:
|
132
132
|
- ".gitignore"
|
133
133
|
- ".travis.yml"
|
134
|
+
- CHANGELOG.md
|
134
135
|
- Gemfile
|
135
136
|
- Gemfile.lock
|
136
137
|
- Guardfile
|