From 53bc7637e208cdee416129c8de90b2eacb85d619 Mon Sep 17 00:00:00 2001 From: Peter Williams Date: Tue, 31 May 2011 16:31:37 -0600 Subject: [PATCH] tests for #create and #create! init blocks --- lib/couchrest/model/persistence.rb | 4 ++-- spec/couchrest/persistence_spec.rb | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/couchrest/model/persistence.rb b/lib/couchrest/model/persistence.rb index 21f6455..05d199b 100644 --- a/lib/couchrest/model/persistence.rb +++ b/lib/couchrest/model/persistence.rb @@ -21,8 +21,8 @@ module CouchRest # Creates the document in the db. Raises an exception # if the document is not created properly. - def create! - self.class.fail_validate!(self) unless self.create + def create!(options = {}) + self.class.fail_validate!(self) unless self.create(options) end # Trigger the callbacks (before, after, around) diff --git a/spec/couchrest/persistence_spec.rb b/spec/couchrest/persistence_spec.rb index 86ded62..6e04f30 100644 --- a/spec/couchrest/persistence_spec.rb +++ b/spec/couchrest/persistence_spec.rb @@ -81,6 +81,18 @@ describe "Model Persistence" do article.should_not be_new end + it "yields new instance to block before saving (#create)" do + article = Article.create{|a| a.title = 'my create init block test'} + article.title.should == 'my create init block test' + article.should_not be_new + end + + it "yields new instance to block before saving (#create!)" do + article = Article.create{|a| a.title = 'my create bang init block test'} + article.title.should == 'my create bang init block test' + article.should_not be_new + end + it "should trigger the create callbacks" do doc = WithCallBacks.create(:name => 'my other test') doc.run_before_create.should be_true