Skip to content
Browse files

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
  • Loading branch information...
1 parent 8a8d396 commit 85882d1b26b033afb2643865afe6410673949d32 Arthur Taylor committed Apr 28, 2011
View
1 lib/arel.rb
@@ -4,6 +4,7 @@
require 'arel/expressions'
require 'arel/predications'
require 'arel/math'
+require 'arel/order_predications'
require 'arel/table'
require 'arel/attributes'
require 'arel/compatibility/wheres'
View
1 lib/arel/attributes/attribute.rb
@@ -3,6 +3,7 @@ module Attributes
class Attribute < Struct.new :relation, :name
include Arel::Expressions
include Arel::Predications
+ include Arel::OrderPredications
include Arel::Math
###
View
1 lib/arel/expression.rb
@@ -1,4 +1,5 @@
module Arel
module Expression
+ include Arel::OrderPredications
end
end
View
1 lib/arel/nodes/sql_literal.rb
@@ -3,6 +3,7 @@ module Nodes
class SqlLiteral < String
include Arel::Expressions
include Arel::Predications
+ include Arel::OrderPredications
end
end
end
View
13 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
View
9 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 @@ def lteq_all others
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
View
23 test/test_select_manager.rb
@@ -484,6 +484,29 @@ def test_join_sources
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

0 comments on commit 85882d1

Please sign in to comment.
Something went wrong with that request. Please try again.