First read about WillPaginate::Finder::ClassMethods, then see WillPaginate::ViewHelpers. The magical array you‘re handling in-between is WillPaginate::Collection.
Happy paginating!
shortcut for enable_actionpack and enable_activerecord combined
# File lib/will_paginate.rb, line 13 13: def enable 14: enable_actionpack 15: enable_activerecord 16: end
hooks WillPaginate::ViewHelpers into ActionView::Base
# File lib/will_paginate.rb, line 19 19: def enable_actionpack 20: return if ActionView::Base.instance_methods.include? 'will_paginate' 21: require 'will_paginate/view_helpers' 22: ActionView::Base.send :include, ViewHelpers 23: 24: if defined?(ActionController::Base) and ActionController::Base.respond_to? :rescue_responses 25: ActionController::Base.rescue_responses['WillPaginate::InvalidPage'] = :not_found 26: end 27: end
hooks WillPaginate::Finder into ActiveRecord::Base and classes that deal with associations
# File lib/will_paginate.rb, line 31 31: def enable_activerecord 32: return if ActiveRecord::Base.respond_to? :paginate 33: require 'will_paginate/finder' 34: ActiveRecord::Base.send :include, Finder 35: 36: # support pagination on associations 37: a = ActiveRecord::Associations 38: returning([ a::AssociationCollection ]) { |classes| 39: # detect http://dev.rubyonrails.org/changeset/9230 40: unless a::HasManyThroughAssociation.superclass == a::HasManyAssociation 41: classes << a::HasManyThroughAssociation 42: end 43: }.each do |klass| 44: klass.send :include, Finder::ClassMethods 45: klass.class_eval { alias_method_chain :method_missing, :paginate } 46: end 47: 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 57 57: def enable_named_scope(patch = true) 58: return if defined? ActiveRecord::NamedScope 59: require 'will_paginate/named_scope' 60: require 'will_paginate/named_scope_patch' if patch 61: 62: ActiveRecord::Base.send :include, WillPaginate::NamedScope 63: end