lib/will_paginate/finder.rb in will_paginate-2.2.2 vs lib/will_paginate/finder.rb in will_paginate-2.3.11
- old
+ new
@@ -59,11 +59,11 @@
# * <tt>:count</tt> -- additional options that are passed on to +count+
# * <tt>:finder</tt> -- name of the ActiveRecord finder used (default: "find")
#
# All other options (+conditions+, +order+, ...) are forwarded to +find+
# and +count+ calls.
- def paginate(*args, &block)
+ def paginate(*args)
options = args.pop
page, per_page, total_entries = wp_parse_options(options)
finder = (options[:finder] || 'find').to_s
if finder == 'find'
@@ -77,11 +77,11 @@
count_options = options.except :page, :per_page, :total_entries, :finder
find_options = count_options.except(:count).update(:offset => pager.offset, :limit => pager.per_page)
args << find_options
# @options_from_last_find = nil
- pager.replace send(finder, *args, &block)
+ pager.replace(send(finder, *args) { |*a| yield(*a) if block_given? })
# magic counting for user convenience:
pager.total_entries = wp_count(count_options, args, finder) unless pager.total_entries
end
end
@@ -92,21 +92,24 @@
#
# It uses +paginate+ internally; therefore it accepts all of its options.
# You can specify a starting page with <tt>:page</tt> (default is 1). Default
# <tt>:order</tt> is <tt>"id"</tt>, override if necessary.
#
- # See http://weblog.jamisbuck.org/2007/4/6/faking-cursors-in-activerecord where
- # Jamis Buck describes this and also uses a more efficient way for MySQL.
- def paginated_each(options = {}, &block)
+ # See {Faking Cursors in ActiveRecord}[http://weblog.jamisbuck.org/2007/4/6/faking-cursors-in-activerecord]
+ # where Jamis Buck describes this and a more efficient way for MySQL.
+ def paginated_each(options = {})
options = { :order => 'id', :page => 1 }.merge options
options[:page] = options[:page].to_i
options[:total_entries] = 0 # skip the individual count queries
total = 0
begin
collection = paginate(options)
- total += collection.each(&block).size
+ with_exclusive_scope(:find => {}) do
+ # using exclusive scope so that the block is yielded in scope-free context
+ total += collection.each { |item| yield item }.size
+ end
options[:page] += 1
end until collection.size < collection.per_page
total
end
@@ -125,22 +128,22 @@
# generated SQL, you might want to perform the count manually in your
# application.
#
def paginate_by_sql(sql, options)
WillPaginate::Collection.create(*wp_parse_options(options)) do |pager|
- query = sanitize_sql(sql)
+ query = sanitize_sql(sql.dup)
original_query = query.dup
# add limit, offset
add_limit! query, :offset => pager.offset, :limit => pager.per_page
# perfom the find
pager.replace find_by_sql(query)
unless pager.total_entries
count_query = original_query.sub /\bORDER\s+BY\s+[\w`,\s]+$/mi, ''
count_query = "SELECT COUNT(*) FROM (#{count_query})"
- unless ['oracle', 'oci'].include?(self.connection.adapter_name.downcase)
+ unless self.connection.adapter_name =~ /^(oracle|oci$)/i
count_query << ' AS count_table'
end
# perform the count query
pager.total_entries = count_by_sql(count_query)
end
@@ -156,14 +159,18 @@
end
end
protected
- def method_missing_with_paginate(method, *args, &block) #:nodoc:
+ def method_missing_with_paginate(method, *args) #:nodoc:
# did somebody tried to paginate? if not, let them be
unless method.to_s.index('paginate') == 0
- return method_missing_without_paginate(method, *args, &block)
+ if block_given?
+ return method_missing_without_paginate(method, *args) { |*a| yield(*a) }
+ else
+ return method_missing_without_paginate(method, *args)
+ end
end
# paginate finders are really just find_* with limit and offset
finder = method.to_s.sub('paginate', 'find')
finder.sub!('find', 'find_all') if finder.index('find_by_') == 0
@@ -172,39 +179,57 @@
raise ArgumentError, 'parameter hash expected' unless options.respond_to? :symbolize_keys
options = options.dup
options[:finder] = finder
args << options
- paginate(*args, &block)
+ paginate(*args) { |*a| yield(*a) if block_given? }
end
# Does the not-so-trivial job of finding out the total number of entries
# in the database. It relies on the ActiveRecord +count+ method.
def wp_count(options, args, finder)
excludees = [:count, :order, :limit, :offset, :readonly]
- unless options[:select] and options[:select] =~ /^\s*DISTINCT\b/i
+ excludees << :from unless ActiveRecord::Calculations::CALCULATIONS_OPTIONS.include?(:from)
+
+ # we may be in a model or an association proxy
+ klass = (@owner and @reflection) ? @reflection.klass : self
+
+ # Use :select from scope if it isn't already present.
+ options[:select] = scope(:find, :select) unless options[:select]
+
+ if options[:select] and options[:select] =~ /^\s*DISTINCT\b/i
+ # Remove quoting and check for table_name.*-like statement.
+ if options[:select].gsub('`', '') =~ /\w+\.\*/
+ options[:select] = "DISTINCT #{klass.table_name}.#{klass.primary_key}"
+ end
+ else
excludees << :select # only exclude the select param if it doesn't begin with DISTINCT
end
+
# count expects (almost) the same options as find
count_options = options.except *excludees
# merge the hash found in :count
# this allows you to specify :select, :order, or anything else just for the count query
count_options.update options[:count] if options[:count]
+ # forget about includes if they are irrelevant (Rails 2.1)
+ if count_options[:include] and
+ klass.private_methods.include_method?(:references_eager_loaded_tables?) and
+ !klass.send(:references_eager_loaded_tables?, count_options)
+ count_options.delete :include
+ end
+
# we may have to scope ...
counter = Proc.new { count(count_options) }
- # we may be in a model or an association proxy!
- klass = (@owner and @reflection) ? @reflection.klass : self
-
count = if finder.index('find_') == 0 and klass.respond_to?(scoper = finder.sub('find', 'with'))
# scope_out adds a 'with_finder' method which acts like with_scope, if it's present
# then execute the count with the scoping provided by the with_finder
send(scoper, &counter)
- elsif match = /^find_(all_by|by)_([_a-zA-Z]\w*)$/.match(finder)
+ elsif finder =~ /^find_(all_by|by)_([_a-zA-Z]\w*)$/
# extract conditions from calls like "paginate_by_foo_and_bar"
- attribute_names = extract_attribute_names_from_match(match)
+ attribute_names = $2.split('_and_')
conditions = construct_attributes_from_arguments(attribute_names, args)
with_scope(:find => { :conditions => conditions }, &counter)
else
counter.call
end