diff options
author | Ernie Miller <ernie@metautonomo.us> | 2010-09-29 20:08:57 -0400 |
---|---|---|
committer | Ernie Miller <ernie@metautonomo.us> | 2010-09-29 20:08:57 -0400 |
commit | eef61ab909b614ce6f2d100cfb50044c063bc24a (patch) | |
tree | eaa50f239fab9b1b3ed88be89ba743b5724fb19a /lib | |
parent | f5b76c220ed6583628a84fd899fbb24c39c997ee (diff) | |
download | rails-eef61ab909b614ce6f2d100cfb50044c063bc24a.tar.gz rails-eef61ab909b614ce6f2d100cfb50044c063bc24a.tar.bz2 rails-eef61ab909b614ce6f2d100cfb50044c063bc24a.zip |
Support Attribute#asc and Attribute#desc to create orderings
Diffstat (limited to 'lib')
-rw-r--r-- | lib/arel/attributes/attribute.rb | 8 | ||||
-rw-r--r-- | lib/arel/nodes.rb | 1 | ||||
-rw-r--r-- | lib/arel/nodes/ordering.rb | 19 | ||||
-rw-r--r-- | lib/arel/visitors/dot.rb | 5 | ||||
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 4 |
5 files changed, 37 insertions, 0 deletions
diff --git a/lib/arel/attributes/attribute.rb b/lib/arel/attributes/attribute.rb index 25ffe717b2..ba18533590 100644 --- a/lib/arel/attributes/attribute.rb +++ b/lib/arel/attributes/attribute.rb @@ -151,6 +151,14 @@ module Arel grouping_all :lteq, others end + def asc + Nodes::Ordering.new self, :asc + end + + def desc + Nodes::Ordering.new self, :desc + end + private def grouping_any method_id, others diff --git a/lib/arel/nodes.rb b/lib/arel/nodes.rb index 0063ed1cd5..47cbd64eb5 100644 --- a/lib/arel/nodes.rb +++ b/lib/arel/nodes.rb @@ -15,6 +15,7 @@ require 'arel/nodes/does_not_match' require 'arel/nodes/in' require 'arel/nodes/not_in' +require 'arel/nodes/ordering' require 'arel/nodes/lock' require 'arel/nodes/function' require 'arel/nodes/count' diff --git a/lib/arel/nodes/ordering.rb b/lib/arel/nodes/ordering.rb new file mode 100644 index 0000000000..d395c8631d --- /dev/null +++ b/lib/arel/nodes/ordering.rb @@ -0,0 +1,19 @@ +module Arel + module Nodes + class Ordering < Arel::Nodes::Node + attr_accessor :expr, :direction + + def initialize expression, direction = :asc + @expr, @direction = expression, direction + end + + def ascending? + direction == :asc + end + + def descending? + direction == :desc + end + end + end +end diff --git a/lib/arel/visitors/dot.rb b/lib/arel/visitors/dot.rb index 655c75aab9..5202c5127e 100644 --- a/lib/arel/visitors/dot.rb +++ b/lib/arel/visitors/dot.rb @@ -32,6 +32,11 @@ module Arel visit_edge o, "expr" end + def visit_Arel_Nodes_Ordering o + visit_edge o, "expr" + visit_edge o, "direction" + end + def visit_Arel_Nodes_TableAlias o visit_edge o, "name" visit_edge o, "relation" diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 405578f604..17779b4037 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -107,6 +107,10 @@ module Arel "(#{visit o.expr})" end + def visit_Arel_Nodes_Ordering o + "#{visit o.expr} #{o.descending? ? 'DESC' : 'ASC'}" + end + def visit_Arel_Nodes_Group o visit o.expr end |