Permalink
Browse files

fix bug which kept saving old copies dicarding the existence of max_v…

…ersions
  • Loading branch information...
1 parent ae67ac7 commit 79a6d19a49b33973949d6b99a903a8a6210992e1 @boblu boblu committed Oct 5, 2010
Showing with 61 additions and 33 deletions.
  1. +8 −2 lib/mongoid/versioning.rb
  2. +53 −31 spec/unit/mongoid/versioning_spec.rb
View
@@ -14,6 +14,8 @@ module Versioning
module ClassMethods #:nodoc:
attr_accessor :version_max
+
+ # the number of max_version should >= 0
def max_versions(number)
self.version_max = number.to_i
end
@@ -26,10 +28,14 @@ def max_versions(number)
def revise
last_version = self.class.first(:conditions => { :_id => id, :version => version })
if last_version
+ old_versions = ( @attributes['versions'].duplicable? ? @attributes['versions'].dup : nil )
self.versions << last_version.clone
- self.versions.shift if self.class.version_max.present? && self.versions.length > self.class.version_max
+ if self.class.version_max.present? && ( self.class.version_max >= 0 ) && ( self.versions.length > self.class.version_max )
+ self.versions.shift
+ @attributes['versions'].shift
+ end
self.version = (version || 1 ) + 1
- @modifications["versions"] = [ nil, @attributes["versions"] ] if @modifications
+ @modifications["versions"] = [ old_versions, @attributes['versions'] ] if @modifications
end
end
end
@@ -4,6 +4,14 @@
describe "#version" do
+ def first_update_post
+ @post.title = "New"
+ @version = Post.new(:title => "Test")
+ Post.expects(:first).at_least(1).with(:conditions => { :_id => @post.id, :version => 1 }).returns(@version)
+ @post.save
+ @post.reload
+ end
+
before do
@post = Post.new
end
@@ -15,10 +23,7 @@
context "when document is saved" do
before do
- @post.title = "New"
- @version = Post.new(:title => "Test")
- Post.expects(:first).at_least(1).with(:conditions => { :_id => @post.id, :version => 1 }).returns(@version)
- @post.revise
+ first_update_post
end
it "increments the version" do
@@ -33,35 +38,52 @@
version.title.should == "Test"
version.version.should == 1
end
+
+ end
+
+ context "when a max_versions limit has been set to 0" do
+
+ before do
+ Post.max_versions 0
+ first_update_post
+ end
- context "when a max_versions limit has been set" do
-
- before do
- Post.max_versions 1
- end
-
- it "adds a snapshot of the document to the versions if it hasn't been exceeded" do
- @post.title.should == "New"
- @post.version.should == 2
- @post.versions.size.should == 1
- version = @post.versions.first
- version.title.should == "Test"
- version.version.should == 1
- end
-
- it "discards the oldest version if it's been exceeded" do
- @previous_version = @post.clone
- @post.title = "Another change"
- Post.expects(:first).at_least(1).with(:conditions => { :_id => @post.id, :version => 2 }).returns(@previous_version)
- @post.revise
- @post.title.should == "Another change"
- @post.version.should == 3
- @post.versions.size.should == 1
- latest_version = @post.versions.first
- latest_version.title.should == "New"
- latest_version.version.should == 2
- end
+ it "update version number without actually saving old versions" do
+ @post.title.should == "New"
+ @post.version.should == 2
+ @post.versions.size.should == 0
+ end
+ end
+
+ context "when a max_versions limit has been set to > 0" do
+
+ before do
+ Post.max_versions 1
+ first_update_post
+ end
+
+ it "update version number and save old versions" do
+ @post.title.should == "New"
+ @post.version.should == 2
+ @post.versions.size.should == 1
+ version = @post.versions.first
+ version.title.should == "Test"
+ version.version.should == 1
+ end
+
+ it "discards the oldest version if it's been exceeded" do
+ @previous_version = @post.clone
+ @post.title = "Another change"
+ Post.expects(:first).at_least(1).with(:conditions => { :_id => @post.id, :version => 2 }).returns(@previous_version)
+ @post.save
+ @post.reload
+ @post.title.should == "Another change"
+ @post.version.should == 3
+ @post.versions.size.should == 1
+ latest_version = @post.versions.first
+ latest_version.title.should == "New"
+ latest_version.version.should == 2
end
end

0 comments on commit 79a6d19

Please sign in to comment.