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; enable_activerecord
# File lib/will_paginate.rb, line 13 13: def enable 14: enable_actionpack 15: enable_activerecord 16: end
mixes in WillPaginate::ViewHelpers in 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.class_eval { 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
mixes in WillPaginate::Finder in 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.class_eval { 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.class_eval do 45: include Finder::ClassMethods 46: alias_method_chain :method_missing, :paginate 47: end 48: end 49: 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 59 59: def enable_named_scope(patch = true) 60: return if defined? ActiveRecord::NamedScope 61: require 'will_paginate/named_scope' 62: require 'will_paginate/named_scope_patch' if patch 63: 64: ActiveRecord::Base.class_eval do 65: include WillPaginate::NamedScope 66: end 67: end