Skip to content

Commit

Permalink
Merge pull request #274 from keiko713/max_per_page
Browse files Browse the repository at this point in the history
Add max_per_page config value
  • Loading branch information
amatsuda committed Sep 2, 2012
2 parents a8637f2 + 7af1e87 commit dfd380d
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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 # nil by default
window # 4 by default
outer_window # 0 by default
left # 0 by default
Expand All @@ -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 nil, which means you are not imposing any max +per_page+ value.
class User < ActiveRecord::Base
max_paginates_per 100
end

=== Controllers

* the page parameter is in <tt>params[:page]</tt>
Expand Down
2 changes: 2 additions & 0 deletions lib/kaminari/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def self.config
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
Expand All @@ -37,6 +38,7 @@ def param_name
# 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 = nil
config.window = 4
config.outer_window = 0
config.left = 0
Expand Down
14 changes: 14 additions & 0 deletions lib/kaminari/models/configuration_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ def paginates_per(val)
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
2 changes: 2 additions & 0 deletions lib/kaminari/models/page_scope_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module PageScopeMethods
def per(num)
if (n = num.to_i) <= 0
self
elsif max_per_page && 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
Expand Down
15 changes: 15 additions & 0 deletions spec/config/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@
end
end

describe 'max_per_page' do
context 'by default' do
its(:max_per_page) { should == nil }
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 = nil}
end
end
end

describe 'window' do
context 'by default' do
its(:window) { should == 4 }
Expand Down
32 changes: 32 additions & 0 deletions spec/models/active_record/max_per_page_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit dfd380d

Please sign in to comment.