Skip to content
Browse files

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

  • Loading branch information...
1 parent d09a882 commit 7361b6cbd5e4c6f03198ed42eed4e3dd4feb149e @ernie ernie committed with tenderlove Apr 29, 2011
View
1 lib/arel.rb
@@ -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'
View
7 lib/arel/alias_predication.rb
@@ -0,0 +1,7 @@
+module Arel
+ module AliasPredication
+ def as other
+ Nodes::As.new self, Nodes::SqlLiteral.new(other.to_s)
+ end
+ end
+end
View
1 lib/arel/attributes/attribute.rb
@@ -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
View
4 lib/arel/nodes/function.rb
@@ -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
View
1 lib/arel/nodes/infix_operation.rb
@@ -4,6 +4,7 @@ module Nodes
class InfixOperation < Binary
include Arel::Expressions
include Arel::Predications
+ include Arel::AliasPredication
include Arel::Math
attr_reader :operator
View
1 lib/arel/nodes/sql_literal.rb
@@ -3,6 +3,7 @@ module Nodes
class SqlLiteral < String
include Arel::Expressions
include Arel::Predications
+ include Arel::AliasPredication
include Arel::OrderPredications
end
end
View
4 lib/arel/predications.rb
@@ -1,10 +1,6 @@
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
View
17 test/nodes/test_named_function.rb
@@ -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
View
6 test/visitors/test_depth_first.rb
@@ -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

0 comments on commit 7361b6c

Please sign in to comment.
Something went wrong with that request. Please try again.