Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

global configurable limit: `WillPaginate.per_page = 30`

  • Loading branch information...
commit 95a03d8ad362e246214772e1b5040cd095372a21 1 parent ca42e3c
@mislav authored
Showing with 59 additions and 0 deletions.
  1. +27 −0 lib/will_paginate/per_page.rb
  2. +32 −0 spec/per_page_spec.rb
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
32 spec/per_page_spec.rb
@@ -0,0 +1,32 @@
+require 'spec_helper'
+require 'will_paginate/per_page'
+
+describe WillPaginate::PerPage do
+
+ class MyModel
+ extend WillPaginate::PerPage
+ end
+
+ it "has the default value" do
+ MyModel.per_page.should == 30
+
+ WillPaginate.per_page = 10
+ begin
+ MyModel.per_page.should == 10
+ ensure
+ WillPaginate.per_page = 30
+ end
+ end
+
+ it "has an explicit value" do
+ MyModel.per_page = 12
+ begin
+ MyModel.per_page.should == 12
+ subclass = Class.new(MyModel)
+ subclass.per_page.should == 12
+ ensure
+ MyModel.send(:remove_instance_variable, '@per_page')
+ end
+ end
+
+end

2 comments on commit 95a03d8

@dolzenko

This is a very good case for something like https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/class/attribute.rb , right? Then we don't have to keep inherited hook.

@mislav
Owner

Yes. But keep in mind that will_paginate also runs in environments without ActiveSupport, namely Sinatra and Merb.

Please sign in to comment.
Something went wrong with that request. Please try again.