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:
parent
e2652f1461
commit
011adedfec
6 changed files with 104 additions and 53 deletions
|
@ -43,6 +43,10 @@ module Kaminari
|
||||||
@_total_count || @_original_array.count
|
@_total_count || @_original_array.count
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def current_page_count #:nodoc:
|
||||||
|
count
|
||||||
|
end
|
||||||
|
|
||||||
# returns another chunk of the original array
|
# returns another chunk of the original array
|
||||||
def offset(num)
|
def offset(num)
|
||||||
self.class.new @_original_array, :limit => @_limit_value, :offset => num, :total_count => @_total_count
|
self.class.new @_original_array, :limit => @_limit_value, :offset => num, :total_count => @_total_count
|
||||||
|
|
|
@ -11,5 +11,10 @@ module Kaminari
|
||||||
def total_count #:nodoc:
|
def total_count #:nodoc:
|
||||||
model.count(query.options.except(:limit, :offset, :order))
|
model.count(query.options.except(:limit, :offset, :order))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def current_page_count #:nodoc:
|
||||||
|
count
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -12,6 +12,15 @@ module Kaminari
|
||||||
embedded? ? unpage.count : count
|
embedded? ? unpage.count : count
|
||||||
end
|
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
|
private
|
||||||
def unpage
|
def unpage
|
||||||
clone.tap do |crit|
|
clone.tap do |crit|
|
||||||
|
|
|
@ -11,5 +11,11 @@ module Kaminari
|
||||||
def total_count #:nodoc:
|
def total_count #:nodoc:
|
||||||
count
|
count
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,80 +3,90 @@ require 'mongo_mapper'
|
||||||
require 'kaminari/models/mongo_mapper_extension'
|
require 'kaminari/models/mongo_mapper_extension'
|
||||||
|
|
||||||
describe Kaminari::MongoMapperExtension do
|
describe Kaminari::MongoMapperExtension do
|
||||||
|
|
||||||
before do
|
before do
|
||||||
begin
|
begin
|
||||||
MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
|
MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
|
||||||
MongoMapper.database = "kaminari_test"
|
MongoMapper.database = "kaminari_test"
|
||||||
class Developer
|
class MongoMapperExtension_Developer
|
||||||
include ::MongoMapper::Document
|
include ::MongoMapper::Document
|
||||||
key :salary, Integer
|
key :salary, Integer
|
||||||
end
|
end
|
||||||
|
|
||||||
stub(subject).count { 300 } # in order to avoid DB access...
|
|
||||||
rescue Mongo::ConnectionFailure
|
rescue Mongo::ConnectionFailure
|
||||||
pending 'can not connect to MongoDB'
|
pending 'can not connect to MongoDB'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
before(:each) do
|
||||||
|
MongoMapperExtension_Developer.destroy_all
|
||||||
|
41.times { MongoMapperExtension_Developer.create!({:salary => 1}) }
|
||||||
|
end
|
||||||
|
|
||||||
describe '#page' do
|
describe '#page' do
|
||||||
context 'page 1' do
|
context 'page 1' do
|
||||||
subject { Developer.page(1) }
|
subject { MongoMapperExtension_Developer.page(1) }
|
||||||
it { should be_a Plucky::Query }
|
it { should be_a Plucky::Query }
|
||||||
its(:current_page) { should == 1 }
|
its(:current_page) { should == 1 }
|
||||||
its(:limit_value) { should == 25 }
|
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) }
|
it { should skip(0) }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'page 2' do
|
context 'page 2' do
|
||||||
subject { Developer.page 2 }
|
subject { MongoMapperExtension_Developer.page 2 }
|
||||||
it { should be_a Plucky::Query }
|
it { should be_a Plucky::Query }
|
||||||
its(:current_page) { should == 2 }
|
its(:current_page) { should == 2 }
|
||||||
its(:limit_value) { should == 25 }
|
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 }
|
it { should skip 25 }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'page "foobar"' do
|
context 'page "foobar"' do
|
||||||
subject { Developer.page 'foobar' }
|
subject { MongoMapperExtension_Developer.page 'foobar' }
|
||||||
it { should be_a Plucky::Query }
|
it { should be_a Plucky::Query }
|
||||||
its(:current_page) { should == 1 }
|
its(:current_page) { should == 1 }
|
||||||
its(:limit_value) { should == 25 }
|
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 }
|
it { should skip 0 }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with criteria before' do
|
context 'with criteria before' do
|
||||||
it "should have the proper criteria source" 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
|
end
|
||||||
|
|
||||||
subject { Developer.where(:salary => 1).page 2 }
|
subject { MongoMapperExtension_Developer.where(:salary => 1).page 2 }
|
||||||
its(:current_page) { should == 2 }
|
its(:current_page) { should == 2 }
|
||||||
its(:limit_value) { should == 25 }
|
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 }
|
it { should skip 25 }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with criteria after' do
|
context 'with criteria after' do
|
||||||
it "should have the proper criteria source" 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
|
end
|
||||||
|
|
||||||
subject { Developer.page(2).where(:salary => 1) }
|
subject { MongoMapperExtension_Developer.page(2).where(:salary => 1) }
|
||||||
its(:current_page) { should == 2 }
|
its(:current_page) { should == 2 }
|
||||||
its(:limit_value) { should == 25 }
|
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 }
|
it { should skip 25 }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#per' do
|
describe '#per' do
|
||||||
subject { Developer.page(2).per(10) }
|
subject { MongoMapperExtension_Developer.page(2).per(10) }
|
||||||
it { should be_a Plucky::Query }
|
it { should be_a Plucky::Query }
|
||||||
its(:current_page) { should == 2 }
|
its(:current_page) { should == 2 }
|
||||||
its(:limit_value) { should == 10 }
|
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 }
|
it { should skip 10 }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,80 +3,94 @@ require 'mongoid'
|
||||||
require 'kaminari/models/mongoid_extension'
|
require 'kaminari/models/mongoid_extension'
|
||||||
|
|
||||||
describe Kaminari::MongoidExtension do
|
describe Kaminari::MongoidExtension do
|
||||||
before :all do
|
|
||||||
class Developer
|
before do
|
||||||
|
begin
|
||||||
|
Mongoid.configure do |config|
|
||||||
|
config.master = Mongo::Connection.new.db("kaminari_test")
|
||||||
|
end
|
||||||
|
class MongoidExtension_Developer
|
||||||
include ::Mongoid::Document
|
include ::Mongoid::Document
|
||||||
field :salary, :type => Integer
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#page' do
|
describe '#page' do
|
||||||
before do
|
|
||||||
stub(subject).count { 300 } # in order to avoid DB access...
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'page 1' do
|
context 'page 1' do
|
||||||
subject { Developer.page 1 }
|
subject { MongoidExtension_Developer.page 1 }
|
||||||
it { should be_a Mongoid::Criteria }
|
it { should be_a Mongoid::Criteria }
|
||||||
its(:current_page) { should == 1 }
|
its(:current_page) { should == 1 }
|
||||||
its(:limit_value) { should == 25 }
|
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) }
|
it { should skip(0) }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'page 2' do
|
context 'page 2' do
|
||||||
subject { Developer.page 2 }
|
subject { MongoidExtension_Developer.page 2 }
|
||||||
it { should be_a Mongoid::Criteria }
|
it { should be_a Mongoid::Criteria }
|
||||||
its(:current_page) { should == 2 }
|
its(:current_page) { should == 2 }
|
||||||
its(:limit_value) { should == 25 }
|
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 }
|
it { should skip 25 }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'page "foobar"' do
|
context 'page "foobar"' do
|
||||||
subject { Developer.page 'foobar' }
|
subject { MongoidExtension_Developer.page 'foobar' }
|
||||||
it { should be_a Mongoid::Criteria }
|
it { should be_a Mongoid::Criteria }
|
||||||
its(:current_page) { should == 1 }
|
its(:current_page) { should == 1 }
|
||||||
its(:limit_value) { should == 25 }
|
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 }
|
it { should skip 0 }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with criteria before' do
|
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(:selector) { should == {:salary => 1} }
|
||||||
its(:current_page) { should == 2 }
|
its(:current_page) { should == 2 }
|
||||||
its(:limit_value) { should == 25 }
|
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 }
|
it { should skip 25 }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with criteria after' do
|
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(:selector) { should == {:salary => 1} }
|
||||||
its(:current_page) { should == 2 }
|
its(:current_page) { should == 2 }
|
||||||
its(:limit_value) { should == 25 }
|
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 }
|
it { should skip 25 }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#per' do
|
describe '#per' do
|
||||||
before do
|
subject { MongoidExtension_Developer.page(2).per(10) }
|
||||||
stub(subject).count { 300 } # in order to avoid DB access...
|
|
||||||
end
|
|
||||||
|
|
||||||
subject { Developer.page(2).per(10) }
|
|
||||||
it { should be_a Mongoid::Criteria }
|
it { should be_a Mongoid::Criteria }
|
||||||
its(:current_page) { should == 2 }
|
its(:current_page) { should == 2 }
|
||||||
its(:limit_value) { should == 10 }
|
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 }
|
it { should skip 10 }
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#page in embedded documents' do
|
describe '#page in embedded documents' do
|
||||||
before :all do
|
before :all do
|
||||||
class MongoDeveloper
|
class MongoMongoidExtension_Developer
|
||||||
include ::Mongoid::Document
|
include ::Mongoid::Document
|
||||||
field :salary, :type => Integer
|
field :salary, :type => Integer
|
||||||
embeds_many :frameworks
|
embeds_many :frameworks
|
||||||
|
@ -86,44 +100,47 @@ describe Kaminari::MongoidExtension do
|
||||||
include ::Mongoid::Document
|
include ::Mongoid::Document
|
||||||
field :name, :type => String
|
field :name, :type => String
|
||||||
field :language, :type => String
|
field :language, :type => String
|
||||||
embedded_in :mongo_developer
|
embedded_in :mongo_MongoidExtension_Developer
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
before :all do
|
before :all do
|
||||||
@mongo_developer = MongoDeveloper.new
|
@mongo_MongoidExtension_Developer = MongoMongoidExtension_Developer.new
|
||||||
@mongo_developer.frameworks.new(:name => "rails", :language => "ruby")
|
@mongo_MongoidExtension_Developer.frameworks.new(:name => "rails", :language => "ruby")
|
||||||
@mongo_developer.frameworks.new(:name => "merb", :language => "ruby")
|
@mongo_MongoidExtension_Developer.frameworks.new(:name => "merb", :language => "ruby")
|
||||||
@mongo_developer.frameworks.new(:name => "sinatra", :language => "ruby")
|
@mongo_MongoidExtension_Developer.frameworks.new(:name => "sinatra", :language => "ruby")
|
||||||
@mongo_developer.frameworks.new(:name => "cakephp", :language => "php")
|
@mongo_MongoidExtension_Developer.frameworks.new(:name => "cakephp", :language => "php")
|
||||||
@mongo_developer.frameworks.new(:name => "tornado", :language => "python")
|
@mongo_MongoidExtension_Developer.frameworks.new(:name => "tornado", :language => "python")
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'page 1' do
|
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 }
|
it { should be_a Mongoid::Criteria }
|
||||||
its(:total_count) { should == 5 }
|
its(:total_count) { should == 5 }
|
||||||
its(:limit_value) { should == 1 }
|
its(:limit_value) { should == 1 }
|
||||||
its(:current_page) { should == 1 }
|
its(:current_page) { should == 1 }
|
||||||
its(:num_pages) { should == 5 }
|
its(:num_pages) { should == 5 }
|
||||||
|
its(:current_page_count) { should == 1 }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with criteria after' do
|
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 }
|
it { should be_a Mongoid::Criteria }
|
||||||
its(:total_count) { should == 3 }
|
its(:total_count) { should == 3 }
|
||||||
its(:limit_value) { should == 2 }
|
its(:limit_value) { should == 2 }
|
||||||
its(:current_page) { should == 1 }
|
its(:current_page) { should == 1 }
|
||||||
its(:num_pages) { should == 2 }
|
its(:num_pages) { should == 2 }
|
||||||
|
its(:current_page_count) { should == 2 }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with criteria before' do
|
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 }
|
it { should be_a Mongoid::Criteria }
|
||||||
its(:total_count) { should == 3 }
|
its(:total_count) { should == 3 }
|
||||||
its(:limit_value) { should == 2 }
|
its(:limit_value) { should == 2 }
|
||||||
its(:current_page) { should == 1 }
|
its(:current_page) { should == 1 }
|
||||||
its(:num_pages) { should == 2 }
|
its(:num_pages) { should == 2 }
|
||||||
|
its(:current_page_count) { should == 2 }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue