aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel
diff options
context:
space:
mode:
authorDaniel Cadenas <dcadenas@gmail.com>2011-05-31 00:40:11 -0300
committerDaniel Cadenas <dcadenas@gmail.com>2011-08-03 20:50:03 -0300
commit11f929b5c485adab60ea2d8b515ef2abcf5400f4 (patch)
tree911f4e22c86b8098d218939fbfadb992a0eaaa1d /lib/arel
parentc9517c1b62e7d8acd5a45f72b148942ff2f549a7 (diff)
downloadrails-11f929b5c485adab60ea2d8b515ef2abcf5400f4.tar.gz
rails-11f929b5c485adab60ea2d8b515ef2abcf5400f4.tar.bz2
rails-11f929b5c485adab60ea2d8b515ef2abcf5400f4.zip
Add nodes for boolean constants
This is useful for dynamically created predicates e.g: expr1 = table.create_false expr2 = table.create_false expr1 = create_a_predicate() if some_condition expr2 = create_another_predicate() if some_other_condition table.where(expr1.and(expr2))
Diffstat (limited to 'lib/arel')
-rw-r--r--lib/arel/factory_methods.rb8
-rw-r--r--lib/arel/nodes.rb2
-rw-r--r--lib/arel/nodes/false.rb17
-rw-r--r--lib/arel/nodes/true.rb17
-rw-r--r--lib/arel/predications.rb1
-rw-r--r--lib/arel/visitors/to_sql.rb8
6 files changed, 52 insertions, 1 deletions
diff --git a/lib/arel/factory_methods.rb b/lib/arel/factory_methods.rb
index 9fd0878ada..3b16feae10 100644
--- a/lib/arel/factory_methods.rb
+++ b/lib/arel/factory_methods.rb
@@ -2,6 +2,14 @@ module Arel
###
# Methods for creating various nodes
module FactoryMethods
+ def create_true
+ Arel::Nodes::True.new
+ end
+
+ def create_false
+ Arel::Nodes::False.new
+ end
+
def create_table_alias relation, name
Nodes::TableAlias.new(relation, name)
end
diff --git a/lib/arel/nodes.rb b/lib/arel/nodes.rb
index 9edf3a9a95..e37b09a8ac 100644
--- a/lib/arel/nodes.rb
+++ b/lib/arel/nodes.rb
@@ -8,6 +8,8 @@ require 'arel/nodes/update_statement'
# terminal
require 'arel/nodes/terminal'
+require 'arel/nodes/true'
+require 'arel/nodes/false'
# unary
require 'arel/nodes/unary'
diff --git a/lib/arel/nodes/false.rb b/lib/arel/nodes/false.rb
new file mode 100644
index 0000000000..4196070946
--- /dev/null
+++ b/lib/arel/nodes/false.rb
@@ -0,0 +1,17 @@
+module Arel
+ module Nodes
+ class False < Arel::Nodes::Node
+ def not
+ True.new
+ end
+
+ def or right
+ right
+ end
+
+ def and right
+ self
+ end
+ end
+ end
+end
diff --git a/lib/arel/nodes/true.rb b/lib/arel/nodes/true.rb
new file mode 100644
index 0000000000..82f20837f8
--- /dev/null
+++ b/lib/arel/nodes/true.rb
@@ -0,0 +1,17 @@
+module Arel
+ module Nodes
+ class True < Arel::Nodes::Node
+ def not
+ False.new
+ end
+
+ def or right
+ self
+ end
+
+ def and right
+ right
+ end
+ end
+ end
+end
diff --git a/lib/arel/predications.rb b/lib/arel/predications.rb
index 4b124ce05c..e3f72d46a2 100644
--- a/lib/arel/predications.rb
+++ b/lib/arel/predications.rb
@@ -1,6 +1,5 @@
module Arel
module Predications
-
def not_eq other
Nodes::NotEqual.new self, other
end
diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb
index 933edc15f2..8aec4cb147 100644
--- a/lib/arel/visitors/to_sql.rb
+++ b/lib/arel/visitors/to_sql.rb
@@ -90,6 +90,14 @@ key on UpdateManager using UpdateManager#key=
o.alias ? " AS #{visit o.alias}" : ''}"
end
+ def visit_Arel_Nodes_True o
+ "TRUE"
+ end
+
+ def visit_Arel_Nodes_False o
+ "FALSE"
+ end
+
def table_exists? name
@pool.table_exists? name
end