Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 83 lines (66 sloc) 2.709 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
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
  let(:criteria) { MongoidModel.criteria }

  it "should have the #paginate method" do
    criteria.should respond_to(:paginate)
  end

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

    it "should use criteria" do
      criteria.paginate.should be_instance_of(::Mongoid::Criteria)
    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 "per_page should default to value configured for WillPaginate" 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
      it "should calculate total_count" do
        criteria.paginate(:per_page => 1).total_entries.should == 4
        criteria.paginate(:per_page => 3).total_entries.should == 4
      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

      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 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 per_page 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
Something went wrong with that request. Please try again.