include-docs support
This commit is contained in:
parent
e2f7163816
commit
7a92723260
|
@ -121,8 +121,7 @@ module CouchRest
|
||||||
end
|
end
|
||||||
view_name = "#{design_doc_slug}/all"
|
view_name = "#{design_doc_slug}/all"
|
||||||
raw = opts.delete(:raw)
|
raw = opts.delete(:raw)
|
||||||
view = fetch_view(view_name, opts)
|
fetch_view_with_docs(view_name, opts, raw)
|
||||||
process_view_results view, raw
|
|
||||||
end
|
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
|
||||||
|
@ -310,8 +309,7 @@ module CouchRest
|
||||||
end
|
end
|
||||||
raw = query.delete(:raw)
|
raw = query.delete(:raw)
|
||||||
view_name = "#{design_doc_slug}/#{method_name}"
|
view_name = "#{design_doc_slug}/#{method_name}"
|
||||||
view = fetch_view(view_name, query)
|
fetch_view_with_docs(view_name, query, raw)
|
||||||
process_view_results view, raw
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -323,14 +321,21 @@ module CouchRest
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def process_view_results view, raw=false
|
def fetch_view_with_docs name, opts, raw=false
|
||||||
if raw
|
if raw
|
||||||
view
|
fetch_view name, opts
|
||||||
else
|
else
|
||||||
# TODO this can be optimized once the include-docs patch is applied
|
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']))}
|
view['rows'].collect{|r|new(database.get(r['id']))}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def fetch_view view_name, opts
|
def fetch_view view_name, opts
|
||||||
retryable = true
|
retryable = true
|
||||||
|
|
|
@ -422,22 +422,33 @@ describe CouchRest::Database do
|
||||||
ds['total_rows'].should == 5
|
ds['total_rows'].should == 5
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should list documents with keys and such" do
|
describe "documents / _all_docs" do
|
||||||
|
before(:each) do
|
||||||
9.times do |i|
|
9.times do |i|
|
||||||
@db.save({'_id' => "doc#{i}",'another' => 'doc', 'will-exist' => 'here'})
|
@db.save({'_id' => "doc#{i}",'another' => 'doc', 'will-exist' => 'here'})
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
it "should list documents with keys and such" do
|
||||||
ds = @db.documents
|
ds = @db.documents
|
||||||
ds['rows'].should be_an_instance_of(Array)
|
ds['rows'].should be_an_instance_of(Array)
|
||||||
ds['rows'][0]['id'].should == "doc0"
|
ds['rows'][0]['id'].should == "doc0"
|
||||||
ds['total_rows'].should == 9
|
ds['total_rows'].should == 9
|
||||||
|
end
|
||||||
|
it "should take query params" do
|
||||||
ds = @db.documents(:startkey => 'doc0', :endkey => 'doc3')
|
ds = @db.documents(:startkey => 'doc0', :endkey => 'doc3')
|
||||||
ds['rows'].length.should == 4
|
ds['rows'].length.should == 4
|
||||||
ds = @db.documents(:key => 'doc0')
|
ds = @db.documents(:key => 'doc0')
|
||||||
ds['rows'].length.should == 1
|
ds['rows'].length.should == 1
|
||||||
|
end
|
||||||
|
it "should work with multi-key" do
|
||||||
rs = @db.documents :keys => ["doc0", "doc7"]
|
rs = @db.documents :keys => ["doc0", "doc7"]
|
||||||
rs['rows'].length.should == 2
|
rs['rows'].length.should == 2
|
||||||
end
|
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
|
||||||
|
end
|
||||||
|
|
||||||
describe "deleting a database" do
|
describe "deleting a database" do
|
||||||
it "should start with the test database" do
|
it "should start with the test database" do
|
||||||
|
|
Loading…
Reference in a new issue