First read about WillPaginate::Finder::ClassMethods, then see WillPaginate::ViewHelpers. The magical array you‘re handling in-between is WillPaginate::Collection.
Happy paginating!
# File lib/will_paginate.rb, line 52 52: def self.construct_count_options_from_args(*args) 53: result = super 54: result[0] = '*' if result[0].is_a?(String) and result[0] =~ /\.\*$/ 55: result 56: end
shortcut for enable_actionpack and enable_activerecord combined
# File lib/will_paginate.rb, line 14 14: def enable 15: enable_actionpack 16: enable_activerecord 17: end
hooks WillPaginate::ViewHelpers into ActionView::Base
# File lib/will_paginate.rb, line 20 20: def enable_actionpack 21: return if ActionView::Base.instance_methods.include_method? :will_paginate 22: require 'will_paginate/view_helpers' 23: ActionView::Base.send :include, ViewHelpers 24: 25: if defined?(ActionController::Base) and ActionController::Base.respond_to? :rescue_responses 26: ActionController::Base.rescue_responses['WillPaginate::InvalidPage'] = :not_found 27: end 28: end
hooks WillPaginate::Finder into ActiveRecord::Base and classes that deal with associations
# File lib/will_paginate.rb, line 32 32: def enable_activerecord 33: return if ActiveRecord::Base.respond_to? :paginate 34: require 'will_paginate/finder' 35: ActiveRecord::Base.send :include, Finder 36: 37: # support pagination on associations 38: a = ActiveRecord::Associations 39: returning([ a::AssociationCollection ]) { |classes| 40: # detect http://dev.rubyonrails.org/changeset/9230 41: unless a::HasManyThroughAssociation.superclass == a::HasManyAssociation 42: classes << a::HasManyThroughAssociation 43: end 44: }.each do |klass| 45: klass.send :include, Finder::ClassMethods 46: klass.class_eval { alias_method_chain :method_missing, :paginate } 47: end 48: 49: # monkeypatch Rails ticket #2189: "count breaks has_many :through" 50: ActiveRecord::Base.class_eval do 51: protected 52: def self.construct_count_options_from_args(*args) 53: result = super 54: result[0] = '*' if result[0].is_a?(String) and result[0] =~ /\.\*$/ 55: result 56: end 57: end 58: end
Enable named_scope, a feature of Rails 2.1, even if you have older Rails (tested on Rails 2.0.2 and 1.2.6).
You can pass false for patch parameter to skip monkeypatching associations. Use this if you feel that named_scope broke has_many, has_many :through or has_and_belongs_to_many associations in your app. By passing false, you can still use named_scope in your models, but not through associations.
# File lib/will_paginate.rb, line 68 68: def enable_named_scope(patch = true) 69: return if defined? ActiveRecord::NamedScope 70: require 'will_paginate/named_scope' 71: require 'will_paginate/named_scope_patch' if patch 72: 73: ActiveRecord::Base.send :include, WillPaginate::NamedScope 74: end