From 19f587c0d6be2500aeab5ba708c696550967f308 Mon Sep 17 00:00:00 2001 From: Matt Aimonetti Date: Mon, 3 Nov 2008 16:40:19 -0800 Subject: [PATCH 1/2] added support for model#first --- lib/couchrest/core/model.rb | 5 +++++ spec/couchrest/core/model_spec.rb | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/lib/couchrest/core/model.rb b/lib/couchrest/core/model.rb index 463eed5..fa3692e 100644 --- a/lib/couchrest/core/model.rb +++ b/lib/couchrest/core/model.rb @@ -124,6 +124,11 @@ module CouchRest fetch_view_with_docs(view_name, opts, raw) end + def first opts = {} + first_instance = self.all(opts.merge!(:count => 1)) + first_instance.empty? ? nil : first_instance.first + end + # Cast a field as another class. The class must be happy to have the # field's primitive type as the argument to it's constucture. Classes # which inherit from CouchRest::Model are happy to act as sub-objects diff --git a/spec/couchrest/core/model_spec.rb b/spec/couchrest/core/model_spec.rb index d350335..e981778 100644 --- a/spec/couchrest/core/model_spec.rb +++ b/spec/couchrest/core/model_spec.rb @@ -232,6 +232,28 @@ describe CouchRest::Model do end end + describe "finding the first instance of a model" do + before(:all) do + WithTemplate.new('important-field' => '1').save + WithTemplate.new('important-field' => '2').save + WithTemplate.new('important-field' => '3').save + WithTemplate.new('important-field' => '4').save + end + it "should make the design doc" do + WithTemplate.all + d = WithTemplate.design_doc + d['views']['all']['map'].should include('WithTemplate') + end + it "should find first" do + rs = WithTemplate.first + rs['important-field'].should == "1" + end + it "should return nil if no instances are found" do + WithTemplate.all.each {|obj| obj.destroy } + WithTemplate.first.should be_nil + end + end + describe "getting a model with a subobject field" do before(:all) do course_doc = { From b1315d20f4322a6a9d468bb7beab3a4bf4b51255 Mon Sep 17 00:00:00 2001 From: Matt Aimonetti Date: Mon, 3 Nov 2008 17:45:21 -0800 Subject: [PATCH 2/2] added model#save! raising an exception if the document isn't saved properly --- lib/couchrest/core/model.rb | 6 ++++++ spec/couchrest/core/model_spec.rb | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/couchrest/core/model.rb b/lib/couchrest/core/model.rb index fa3692e..930cbc9 100644 --- a/lib/couchrest/core/model.rb +++ b/lib/couchrest/core/model.rb @@ -459,6 +459,12 @@ module CouchRest update end end + + # Saves the document to the db using create or update. Raises an exception + # if the document is not saved properly. + def save! + raise "#{self.inspect} failed to save" unless self.save + end # Deletes the document from the database. Runs the :delete callbacks. # Removes the _id and _rev fields, preparing the diff --git a/spec/couchrest/core/model_spec.rb b/spec/couchrest/core/model_spec.rb index e981778..36c2aa3 100644 --- a/spec/couchrest/core/model_spec.rb +++ b/spec/couchrest/core/model_spec.rb @@ -3,6 +3,16 @@ require File.dirname(__FILE__) + '/../../spec_helper' class Basic < CouchRest::Model end +class BasicWithValidation < CouchRest::Model + + before :save, :validate + key_accessor :name + + def validate + throw(:halt, false) unless name + end +end + class WithTemplate < CouchRest::Model unique_id do |model| model['important-field'] @@ -299,6 +309,21 @@ describe CouchRest::Model do @obj['couchrest-type'].should == 'Basic' end end + + describe "saving a model with validation hooks added as extlib" do + before(:all) do + @obj = BasicWithValidation.new + end + + it "save should return false is the model doesn't save as expected" do + @obj.save.should be_false + end + + it "save! should raise and exception if the model doesn't save" do + lambda{ @obj.save!}.should raise_error("#{@obj.inspect} failed to save") + end + + end describe "saving a model with a unique_id configured" do before(:each) do