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

refs #41 general configuration options

make DEFAULT_PER_PAGE configurable via Kaminiar.configure method
This commit is contained in:
Akira Matsuda 2011-04-21 15:30:34 +09:00
parent 74736637c8
commit 5a9303fe79
4 changed files with 48 additions and 2 deletions

27
lib/kaminari/config.rb Normal file
View file

@ -0,0 +1,27 @@
require 'active_support/configurable'
module Kaminari
# Configures global settings for Kaminari
# Kaminari.configure do |config|
# config.default_per_page = 10
# end
def self.configure(&block)
yield @config ||= Kaminari::Configuration.new
end
# Global settings for Kaminari
def self.config
@config
end
# need a Class for 3.0
class Configuration #:nodoc:
include ActiveSupport::Configurable
config_accessor :default_per_page
end
# this is ugly. why can't we pass the default value to config_accessor...?
configure do |config|
config.default_per_page = 25
end
end

View file

@ -13,7 +13,7 @@ module Kaminari
# This model's default per_page value # This model's default per_page value
# returns 25 unless explicitly overridden via <tt>paginates_per</tt> # returns 25 unless explicitly overridden via <tt>paginates_per</tt>
def default_per_page def default_per_page
@_default_per_page || Kaminari::DEFAULT_PER_PAGE @_default_per_page || Kaminari.config.default_per_page
end end
end end
end end

View file

@ -2,13 +2,13 @@ require 'rails'
# ensure ORMs are loaded *before* initializing Kaminari # ensure ORMs are loaded *before* initializing Kaminari
begin; require 'mongoid'; rescue LoadError; end begin; require 'mongoid'; rescue LoadError; end
require File.join(File.dirname(__FILE__), 'config')
require File.join(File.dirname(__FILE__), 'helpers/action_view_extension') require File.join(File.dirname(__FILE__), 'helpers/action_view_extension')
require File.join(File.dirname(__FILE__), 'helpers/paginator') require File.join(File.dirname(__FILE__), 'helpers/paginator')
require File.join(File.dirname(__FILE__), 'models/page_scope_methods') require File.join(File.dirname(__FILE__), 'models/page_scope_methods')
require File.join(File.dirname(__FILE__), 'models/configuration_methods') require File.join(File.dirname(__FILE__), 'models/configuration_methods')
module Kaminari module Kaminari
DEFAULT_PER_PAGE = 25 unless defined? ::Kaminari::DEFAULT_PER_PAGE
class Railtie < ::Rails::Railtie #:nodoc: class Railtie < ::Rails::Railtie #:nodoc:
initializer 'kaminari' do |app| initializer 'kaminari' do |app|
ActiveSupport.on_load(:active_record) do ActiveSupport.on_load(:active_record) do

View file

@ -0,0 +1,19 @@
require File.expand_path('../spec_helper', File.dirname(__FILE__))
describe Kaminari::Configuration do
subject { Kaminari.config }
describe 'default_per_page' do
context 'by default' do
its(:default_per_page) { should == 25 }
end
context 'configured via config block' do
before do
Kaminari.configure {|c| c.default_per_page = 17}
end
its(:default_per_page) { should == 17 }
after do
Kaminari.configure {|c| c.default_per_page = 25}
end
end
end
end