fixed merge
This commit is contained in:
commit
0c91f5d234
|
@ -119,6 +119,11 @@ module CouchRest
|
||||||
view :all, opts, &block
|
view :all, opts, &block
|
||||||
end
|
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
|
# Cast a field as another class. The class must be happy to have the
|
||||||
# field's primitive type as the argument to it's constuctur. Classes
|
# field's primitive type as the argument to it's constuctur. Classes
|
||||||
# which inherit from CouchRest::Model are happy to act as sub-objects
|
# which inherit from CouchRest::Model are happy to act as sub-objects
|
||||||
|
@ -477,10 +482,28 @@ 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
|
||||||
|
|
||||||
def update
|
def update
|
||||||
save :actually
|
save :actually
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Deletes the document from the database. Runs the :delete callbacks.
|
||||||
|
# Removes the <tt>_id</tt> and <tt>_rev</tt> fields, preparing the
|
||||||
|
# document to be saved to a new <tt>_id</tt>.
|
||||||
|
def destroy
|
||||||
|
result = database.delete self
|
||||||
|
if result['ok']
|
||||||
|
self['_rev'] = nil
|
||||||
|
self['_id'] = nil
|
||||||
|
end
|
||||||
|
result['ok']
|
||||||
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
# can we use the callbacks for this?
|
# can we use the callbacks for this?
|
||||||
set_unique_id if self.respond_to?(:set_unique_id)
|
set_unique_id if self.respond_to?(:set_unique_id)
|
||||||
|
|
|
@ -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']
|
||||||
|
@ -241,6 +251,28 @@ describe CouchRest::Model do
|
||||||
end
|
end
|
||||||
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
|
describe "getting a model with a subobject field" do
|
||||||
before(:all) do
|
before(:all) do
|
||||||
course_doc = {
|
course_doc = {
|
||||||
|
@ -287,6 +319,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
|
||||||
|
|
Loading…
Reference in a new issue