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

Add next_page and prev_page methods

closes #237
This commit is contained in:
Yuki Nishijima 2012-12-04 19:16:48 +09:00
parent 7d20935411
commit 2ac0c19a64
6 changed files with 110 additions and 1 deletions

View file

@ -48,6 +48,16 @@ module Kaminari
(offset_without_padding / limit_value) + 1
end
# Next page number in the collection
def next_page
current_page + 1 unless last_page?
end
# Previous page number in the collection
def prev_page
current_page - 1 unless first_page?
end
# First page of the collection ?
def first_page?
current_page == 1

View file

@ -136,7 +136,6 @@ if defined? ActiveRecord
end
end
describe '#current_page' do
context 'page 1' do
subject { model_class.page }
@ -154,6 +153,30 @@ if defined? ActiveRecord
end
end
describe '#next_page' do
context 'page 1' do
subject { model_class.page }
its(:next_page) { should == 2 }
end
context 'page 5' do
subject { model_class.page(5) }
its(:next_page) { should be_nil }
end
end
describe '#prev_page' do
context 'page 1' do
subject { model_class.page }
its(:prev_page) { should be_nil }
end
context 'page 5' do
subject { model_class.page(5) }
its(:prev_page) { should == 4 }
end
end
describe '#first_page?' do
context 'on first page' do
subject { model_class.page(1).per(10) }

View file

@ -100,6 +100,30 @@ describe Kaminari::PaginatableArray do
end
end
describe '#next_page' do
context 'page 1' do
subject { array.page }
its(:next_page) { should == 2 }
end
context 'page 5' do
subject { array.page 5 }
its(:next_page) { should be_nil }
end
end
describe '#prev_page' do
context 'page 1' do
subject { array.page }
its(:prev_page) { should be_nil }
end
context 'page 5' do
subject { array.page 5 }
its(:prev_page) { should == 4 }
end
end
describe '#count' do
context 'page 1' do
subject { array.page }

View file

@ -32,6 +32,8 @@ if defined? DataMapper
subject { User.all(:age.gte => 60).page 0 }
it { should be_a DataMapper::Collection }
its(:current_page) { should == 1 }
its(:prev_page) { should be_nil }
its(:next_page) { should == 2 }
its('query.limit') { should == 25 }
its('query.offset') { should == 0 }
its(:total_count) { should == User.count(:age.gte => 60) }
@ -42,6 +44,8 @@ if defined? DataMapper
subject { User.all(:age.gte => 0).page 1 }
it { should be_a DataMapper::Collection }
its(:current_page) { should == 1 }
its(:prev_page) { should be_nil }
its(:next_page) { should == 2 }
its('query.limit') { should == 25 }
its('query.offset') { should == 0 }
its(:total_count) { should == 100 }
@ -52,6 +56,8 @@ if defined? DataMapper
subject { User.page 2 }
it { should be_a DataMapper::Collection }
its(:current_page) { should == 2 }
its(:prev_page) { should == 1 }
its(:next_page) { should == 3 }
its(:limit_value) { should == 25 }
its('query.limit') { should == 25 }
its('query.offset') { should == 25 }
@ -63,6 +69,8 @@ if defined? DataMapper
subject { User.page 'foobar' }
it { should be_a DataMapper::Collection }
its(:current_page) { should == 1 }
its(:prev_page) { should be_nil }
its(:next_page) { should == 2 }
its('query.limit') { should == 25 }
its('query.offset') { should == 0 }
its(:total_count) { should == 100 }
@ -73,6 +81,8 @@ if defined? DataMapper
subject { User.all(:age.gt => 60).page 2 }
it { should be_a DataMapper::Collection }
its(:current_page) { should == 2 }
its(:prev_page) { should == 1 }
its(:next_page) { should be_nil }
its('query.limit') { should == 25 }
its('query.offset') { should == 25 }
its(:total_count) { should == User.count(:age.gt => 60) }
@ -83,6 +93,8 @@ if defined? DataMapper
subject { User.page(2).all(:age.gt => 60) }
it { should be_a DataMapper::Collection }
its(:current_page) { should == 2 }
its(:prev_page) { should == 1 }
its(:next_page) { should be_nil }
its('query.limit') { should == 25 }
its('query.offset') { should == 25 }
its(:total_count) { should == User.count(:age.gt => 60) }
@ -95,6 +107,8 @@ if defined? DataMapper
subject { User.page(2).per(20) }
it { should be_a DataMapper::Collection }
its(:current_page) { should == 2 }
its(:prev_page) { should == 1 }
its(:next_page) { should == 3 }
its('query.limit') { should == 20 }
its(:limit_value) { should == 20 }
its('query.offset') { should == 20 }
@ -105,6 +119,8 @@ if defined? DataMapper
context 'on query with condition' do
subject { User.page(5).all(:age.lte => 80).per(13) }
its(:current_page) { should == 5 }
its(:prev_page) { should == 4 }
its(:next_page) { should == 6 }
its('query.limit') { should == 13 }
its('query.offset') { should == 52 }
its(:total_count) { should == 81 }
@ -118,6 +134,8 @@ if defined? DataMapper
it('includes user with age 52') { should include(User.first(:age => 64)) }
it('does not include user with age 51') { should_not include(User.first(:age => 65)) }
its(:current_page) { should == 5 }
its(:prev_page) { should == 4 }
its(:next_page) { should == 6 }
its('query.limit') { should == 13 }
its('query.offset') { should == 52 }
its(:total_count) { should == 81 }
@ -127,6 +145,8 @@ if defined? DataMapper
context 'on chained queries' do
subject { User.all(:age.gte => 50).page(3).all(:age.lte => 80).per(13) }
its(:current_page) { should == 3 }
its(:prev_page) { should == 2 }
its(:next_page) { should be_nil }
its('query.limit') { should == 13 }
its('query.offset') { should == 26 }
its(:total_count) { should == 31 }
@ -136,6 +156,8 @@ if defined? DataMapper
context 'on query on association' do
subject { User[0].projects.page(3).all(:name.like => 'Project%').per(5) }
its(:current_page) { should == 3 }
its(:prev_page) { should == 2 }
its(:next_page) { should == 4 }
its('query.limit') { should == 5 }
its('query.offset') { should == 10 }
its(:total_count) { should == 50 }
@ -145,6 +167,8 @@ if defined? DataMapper
context 'on query with association conditions' do
subject { User.page(3).all(:projects => Project.all).per(5) }
its(:current_page) { should == 3 }
its(:prev_page) { should == 2 }
its(:next_page) { should == 4 }
its('query.limit') { should == 5 }
its('query.offset') { should == 10 }
its(:total_count) { should == 50 }

View file

@ -12,6 +12,8 @@ if defined? MongoMapper
subject { User.page(1) }
it { should be_a Plucky::Query }
its(:current_page) { should == 1 }
its(:prev_page) { should be_nil }
its(:next_page) { should == 2 }
its(:limit_value) { should == 25 }
its(:total_pages) { should == 2 }
it { should skip(0) }
@ -21,6 +23,8 @@ if defined? MongoMapper
subject { User.page 2 }
it { should be_a Plucky::Query }
its(:current_page) { should == 2 }
its(:prev_page) { should == 1 }
its(:next_page) { should be_nil }
its(:limit_value) { should == 25 }
its(:total_pages) { should == 2 }
it { should skip 25 }
@ -30,6 +34,8 @@ if defined? MongoMapper
subject { User.page 'foobar' }
it { should be_a Plucky::Query }
its(:current_page) { should == 1 }
its(:prev_page) { should be_nil }
its(:next_page) { should == 2 }
its(:limit_value) { should == 25 }
its(:total_pages) { should == 2 }
it { should skip 0 }
@ -42,6 +48,8 @@ if defined? MongoMapper
subject { User.where(:salary => 1).page 2 }
its(:current_page) { should == 2 }
its(:prev_page) { should == 1 }
its(:next_page) { should be_nil }
its(:limit_value) { should == 25 }
its(:total_pages) { should == 2 }
it { should skip 25 }
@ -54,6 +62,8 @@ if defined? MongoMapper
subject { User.page(2).where(:salary => 1) }
its(:current_page) { should == 2 }
its(:prev_page) { should == 1 }
its(:next_page) { should be_nil }
its(:limit_value) { should == 25 }
its(:total_pages) { should == 2 }
it { should skip 25 }
@ -64,6 +74,8 @@ if defined? MongoMapper
subject { User.page(2).per(10) }
it { should be_a Plucky::Query }
its(:current_page) { should == 2 }
its(:prev_page) { should == 1 }
its(:next_page) { should == 3 }
its(:limit_value) { should == 10 }
its(:total_pages) { should == 5 }
it { should skip 10 }

View file

@ -14,6 +14,8 @@ if defined? Mongoid
subject { User.page 1 }
it { should be_a Mongoid::Criteria }
its(:current_page) { should == 1 }
its(:prev_page) { should be_nil }
its(:next_page) { should == 2 }
its(:limit_value) { should == 25 }
its(:total_pages) { should == 2 }
it { should skip(0) }
@ -23,6 +25,8 @@ if defined? Mongoid
subject { User.page 2 }
it { should be_a Mongoid::Criteria }
its(:current_page) { should == 2 }
its(:prev_page) { should == 1 }
its(:next_page) { should be_nil }
its(:limit_value) { should == 25 }
its(:total_pages) { should == 2 }
it { should skip 25 }
@ -32,6 +36,8 @@ if defined? Mongoid
subject { User.page 'foobar' }
it { should be_a Mongoid::Criteria }
its(:current_page) { should == 1 }
its(:prev_page) { should be_nil }
its(:next_page) { should == 2 }
its(:limit_value) { should == 25 }
its(:total_pages) { should == 2 }
it { should skip 0 }
@ -44,6 +50,8 @@ if defined? Mongoid
its(:selector) { should == {:salary => 1} }
end
its(:current_page) { should == 2 }
its(:prev_page) { should == 1 }
its(:next_page) { should be_nil }
its(:limit_value) { should == 25 }
its(:total_pages) { should == 2 }
it { should skip 25 }
@ -64,6 +72,8 @@ if defined? Mongoid
subject { User.page(2).per(10) }
it { should be_a Mongoid::Criteria }
its(:current_page) { should == 2 }
its(:prev_page) { should == 1 }
its(:next_page) { should == 3 }
its(:limit_value) { should == 10 }
its(:total_pages) { should == 5 }
it { should skip 10 }
@ -85,6 +95,8 @@ if defined? Mongoid
its(:total_count) { should == 5 }
its(:limit_value) { should == 1 }
its(:current_page) { should == 1 }
its(:prev_page) { should be_nil }
its(:next_page) { should == 2 }
its(:total_pages) { should == 5 }
end
@ -94,6 +106,8 @@ if defined? Mongoid
its(:total_count) { should == 3 }
its(:limit_value) { should == 2 }
its(:current_page) { should == 1 }
its(:prev_page) { should be_nil }
its(:next_page) { should == 2 }
its(:total_pages) { should == 2 }
end
@ -103,6 +117,8 @@ if defined? Mongoid
its(:total_count) { should == 3 }
its(:limit_value) { should == 2 }
its(:current_page) { should == 1 }
its(:prev_page) { should be_nil }
its(:next_page) { should == 2 }
its(:total_pages) { should == 2 }
end
end