Skip to content

Commit

Permalink
Fixed a bug in the pagination code that caused it to paginate over re…
Browse files Browse the repository at this point in the history
…cords outside of the scope of the view parameters.
  • Loading branch information
John Wood authored and mattetti committed Aug 20, 2009
1 parent 2057e59 commit b0dca70
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/couchrest/mixins/collection.rb
Expand Up @@ -204,8 +204,9 @@ def convert_to_container_array(results)
def pagination_options(page, per_page)
view_options = @view_options.clone
if @last_key && @last_docid && @last_page == page - 1
view_options.delete(:key)
options = { :startkey => @last_key, :startkey_docid => @last_docid, :limit => per_page, :skip => 1 }
key = view_options.delete(:key)
end_key = view_options[:endkey] || key
options = { :startkey => @last_key, :endkey => end_key, :startkey_docid => @last_docid, :limit => per_page, :skip => 1 }
else
options = { :limit => per_page, :skip => per_page * (page - 1) }
end
Expand Down
19 changes: 17 additions & 2 deletions spec/couchrest/more/extended_doc_view_spec.rb
Expand Up @@ -348,12 +348,19 @@ class Unattached < CouchRest::ExtendedDocument
describe "with a collection" do
before(:all) do
reset_test_db!
@titles = ["very uniq one", "really interesting", "some fun",
titles = ["very uniq one", "really interesting", "some fun",
"really awesome", "crazy bob", "this rocks", "super rad"]
@titles.each_with_index do |title,i|
titles.each_with_index do |title,i|
a = Article.new(:title => title, :date => Date.today)
a.save
end

titles = ["yesterday very uniq one", "yesterday really interesting", "yesterday some fun",
"yesterday really awesome", "yesterday crazy bob", "yesterday this rocks"]
titles.each_with_index do |title,i|
a = Article.new(:title => title, :date => Date.today - 1)
a.save
end
end
require 'date'
it "should return a proxy that looks like an array of 7 Article objects" do
Expand Down Expand Up @@ -421,6 +428,14 @@ class Article
lambda{Article.collection_proxy_for('Article', nil)}.should raise_error
lambda{Article.paginate(:design_doc => 'Article')}.should raise_error
end
it "should be able to span multiple keys" do
articles = Article.by_date :startkey => Date.today, :endkey => Date.today - 1
articles.paginate(:page => 1, :per_page => 3).size.should == 3
articles.paginate(:page => 2, :per_page => 3).size.should == 3
articles.paginate(:page => 3, :per_page => 3).size.should == 3
articles.paginate(:page => 4, :per_page => 3).size.should == 3
articles.paginate(:page => 5, :per_page => 3).size.should == 1
end
end

end

0 comments on commit b0dca70

Please sign in to comment.