Skip to content

Commit

Permalink
Refactor arel_column not to require block
Browse files Browse the repository at this point in the history
  • Loading branch information
kamipo committed Sep 28, 2024
1 parent 615f049 commit ba468db
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 37 deletions.
36 changes: 12 additions & 24 deletions activerecord/lib/active_record/relation/calculations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -447,10 +447,13 @@ def distinct_select?(column_name)
end

def aggregate_column(column_name)
return column_name if Arel::Expressions === column_name

arel_column(column_name.to_s) do |name|
column_name == :all ? Arel.sql("*", retryable: true) : Arel.sql(name)
case column_name
when Arel::Expressions
column_name
when :all
Arel.star
else
arel_column(column_name)
end
end

Expand Down Expand Up @@ -630,27 +633,12 @@ def type_cast_calculated_value(value, operation, type)
end

def select_for_count
if select_values.present?
return select_values.first if select_values.one?

adapter_class = model.adapter_class
select_values.map do |field|
column = if Arel.arel_node?(field)
field
else
arel_column(field.to_s) do |attr_name|
Arel.sql(attr_name)
end
end

if column.is_a?(Arel::Nodes::SqlLiteral)
column
else
"#{adapter_class.quote_table_name(column.relation.name)}.#{adapter_class.quote_column_name(column.name)}"
end
end.join(", ")
else
if select_values.empty?
:all
else
with_connection do |conn|
arel_columns(select_values).map { |column| conn.visitor.compile(column) }.join(", ")
end
end
end

Expand Down
22 changes: 9 additions & 13 deletions activerecord/lib/active_record/relation/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1932,12 +1932,8 @@ def build_with_join_node(name, kind = Arel::Nodes::InnerJoin)
def arel_columns(columns)
columns.flat_map do |field|
case field
when Symbol
arel_column(field.to_s) do |attr_name|
model.adapter_class.quote_table_name(attr_name)
end
when String
arel_column(field, &:itself)
when Symbol, String
arel_column(field)
when Proc
field.call
when Hash
Expand All @@ -1949,6 +1945,8 @@ def arel_columns(columns)
end

def arel_column(field)
field = field.name if is_symbol = field.is_a?(Symbol)

field = model.attribute_aliases[field] || field
from = from_clause.name || from_clause.value

Expand All @@ -1959,8 +1957,10 @@ def arel_column(field)
predicate_builder.resolve_arel_attribute(table, column) do
lookup_table_klass_from_join_dependencies(table)
end
else
elsif block_given?
yield field
else
Arel.sql(is_symbol ? model.adapter_class.quote_table_name(field) : field)
end
end

Expand Down Expand Up @@ -2194,18 +2194,14 @@ def arel_columns_from_hash(fields)
case columns_aliases
when Hash
columns_aliases.map do |column, column_alias|
arel_column("#{key}.#{column}") do
predicate_builder.resolve_arel_attribute(key.to_s, column)
end.as(column_alias.to_s)
arel_column("#{key}.#{column}").as(column_alias.to_s)
end
when Array
columns_aliases.map do |column|
arel_column("#{key}.#{column}", &:itself)
end
when String, Symbol
arel_column(key.to_s) do
predicate_builder.resolve_arel_attribute(model.table_name, key.to_s)
end.as(columns_aliases.to_s)
arel_column(key).as(columns_aliases.to_s)
end
end
end
Expand Down

0 comments on commit ba468db

Please sign in to comment.