From 51408990411411f3bf0826cdd0c042f9ccc0c7c7 Mon Sep 17 00:00:00 2001 From: Matt Aimonetti Date: Fri, 17 Jul 2009 00:12:33 -0700 Subject: [PATCH] Added ExtendedDocument.create({}) and #create!({}) so you don't have to do Model.new.create --- history.txt | 3 ++- lib/couchrest/more/extended_document.rb | 19 ++++++++++++++++++- spec/couchrest/more/extended_doc_spec.rb | 16 ++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/history.txt b/history.txt index b434056..f691eae 100644 --- a/history.txt +++ b/history.txt @@ -3,10 +3,11 @@ * Major enhancements * Created an abstraction HTTP layer to support different http adapters (Matt Aimonetti) + * Added ExtendedDocument.create({}) and #create!({}) so you don't have to do Model.new.create (Matt Aimonetti) * Minor enhancements - * Optimized Model.count to run about 3x faster (Matt Aimonetti) + * Optimized ExtendedDocument.count to run about 3x faster (Matt Aimonetti) * Added Float casting (Ryan Felton & Matt Aimonetti) == 0.30 diff --git a/lib/couchrest/more/extended_document.rb b/lib/couchrest/more/extended_document.rb index 14006ed..7fd067a 100644 --- a/lib/couchrest/more/extended_document.rb +++ b/lib/couchrest/more/extended_document.rb @@ -52,8 +52,25 @@ module CouchRest end end - + # Defines an instance and save it directly to the database + # + # ==== Returns + # returns the reloaded document + def self.create(options) + instance = new(options) + instance.create + instance + end + # Defines an instance and save it directly to the database + # + # ==== Returns + # returns the reloaded document or raises an exception + def self.create!(options) + instance = new(options) + instance.create! + instance + end # Automatically set updated_at and created_at fields # on the document whenever saving occurs. CouchRest uses a pretty diff --git a/spec/couchrest/more/extended_doc_spec.rb b/spec/couchrest/more/extended_doc_spec.rb index 618ee0c..5d71d7f 100644 --- a/spec/couchrest/more/extended_doc_spec.rb +++ b/spec/couchrest/more/extended_doc_spec.rb @@ -97,6 +97,22 @@ describe "ExtendedDocument" do end end + describe "creating a new document" do + it "should instantialize and save a document" do + article = Article.create(:title => 'my test') + article.title.should == 'my test' + article.should_not be_new_document + end + + it "should trigger the create callbacks" do + doc = WithCallBacks.create(:name => 'my other test') + doc.run_before_create.should be_true + doc.run_after_create.should be_true + doc.run_before_save.should be_true + doc.run_after_save.should be_true + end + end + describe "update attributes without saving" do before(:each) do a = Article.get "big-bad-danger" rescue nil