commit
84d7063608
|
@ -21,8 +21,8 @@ module CouchRest
|
||||||
|
|
||||||
# Creates the document in the db. Raises an exception
|
# Creates the document in the db. Raises an exception
|
||||||
# if the document is not created properly.
|
# if the document is not created properly.
|
||||||
def create!
|
def create!(options = {})
|
||||||
self.class.fail_validate!(self) unless self.create
|
self.class.fail_validate!(self) unless self.create(options)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Trigger the callbacks (before, after, around)
|
# Trigger the callbacks (before, after, around)
|
||||||
|
@ -118,8 +118,8 @@ module CouchRest
|
||||||
#
|
#
|
||||||
# ==== Returns
|
# ==== Returns
|
||||||
# returns the reloaded document
|
# returns the reloaded document
|
||||||
def create(attributes = {})
|
def create(attributes = {}, &block)
|
||||||
instance = new(attributes)
|
instance = new(attributes, &block)
|
||||||
instance.create
|
instance.create
|
||||||
instance
|
instance
|
||||||
end
|
end
|
||||||
|
@ -128,8 +128,8 @@ module CouchRest
|
||||||
#
|
#
|
||||||
# ==== Returns
|
# ==== Returns
|
||||||
# returns the reloaded document or raises an exception
|
# returns the reloaded document or raises an exception
|
||||||
def create!(attributes = {})
|
def create!(attributes = {}, &block)
|
||||||
instance = new(attributes)
|
instance = new(attributes, &block)
|
||||||
instance.create!
|
instance.create!
|
||||||
instance
|
instance
|
||||||
end
|
end
|
||||||
|
|
|
@ -81,6 +81,18 @@ describe "Model Persistence" do
|
||||||
article.should_not be_new
|
article.should_not be_new
|
||||||
end
|
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
|
it "should trigger the create callbacks" do
|
||||||
doc = WithCallBacks.create(:name => 'my other test')
|
doc = WithCallBacks.create(:name => 'my other test')
|
||||||
doc.run_before_create.should be_true
|
doc.run_before_create.should be_true
|
||||||
|
|
Loading…
Reference in a new issue