mongoid 0.7.7 → 0.7.8
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/mongoid/document.rb +6 -0
- data/lib/mongoid/versioning.rb +2 -2
- data/mongoid.gemspec +1 -1
- data/spec/integration/mongoid/document_spec.rb +29 -6
- data/spec/unit/mongoid/document_spec.rb +14 -0
- data/spec/unit/mongoid/versioning_spec.rb +1 -1
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.7.
|
1
|
+
0.7.8
|
data/lib/mongoid/document.rb
CHANGED
@@ -205,6 +205,12 @@ module Mongoid #:nodoc:
|
|
205
205
|
other.attributes.except(:modified_at).except(:created_at)
|
206
206
|
end
|
207
207
|
|
208
|
+
# Clone the current +Document+. This will return all attributes with the
|
209
|
+
# exception of the document's id and versions.
|
210
|
+
def clone
|
211
|
+
self.class.new(@attributes.except(:_id).except(:versions).dup)
|
212
|
+
end
|
213
|
+
|
208
214
|
# Get the Mongo::Collection associated with this Document.
|
209
215
|
def collection
|
210
216
|
self.class.collection
|
data/lib/mongoid/versioning.rb
CHANGED
@@ -16,9 +16,9 @@ module Mongoid #:nodoc:
|
|
16
16
|
# document from the database and set it as the next version before saving
|
17
17
|
# the current document. It then increments the version number.
|
18
18
|
def revise
|
19
|
-
last_version = self.class.
|
19
|
+
last_version = self.class.first(:conditions => { :_id => id, :version => version })
|
20
20
|
if last_version
|
21
|
-
self.versions << last_version
|
21
|
+
self.versions << last_version.clone
|
22
22
|
self.version = version + 1
|
23
23
|
end
|
24
24
|
end
|
data/mongoid.gemspec
CHANGED
@@ -252,12 +252,35 @@ describe Mongoid::Document do
|
|
252
252
|
@comment.save
|
253
253
|
end
|
254
254
|
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
255
|
+
context "first save" do
|
256
|
+
|
257
|
+
it "creates a new version" do
|
258
|
+
@from_db = Comment.find(@comment.id)
|
259
|
+
@from_db.text = "New"
|
260
|
+
@from_db.save
|
261
|
+
@from_db.versions.size.should == 1
|
262
|
+
@from_db.version.should == 2
|
263
|
+
end
|
264
|
+
|
265
|
+
end
|
266
|
+
|
267
|
+
context "multiple saves" do
|
268
|
+
|
269
|
+
before do
|
270
|
+
5.times do |n|
|
271
|
+
@comment.text = "#{n}"
|
272
|
+
@comment.save
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
it "creates new versions" do
|
277
|
+
p @comment
|
278
|
+
@from_db = Comment.find(@comment.id)
|
279
|
+
@from_db.version.should == 6
|
280
|
+
p @from_db
|
281
|
+
@from_db.versions.size.should == 5
|
282
|
+
end
|
283
|
+
|
261
284
|
end
|
262
285
|
|
263
286
|
end
|
@@ -85,6 +85,20 @@ describe Mongoid::Document do
|
|
85
85
|
|
86
86
|
end
|
87
87
|
|
88
|
+
describe "#clone" do
|
89
|
+
|
90
|
+
before do
|
91
|
+
@comment = Comment.new(:text => "Woooooo")
|
92
|
+
@clone = @comment.clone
|
93
|
+
end
|
94
|
+
|
95
|
+
it "returns a new document sans id and versions" do
|
96
|
+
@clone.id.should_not == @comment.id
|
97
|
+
@clone.versions.should be_empty
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
|
88
102
|
describe "#collection" do
|
89
103
|
|
90
104
|
before do
|
@@ -17,7 +17,7 @@ describe Mongoid::Versioning do
|
|
17
17
|
before do
|
18
18
|
@post.title = "New"
|
19
19
|
@version = Post.new(:title => "Test")
|
20
|
-
Post.expects(:
|
20
|
+
Post.expects(:first).at_least(1).with(:conditions => { :_id => @post.id, :version => 1 }).returns(@version)
|
21
21
|
@post.revise
|
22
22
|
end
|
23
23
|
|