2009-01-06 11:49:19 +01:00
|
|
|
require 'active_support'
|
|
|
|
|
|
|
|
# = You *will* paginate!
|
|
|
|
#
|
|
|
|
# First read about WillPaginate::Finder::ClassMethods, then see
|
|
|
|
# WillPaginate::ViewHelpers. The magical array you're handling in-between is
|
|
|
|
# WillPaginate::Collection.
|
|
|
|
#
|
|
|
|
# Happy paginating!
|
|
|
|
module WillPaginate
|
|
|
|
class << self
|
|
|
|
# shortcut for <tt>enable_actionpack</tt> and <tt>enable_activerecord</tt> combined
|
|
|
|
def enable
|
|
|
|
enable_actionpack
|
|
|
|
enable_activerecord
|
|
|
|
end
|
|
|
|
|
|
|
|
# hooks WillPaginate::ViewHelpers into ActionView::Base
|
|
|
|
def enable_actionpack
|
|
|
|
return if ActionView::Base.instance_methods.include? 'will_paginate'
|
|
|
|
require 'will_paginate/view_helpers'
|
|
|
|
ActionView::Base.send :include, ViewHelpers
|
|
|
|
|
|
|
|
if defined?(ActionController::Base) and ActionController::Base.respond_to? :rescue_responses
|
|
|
|
ActionController::Base.rescue_responses['WillPaginate::InvalidPage'] = :not_found
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# hooks WillPaginate::Finder into ActiveRecord::Base and classes that deal
|
|
|
|
# with associations
|
|
|
|
def enable_activerecord
|
|
|
|
return if ActiveRecord::Base.respond_to? :paginate
|
|
|
|
require 'will_paginate/finder'
|
|
|
|
ActiveRecord::Base.send :include, Finder
|
|
|
|
|
|
|
|
# support pagination on associations
|
|
|
|
a = ActiveRecord::Associations
|
|
|
|
returning([ a::AssociationCollection ]) { |classes|
|
|
|
|
# detect http://dev.rubyonrails.org/changeset/9230
|
|
|
|
unless a::HasManyThroughAssociation.superclass == a::HasManyAssociation
|
|
|
|
classes << a::HasManyThroughAssociation
|
|
|
|
end
|
|
|
|
}.each do |klass|
|
|
|
|
klass.send :include, Finder::ClassMethods
|
|
|
|
klass.class_eval { alias_method_chain :method_missing, :paginate }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-11 12:21:21 +02:00
|
|
|
# Enable scope, a feature of Rails 2.1, even if you have older Rails
|
2009-01-06 11:49:19 +01:00
|
|
|
# (tested on Rails 2.0.2 and 1.2.6).
|
|
|
|
#
|
|
|
|
# You can pass +false+ for +patch+ parameter to skip monkeypatching
|
2011-05-11 12:21:21 +02:00
|
|
|
# *associations*. Use this if you feel that <tt>scope</tt> broke
|
2009-01-06 11:49:19 +01:00
|
|
|
# has_many, has_many :through or has_and_belongs_to_many associations in
|
2011-05-11 12:21:21 +02:00
|
|
|
# your app. By passing +false+, you can still use <tt>scope</tt> in
|
2009-01-06 11:49:19 +01:00
|
|
|
# your models, but not through associations.
|
2011-05-11 12:21:21 +02:00
|
|
|
def enable_scope(patch = true)
|
2009-01-06 11:49:19 +01:00
|
|
|
return if defined? ActiveRecord::NamedScope
|
2011-05-11 12:21:21 +02:00
|
|
|
require 'will_paginate/scope'
|
|
|
|
require 'will_paginate/scope_patch' if patch
|
2009-01-06 11:49:19 +01:00
|
|
|
|
|
|
|
ActiveRecord::Base.send :include, WillPaginate::NamedScope
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module Deprecation # :nodoc:
|
|
|
|
extend ActiveSupport::Deprecation
|
|
|
|
|
|
|
|
def self.warn(message, callstack = caller)
|
|
|
|
message = 'WillPaginate: ' + message.strip.gsub(/\s+/, ' ')
|
|
|
|
behavior.call(message, callstack) if behavior && !silenced?
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.silenced?
|
|
|
|
ActiveSupport::Deprecation.silenced?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if defined?(Rails) and defined?(ActiveRecord) and defined?(ActionController)
|
|
|
|
WillPaginate.enable
|
|
|
|
end
|