0
@@ -11,6 +11,7 @@ module WillPaginate
0
base.extend ClassMethods
0
alias_method_chain :method_missing, :paginate
0
+ alias_method_chain :find_every, :paginate
0
define_method(:per_page) { 30 } unless respond_to?(:per_page)
0
@@ -100,29 +101,43 @@ module WillPaginate
0
unless method.to_s.index('paginate') == 0
0
return method_missing_without_paginate(method, *args, &block)
0
- page, per_page, total_entries = wp_parse_options!(options)
0
- # an array of IDs may have been given:
0
- total_entries ||= (Array === args.first and args.first.size)
0
# paginate finders are really just find_* with limit and offset
0
- finder = method.to_s.sub /^paginate/, 'find'
0
- args.unshift(:all) if args.empty?
0
- elsif finder.index('find_by_') == 0
0
- finder.sub! /^find/, 'find_all'
0
- WillPaginate::Collection.create(page, per_page, total_entries) do |pager|
0
- args << options.except(:count).merge(:offset => pager.offset, :limit => pager.per_page)
0
- pager.replace send(finder, *args)
0
- # magic counting for user convenience:
0
- pager.total_entries = wp_count!(options, args, finder) unless pager.total_entries
0
+ finder = method.to_s.sub('paginate', 'find')
0
+ finder.sub!('find', 'find_all') if finder.index('find_by_') == 0
0
+ if @owner and @reflection
0
+ unless @wp_extension_module
0
+ @wp_extension_module = Module.new
0
+ self.proxy_extend @wp_extension_module
0
+ eval_mode = 'module_eval'
0
+ eval_mode = 'instance_eval'
0
+ end.send eval_mode, %{
0
+ def #{method}(*args, &block)
0
+ page, per_page, total_entries = wp_parse_options!(options)
0
+ # an array of IDs may have been given:
0
+ total_entries ||= (Array === args.first and args.first.size)
0
+ #{finder == 'find' ? 'args.unshift(:all) if args.empty?' : ''}
0
+ WillPaginate::Collection.create(page, per_page, total_entries) do |pager|
0
+ args << options.except(:count).merge(:offset => pager.offset, :limit => pager.per_page)
0
+ @options_from_last_find = nil
0
+ pager.replace #{finder}(*args, &block)
0
+ # magic counting for user convenience:
0
+ pager.total_entries = wp_count!(options, args, '#{finder}') unless pager.total_entries
0
+ # paginating finder is now defined
0
+ __send__(method, *args, &block)
0
def wp_count!(options, args, finder)
0
@@ -143,12 +158,14 @@ module WillPaginate
0
# we may be in a model or an association proxy!
0
klass = (@owner and @reflection) ? @reflection.klass : self
0
- count = if finder
=~ /^find_/ and klass.respond_to?(scoper = finder.sub(/^find_/, 'with_'))
0
+ count = if finder
.index('find_') == 0 and klass.respond_to?(scoper = finder.sub('find', 'with'))
0
# scope_out adds a 'with_finder' method which acts like with_scope, if it's present
0
- # then execute the count with the scoping provided by the with_finder
0
+ # then execute the count with the scoping provided by the with_finder
0
- elsif conditions = wp_extract_finder_conditions(finder, args)
0
- # extracted the conditions from calls like "paginate_by_foo_and_bar"
0
+ elsif match = /^find_(all_by|by)_([_a-zA-Z]\w*)$/.match(finder)
0
+ # extract conditions from calls like "paginate_by_foo_and_bar"
0
+ attribute_names = extract_attribute_names_from_match(match)
0
+ conditions = construct_attributes_from_arguments(attribute_names, args)
0
with_scope(:find => { :conditions => conditions }, &counter)
0
@@ -174,15 +191,9 @@ module WillPaginate
0
- # thanks to active record for making us duplicate this code
0
- def wp_extract_finder_conditions(finder, arguments)
0
- return unless match = /^find_(all_by|by)_([_a-zA-Z]\w*)$/.match(finder.to_s)
0
- attribute_names = extract_attribute_names_from_match(match)
0
- unless all_attributes_exists?(attribute_names)
0
- raise "I can't make sense of `#{finder}`. Try doing the count manually"
0
- construct_attributes_from_arguments(attribute_names, arguments)
0
+ def find_every_with_paginate(options)
0
+ @options_from_last_find = options
0
+ find_every_without_paginate(options)
Comments
No one has commented yet.