提交 7361b6cb 编写于 作者: E Ernie Miller 提交者: Aaron Patterson

Move #as to AliasPredication, stop overriding Function's #as.

上级 d09a8823
......@@ -4,6 +4,7 @@
require 'arel/expressions'
require 'arel/predications'
require 'arel/math'
require 'arel/alias_predication'
require 'arel/order_predications'
require 'arel/table'
require 'arel/attributes'
......
module Arel
module AliasPredication
def as other
Nodes::As.new self, Nodes::SqlLiteral.new(other.to_s)
end
end
end
\ No newline at end of file
......@@ -3,6 +3,7 @@ module Attributes
class Attribute < Struct.new :relation, :name
include Arel::Expressions
include Arel::Predications
include Arel::AliasPredication
include Arel::OrderPredications
include Arel::Math
......
......@@ -6,12 +6,12 @@ class Function < Arel::Nodes::Node
def initialize expr, aliaz = nil
@expressions = expr
@alias = aliaz
@alias = aliaz && SqlLiteral.new(aliaz.to_s)
@distinct = false
end
def as aliaz
self.alias = SqlLiteral.new(aliaz)
self.alias = SqlLiteral.new(aliaz.to_s)
self
end
end
......
......@@ -4,6 +4,7 @@ module Nodes
class InfixOperation < Binary
include Arel::Expressions
include Arel::Predications
include Arel::AliasPredication
include Arel::Math
attr_reader :operator
......
......@@ -3,6 +3,7 @@ module Nodes
class SqlLiteral < String
include Arel::Expressions
include Arel::Predications
include Arel::AliasPredication
include Arel::OrderPredications
end
end
......
module Arel
module Predications
def as other
Nodes::As.new self, Nodes::SqlLiteral.new(other)
end
def not_eq other
Nodes::NotEqual.new self, other
end
......
......@@ -8,6 +8,23 @@ def test_construct
assert_equal 'omg', function.name
assert_equal 'zomg', function.expressions
end
def test_function_alias
function = NamedFunction.new 'omg', 'zomg'
function = function.as('wth')
assert_equal 'omg', function.name
assert_equal 'zomg', function.expressions
assert_kind_of SqlLiteral, function.alias
assert_equal 'wth', function.alias
end
def test_construct_with_alias
function = NamedFunction.new 'omg', 'zomg', 'wth'
assert_equal 'omg', function.name
assert_equal 'zomg', function.expressions
assert_kind_of SqlLiteral, function.alias
assert_equal 'wth', function.alias
end
end
end
end
......@@ -52,14 +52,14 @@ def test_raises_with_object
define_method("test_#{klass.name.gsub('::', '_')}") do
func = klass.new(:a, :b)
@visitor.accept func
assert_equal [:a, :b, false, func], @collector.calls
assert_equal [:a, "b", false, func], @collector.calls
end
end
def test_named_function
func = Arel::Nodes::NamedFunction.new(:a, :b, :c)
@visitor.accept func
assert_equal [:a, :b, false, :c, func], @collector.calls
assert_equal [:a, :b, false, "c", func], @collector.calls
end
def test_lock
......@@ -71,7 +71,7 @@ def test_lock
def test_count
count = Nodes::Count.new :a, :b, :c
@visitor.accept count
assert_equal [:a, :c, :b, count], @collector.calls
assert_equal [:a, "c", :b, count], @collector.calls
end
def test_inner_join
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册