Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions activerecord/lib/arel/nodes/homogeneous_in.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@ def right
attribute.quoted_array(values)
end

def table_name
attribute.relation.table_alias || attribute.relation.name
end

def column_name
attribute.name
end

def casted_values
type = attribute.type_caster

Expand Down
3 changes: 1 addition & 2 deletions activerecord/lib/arel/visitors/to_sql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def visit_Arel_Nodes_Grouping(o, collector)
def visit_Arel_Nodes_HomogeneousIn(o, collector)
collector.preparable = false

collector << quote_table_name(o.table_name) << "." << quote_column_name(o.column_name)
visit o.left, collector
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you write a failing test for the behavior you're changing?

I'm not quite following the code example or the changes in the linked PR. Generally Arel is considered a private API so understanding what's broken will inform us whether we want to accept a change to this internal implementation.


if o.type == :in
collector << " IN ("
Expand All @@ -350,7 +350,6 @@ def visit_Arel_Nodes_HomogeneousIn(o, collector)
end

collector << ")"
collector
end

def visit_Arel_SelectManager(o, collector)
Expand Down
52 changes: 52 additions & 0 deletions activerecord/test/cases/arel/nodes/homogeneous_in_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

require_relative "../helper"
require "active_record/type_caster/map"
require "active_model"
class Arel::Nodes::HomogeneousInTest < Arel::Spec
def test_in
table = Arel::Table.new :users, type_caster: fake_pg_caster

expr = Arel::Nodes::HomogeneousIn.new(["Bobby", "Robert"], table[:name], :in)

_(expr.to_sql).must_be_like %{
"users"."name" IN (?, ?)
}
end

def test_custom_attribute_node
table = Arel::Table.new :users, type_caster: fake_pg_caster

node = TypedNode.new("COALESCE",
[table[:nickname], table[:name]],
STRING_TYPE
)
expr = Arel::Nodes::HomogeneousIn.new(["Bobby", "Robert"], node, :in)

_(expr.to_sql).must_be_like %{
COALESCE("users"."nickname", "users"."name") IN (?, ?)
}
end

private
STRING_TYPE = ActiveModel::Type::String.new.freeze

# this is a named function that also has a data type
class TypedNode < Arel::Nodes::NamedFunction
attr_reader :type_caster

def initialize(name, expr, type)
super(name, expr, nil)
@type_caster = type
end
end

# map that converts attribute names to a caster
def fake_pg_caster
Object.new.tap do |caster|
def caster.type_for_attribute(attr_name)
STRING_TYPE
end
end
end
end