From ba468db0bdc880c694df091b5800d114e963eff0 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Sun, 29 Sep 2024 04:57:42 +0900 Subject: [PATCH] Refactor `arel_column` not to require block --- .../active_record/relation/calculations.rb | 36 +++++++------------ .../active_record/relation/query_methods.rb | 22 +++++------- 2 files changed, 21 insertions(+), 37 deletions(-) diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index 43a2d990c11bf..ed2b99a7786d2 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -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 @@ -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 diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 01e0dd3d0e637..f27d153e66f7c 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -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 @@ -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 @@ -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 @@ -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