From c460b8f34cfd102ca9961798699446053123289d Mon Sep 17 00:00:00 2001 From: Dave Date: Tue, 16 May 2017 22:42:19 -0400 Subject: [PATCH 1/2] Update spec file to properly load mongoid db --- mongoid-pagination.gemspec | 1 + spec/config/mongoid.yml | 6 ++++++ spec/spec_helper.rb | 11 +++++------ 3 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 spec/config/mongoid.yml diff --git a/mongoid-pagination.gemspec b/mongoid-pagination.gemspec index 5c33a10..6a5d6e4 100644 --- a/mongoid-pagination.gemspec +++ b/mongoid-pagination.gemspec @@ -21,4 +21,5 @@ Gem::Specification.new do |s| s.add_dependency 'mongoid' s.add_dependency 'activesupport' + s.add_dependency 'mongo' end diff --git a/spec/config/mongoid.yml b/spec/config/mongoid.yml new file mode 100644 index 0000000..5bbb6db --- /dev/null +++ b/spec/config/mongoid.yml @@ -0,0 +1,6 @@ +test: + clients: + default: + database: mongoid_pagination_test + hosts: + - localhost:27017 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a55230f..54f73a0 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 \ No newline at end of file +end From e1ce4d892341bd4e95f2559ba99e51e03ac6fb1d Mon Sep 17 00:00:00 2001 From: Dave Date: Tue, 16 May 2017 23:33:39 -0400 Subject: [PATCH 2/2] Formalize default page limit as constant, Add total_pages method --- lib/mongoid/pagination.rb | 13 +++++++++++-- mongoid-pagination.gemspec | 2 +- spec/pagination_spec.rb | 25 +++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/lib/mongoid/pagination.rb b/lib/mongoid/pagination.rb index 808fd3e..27997d1 100644 --- a/lib/mongoid/pagination.rb +++ b/lib/mongoid/pagination.rb @@ -5,6 +5,7 @@ module Pagination extend ActiveSupport::Concern module ClassMethods + DEFAULT_PAGE_LIMIT = 25 # Paginate the results # @@ -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? @@ -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 diff --git a/mongoid-pagination.gemspec b/mongoid-pagination.gemspec index 6a5d6e4..27afeaf 100644 --- a/mongoid-pagination.gemspec +++ b/mongoid-pagination.gemspec @@ -21,5 +21,5 @@ Gem::Specification.new do |s| s.add_dependency 'mongoid' s.add_dependency 'activesupport' - s.add_dependency 'mongo' + s.add_development_dependency 'pry' end diff --git a/spec/pagination_spec.rb b/spec/pagination_spec.rb index 3f9d676..0bfde9d 100644 --- a/spec/pagination_spec.rb +++ b/spec/pagination_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require 'pry' describe Mongoid::Pagination do class Person @@ -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