public
Description: The open source social networking platform in Ruby on Rails from the author of RailsSpace
Homepage: http://insoshi.com
Clone URL: git://github.com/insoshi/insoshi.git
Search Repo:
Piotr Jakubowski (author)
Tue Jul 01 00:31:01 -0700 2008
Michael Hartl (committer)
Tue Jul 01 12:17:04 -0700 2008
insoshi / lib / will_paginate.rb
100644 61 lines (53 sloc) 2.017 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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; enable_activerecord</tt>
    def enable
      enable_actionpack
      enable_activerecord
    end
    
    # mixes in WillPaginate::ViewHelpers in ActionView::Base
    def enable_actionpack
      return if ActionView::Base.instance_methods.include? 'will_paginate'
      require 'will_paginate/view_helpers'
      ActionView::Base.class_eval { include ViewHelpers }
    end
    
    # mixes in WillPaginate::Finder in ActiveRecord::Base and classes that deal
    # with associations
    def enable_activerecord
      return if ActiveRecord::Base.respond_to? :paginate
      require 'will_paginate/finder'
      ActiveRecord::Base.class_eval { include Finder }
 
      associations = ActiveRecord::Associations
      collection = associations::AssociationCollection
      
      # to support paginating finders on associations, we have to mix in the
      # method_missing magic from WillPaginate::Finder::ClassMethods to AssociationProxy
      # subclasses, but in a different way for Rails 1.2.x and 2.0
      (collection.instance_methods.include?(:create!) ?
        collection : collection.subclasses.map(&:constantize)
      ).push(associations::HasManyThroughAssociation).each do |klass|
        klass.class_eval do
          include Finder::ClassMethods
          alias_method_chain :method_missing, :paginate
        end
      end
    end
  end
 
  module Deprecation #:nodoc:
    extend ActiveSupport::Deprecation
 
    def self.warn(message, callstack = caller)
      message = 'WillPaginate: ' + message.strip.gsub(/ {3,}/, ' ')
      behavior.call(message, callstack) if behavior && !silenced?
    end
 
    def self.silenced?
      ActiveSupport::Deprecation.silenced?
    end
  end
end