diff options
author | Ernie Miller <ernie@metautonomo.us> | 2011-06-14 17:43:22 -0400 |
---|---|---|
committer | Ernie Miller <ernie@metautonomo.us> | 2011-06-15 09:18:26 -0400 |
commit | ba3578a22f824da3478b6dceb100deb9f41a56e9 (patch) | |
tree | 8caaa5ad9018fe61c838907cd0854a68a55f91cd /lib | |
parent | 593002080cbcbb6e14acf3bc909aab819811f215 (diff) | |
download | rails-ba3578a22f824da3478b6dceb100deb9f41a56e9.tar.gz rails-ba3578a22f824da3478b6dceb100deb9f41a56e9.tar.bz2 rails-ba3578a22f824da3478b6dceb100deb9f41a56e9.zip |
Break Ordering into Ascending/Descending nodes, allow reversal
Diffstat (limited to 'lib')
-rw-r--r-- | lib/arel/nodes.rb | 3 | ||||
-rw-r--r-- | lib/arel/nodes/ascending.rb | 23 | ||||
-rw-r--r-- | lib/arel/nodes/descending.rb | 23 | ||||
-rw-r--r-- | lib/arel/nodes/ordering.rb | 16 | ||||
-rw-r--r-- | lib/arel/nodes/unary.rb | 1 | ||||
-rw-r--r-- | lib/arel/order_predications.rb | 4 | ||||
-rw-r--r-- | lib/arel/visitors/depth_first.rb | 2 | ||||
-rw-r--r-- | lib/arel/visitors/dot.rb | 1 | ||||
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 8 |
9 files changed, 59 insertions, 22 deletions
diff --git a/lib/arel/nodes.rb b/lib/arel/nodes.rb index 9576930a54..9edf3a9a95 100644 --- a/lib/arel/nodes.rb +++ b/lib/arel/nodes.rb @@ -11,6 +11,8 @@ require 'arel/nodes/terminal' # unary require 'arel/nodes/unary' +require 'arel/nodes/ascending' +require 'arel/nodes/descending' require 'arel/nodes/unqualified_column' require 'arel/nodes/with' @@ -19,7 +21,6 @@ require 'arel/nodes/binary' require 'arel/nodes/equality' require 'arel/nodes/in' # Why is this subclassed from equality? require 'arel/nodes/join_source' -require 'arel/nodes/ordering' require 'arel/nodes/delete_statement' require 'arel/nodes/table_alias' require 'arel/nodes/infix_operation' diff --git a/lib/arel/nodes/ascending.rb b/lib/arel/nodes/ascending.rb new file mode 100644 index 0000000000..bca00a8339 --- /dev/null +++ b/lib/arel/nodes/ascending.rb @@ -0,0 +1,23 @@ +module Arel + module Nodes + class Ascending < Ordering + + def reverse + Descending.new(expr) + end + + def direction + :asc + end + + def ascending? + true + end + + def descending? + false + end + + end + end +end diff --git a/lib/arel/nodes/descending.rb b/lib/arel/nodes/descending.rb new file mode 100644 index 0000000000..d886bdcb5f --- /dev/null +++ b/lib/arel/nodes/descending.rb @@ -0,0 +1,23 @@ +module Arel + module Nodes + class Descending < Ordering + + def reverse + Ascending.new(expr) + end + + def direction + :desc + end + + def ascending? + false + end + + def descending? + true + end + + end + end +end diff --git a/lib/arel/nodes/ordering.rb b/lib/arel/nodes/ordering.rb index 0a3621cf54..efb4d18ae4 100644 --- a/lib/arel/nodes/ordering.rb +++ b/lib/arel/nodes/ordering.rb @@ -1,20 +1,6 @@ module Arel module Nodes - class Ordering < Arel::Nodes::Binary - alias :expr :left - alias :direction :right - - def initialize expr, direction = :asc - super - end - - def ascending? - direction == :asc - end - - def descending? - direction == :desc - end + class Ordering < Unary end end end diff --git a/lib/arel/nodes/unary.rb b/lib/arel/nodes/unary.rb index 5c4add4792..4688fff623 100644 --- a/lib/arel/nodes/unary.rb +++ b/lib/arel/nodes/unary.rb @@ -18,6 +18,7 @@ module Arel Not Offset On + Ordering Top Lock DistinctOn diff --git a/lib/arel/order_predications.rb b/lib/arel/order_predications.rb index af163c9454..153fcffb41 100644 --- a/lib/arel/order_predications.rb +++ b/lib/arel/order_predications.rb @@ -2,11 +2,11 @@ module Arel module OrderPredications def asc - Nodes::Ordering.new self, :asc + Nodes::Ascending.new self end def desc - Nodes::Ordering.new self, :desc + Nodes::Descending.new self end end diff --git a/lib/arel/visitors/depth_first.rb b/lib/arel/visitors/depth_first.rb index 914b2d1999..6f9385de1b 100644 --- a/lib/arel/visitors/depth_first.rb +++ b/lib/arel/visitors/depth_first.rb @@ -22,6 +22,7 @@ module Arel alias :visit_Arel_Nodes_Not :unary alias :visit_Arel_Nodes_Offset :unary alias :visit_Arel_Nodes_On :unary + alias :visit_Arel_Nodes_Ordering :unary alias :visit_Arel_Nodes_Top :unary alias :visit_Arel_Nodes_UnqualifiedColumn :unary @@ -75,7 +76,6 @@ module Arel alias :visit_Arel_Nodes_NotEqual :binary alias :visit_Arel_Nodes_NotIn :binary alias :visit_Arel_Nodes_Or :binary - alias :visit_Arel_Nodes_Ordering :binary alias :visit_Arel_Nodes_OuterJoin :binary alias :visit_Arel_Nodes_TableAlias :binary alias :visit_Arel_Nodes_Values :binary diff --git a/lib/arel/visitors/dot.rb b/lib/arel/visitors/dot.rb index 92d05c7b67..8303279211 100644 --- a/lib/arel/visitors/dot.rb +++ b/lib/arel/visitors/dot.rb @@ -30,7 +30,6 @@ module Arel private def visit_Arel_Nodes_Ordering o visit_edge o, "expr" - visit_edge o, "direction" end def visit_Arel_Nodes_TableAlias o diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 29e965bef8..933edc15f2 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -203,8 +203,12 @@ key on UpdateManager using UpdateManager#key= "(#{visit o.expr})" end - def visit_Arel_Nodes_Ordering o - "#{visit o.expr} #{o.descending? ? 'DESC' : 'ASC'}" + def visit_Arel_Nodes_Ascending o + "#{visit o.expr} ASC" + end + + def visit_Arel_Nodes_Descending o + "#{visit o.expr} DESC" end def visit_Arel_Nodes_Group o |