diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2010-08-23 14:44:10 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2010-08-23 14:44:10 -0700 |
commit | c151df07acc06b06179c2dc6205db029ff9ce001 (patch) | |
tree | 24717fd5502ac9fa2634df8694e04aa800d3e287 | |
parent | 3bc3b145aedea216eb84e213bac1017c6090d42b (diff) | |
download | rails-c151df07acc06b06179c2dc6205db029ff9ce001.tar.gz rails-c151df07acc06b06179c2dc6205db029ff9ce001.tar.bz2 rails-c151df07acc06b06179c2dc6205db029ff9ce001.zip |
AND nodes are supported
-rw-r--r-- | lib/arel/nodes.rb | 1 | ||||
-rw-r--r-- | lib/arel/nodes/and.rb | 6 | ||||
-rw-r--r-- | lib/arel/nodes/binary.rb | 4 | ||||
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 4 | ||||
-rw-r--r-- | spec/arel/nodes/equality_spec.rb | 11 | ||||
-rw-r--r-- | spec/arel/visitors/to_sql_spec.rb | 7 |
6 files changed, 33 insertions, 0 deletions
diff --git a/lib/arel/nodes.rb b/lib/arel/nodes.rb index 98fc045690..f2332694a8 100644 --- a/lib/arel/nodes.rb +++ b/lib/arel/nodes.rb @@ -1,6 +1,7 @@ require 'arel/nodes/binary' require 'arel/nodes/equality' require 'arel/nodes/or' +require 'arel/nodes/and' require 'arel/nodes/in' require 'arel/nodes/count' diff --git a/lib/arel/nodes/and.rb b/lib/arel/nodes/and.rb new file mode 100644 index 0000000000..80f420b4f1 --- /dev/null +++ b/lib/arel/nodes/and.rb @@ -0,0 +1,6 @@ +module Arel + module Nodes + class And < Arel::Nodes::Binary + end + end +end diff --git a/lib/arel/nodes/binary.rb b/lib/arel/nodes/binary.rb index fdfcab2d3f..3cd9583e79 100644 --- a/lib/arel/nodes/binary.rb +++ b/lib/arel/nodes/binary.rb @@ -12,6 +12,10 @@ module Arel Nodes::Or.new self, right end + def and right + Nodes::And.new self, right + end + # FIXME: this method should go away. I don't like people calling # to_sql on non-head nodes. This forces us to walk the AST until we # can find a node that has a "relation" member. diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 6576d82c41..8136039040 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -86,6 +86,10 @@ module Arel "#{visit o.left} IN (#{o.right.map { |x| visit x }.join ', '})" end + def visit_Arel_Nodes_And o + "#{visit o.left} AND #{visit o.right}" + end + def visit_Arel_Nodes_Or o "#{visit o.left} OR #{visit o.right}" end diff --git a/spec/arel/nodes/equality_spec.rb b/spec/arel/nodes/equality_spec.rb index b74af3a039..d91fa0df03 100644 --- a/spec/arel/nodes/equality_spec.rb +++ b/spec/arel/nodes/equality_spec.rb @@ -11,6 +11,17 @@ module Arel check node.right.should == right end end + + describe 'and' do + it 'makes and AND node' do + attr = Table.new(:users)[:id] + left = attr.eq(10) + right = attr.eq(11) + node = left.and right + check node.left.should == left + check node.right.should == right + end + end end end end diff --git a/spec/arel/visitors/to_sql_spec.rb b/spec/arel/visitors/to_sql_spec.rb index b70c392630..9807c89b3a 100644 --- a/spec/arel/visitors/to_sql_spec.rb +++ b/spec/arel/visitors/to_sql_spec.rb @@ -8,6 +8,13 @@ module Arel @attr = Table.new(:users)[:id] end + it "should visit_Arel_Nodes_And" do + node = Nodes::And.new @attr.eq(10), @attr.eq(11) + @visitor.accept(node).should be_like %{ + "users"."id" = 10 AND "users"."id" = 11 + } + end + it "should visit_Arel_Nodes_Or" do node = Nodes::Or.new @attr.eq(10), @attr.eq(11) @visitor.accept(node).should be_like %{ |