If model class' max_paginates_per is smaller than the default per_page, then it should be the max per_page

This commit is contained in:
Akira Matsuda 2016-11-19 13:49:05 +09:00
parent 1ecd32e477
commit 69f0160b17
2 changed files with 33 additions and 1 deletions

View File

@ -12,7 +12,8 @@ module Kaminari
# Model.page(5)
eval <<-RUBY, nil, __FILE__, __LINE__ + 1
def self.#{Kaminari.config.page_method_name}(num = nil)
limit(default_per_page).offset(default_per_page * ((num = num.to_i - 1) < 0 ? 0 : num)).extending do
per_page = max_per_page && (default_per_page > max_per_page) ? max_per_page : default_per_page
limit(per_page).offset(per_page * ((num = num.to_i - 1) < 0 ? 0 : num)).extending do
include Kaminari::ActiveRecordRelationMethods
include Kaminari::PageScopeMethods
end

View File

@ -109,6 +109,37 @@ if defined? ActiveRecord
subject { model_class.page(1).per(0) }
it { should have(0).users }
end
# I know it's a bit strange to have this here, but I couldn't find any better place for this case
context 'when max_per_page is given via model class, and `per` is not actually called' do
subject { model_class.page(1) }
context 'with max_per_page > default_per_page' do
around do |example|
begin
model_class.max_paginates_per(200)
example.run
ensure
model_class.max_paginates_per(nil)
end
end
it { should have(25).users }
end
context 'with max_per_page < default_per_page' do
around do |example|
begin
model_class.max_paginates_per(5)
example.run
ensure
model_class.max_paginates_per(nil)
end
end
it { should have(5).users }
end
end
end
describe '#max_paginates_per' do