Skip to content

Commit 5cc7e77

Browse files
authored
Merge pull request #500 from jcoleman/fix_incorrect_typecasting_of_raw_sql_strings
Type-castable attributes should not try to cast SqlLiteral nodes
2 parents 1ede2ad + b416fca commit 5cc7e77

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/arel/nodes/casted.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def nil?; val.nil?; end
3030

3131
def self.build_quoted other, attribute = nil
3232
case other
33-
when Arel::Nodes::Node, Arel::Attributes::Attribute, Arel::Table, Arel::Nodes::BindParam, Arel::SelectManager, Arel::Nodes::Quoted
33+
when Arel::Nodes::Node, Arel::Attributes::Attribute, Arel::Table, Arel::Nodes::BindParam, Arel::SelectManager, Arel::Nodes::Quoted, Arel::Nodes::SqlLiteral
3434
other
3535
else
3636
case attribute

test/attributes/test_attribute.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,18 @@ def fake_caster.type_cast_for_database(attr_name, value)
997997
assert table.able_to_type_cast?
998998
condition.to_sql.must_equal %("foo"."id" = 1 AND "foo"."other_id" = '2')
999999
end
1000+
1001+
it 'does not type cast SqlLiteral nodes' do
1002+
fake_caster = Object.new
1003+
def fake_caster.type_cast_for_database(attr_name, value)
1004+
value.to_i
1005+
end
1006+
table = Table.new(:foo, type_caster: fake_caster)
1007+
condition = table["id"].eq(Arel.sql("(select 1)"))
1008+
1009+
assert table.able_to_type_cast?
1010+
condition.to_sql.must_equal %("foo"."id" = (select 1))
1011+
end
10001012
end
10011013
end
10021014
end

0 commit comments

Comments
 (0)