aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorErnie Miller <ernie@metautonomo.us>2011-04-29 13:36:22 -0400
committerAaron Patterson <aaron.patterson@gmail.com>2011-04-29 11:13:41 -0700
commit7361b6cbd5e4c6f03198ed42eed4e3dd4feb149e (patch)
treefdc15b45bcc57407e7192ba2e9db537ad871b94e
parentd09a882329e8eeebac89e907289c537077f81d44 (diff)
downloadrails-7361b6cbd5e4c6f03198ed42eed4e3dd4feb149e.tar.gz
rails-7361b6cbd5e4c6f03198ed42eed4e3dd4feb149e.tar.bz2
rails-7361b6cbd5e4c6f03198ed42eed4e3dd4feb149e.zip
Move #as to AliasPredication, stop overriding Function's #as.
-rw-r--r--lib/arel.rb1
-rw-r--r--lib/arel/alias_predication.rb7
-rw-r--r--lib/arel/attributes/attribute.rb1
-rw-r--r--lib/arel/nodes/function.rb4
-rw-r--r--lib/arel/nodes/infix_operation.rb1
-rw-r--r--lib/arel/nodes/sql_literal.rb1
-rw-r--r--lib/arel/predications.rb4
-rw-r--r--test/nodes/test_named_function.rb17
-rw-r--r--test/visitors/test_depth_first.rb6
9 files changed, 33 insertions, 9 deletions
diff --git a/lib/arel.rb b/lib/arel.rb
index bc599514e2..ff43de2067 100644
--- a/lib/arel.rb
+++ b/lib/arel.rb
@@ -4,6 +4,7 @@ require 'arel/factory_methods'
require 'arel/expressions'
require 'arel/predications'
require 'arel/math'
+require 'arel/alias_predication'
require 'arel/order_predications'
require 'arel/table'
require 'arel/attributes'
diff --git a/lib/arel/alias_predication.rb b/lib/arel/alias_predication.rb
new file mode 100644
index 0000000000..3f3db7671e
--- /dev/null
+++ b/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 \ No newline at end of file
diff --git a/lib/arel/attributes/attribute.rb b/lib/arel/attributes/attribute.rb
index b9fd8da67e..0906fa4f1d 100644
--- a/lib/arel/attributes/attribute.rb
+++ b/lib/arel/attributes/attribute.rb
@@ -3,6 +3,7 @@ module Arel
class Attribute < Struct.new :relation, :name
include Arel::Expressions
include Arel::Predications
+ include Arel::AliasPredication
include Arel::OrderPredications
include Arel::Math
diff --git a/lib/arel/nodes/function.rb b/lib/arel/nodes/function.rb
index b58eba7f38..3263ff9cd4 100644
--- a/lib/arel/nodes/function.rb
+++ b/lib/arel/nodes/function.rb
@@ -6,12 +6,12 @@ module Arel
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
diff --git a/lib/arel/nodes/infix_operation.rb b/lib/arel/nodes/infix_operation.rb
index 6847650fe4..75879ce43f 100644
--- a/lib/arel/nodes/infix_operation.rb
+++ b/lib/arel/nodes/infix_operation.rb
@@ -4,6 +4,7 @@ module Arel
class InfixOperation < Binary
include Arel::Expressions
include Arel::Predications
+ include Arel::AliasPredication
include Arel::Math
attr_reader :operator
diff --git a/lib/arel/nodes/sql_literal.rb b/lib/arel/nodes/sql_literal.rb
index ad0bb00484..2e934b2a1b 100644
--- a/lib/arel/nodes/sql_literal.rb
+++ b/lib/arel/nodes/sql_literal.rb
@@ -3,6 +3,7 @@ module Arel
class SqlLiteral < String
include Arel::Expressions
include Arel::Predications
+ include Arel::AliasPredication
include Arel::OrderPredications
end
end
diff --git a/lib/arel/predications.rb b/lib/arel/predications.rb
index 08cbf16d9d..4b124ce05c 100644
--- a/lib/arel/predications.rb
+++ b/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
diff --git a/test/nodes/test_named_function.rb b/test/nodes/test_named_function.rb
index 18ecdd2851..3e0b2c3972 100644
--- a/test/nodes/test_named_function.rb
+++ b/test/nodes/test_named_function.rb
@@ -8,6 +8,23 @@ module Arel
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
diff --git a/test/visitors/test_depth_first.rb b/test/visitors/test_depth_first.rb
index 06e7bba9fb..e078d4add5 100644
--- a/test/visitors/test_depth_first.rb
+++ b/test/visitors/test_depth_first.rb
@@ -52,14 +52,14 @@ module Arel
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 @@ module Arel
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