Permalink
Browse files

Saving documents without validation

  • Loading branch information...
1 parent 989423c commit a066b8461ba8486d52092f94c96ca4d359f4c02d @durran durran committed Dec 28, 2009
View
@@ -46,17 +46,17 @@ def destroy
end
# Save the +Document+. Delegates to the Save command.
- def save
- new_record? ? Create.execute(self) : Save.execute(self)
+ def save(validate = true)
+ new_record? ? Create.execute(self, validate) : Save.execute(self, validate)
end
# Save the +Document+. Delegates to the Save command. If the command
# returns false then a +ValidationError+ will be raised.
def save!
if new_record?
- return Create.execute(self) || (raise Errors::Validations.new(self.errors))
+ return Create.execute(self, true) || (raise Errors::Validations.new(self.errors))
else
- return Save.execute(self) || (raise Errors::Validations.new(self.errors))
+ return Save.execute(self, true) || (raise Errors::Validations.new(self.errors))
end
end
@@ -10,9 +10,9 @@ class Create
# doc: A new +Document+ that is going to be persisted.
#
# Returns: +Document+.
- def self.execute(doc)
+ def self.execute(doc, validate = true)
doc.run_callbacks :before_create
- Save.execute(doc)
+ Save.execute(doc, validate)
doc.run_callbacks :after_create
return doc
end
@@ -9,19 +9,19 @@
end
it "executes a save command" do
- Mongoid::Commands::Save.expects(:execute).with(@document).returns(@document)
+ Mongoid::Commands::Save.expects(:execute).with(@document, true).returns(@document)
Mongoid::Commands::Create.execute(@document)
end
it "runs the before and after create callbacks" do
@document.expects(:run_callbacks).with(:before_create)
- Mongoid::Commands::Save.expects(:execute).with(@document).returns(@document)
+ Mongoid::Commands::Save.expects(:execute).with(@document, true).returns(@document)
@document.expects(:run_callbacks).with(:after_create)
Mongoid::Commands::Create.execute(@document)
end
it "returns the document" do
- Mongoid::Commands::Save.expects(:execute).with(@document).returns(@document)
+ Mongoid::Commands::Save.expects(:execute).with(@document, true).returns(@document)
Mongoid::Commands::Create.execute(@document).should == @document
end
@@ -27,7 +27,7 @@
describe "#save" do
it "delegates to the Save command" do
- Mongoid::Commands::Save.expects(:execute).with(@person).returns(true)
+ Mongoid::Commands::Save.expects(:execute).with(@person, true).returns(true)
@person.save
end
@@ -38,29 +38,42 @@
end
it "delegates to the Create command" do
- Mongoid::Commands::Create.expects(:execute).with(@person).returns(@person)
+ Mongoid::Commands::Create.expects(:execute).with(@person, true).returns(true)
@person.save
end
end
+ context "when not validating" do
+
+ before do
+ @person = Person.new
+ end
+
+ it "passes the validate param to the command" do
+ Mongoid::Commands::Create.expects(:execute).with(@person, false).returns(true)
+ @person.save(false)
+ end
+
+ end
+
end
describe "#save!" do
context "when validation passes" do
it "it returns the person" do
- Mongoid::Commands::Save.expects(:execute).with(@person).returns(@person)
- @person.save!.should == @person
+ Mongoid::Commands::Save.expects(:execute).with(@person, true).returns(true)
+ @person.save!
end
end
context "when validation fails" do
it "it raises an error" do
- Mongoid::Commands::Save.expects(:execute).with(@person).returns(false)
+ Mongoid::Commands::Save.expects(:execute).with(@person, true).returns(false)
lambda { @person.save! }.should raise_error
end
@@ -73,14 +86,14 @@
end
it "delegates to the Create command" do
- Mongoid::Commands::Create.expects(:execute).with(@person).returns(@person)
+ Mongoid::Commands::Create.expects(:execute).with(@person, true).returns(true)
@person.save!
end
context "when validation fails" do
it "it raises an error " do
- Mongoid::Commands::Create.expects(:execute).with(@person).returns(false)
+ Mongoid::Commands::Create.expects(:execute).with(@person, true).returns(false)
lambda { @person.save! }.should raise_error
end
@@ -93,7 +106,7 @@
describe "#update_attributes" do
it "delegates to the Save command" do
- Mongoid::Commands::Save.expects(:execute).with(@person).returns(@person)
+ Mongoid::Commands::Save.expects(:execute).with(@person, true).returns(true)
@person.update_attributes({})
end
@@ -104,16 +117,16 @@
context "when validation passes" do
it "it returns the person" do
- Mongoid::Commands::Save.expects(:execute).with(@person).returns(@person)
- @person.update_attributes({}).should == @person
+ Mongoid::Commands::Save.expects(:execute).with(@person, true).returns(true)
+ @person.update_attributes({}).should be_true
end
end
context "when validation fails" do
it "it raises an error" do
- Mongoid::Commands::Save.expects(:execute).with(@person).returns(false)
+ Mongoid::Commands::Save.expects(:execute).with(@person, true).returns(false)
lambda { @person.update_attributes!({}) }.should raise_error
end

0 comments on commit a066b84

Please sign in to comment.