added model#save! raising an exception if the document isn't saved properly

This commit is contained in:
Matt Aimonetti 2008-11-03 17:45:21 -08:00
parent 19f587c0d6
commit b1315d20f4
2 changed files with 31 additions and 0 deletions

View file

@ -460,6 +460,12 @@ module CouchRest
end end
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. # Deletes the document from the database. Runs the :delete callbacks.
# Removes the <tt>_id</tt> and <tt>_rev</tt> fields, preparing the # Removes the <tt>_id</tt> and <tt>_rev</tt> fields, preparing the
# document to be saved to a new <tt>_id</tt>. # document to be saved to a new <tt>_id</tt>.

View file

@ -3,6 +3,16 @@ require File.dirname(__FILE__) + '/../../spec_helper'
class Basic < CouchRest::Model class Basic < CouchRest::Model
end end
class BasicWithValidation < CouchRest::Model
before :save, :validate
key_accessor :name
def validate
throw(:halt, false) unless name
end
end
class WithTemplate < CouchRest::Model class WithTemplate < CouchRest::Model
unique_id do |model| unique_id do |model|
model['important-field'] model['important-field']
@ -300,6 +310,21 @@ describe CouchRest::Model do
end end
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 describe "saving a model with a unique_id configured" do
before(:each) do before(:each) do
@art = Article.new @art = Article.new