public
Fork of mislav/will_paginate
Description: Most awesome pagination solution for Rails
Homepage: http://github.com/mislav/will_paginate/wikis
Clone URL: git://github.com/technoweenie/will_paginate.git
will_paginate / lib / will_paginate / named_scope_patch.rb
100644 50 lines (47 sloc) 1.667 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
ActiveRecord::Associations::AssociationProxy.class_eval do
  protected
  def with_scope(*args, &block)
    @reflection.klass.send :with_scope, *args, &block
  end
end
 
# support pagination on associations
[ ActiveRecord::Associations::AssociationCollection,
    ActiveRecord::Associations::HasManyThroughAssociation ].each do |klass|
  klass.class_eval do
    protected
    def method_missing_without_paginate(method, *args)
      if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
        if block_given?
          super { |*block_args| yield(*block_args) }
        else
          super
        end
      elsif @reflection.klass.scopes.include?(method)
        @reflection.klass.scopes[method].call(self, *args)
      else
        with_scope construct_scope do
          if block_given?
            @reflection.klass.send(method, *args) { |*block_args| yield(*block_args) }
          else
            @reflection.klass.send(method, *args)
          end
        end
      end
    end
  end
end
 
# Rails 1.2.6
ActiveRecord::Associations::HasAndBelongsToManyAssociation.class_eval do
  protected
  def method_missing(method, *args, &block)
    if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
      super
    elsif @reflection.klass.scopes.include?(method)
      @reflection.klass.scopes[method].call(self, *args)
    else
      @reflection.klass.with_scope(:find => { :conditions => @finder_sql, :joins => @join_sql, :readonly => false }) do
        @reflection.klass.send(method, *args, &block)
      end
    end
  end
end if ActiveRecord::VERSION::MAJOR < 2