mirror of
https://github.com/kaminari/kaminari.git
synced 2022-11-09 13:44:37 -05:00
enable to paginate throurh embedded documents in mongoid
This commit is contained in:
parent
beeb13bbde
commit
4276a2c4b3
3 changed files with 71 additions and 6 deletions
|
@ -11,7 +11,15 @@ module Kaminari
|
|||
end
|
||||
|
||||
def total_count #:nodoc:
|
||||
count
|
||||
embedded? ? unpage.count : count
|
||||
end
|
||||
|
||||
private
|
||||
def unpage
|
||||
clone.tap do |crit|
|
||||
crit.options.delete :limit
|
||||
crit.options.delete :skip
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,7 +7,7 @@ module Kaminari
|
|||
|
||||
included do
|
||||
def page(*args)
|
||||
self.klass.page(*args).criteria.merge(self)
|
||||
super(*args).criteria.merge(self)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -9,11 +9,12 @@ describe Kaminari::MongoidExtension do
|
|||
field :salary, :type => Integer
|
||||
end
|
||||
end
|
||||
before do
|
||||
stub(subject).count { 300 } # in order to avoid DB access...
|
||||
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 }
|
||||
it { should be_a Mongoid::Criteria }
|
||||
|
@ -58,10 +59,13 @@ describe Kaminari::MongoidExtension do
|
|||
its(:num_pages) { should == 12 }
|
||||
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) }
|
||||
it { should be_a Mongoid::Criteria }
|
||||
its(:current_page) { should == 2 }
|
||||
|
@ -69,4 +73,57 @@ describe Kaminari::MongoidExtension do
|
|||
its(:num_pages) { should == 30 }
|
||||
it { should skip 10 }
|
||||
end
|
||||
|
||||
describe '#page in embedded documents' do
|
||||
before :all do
|
||||
class MongoDeveloper
|
||||
include ::Mongoid::Document
|
||||
field :salary, :type => Integer
|
||||
embeds_many :frameworks
|
||||
end
|
||||
|
||||
class Framework
|
||||
include ::Mongoid::Document
|
||||
field :name, :type => String
|
||||
field :language, :type => String
|
||||
embedded_in :mongo_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")
|
||||
end
|
||||
|
||||
context 'page 1' do
|
||||
subject { @mongo_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 }
|
||||
end
|
||||
|
||||
context 'with criteria after' do
|
||||
subject { @mongo_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 }
|
||||
end
|
||||
|
||||
context 'with criteria before' do
|
||||
subject { @mongo_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 }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue