"paginates_per" is a declarative DSL to set default per_page value for each model
This commit is contained in:
parent
ecf8316429
commit
575a0b303b
|
@ -1,7 +1,7 @@
|
|||
module Kaminari
|
||||
module ActiveRecord
|
||||
extend ActiveSupport::Concern
|
||||
PER_PAGE = 25
|
||||
DEFAULT_PER_PAGE = 25
|
||||
|
||||
included do
|
||||
def self.inherited(kls)
|
||||
|
@ -12,10 +12,12 @@ module Kaminari
|
|||
end]
|
||||
kls.instance_variable_set('@inheritable_attributes', new_inheritable_attributes)
|
||||
end
|
||||
|
||||
kls.class_eval do
|
||||
# page(5)
|
||||
scope :page, lambda {|num|
|
||||
limit(PER_PAGE).offset(PER_PAGE * ([num.to_i, 1].max - 1))
|
||||
per_page = @_default_per_page || Kaminari::ActiveRecord::DEFAULT_PER_PAGE
|
||||
limit(per_page).offset(per_page * ([num.to_i, 1].max - 1))
|
||||
} do
|
||||
# page(3).per(10)
|
||||
def per(num)
|
||||
|
@ -30,6 +32,14 @@ module Kaminari
|
|||
(offset_value / limit_value) + 1
|
||||
end
|
||||
end
|
||||
|
||||
# overrides the default per_page value per model
|
||||
# class Article < ActiveRecord::Base
|
||||
# paginates_per 10
|
||||
# end
|
||||
def self.paginates_per(val)
|
||||
@_default_per_page = val
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
require File.expand_path('../spec_helper', File.dirname(__FILE__))
|
||||
|
||||
describe 'default per_page' do
|
||||
subject { User.page 0 }
|
||||
|
||||
context 'by default' do
|
||||
its(:limit_value) { should == 25 }
|
||||
end
|
||||
|
||||
context 'when explicitly set via paginates_per' do
|
||||
before { User.paginates_per 1326 }
|
||||
its(:limit_value) { should == 1326 }
|
||||
after { User.paginates_per nil }
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue