def wp_count(options, args, finder)
excludees = [:count, :order, :limit, :offset, :readonly]
excludees << :from unless ActiveRecord::Calculations::CALCULATIONS_OPTIONS.include?(:from)
klass = (@owner and @reflection) ? @reflection.klass : self
options[:select] = scope(:find, :select) unless options[:select]
if options[:select] and options[:select] =~ /^\s*DISTINCT\b/i
if options[:select].gsub(/[`"]/, '') =~ /\w+\.\*/
options[:select] = "DISTINCT #{klass.table_name}.#{klass.primary_key}"
end
else
excludees << :select
end
count_options = options.except *excludees
count_options.update options[:count] if options[:count]
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
counter = Proc.new { count(count_options) }
count = if finder.index('find_') == 0 and klass.respond_to?(scoper = finder.sub('find', 'with'))
send(scoper, &counter)
elsif finder =~ /^find_(all_by|by)_([_a-zA-Z]\w*)$/
attribute_names = $2.split('_and_')
conditions = construct_attributes_from_arguments(attribute_names, args)
with_scope(:find => { :conditions => conditions }, &counter)
else
counter.call
end
(!count.is_a?(Integer) && count.respond_to?(:length)) ? count.length : count
end