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

Merge pull request #301 from zpieslak/master

Add max_pages option (similar as in #88)
This commit is contained in:
Akira Matsuda 2012-11-26 05:24:54 -08:00
commit 992fb9f935
6 changed files with 80 additions and 1 deletions

View file

@ -24,6 +24,7 @@ module Kaminari
config_accessor :left config_accessor :left
config_accessor :right config_accessor :right
config_accessor :page_method_name config_accessor :page_method_name
config_accessor :max_pages
def param_name def param_name
config.param_name.respond_to?(:call) ? config.param_name.call : config.param_name config.param_name.respond_to?(:call) ? config.param_name.call : config.param_name
@ -45,5 +46,6 @@ module Kaminari
config.right = 0 config.right = 0
config.page_method_name = :page config.page_method_name = :page
config.param_name = :page config.param_name = :page
config.max_pages = nil
end end
end end

View file

@ -29,6 +29,20 @@ module Kaminari
def max_per_page def max_per_page
@_max_per_page || Kaminari.config.max_per_page @_max_per_page || Kaminari.config.max_per_page
end end
# Overrides the max_pages value per model
# class Article < ActiveRecord::Base
# max_pages_per 100
# end
def max_pages_per(val)
@_max_pages = val
end
# This model's max_pages value
# returns max_pages value unless explicitly overridden via <tt>max_pages_per</tt>
def max_pages
@_max_pages || Kaminari.config.max_pages
end
end end
end end
end end

View file

@ -18,7 +18,12 @@ module Kaminari
# Total number of pages # Total number of pages
def total_pages def total_pages
(total_count.to_f / limit_value).ceil total_pages_count = (total_count.to_f / limit_value).ceil
if max_pages.present? && max_pages < total_pages_count
max_pages
else
total_pages_count
end
end end
#FIXME for compatibility. remove num_pages at some time in the future #FIXME for compatibility. remove num_pages at some time in the future
alias num_pages total_pages alias num_pages total_pages

View file

@ -73,4 +73,19 @@ describe Kaminari::Configuration do
end end
end end
end end
describe 'max_pages' do
context 'by default' do
its(:max_pages) { should == nil }
end
context 'configure via config block' do
before do
Kaminari.configure {|c| c.max_pages = 5}
end
its(:max_pages) { should == 5 }
after do
Kaminari.configure {|c| c.max_pages = nil}
end
end
end
end end

View file

@ -0,0 +1,23 @@
require 'spec_helper'
if defined? ActiveRecord
describe 'max pages' do
describe 'AR::Base' do
subject { ActiveRecord::Base }
it { should_not respond_to :max_pages_per }
end
subject { User.page 0 }
context 'by default' do
its(:max_pages) { should == nil }
end
context 'when explicitly set via max_pages_per' do
before { User.max_pages_per 3 }
its(:max_pages) { should == 3 }
after { User.max_pages_per nil }
end
end
end

View file

@ -98,6 +98,26 @@ if defined? ActiveRecord
subject { model_class.page(5).per('aho') } subject { model_class.page(5).per('aho') }
its(:total_pages) { should == 4 } its(:total_pages) { should == 4 }
end end
context 'with max_pages < total pages count from database' do
before { model_class.max_pages_per 3 }
subject { model_class.page }
its(:total_pages) { should == 3 }
after { model_class.max_pages_per nil }
end
context 'with max_pages > total pages count from database' do
before { model_class.max_pages_per 11 }
subject { model_class.page }
its(:total_pages) { should == 4 }
after { model_class.max_pages_per nil }
end
context 'with max_pages is nil' do
before { model_class.max_pages_per nil }
subject { model_class.page }
its(:total_pages) { should == 4 }
end
end end