Add max_pages and max_pages_per methods to limit displayed pages per model or globally
This commit is contained in:
parent
d45a762b5b
commit
c1807031ac
|
@ -24,6 +24,7 @@ module Kaminari
|
|||
config_accessor :left
|
||||
config_accessor :right
|
||||
config_accessor :page_method_name
|
||||
config_accessor :max_pages
|
||||
|
||||
def 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.page_method_name = :page
|
||||
config.param_name = :page
|
||||
config.max_pages = nil
|
||||
end
|
||||
end
|
||||
|
|
|
@ -29,6 +29,20 @@ module Kaminari
|
|||
def max_per_page
|
||||
@_max_per_page || Kaminari.config.max_per_page
|
||||
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
|
||||
|
|
|
@ -18,7 +18,12 @@ module Kaminari
|
|||
|
||||
# Total number of 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
|
||||
#FIXME for compatibility. remove num_pages at some time in the future
|
||||
alias num_pages total_pages
|
||||
|
|
Loading…
Reference in New Issue