Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mislav/will_paginate
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: fortnightlabs/will_paginate
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 11 commits
  • 4 files changed
  • 1 contributor

Commits on Oct 24, 2011

  1. Copy the full SHA
    9b72d4f View commit details

Commits on Oct 25, 2011

  1. Copy the full SHA
    36052ea View commit details
  2. Copy the full SHA
    18d5d17 View commit details
  3. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    a14d97d View commit details
  4. Copy the full SHA
    978a331 View commit details
  5. Copy the full SHA
    2fd5a32 View commit details
  6. Copy the full SHA
    53f1696 View commit details
  7. Copy the full SHA
    f41372a View commit details
  8. Copy the full SHA
    ac9ee09 View commit details
  9. Support setting per_page through a #per_page criteria method and have…

    … that use limit for its state.
    dbackeus committed Oct 25, 2011
    Copy the full SHA
    6ca3f64 View commit details
  10. Copy the full SHA
    4438f09 View commit details
Showing with 184 additions and 1 deletion.
  1. +1 −0 Gemfile
  2. +9 −1 Gemfile.lock
  3. +42 −0 lib/will_paginate/mongoid.rb
  4. +132 −0 spec/finders/mongoid_spec.rb
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ gem 'dm-core'
gem 'dm-aggregates'
gem 'dm-migrations'
gem 'dm-sqlite-adapter'
gem 'mongoid'

group :mysql do
gem 'mysql', '~> 2.8.1'
10 changes: 9 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ GEM
addressable (2.2.6)
archive-tar-minitar (0.5.2)
arel (2.2.1)
bson (1.4.1)
builder (3.0.0)
columnize (0.3.4)
data_objects (0.10.6)
@@ -53,6 +54,12 @@ GEM
linecache19 (0.5.12)
ruby_core_source (>= 0.1.4)
mocha (0.9.12)
mongo (1.4.1)
bson (= 1.4.1)
mongoid (2.3.2)
activemodel (~> 3.1)
mongo (~> 1.4)
tzinfo (~> 0.3.22)
multi_json (1.0.3)
mysql (2.8.1)
mysql2 (0.3.7)
@@ -93,7 +100,7 @@ GEM
sprockets (2.0.0)
hike (~> 1.2)
rack (~> 1.0)
tilt (!= 1.3.0, ~> 1.1)
tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.4)
tilt (1.3.3)
tzinfo (0.3.29)
@@ -109,6 +116,7 @@ DEPENDENCIES
dm-migrations
dm-sqlite-adapter
mocha (~> 0.9.8)
mongoid
mysql (~> 2.8.1)
mysql2 (>= 0.3.6)
pg (~> 0.11)
42 changes: 42 additions & 0 deletions lib/will_paginate/mongoid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'mongoid'
require 'will_paginate/collection'

module WillPaginate
module Mongoid
module CriteriaMethods
def paginate(options = {})
extend CollectionMethods
@current_page = WillPaginate::PageNumber(options[:page] || @current_page || 1)
@page_multiplier = current_page - 1
pp = (options[:per_page] || per_page || WillPaginate.per_page).to_i
limit(pp).skip(@page_multiplier * pp)
end

def per_page(value = :non_given)
value == :non_given ? options[:limit] : limit(value)
end

def page(page)
paginate(:page => page)
end
end

module CollectionMethods
attr_reader :current_page

def total_entries
@total_entries ||= count
end

def total_pages
(total_entries / per_page.to_f).ceil
end

def offset
@page_multiplier * per_page
end
end

::Mongoid::Criteria.send(:include, CriteriaMethods)
end
end
132 changes: 132 additions & 0 deletions spec/finders/mongoid_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
require 'spec_helper'
require 'will_paginate/mongoid'

Mongoid.database = Mongo::Connection.new.db('will_paginate_test')

class MongoidModel
include Mongoid::Document
end

describe "will paginate mongoid" do
before(:all) do
MongoidModel.delete_all
4.times { MongoidModel.create! }
end

let(:criteria) { MongoidModel.criteria }

describe "#page" do
it "should forward to the paginate method" do
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 "#per_page" do
it "should set the limit if given an argument" do
criteria.per_page(10).options[:limit].should == 10
end

it "should return the current limit if no argument is given" do
criteria.per_page.should == nil
criteria.per_page(10).per_page.should == 10
end

it "should be interchangable with limit" do
criteria.limit(15).per_page.should == 15
end

it "should be nil'able" do
criteria.per_page(nil).per_page.should be_nil
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

it "should skip according to page and per_page parameters" do
criteria.paginate(:page => 2, :per_page => 5).options.should include(:skip => 5)
end

specify "first fallback value for per_page option is the current limit" do
criteria.limit(12).paginate.options.should include(:limit => 12)
end

specify "second fallback value for per_page option is WillPaginate.per_page" do
criteria.paginate.options.should include(:limit => WillPaginate.per_page)
end

specify "page should default to 1" do
criteria.paginate.options.should include(:skip => 0)
end

it "should convert strings to integers" do
criteria.paginate(:page => "2", :per_page => "3").options.should include(:limit => 3, :limit => 3)
end

describe "collection compatibility" do
describe "#total_count" do
it "should be calculated correctly" do
criteria.paginate(:per_page => 1).total_entries.should == 4
criteria.paginate(:per_page => 3).total_entries.should == 4
end

it "should be cached" do
criteria.expects(:count).once.returns(123)
criteria.paginate
2.times { criteria.total_entries.should == 123 }
end
end

it "should calculate total_pages" do
criteria.paginate(:per_page => 1).total_pages.should == 4
criteria.paginate(:per_page => 3).total_pages.should == 2
criteria.paginate(:per_page => 10).total_pages.should == 1
end

it "should return per_page" do
criteria.paginate(:per_page => 1).per_page.should == 1
criteria.paginate(:per_page => 5).per_page.should == 5
end

describe "#current_page" do
it "should return current_page" do
criteria.paginate(:page => 1).current_page.should == 1
criteria.paginate(:page => 3).current_page.should == 3
end

it "should be casted to PageNumber" do
criteria.paginate(:page => 1).current_page.should be_instance_of(WillPaginate::PageNumber)
end
end

it "should return offset" do
criteria.paginate(:page => 1).offset.should == 0
criteria.paginate(:page => 2, :per_page => 5).offset.should == 5
criteria.paginate(:page => 3, :per_page => 10).offset.should == 20
end

it "should not pollute plain mongoid criterias" do
%w(total_entries total_pages current_page).each do |method|
criteria.should_not respond_to(method)
end
criteria.offset.should be_nil # this is already a criteria method
end
end
end
end