mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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
This commit is contained in:
parent
8a8d396cae
commit
85882d1b26
7 changed files with 41 additions and 8 deletions
|
@ -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'
|
||||
|
|
|
@ -3,6 +3,7 @@ module Arel
|
|||
class Attribute < Struct.new :relation, :name
|
||||
include Arel::Expressions
|
||||
include Arel::Predications
|
||||
include Arel::OrderPredications
|
||||
include Arel::Math
|
||||
|
||||
###
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
module Arel
|
||||
module Expression
|
||||
include Arel::OrderPredications
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,6 +3,7 @@ module Arel
|
|||
class SqlLiteral < String
|
||||
include Arel::Expressions
|
||||
include Arel::Predications
|
||||
include Arel::OrderPredications
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
13
lib/arel/order_predications.rb
Normal file
13
lib/arel/order_predications.rb
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue