-
Notifications
You must be signed in to change notification settings - Fork 21.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45642 from kbrock/quoted_columns
[ActiveRecord] ToSql#HomogeneousIn uses visitor pattern to produce Attribute sql
- Loading branch information
Showing
3 changed files
with
53 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |