From 7a9272326098c12e925b8eef5b665368d4be1ad7 Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Wed, 8 Oct 2008 12:32:22 -0700 Subject: [PATCH] include-docs support --- lib/couchrest/core/model.rb | 21 +++++++++------ spec/couchrest/core/database_spec.rb | 39 ++++++++++++++++++---------- 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/lib/couchrest/core/model.rb b/lib/couchrest/core/model.rb index 01f86ec..d84504c 100644 --- a/lib/couchrest/core/model.rb +++ b/lib/couchrest/core/model.rb @@ -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 diff --git a/spec/couchrest/core/database_spec.rb b/spec/couchrest/core/database_spec.rb index 29608d2..768db3e 100644 --- a/spec/couchrest/core/database_spec.rb +++ b/spec/couchrest/core/database_spec.rb @@ -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