Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow chaining paginate and page methods without unexpected results.
  • Loading branch information
dbackeus committed Jun 14, 2012
1 parent 57eee48 commit 64cd0ec
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/will_paginate/mongoid.rb
Expand Up @@ -6,8 +6,8 @@ module Mongoid
module CriteriaMethods
def paginate(options = {})
extend CollectionMethods
@current_page = WillPaginate::PageNumber(options[:page] || 1)
@per_page = (options[:per_page] || WillPaginate.per_page).to_i
@current_page = WillPaginate::PageNumber(options[:page] || @current_page || 1)
@per_page = (options[:per_page] || @per_page || WillPaginate.per_page).to_i
@page_multiplier = current_page - 1
limit(per_page).skip(@page_multiplier * per_page)
end
Expand Down
9 changes: 9 additions & 0 deletions spec/finders/mongoid_spec.rb
Expand Up @@ -20,13 +20,22 @@ class MongoidModel
criteria.expects(:paginate).with(:page => 2).returns("itself")
criteria.page(2).should == "itself"
end

it "should not override per_page if set earlier in the chain" do
criteria.paginate(:per_page => 10).page(1).per_page.should == 10
criteria.paginate(:per_page => 20).page(1).per_page.should == 20
end
end

describe "#paginate" do
it "should use criteria" do
criteria.paginate.should be_instance_of(::Mongoid::Criteria)
end

it "should not override page number if set earlier in the chain" do
criteria.page(3).paginate.current_page.should == 3
end

it "should limit according to per_page parameter" do
criteria.paginate(:per_page => 10).options.should include(:limit => 10)
end
Expand Down

0 comments on commit 64cd0ec

Please sign in to comment.