mirror of
https://github.com/kaminari/kaminari.git
synced 2022-11-09 13:44:37 -05:00
Add max_per_page configuration option
This commit is contained in:
parent
82a38e07db
commit
c77867681e
6 changed files with 76 additions and 0 deletions
11
README.rdoc
11
README.rdoc
|
@ -73,6 +73,7 @@ Then bundle:
|
|||
|
||||
You can configure the following default values by overriding these values using <tt>Kaminari.configure</tt> method.
|
||||
default_per_page # 25 by default
|
||||
max_per_page # 0 by default
|
||||
window # 4 by default
|
||||
outer_window # 0 by default
|
||||
left # 0 by default
|
||||
|
@ -98,6 +99,16 @@ Run the following generator command, then edit the generated file.
|
|||
paginates_per 50
|
||||
end
|
||||
|
||||
=== Configuring max +per_page+ value for each model
|
||||
|
||||
* +max_paginates_per+
|
||||
|
||||
You can specify max +per_page+ value per each model using the following declarative DSL.
|
||||
If the variable that specified via +per+ scope is more than this variable, max +paginates_per+ is used instead of it. Default value is 0, which means you are not specifying max +per_page+ value.
|
||||
class User < ActiveRecord::Base
|
||||
max_paginates_per 100
|
||||
end
|
||||
|
||||
=== Controllers
|
||||
|
||||
* the page parameter is in <tt>params[:page]</tt>
|
||||
|
|
|
@ -18,6 +18,7 @@ module Kaminari
|
|||
class Configuration #:nodoc:
|
||||
include ActiveSupport::Configurable
|
||||
config_accessor :default_per_page
|
||||
config_accessor :max_per_page
|
||||
config_accessor :window
|
||||
config_accessor :outer_window
|
||||
config_accessor :left
|
||||
|
@ -37,6 +38,7 @@ module Kaminari
|
|||
# this is ugly. why can't we pass the default value to config_accessor...?
|
||||
configure do |config|
|
||||
config.default_per_page = 25
|
||||
config.max_per_page = 0
|
||||
config.window = 4
|
||||
config.outer_window = 0
|
||||
config.left = 0
|
||||
|
|
|
@ -15,6 +15,20 @@ module Kaminari
|
|||
def default_per_page
|
||||
@_default_per_page ||= Kaminari.config.default_per_page
|
||||
end
|
||||
|
||||
# Overrides the max +per_page+ value per model
|
||||
# class Article < ActiveRecord::Base
|
||||
# max_paginates_per 100
|
||||
# end
|
||||
def max_paginates_per(val)
|
||||
@_max_per_page = val
|
||||
end
|
||||
|
||||
# This model's max +per_page+ value
|
||||
# returns +max_per_page+ value unless explicitly overridden via <tt>max_paginates_per</tt>
|
||||
def max_per_page
|
||||
@_max_per_page ||= Kaminari.config.max_per_page
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,6 +5,8 @@ module Kaminari
|
|||
def per(num)
|
||||
if (n = num.to_i) <= 0
|
||||
self
|
||||
elsif max_per_page != 0 && max_per_page < n
|
||||
limit(max_per_page).offset(offset_value / limit_value * max_per_page)
|
||||
else
|
||||
limit(n).offset(offset_value / limit_value * n)
|
||||
end
|
||||
|
|
|
@ -17,6 +17,21 @@ describe Kaminari::Configuration do
|
|||
end
|
||||
end
|
||||
|
||||
describe 'max_per_page' do
|
||||
context 'by default' do
|
||||
its(:max_per_page) { should == 0 }
|
||||
end
|
||||
context 'configure via config block' do
|
||||
before do
|
||||
Kaminari.configure {|c| c.max_per_page = 100}
|
||||
end
|
||||
its(:max_per_page) { should == 100 }
|
||||
after do
|
||||
Kaminari.configure {|c| c.max_per_page = 0}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'window' do
|
||||
context 'by default' do
|
||||
its(:window) { should == 4 }
|
||||
|
|
32
spec/models/active_record/max_per_page_spec.rb
Normal file
32
spec/models/active_record/max_per_page_spec.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
require 'spec_helper'
|
||||
|
||||
if defined? ActiveRecord
|
||||
|
||||
describe 'max per_page' do
|
||||
describe 'AR::Base' do
|
||||
subject { ActiveRecord::Base }
|
||||
it { should_not respond_to :max_paginates_per }
|
||||
end
|
||||
|
||||
subject { User.page(0).per(100) }
|
||||
|
||||
context 'by default' do
|
||||
its(:limit_value) { should == 100 }
|
||||
end
|
||||
|
||||
context 'when explicitly set via max_paginates_per' do
|
||||
before { User.max_paginates_per 10 }
|
||||
its(:limit_value) { should == 10 }
|
||||
after { User.max_paginates_per nil }
|
||||
end
|
||||
|
||||
describe "max per_page value's independency per model" do
|
||||
context "when User's max per_page was changed" do
|
||||
before { User.max_paginates_per 10 }
|
||||
subject { Book.page(0).per(100) }
|
||||
its(:limit_value) { should == 100 }
|
||||
after { User.max_paginates_per nil }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue