mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Get rid of construct_count_options_from_args
This commit is contained in:
@@ -176,14 +176,15 @@ module ActiveRecord
|
||||
# be used for the query. If no +:counter_sql+ was supplied, but +:finder_sql+ was set, the
|
||||
# descendant's +construct_sql+ method will have set :counter_sql automatically.
|
||||
# Otherwise, construct options and pass them with scope to the target class's +count+.
|
||||
def count(*args)
|
||||
def count(column_name = nil, options = {})
|
||||
if @reflection.options[:counter_sql]
|
||||
@reflection.klass.count_by_sql(@counter_sql)
|
||||
else
|
||||
column_name, options = @reflection.klass.scoped.send(:construct_count_options_from_args, *args)
|
||||
column_name, options = nil, column_name if column_name.is_a?(Hash)
|
||||
|
||||
if @reflection.options[:uniq]
|
||||
# This is needed because 'SELECT count(DISTINCT *)..' is not valid SQL.
|
||||
column_name = "#{@reflection.quoted_table_name}.#{@reflection.klass.primary_key}" if column_name == :all
|
||||
column_name = "#{@reflection.quoted_table_name}.#{@reflection.klass.primary_key}" unless column_name
|
||||
options.merge!(:distinct => true)
|
||||
end
|
||||
|
||||
|
||||
@@ -180,32 +180,6 @@ module ActiveRecord
|
||||
end
|
||||
end
|
||||
|
||||
def construct_count_options_from_args(*args)
|
||||
options = {}
|
||||
column_name = :all
|
||||
|
||||
# Handles count(), count(:column), count(:distinct => true), count(:column, :distinct => true)
|
||||
case args.size
|
||||
when 0
|
||||
select = get_projection_name_from_chained_relations
|
||||
column_name = select if select !~ /(,|\*)/
|
||||
when 1
|
||||
if args[0].is_a?(Hash)
|
||||
select = get_projection_name_from_chained_relations
|
||||
column_name = select if select !~ /(,|\*)/
|
||||
options = args[0]
|
||||
else
|
||||
column_name = args[0]
|
||||
end
|
||||
when 2
|
||||
column_name, options = args
|
||||
else
|
||||
raise ArgumentError, "Unexpected parameters passed to count(): #{args.inspect}"
|
||||
end
|
||||
|
||||
[column_name || :all, options]
|
||||
end
|
||||
|
||||
# Converts the given keys to the value that the database adapter returns as
|
||||
# a usable column name:
|
||||
#
|
||||
@@ -243,10 +217,6 @@ module ActiveRecord
|
||||
column ? column.type_cast(value) : value
|
||||
end
|
||||
|
||||
def get_projection_name_from_chained_relations
|
||||
@select_values.join(", ") if @select_values.present?
|
||||
end
|
||||
|
||||
def select_for_count
|
||||
if @select_values.present?
|
||||
select = @select_values.join(", ")
|
||||
|
||||
Reference in New Issue
Block a user