From f28cce1f0a3d88cc099e2c152d57a2944ec702fc Mon Sep 17 00:00:00 2001 From: Peter Williams Date: Mon, 23 May 2011 11:02:08 -0600 Subject: [PATCH 1/3] Implement #get! and #find! class methods --- lib/couchrest/model/class_proxy.rb | 9 +++++++- lib/couchrest/model/document_queries.rb | 5 ++++- lib/couchrest/model/errors.rb | 2 ++ spec/couchrest/class_proxy_spec.rb | 29 +++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/lib/couchrest/model/class_proxy.rb b/lib/couchrest/model/class_proxy.rb index 3f988b1..16887a3 100644 --- a/lib/couchrest/model/class_proxy.rb +++ b/lib/couchrest/model/class_proxy.rb @@ -87,7 +87,14 @@ module CouchRest doc end alias :find :get - + + def get!(id) + doc = @klass.get!(id, @database) + doc.database = @database if doc && doc.respond_to?(:database) + doc + end + alias :find! :get! + # Views def has_view?(view) diff --git a/lib/couchrest/model/document_queries.rb b/lib/couchrest/model/document_queries.rb index af1083d..839674b 100644 --- a/lib/couchrest/model/document_queries.rb +++ b/lib/couchrest/model/document_queries.rb @@ -86,9 +86,12 @@ module CouchRest # id:: Document ID # db:: optional option to pass a custom database to use def get!(id, db = database) - raise "Missing or empty document ID" if id.to_s.empty? + raise CouchRest::Model::DocumentNotFound if id.blank? + doc = db.get id build_from_database(doc) + rescue RestClient::ResourceNotFound + raise CouchRest::Model::DocumentNotFound end alias :find! :get! diff --git a/lib/couchrest/model/errors.rb b/lib/couchrest/model/errors.rb index 7f14fca..45d8b2f 100644 --- a/lib/couchrest/model/errors.rb +++ b/lib/couchrest/model/errors.rb @@ -19,5 +19,7 @@ module CouchRest end end end + + class DocumentNotFound < Errors::CouchRestModelError; end end end diff --git a/spec/couchrest/class_proxy_spec.rb b/spec/couchrest/class_proxy_spec.rb index f274ca9..1572d8d 100644 --- a/spec/couchrest/class_proxy_spec.rb +++ b/spec/couchrest/class_proxy_spec.rb @@ -123,6 +123,35 @@ describe "Proxy Class" do u.respond_to?(:database).should be_false end end + + describe "#get!" do + it "raises exception when passed a nil" do + expect { @us.get!(nil)}.to raise_error(CouchRest::Model::DocumentNotFound) + end + + it "raises exception when passed an empty string " do + expect { @us.get!("")}.to raise_error(CouchRest::Model::DocumentNotFound) + end + + it "raises exception when document with provided id does not exist" do + expect { @us.get!("thisisnotreallyadocumentid")}.to raise_error(CouchRest::Model::DocumentNotFound) + end + end + + describe "#find!" do + it "raises exception when passed a nil" do + expect { @us.find!(nil)}.to raise_error(CouchRest::Model::DocumentNotFound) + end + + it "raises exception when passed an empty string " do + expect { @us.find!("")}.to raise_error(CouchRest::Model::DocumentNotFound) + end + + it "raises exception when document with provided id does not exist" do + expect { @us.find!("thisisnotreallyadocumentid")}.to raise_error(CouchRest::Model::DocumentNotFound) + end + end + # Sam Lown 2010-04-07 # Removed as unclear why this should happen as before my changes # this happend by accident, not explicitly. From 2b9a5193b43b60694637a1f71d9d976150c15caa Mon Sep 17 00:00:00 2001 From: Peter Williams Date: Tue, 31 May 2011 16:03:08 -0600 Subject: [PATCH 2/3] add init block support to #create and #create! --- lib/couchrest/model/persistence.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/couchrest/model/persistence.rb b/lib/couchrest/model/persistence.rb index e77b663..21f6455 100644 --- a/lib/couchrest/model/persistence.rb +++ b/lib/couchrest/model/persistence.rb @@ -118,8 +118,8 @@ module CouchRest # # ==== Returns # returns the reloaded document - def create(attributes = {}) - instance = new(attributes) + def create(attributes = {}, &block) + instance = new(attributes, &block) instance.create instance end @@ -128,8 +128,8 @@ module CouchRest # # ==== Returns # returns the reloaded document or raises an exception - def create!(attributes = {}) - instance = new(attributes) + def create!(attributes = {}, &block) + instance = new(attributes, &block) instance.create! instance end From 53bc7637e208cdee416129c8de90b2eacb85d619 Mon Sep 17 00:00:00 2001 From: Peter Williams Date: Tue, 31 May 2011 16:31:37 -0600 Subject: [PATCH 3/3] 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