Skip to content

Feature/total pages #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/mongoid/pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Pagination
extend ActiveSupport::Concern

module ClassMethods
DEFAULT_PAGE_LIMIT = 25

# Paginate the results
#
Expand All @@ -16,7 +17,7 @@ module ClassMethods
def paginate(opts = {})
return criteria if opts[:limit].blank? and opts[:page].blank? and opts[:offset].blank?

limit = (opts[:limit] || 25).to_i
limit = (opts[:limit] || DEFAULT_PAGE_LIMIT).to_i
page = (opts[:page] || 1).to_i

if opts[:page].blank?
Expand All @@ -36,9 +37,17 @@ def paginate(opts = {})
#
# @param [Integer] page_limit the max number of results to return
# @return [Mongoid::Criteria]
def per_page(page_limit = 25)
def per_page(page_limit = DEFAULT_PAGE_LIMIT)
limit(page_limit.to_i)
end

# Calculate total number of pages
#
# @return [Integer] total documents in collection divided by limit
def total_pages
limit = criteria.options[:limit] || DEFAULT_PAGE_LIMIT
(criteria.count / limit) + (criteria.count % limit)
end
end
end
end
1 change: 1 addition & 0 deletions mongoid-pagination.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ Gem::Specification.new do |s|

s.add_dependency 'mongoid'
s.add_dependency 'activesupport'
s.add_development_dependency 'pry'
end
6 changes: 6 additions & 0 deletions spec/config/mongoid.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test:
clients:
default:
database: mongoid_pagination_test
hosts:
- localhost:27017
25 changes: 25 additions & 0 deletions spec/pagination_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'
require 'pry'

describe Mongoid::Pagination do
class Person
Expand Down Expand Up @@ -161,4 +162,28 @@ class Person
Person.per_page.options[:limit].should == 25
end
end

describe ".total_pages" do
subject { Person.paginate(:limit => 2) }

context "an even number of records" do
before do
8.times { Person.create! }
end

it "calculates the total number of pages" do
expect(subject.total_pages).to eq 4
end
end

context "an odd number of records" do
before do
7.times { Person.create! }
end

it "calculates the total number of pages" do
expect(subject.total_pages).to eq 4
end
end
end
end
11 changes: 5 additions & 6 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
require 'rubygems'
require 'bundler'

Bundler.setup :default, :test
Bundler.require :default, :test

$:.push(File.expand_path(File.dirname(__FILE__)))
require './lib/mongoid-pagination'

Mongoid.database = Mongo::Connection.new.db('mongoid-pagination_test')
Mongoid.load!('./spec/config/mongoid.yml', 'test')

RSpec.configure do |c|
c.before(:each) do
Mongoid.database.collections.each { |c| c.drop unless c.name =~ /system\.indexes$/}
RSpec.configure do |config|
config.before(:each) do
Mongoid.purge!
end
end
end