Skip to content

Commit

Permalink
Merge pull request #48873 from drnic/arel-cast-function
Browse files Browse the repository at this point in the history
Adds Arel::FactoryMethods#cast(node, type)
  • Loading branch information
rafaelfranca committed Aug 4, 2023
2 parents 83e5988 + b4a594d commit 2e3df04
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions activerecord/lib/arel/factory_methods.rb
Expand Up @@ -45,5 +45,9 @@ def lower(column)
def coalesce(*exprs)
Nodes::NamedFunction.new "COALESCE", exprs
end

def cast(name, type)
Nodes::NamedFunction.new "CAST", [name.as(type)]
end
end
end
12 changes: 12 additions & 0 deletions activerecord/test/cases/arel/factory_methods_test.rb
Expand Up @@ -41,6 +41,18 @@ def test_lower
assert_equal "LOWER", lower.name
assert_equal [:one], lower.expressions.map(&:expr)
end

def test_cast
relation = Table.new(:users)
field_node = relation[:active]
cast = @factory.cast field_node, "boolean"
assert_instance_of Nodes::NamedFunction, cast
assert_equal "CAST", cast.name
as_node = cast.expressions.first
assert_instance_of Nodes::As, as_node
assert_equal field_node, as_node.left
assert_equal "boolean", as_node.right
end
end
end
end

0 comments on commit 2e3df04

Please sign in to comment.