Repository: rails/rails
File: activerecord/lib/active_record/relation/query_methods.rb (Ruby)

Code before:
    end

    protected
      def build_subquery(subquery_alias, select_value) # :nodoc:
        subquery = except(:optimizer_hints).arel.as(subquery_alias)

        Arel::SelectManager.new(subquery).project(select_value).tap do |arel|
          arel.optimizer_hints(*optimizer_hints_values) unless optimizer_hints_values.empty?
        end
      end

      def build_where_clause(opts, rest = []) # :nodoc:
        opts = sanitize_forbidden_attributes(opts)

        if opts.is_a?(Array)
          opts, *rest = opts
        end

        case opts
        when String
          if rest.empty?
            if Arel.arel_node?(opts)
              parts = [opts]
            else
              parts = [Arel.sql(opts)]
            end
          elsif rest.first.is_a?(Hash) && /:\w+/.match?(opts)
            parts = [build_named_bound_sql_literal(opts, rest.first)]
          elsif opts.include?("?")
            parts = [build_bound_sql_literal(opts, rest)]
          else
            parts = [Arel.sql(model.sanitize_sql([opts, *rest]))]
          end
        when Hash
          opts = opts.transform_keys do |key|
            if key.is_a?(Array)
              key.map { |k| model.attribute_aliases[k.to_s] || k.to_s }
            else
              key = key.to_s
              model.attribute_aliases[key] || key
            end
          end
          references = PredicateBuilder.references(opts)
          self.references_values |= references unless references.empty?

          parts = predicate_builder.build_from_hash(opts) do |table_name|
            lookup_table_klass_from_join_dependencies(table_name)
          end
        when Arel::Nodes::Node
          parts = [opts]
        else

Review comment (suggestion):
Shouldn't we just do that in `Arel.sql`? Right now it assumes it always receives a `String`, hence always wrap it, but we could make it return arel nodes unchanged.

Code after:
    def construct_join_dependency(associations, join_type) # :nodoc:
      ActiveRecord::Associations::JoinDependency.new(
        model, table, associations, join_type
      )
    end

    protected
      def build_subquery(subquery_alias, select_value) # :nodoc:
        subquery = except(:optimizer_hints).arel.as(subquery_alias)

        Arel::SelectManager.new(subquery).project(select_value).tap do |arel|
          arel.optimizer_hints(*optimizer_hints_values) unless optimizer_hints_values.empty?
        end
      end

      def build_where_clause(opts, rest = []) # :nodoc:
        opts = sanitize_forbidden_attributes(opts)

        if opts.is_a?(Array)
          opts, *rest = opts
        end

        case opts
        when String
          if rest.empty?
            parts = [Arel.sql(opts)]
          elsif rest.first.is_a?(Hash) && /:\w+/.match?(opts)
            parts = [build_named_bound_sql_literal(opts, rest.first)]
          elsif opts.include?("?")
            parts = [build_bound_sql_literal(opts, rest)]
          else
            parts = [Arel.sql(model.sanitize_sql([opts, *rest]))]
          end
        when Hash
          opts = opts.transform_keys do |key|
            if key.is_a?(Array)
              key.map { |k| model.attribute_aliases[k.to_s] || k.to_s }
            else
              key = key.to_s
              model.attribute_aliases[key] || key
            end
          end
          references = PredicateBuilder.references(opts)
          self.references_values |= references unless references.empty?

          parts = predicate_builder.build_from_hash(opts) do |table_name|
            lookup_table_klass_from_join_dependencies(table_name)
          end
        when Arel::Nodes::Node
          parts = [opts]
        else