Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add global `WillPaginate.per_page = 30` setting

This is a cherry-pick of 95a03d8
  • Loading branch information...
commit ff273af3a92e3c7ae278d837cd4389c4f8ec189f 1 parent 5c09d09
@mislav authored
View
20 README.rdoc
@@ -36,25 +36,21 @@ pagination, just stick this in:
You're done. (You can find the option list at WillPaginate::ViewHelpers.)
How does it know how much items to fetch per page? It asks your model by calling
-its <tt>per_page</tt> class method. You can define it like this:
+its <tt>per_page</tt> class method. You can set a custom value like this:
class Post < ActiveRecord::Base
- cattr_reader :per_page
- @@per_page = 50
+ self.per_page = 10
end
-... or like this:
+... or don't worry about it at all. WillPaginate defines it to be <b>30</b> by default.
+You can always specify the count explicitly when calling +paginate+:
- class Post < ActiveRecord::Base
- def self.per_page
- 50
- end
- end
+ @posts = Post.paginate :page => params[:page], :per_page => 10
-... or don't worry about it at all. WillPaginate defines it to be <b>30</b> by default.
-But you can always specify the count explicitly when calling +paginate+:
+You can also set `per_page` globally, which will affect all models that haven't got a
+custom value. Use this if you're unhappy with 30 as the default:
- @posts = Post.paginate :page => params[:page], :per_page => 50
+ WillPaginate.per_page = 10
The +paginate+ finder wraps the original finder and returns your resultset that now has
some new properties. You can use the collection as you would with any ActiveRecord
View
14 lib/will_paginate/array.rb
@@ -1,16 +1,16 @@
require 'will_paginate/collection'
+require 'will_paginate/per_page'
# http://www.desimcadam.com/archives/8
Array.class_eval do
def paginate(options = {})
raise ArgumentError, "parameter hash expected (got #{options.inspect})" unless Hash === options
-
- WillPaginate::Collection.create(
- options[:page] || 1,
- options[:per_page] || 30,
- options[:total_entries] || self.length
- ) { |pager|
+ page = options[:page] || 1
+ per_page = options[:per_page] || WillPaginate.per_page
+ total = options[:total_entries] || self.length
+
+ WillPaginate::Collection.create(page, per_page, total) do |pager|
pager.replace self[pager.offset, pager.per_page].to_a
- }
+ end
end
end
View
4 lib/will_paginate/collection.rb
@@ -1,3 +1,5 @@
+require 'will_paginate/per_page'
+
module WillPaginate
# = Invalid page number error
# This is an ArgumentError raised in case a page was requested that is either
@@ -60,7 +62,7 @@ class Collection < Array
# and the total number of entries. The last argument is optional because it
# is best to do lazy counting; in other words, count *conditionally* after
# populating the collection using the +replace+ method.
- def initialize(page, per_page, total = nil)
+ def initialize(page, per_page = WillPaginate.per_page, total = nil)
@current_page, @per_page = InvalidPage.validate(page, per_page)
self.total_entries = total if total
end
View
4 lib/will_paginate/finder.rb
@@ -1,4 +1,5 @@
require 'will_paginate/core_ext'
+require 'will_paginate/per_page'
module WillPaginate
# A mixin for ActiveRecord::Base. Provides +per_page+ class method
@@ -8,11 +9,10 @@ module WillPaginate
#
module Finder
def self.included(base)
+ base.extend PerPage
base.extend ClassMethods
class << base
alias_method_chain :method_missing, :paginate
- # alias_method_chain :find_every, :paginate
- define_method(:per_page) { 30 } unless respond_to?(:per_page)
end
end
View
27 lib/will_paginate/per_page.rb
@@ -0,0 +1,27 @@
+module WillPaginate
+ module PerPage
+ def per_page
+ defined?(@per_page) ? @per_page : WillPaginate.per_page
+ end
+
+ def per_page=(limit)
+ @per_page = limit.to_i
+ end
+
+ def self.extended(base)
+ base.extend Inheritance if base.is_a? Class
+ end
+
+ module Inheritance
+ def inherited(subclass)
+ super
+ subclass.per_page = self.per_page
+ end
+ end
+ end
+
+ extend PerPage
+
+ # default number of items per page
+ self.per_page = 30
+end
View
16 test/collection_test.rb
@@ -129,6 +129,22 @@ def test_page_count_was_removed
# It's `total_pages` now.
end
+ def test_inherits_global_per_page
+ col = WillPaginate::Collection.new(1)
+ assert_equal 30, col.per_page
+
+ WillPaginate.per_page = 12
+ begin
+ col = WillPaginate::Collection.new(1)
+ assert_equal 12, col.per_page
+
+ col = ('a'..'z').to_a.paginate(:page => 1)
+ assert_equal 12, col.per_page
+ ensure
+ WillPaginate.per_page = 30
+ end
+ end
+
private
def create(page = 2, limit = 5, total = nil, &block)
if block_given?
View
21 test/finder_test.rb
@@ -27,6 +27,27 @@ def test_simple_paginate
end
end
+ def test_per_page_setting
+ assert_equal 30, Topic.per_page
+ Topic.per_page = 12
+ begin
+ assert_equal 12, Topic.per_page
+ ensure
+ Topic.per_page = 30
+ end
+ assert_equal 10, Developer.per_page
+ end
+
+ def test_per_page_inheritance
+ subclass = Class.new(Topic)
+ assert_equal 30, subclass.per_page
+ subclass.per_page = 12
+ assert_equal 12, subclass.per_page
+ assert_equal 30, Topic.per_page
+ sub_subclass = Class.new(subclass)
+ assert_equal 12, sub_subclass.per_page
+ end
+
def test_parameter_api
# :page parameter in options is required!
assert_raise(ArgumentError){ Topic.paginate }
View
2  test/fixtures/developer.rb
@@ -10,5 +10,5 @@ def self.with_poor_ones(&block)
named_scope :distinct, :select => 'DISTINCT `users`.*'
named_scope :poor, :conditions => ['salary <= ?', 80000], :order => 'salary'
- def self.per_page() 10 end
+ self.per_page = 10
end
View
5 test/helper.rb
@@ -1,6 +1,11 @@
require 'test/unit'
require 'mocha'
require 'will_paginate'
+begin
+ require 'ruby-debug'
+rescue LoadError
+ # no debuggging
+end
class Test::Unit::TestCase
protected
Please sign in to comment.
Something went wrong with that request. Please try again.