1
0
Fork 0
mirror of https://github.com/kaminari/kaminari.git synced 2022-11-09 13:44:37 -05:00

Fixing displayed number of items on each page w/ Mongoid 2.4.x and MongoMapper.

This commit is contained in:
dblock 2012-01-11 13:38:45 -05:00
parent e2652f1461
commit 011adedfec
6 changed files with 104 additions and 53 deletions

View file

@ -43,6 +43,10 @@ module Kaminari
@_total_count || @_original_array.count
end
def current_page_count #:nodoc:
count
end
# returns another chunk of the original array
def offset(num)
self.class.new @_original_array, :limit => @_limit_value, :offset => num, :total_count => @_total_count

View file

@ -11,5 +11,10 @@ module Kaminari
def total_count #:nodoc:
model.count(query.options.except(:limit, :offset, :order))
end
def current_page_count #:nodoc:
count
end
end
end

View file

@ -12,6 +12,15 @@ module Kaminari
embedded? ? unpage.count : count
end
def current_page_count #:nodoc:
# TODO: this needs a better fix, count comes from Mongoid::Context::Mongo or Enumerable which have different signatures
begin
count(true)
rescue ArgumentError
count
end
end
private
def unpage
clone.tap do |crit|

View file

@ -11,5 +11,11 @@ module Kaminari
def total_count #:nodoc:
count
end
def current_page_count #:nodoc:
# TODO: this needs a better fix, ie. pass skip_and_limit = true
# into http://api.mongodb.org/ruby/1.2.0/Mongo/Cursor.html#count-instance_method
to_a.count
end
end
end

View file

@ -3,80 +3,90 @@ require 'mongo_mapper'
require 'kaminari/models/mongo_mapper_extension'
describe Kaminari::MongoMapperExtension do
before do
begin
MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
MongoMapper.database = "kaminari_test"
class Developer
class MongoMapperExtension_Developer
include ::MongoMapper::Document
key :salary, Integer
end
stub(subject).count { 300 } # in order to avoid DB access...
rescue Mongo::ConnectionFailure
pending 'can not connect to MongoDB'
end
end
before(:each) do
MongoMapperExtension_Developer.destroy_all
41.times { MongoMapperExtension_Developer.create!({:salary => 1}) }
end
describe '#page' do
context 'page 1' do
subject { Developer.page(1) }
subject { MongoMapperExtension_Developer.page(1) }
it { should be_a Plucky::Query }
its(:current_page) { should == 1 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 12 }
its(:num_pages) { should == 2 }
its(:current_page_count) { should == 25 }
it { should skip(0) }
end
context 'page 2' do
subject { Developer.page 2 }
subject { MongoMapperExtension_Developer.page 2 }
it { should be_a Plucky::Query }
its(:current_page) { should == 2 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 12 }
its(:num_pages) { should == 2 }
its(:current_page_count) { should == 16 }
it { should skip 25 }
end
context 'page "foobar"' do
subject { Developer.page 'foobar' }
subject { MongoMapperExtension_Developer.page 'foobar' }
it { should be_a Plucky::Query }
its(:current_page) { should == 1 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 12 }
its(:num_pages) { should == 2 }
its(:current_page_count) { should == 25 }
it { should skip 0 }
end
context 'with criteria before' do
it "should have the proper criteria source" do
Developer.where(:salary => 1).page(2).criteria.source.should == {:salary => 1}
MongoMapperExtension_Developer.where(:salary => 1).page(2).criteria.source.should == {:salary => 1}
end
subject { Developer.where(:salary => 1).page 2 }
subject { MongoMapperExtension_Developer.where(:salary => 1).page 2 }
its(:current_page) { should == 2 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 12 }
its(:num_pages) { should == 2 }
its(:current_page_count) { should == 16 }
it { should skip 25 }
end
context 'with criteria after' do
it "should have the proper criteria source" do
Developer.where(:salary => 1).page(2).criteria.source.should == {:salary => 1}
MongoMapperExtension_Developer.where(:salary => 1).page(2).criteria.source.should == {:salary => 1}
end
subject { Developer.page(2).where(:salary => 1) }
subject { MongoMapperExtension_Developer.page(2).where(:salary => 1) }
its(:current_page) { should == 2 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 12 }
its(:num_pages) { should == 2 }
its(:current_page_count) { should == 16 }
it { should skip 25 }
end
end
describe '#per' do
subject { Developer.page(2).per(10) }
subject { MongoMapperExtension_Developer.page(2).per(10) }
it { should be_a Plucky::Query }
its(:current_page) { should == 2 }
its(:limit_value) { should == 10 }
its(:num_pages) { should == 30 }
its(:num_pages) { should == 5 }
its(:current_page_count) { should == 10 }
it { should skip 10 }
end
end

View file

@ -3,80 +3,94 @@ require 'mongoid'
require 'kaminari/models/mongoid_extension'
describe Kaminari::MongoidExtension do
before :all do
class Developer
include ::Mongoid::Document
field :salary, :type => Integer
before do
begin
Mongoid.configure do |config|
config.master = Mongo::Connection.new.db("kaminari_test")
end
class MongoidExtension_Developer
include ::Mongoid::Document
field :salary, type: Integer
end
rescue Mongo::ConnectionFailure
pending 'can not connect to MongoDB'
end
end
before(:each) do
MongoidExtension_Developer.all.destroy
41.times do
MongoidExtension_Developer.create!({:salary => 1})
end
end
describe '#page' do
before do
stub(subject).count { 300 } # in order to avoid DB access...
end
context 'page 1' do
subject { Developer.page 1 }
subject { MongoidExtension_Developer.page 1 }
it { should be_a Mongoid::Criteria }
its(:current_page) { should == 1 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 12 }
its(:num_pages) { should == 2 }
its(:current_page_count) { should == 25 }
it { should skip(0) }
end
context 'page 2' do
subject { Developer.page 2 }
subject { MongoidExtension_Developer.page 2 }
it { should be_a Mongoid::Criteria }
its(:current_page) { should == 2 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 12 }
its(:num_pages) { should == 2 }
its(:current_page_count) { should == 16 }
it { should skip 25 }
end
context 'page "foobar"' do
subject { Developer.page 'foobar' }
subject { MongoidExtension_Developer.page 'foobar' }
it { should be_a Mongoid::Criteria }
its(:current_page) { should == 1 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 12 }
its(:num_pages) { should == 2 }
its(:current_page_count) { should == 25 }
it { should skip 0 }
end
context 'with criteria before' do
subject { Developer.where(:salary => 1).page 2 }
subject { MongoidExtension_Developer.where(:salary => 1).page 2 }
its(:selector) { should == {:salary => 1} }
its(:current_page) { should == 2 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 12 }
its(:num_pages) { should == 2 }
its(:current_page_count) { should == 16 }
it { should skip 25 }
end
context 'with criteria after' do
subject { Developer.page(2).where(:salary => 1) }
subject { MongoidExtension_Developer.page(2).where(:salary => 1) }
its(:selector) { should == {:salary => 1} }
its(:current_page) { should == 2 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 12 }
its(:num_pages) { should == 2 }
its(:current_page_count) { should == 16 }
it { should skip 25 }
end
end
describe '#per' do
before do
stub(subject).count { 300 } # in order to avoid DB access...
end
subject { Developer.page(2).per(10) }
subject { MongoidExtension_Developer.page(2).per(10) }
it { should be_a Mongoid::Criteria }
its(:current_page) { should == 2 }
its(:limit_value) { should == 10 }
its(:num_pages) { should == 30 }
its(:num_pages) { should == 5 }
its(:current_page_count) { should == 10 }
it { should skip 10 }
end
describe '#page in embedded documents' do
before :all do
class MongoDeveloper
class MongoMongoidExtension_Developer
include ::Mongoid::Document
field :salary, :type => Integer
embeds_many :frameworks
@ -86,44 +100,47 @@ describe Kaminari::MongoidExtension do
include ::Mongoid::Document
field :name, :type => String
field :language, :type => String
embedded_in :mongo_developer
embedded_in :mongo_MongoidExtension_Developer
end
end
before :all do
@mongo_developer = MongoDeveloper.new
@mongo_developer.frameworks.new(:name => "rails", :language => "ruby")
@mongo_developer.frameworks.new(:name => "merb", :language => "ruby")
@mongo_developer.frameworks.new(:name => "sinatra", :language => "ruby")
@mongo_developer.frameworks.new(:name => "cakephp", :language => "php")
@mongo_developer.frameworks.new(:name => "tornado", :language => "python")
@mongo_MongoidExtension_Developer = MongoMongoidExtension_Developer.new
@mongo_MongoidExtension_Developer.frameworks.new(:name => "rails", :language => "ruby")
@mongo_MongoidExtension_Developer.frameworks.new(:name => "merb", :language => "ruby")
@mongo_MongoidExtension_Developer.frameworks.new(:name => "sinatra", :language => "ruby")
@mongo_MongoidExtension_Developer.frameworks.new(:name => "cakephp", :language => "php")
@mongo_MongoidExtension_Developer.frameworks.new(:name => "tornado", :language => "python")
end
context 'page 1' do
subject { @mongo_developer.frameworks.page(1).per(1) }
subject { @mongo_MongoidExtension_Developer.frameworks.page(1).per(1) }
it { should be_a Mongoid::Criteria }
its(:total_count) { should == 5 }
its(:limit_value) { should == 1 }
its(:current_page) { should == 1 }
its(:num_pages) { should == 5 }
its(:current_page_count) { should == 1 }
end
context 'with criteria after' do
subject { @mongo_developer.frameworks.page(1).per(2).where(:language => "ruby") }
subject { @mongo_MongoidExtension_Developer.frameworks.page(1).per(2).where(:language => "ruby") }
it { should be_a Mongoid::Criteria }
its(:total_count) { should == 3 }
its(:limit_value) { should == 2 }
its(:current_page) { should == 1 }
its(:num_pages) { should == 2 }
its(:current_page_count) { should == 2 }
end
context 'with criteria before' do
subject { @mongo_developer.frameworks.where(:language => "ruby").page(1).per(2) }
subject { @mongo_MongoidExtension_Developer.frameworks.where(:language => "ruby").page(1).per(2) }
it { should be_a Mongoid::Criteria }
its(:total_count) { should == 3 }
its(:limit_value) { should == 2 }
its(:current_page) { should == 1 }
its(:num_pages) { should == 2 }
its(:current_page_count) { should == 2 }
end
end
end