include-docs support

This commit is contained in:
Chris Anderson 2008-10-08 12:32:22 -07:00
parent e2f7163816
commit 7a92723260
2 changed files with 38 additions and 22 deletions

View file

@ -121,8 +121,7 @@ module CouchRest
end
view_name = "#{design_doc_slug}/all"
raw = opts.delete(:raw)
view = fetch_view(view_name, opts)
process_view_results view, raw
fetch_view_with_docs(view_name, opts, raw)
end
# Cast a field as another class. The class must be happy to have the
@ -310,8 +309,7 @@ module CouchRest
end
raw = query.delete(:raw)
view_name = "#{design_doc_slug}/#{method_name}"
view = fetch_view(view_name, query)
process_view_results view, raw
fetch_view_with_docs(view_name, query, raw)
end
end
end
@ -323,12 +321,19 @@ module CouchRest
private
def process_view_results view, raw=false
def fetch_view_with_docs name, opts, raw=false
if raw
view
fetch_view name, opts
else
# TODO this can be optimized once the include-docs patch is applied
view['rows'].collect{|r|new(database.get(r['id']))}
begin
view = fetch_view name, opts.merge({:include_docs => true})
view['rows'].collect{|r|new(r['doc'])}
rescue
# fallback for old versions of couchdb that don't
# have include_docs support
view = fetch_view name, opts
view['rows'].collect{|r|new(database.get(r['id']))}
end
end
end

View file

@ -422,21 +422,32 @@ describe CouchRest::Database do
ds['total_rows'].should == 5
end
it "should list documents with keys and such" do
9.times do |i|
@db.save({'_id' => "doc#{i}",'another' => 'doc', 'will-exist' => 'here'})
describe "documents / _all_docs" do
before(:each) do
9.times do |i|
@db.save({'_id' => "doc#{i}",'another' => 'doc', 'will-exist' => 'here'})
end
end
it "should list documents with keys and such" do
ds = @db.documents
ds['rows'].should be_an_instance_of(Array)
ds['rows'][0]['id'].should == "doc0"
ds['total_rows'].should == 9
end
it "should take query params" do
ds = @db.documents(:startkey => 'doc0', :endkey => 'doc3')
ds['rows'].length.should == 4
ds = @db.documents(:key => 'doc0')
ds['rows'].length.should == 1
end
it "should work with multi-key" do
rs = @db.documents :keys => ["doc0", "doc7"]
rs['rows'].length.should == 2
end
it "should work with include_docs" do
ds = @db.documents(:startkey => 'doc0', :endkey => 'doc3', :include_docs => true)
ds['rows'][0]['doc']['another'].should == "doc"
end
ds = @db.documents
ds['rows'].should be_an_instance_of(Array)
ds['rows'][0]['id'].should == "doc0"
ds['total_rows'].should == 9
ds = @db.documents(:startkey => 'doc0', :endkey => 'doc3')
ds['rows'].length.should == 4
ds = @db.documents(:key => 'doc0')
ds['rows'].length.should == 1
rs = @db.documents :keys => ["doc0", "doc7"]
rs['rows'].length.should == 2
end
describe "deleting a database" do