From 85882d1b26b033afb2643865afe6410673949d32 Mon Sep 17 00:00:00 2001 From: Arthur Taylor Date: Thu, 28 Apr 2011 10:11:50 +0200 Subject: [PATCH] Add support for ordering on expressions Conflicts: lib/arel.rb lib/arel/attributes/attribute.rb lib/arel/nodes/infix_operation.rb lib/arel/nodes/named_function.rb Conflicts: lib/arel.rb lib/arel/attributes/attribute.rb --- lib/arel.rb | 1 + lib/arel/attributes/attribute.rb | 1 + lib/arel/expression.rb | 1 + lib/arel/nodes/sql_literal.rb | 1 + lib/arel/order_predications.rb | 13 +++++++++++++ lib/arel/predications.rb | 9 +-------- test/test_select_manager.rb | 23 +++++++++++++++++++++++ 7 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 lib/arel/order_predications.rb diff --git a/lib/arel.rb b/lib/arel.rb index de429f532e..bc599514e2 100644 --- a/lib/arel.rb +++ b/lib/arel.rb @@ -4,6 +4,7 @@ require 'arel/factory_methods' require 'arel/expressions' require 'arel/predications' require 'arel/math' +require 'arel/order_predications' require 'arel/table' require 'arel/attributes' require 'arel/compatibility/wheres' diff --git a/lib/arel/attributes/attribute.rb b/lib/arel/attributes/attribute.rb index 240f224d8c..b9fd8da67e 100644 --- a/lib/arel/attributes/attribute.rb +++ b/lib/arel/attributes/attribute.rb @@ -3,6 +3,7 @@ module Arel class Attribute < Struct.new :relation, :name include Arel::Expressions include Arel::Predications + include Arel::OrderPredications include Arel::Math ### diff --git a/lib/arel/expression.rb b/lib/arel/expression.rb index eb2c21bfd3..3884d6ede6 100644 --- a/lib/arel/expression.rb +++ b/lib/arel/expression.rb @@ -1,4 +1,5 @@ module Arel module Expression + include Arel::OrderPredications end end diff --git a/lib/arel/nodes/sql_literal.rb b/lib/arel/nodes/sql_literal.rb index c76a16daf1..ad0bb00484 100644 --- a/lib/arel/nodes/sql_literal.rb +++ b/lib/arel/nodes/sql_literal.rb @@ -3,6 +3,7 @@ module Arel class SqlLiteral < String include Arel::Expressions include Arel::Predications + include Arel::OrderPredications end end end diff --git a/lib/arel/order_predications.rb b/lib/arel/order_predications.rb new file mode 100644 index 0000000000..af163c9454 --- /dev/null +++ b/lib/arel/order_predications.rb @@ -0,0 +1,13 @@ +module Arel + module OrderPredications + + def asc + Nodes::Ordering.new self, :asc + end + + def desc + Nodes::Ordering.new self, :desc + end + + end +end diff --git a/lib/arel/predications.rb b/lib/arel/predications.rb index 7e2425e45f..08cbf16d9d 100644 --- a/lib/arel/predications.rb +++ b/lib/arel/predications.rb @@ -1,5 +1,6 @@ module Arel module Predications + def as other Nodes::As.new self, Nodes::SqlLiteral.new(other) end @@ -152,14 +153,6 @@ 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/test/test_select_manager.rb b/test/test_select_manager.rb index 8de1520b6f..29d317e16a 100644 --- a/test/test_select_manager.rb +++ b/test/test_select_manager.rb @@ -484,6 +484,29 @@ module Arel manager = Arel::SelectManager.new Table.engine manager.order(table[:id]).must_equal manager end + + it 'has order attributes' do + table = Table.new :users + manager = Arel::SelectManager.new Table.engine + manager.project SqlLiteral.new '*' + manager.from table + manager.order table[:id].desc + manager.to_sql.must_be_like %{ + SELECT * FROM "users" ORDER BY "users"."id" DESC + } + end + + it 'has order attributes for expressions' do + table = Table.new :users + manager = Arel::SelectManager.new Table.engine + manager.project SqlLiteral.new '*' + manager.from table + manager.order table[:id].count.desc + manager.to_sql.must_be_like %{ + SELECT * FROM "users" ORDER BY COUNT("users"."id") DESC + } + end + end describe 'on' do