From cacc320235c47c32a32a4d74045dbb9283882ab2 Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Fri, 15 Apr 2011 16:24:52 +0200 Subject: [PATCH] Resetting skip and limit when trying to get total count --- lib/couchrest/model/designs/view.rb | 2 +- spec/couchrest/designs/view_spec.rb | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/couchrest/model/designs/view.rb b/lib/couchrest/model/designs/view.rb index 886632f..95c4ea4 100644 --- a/lib/couchrest/model/designs/view.rb +++ b/lib/couchrest/model/designs/view.rb @@ -113,7 +113,7 @@ module CouchRest def count raise "View#count cannot be used with group options" if query[:group] if can_reduce? - row = reduce.rows.first + row = reduce.skip(0).limit(1).rows.first row.nil? ? 0 : row.value else limit(0).total_rows diff --git a/spec/couchrest/designs/view_spec.rb b/spec/couchrest/designs/view_spec.rb index 44dbdfc..0bf0307 100644 --- a/spec/couchrest/designs/view_spec.rb +++ b/spec/couchrest/designs/view_spec.rb @@ -173,7 +173,7 @@ describe "Design View" do describe "#count" do it "should raise an error if view prepared for group" do @obj.should_receive(:query).and_return({:group => true}) - lambda { @obj.count }.should raise_error + lambda { @obj.count }.should raise_error(/group/) end it "should return first row value if reduce possible" do @@ -181,6 +181,8 @@ describe "Design View" do row = mock("Row") @obj.should_receive(:can_reduce?).and_return(true) @obj.should_receive(:reduce).and_return(view) + view.should_receive(:skip).with(0).and_return(view) + view.should_receive(:limit).with(1).and_return(view) view.should_receive(:rows).and_return([row]) row.should_receive(:value).and_return(2) @obj.count.should eql(2) @@ -189,6 +191,8 @@ describe "Design View" do view = mock("SubView") @obj.should_receive(:can_reduce?).and_return(true) @obj.should_receive(:reduce).and_return(view) + view.should_receive(:skip).with(0).and_return(view) + view.should_receive(:limit).with(1).and_return(view) view.should_receive(:rows).and_return([]) @obj.count.should eql(0) end @@ -770,6 +774,10 @@ describe "Design View" do docs[0].name.should eql("Judith") docs[1].name.should eql("Peter") end + it "should provide count even if limit or skip set" do + docs = DesignViewModel.by_name.limit(20).skip(2) + docs.count.should eql(5) + end end describe "pagination" do