From 11f929b5c485adab60ea2d8b515ef2abcf5400f4 Mon Sep 17 00:00:00 2001 From: Daniel Cadenas Date: Tue, 31 May 2011 00:40:11 -0300 Subject: 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)) --- lib/arel/factory_methods.rb | 8 ++++++++ lib/arel/nodes.rb | 2 ++ lib/arel/nodes/false.rb | 17 +++++++++++++++++ lib/arel/nodes/true.rb | 17 +++++++++++++++++ lib/arel/predications.rb | 1 - lib/arel/visitors/to_sql.rb | 8 ++++++++ 6 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 lib/arel/nodes/false.rb create mode 100644 lib/arel/nodes/true.rb (limited to 'lib/arel') 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 -- cgit v1.2.3