diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2010-12-09 14:48:28 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2010-12-09 14:48:28 -0800 |
commit | 11e29a32af658ee0956b1237fea1033698a457e8 (patch) | |
tree | e1a9b562739c62c20f22d2e74d86f6d18d717072 /lib | |
parent | 99774f8ed3c4f356d318b38ade568ae0d9456de7 (diff) | |
download | rails-11e29a32af658ee0956b1237fea1033698a457e8.tar.gz rails-11e29a32af658ee0956b1237fea1033698a457e8.tar.bz2 rails-11e29a32af658ee0956b1237fea1033698a457e8.zip |
AND nodes are now n-ary
Diffstat (limited to 'lib')
-rw-r--r-- | lib/arel/nodes/and.rb | 17 | ||||
-rw-r--r-- | lib/arel/nodes/node.rb | 2 | ||||
-rw-r--r-- | lib/arel/select_manager.rb | 14 | ||||
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 2 |
4 files changed, 25 insertions, 10 deletions
diff --git a/lib/arel/nodes/and.rb b/lib/arel/nodes/and.rb index 80f420b4f1..b087a10930 100644 --- a/lib/arel/nodes/and.rb +++ b/lib/arel/nodes/and.rb @@ -1,6 +1,23 @@ module Arel module Nodes class And < Arel::Nodes::Binary + attr_reader :children + + def initialize children, right = nil + unless Array === children + warn "(#{caller.first}) AND nodes should be created with a list" + children = [children, right] + end + @children = children + end + + def left + children.first + end + + def right + children[1] + end end end end diff --git a/lib/arel/nodes/node.rb b/lib/arel/nodes/node.rb index 567221aab2..ff788e90d2 100644 --- a/lib/arel/nodes/node.rb +++ b/lib/arel/nodes/node.rb @@ -22,7 +22,7 @@ module Arel ### # Factory method to create an Nodes::And node. def and right - Nodes::And.new self, right + Nodes::And.new [self, right] end # FIXME: this method should go away. I don't like people calling diff --git a/lib/arel/select_manager.rb b/lib/arel/select_manager.rb index 30495fc8d0..40e197910b 100644 --- a/lib/arel/select_manager.rb +++ b/lib/arel/select_manager.rb @@ -198,14 +198,12 @@ module Arel def collapse exprs return exprs.first if exprs.length == 1 - right = exprs.pop - left = exprs.pop - - right = Nodes::SqlLiteral.new(right) if String === right - - right = Nodes::And.new left, right - exprs.reverse.inject(right) { |memo,expr| - Nodes::And.new(expr, memo) + Nodes::And.new exprs.compact.map { |expr| + if String === expr + Arel.sql(expr) + else + expr + end } end end diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 308a5bf400..5767fbae8d 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -254,7 +254,7 @@ module Arel end def visit_Arel_Nodes_And o - "#{visit o.left} AND #{visit o.right}" + o.children.map { |x| visit x }.join ' AND ' end def visit_Arel_Nodes_Or o |