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/expressions'
|
||||||
require 'arel/predications'
|
require 'arel/predications'
|
||||||
require 'arel/math'
|
require 'arel/math'
|
||||||
|
require 'arel/order_predications'
|
||||||
require 'arel/table'
|
require 'arel/table'
|
||||||
require 'arel/attributes'
|
require 'arel/attributes'
|
||||||
require 'arel/compatibility/wheres'
|
require 'arel/compatibility/wheres'
|
||||||
|
|
|
@ -3,6 +3,7 @@ module Arel
|
||||||
class Attribute < Struct.new :relation, :name
|
class Attribute < Struct.new :relation, :name
|
||||||
include Arel::Expressions
|
include Arel::Expressions
|
||||||
include Arel::Predications
|
include Arel::Predications
|
||||||
|
include Arel::OrderPredications
|
||||||
include Arel::Math
|
include Arel::Math
|
||||||
|
|
||||||
###
|
###
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
module Arel
|
module Arel
|
||||||
module Expression
|
module Expression
|
||||||
|
include Arel::OrderPredications
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,6 +3,7 @@ module Arel
|
||||||
class SqlLiteral < String
|
class SqlLiteral < String
|
||||||
include Arel::Expressions
|
include Arel::Expressions
|
||||||
include Arel::Predications
|
include Arel::Predications
|
||||||
|
include Arel::OrderPredications
|
||||||
end
|
end
|
||||||
end
|
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 Arel
|
||||||
module Predications
|
module Predications
|
||||||
|
|
||||||
def as other
|
def as other
|
||||||
Nodes::As.new self, Nodes::SqlLiteral.new(other)
|
Nodes::As.new self, Nodes::SqlLiteral.new(other)
|
||||||
end
|
end
|
||||||
|
@ -152,14 +153,6 @@ module Arel
|
||||||
grouping_all :lteq, others
|
grouping_all :lteq, others
|
||||||
end
|
end
|
||||||
|
|
||||||
def asc
|
|
||||||
Nodes::Ordering.new self, :asc
|
|
||||||
end
|
|
||||||
|
|
||||||
def desc
|
|
||||||
Nodes::Ordering.new self, :desc
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def grouping_any method_id, others
|
def grouping_any method_id, others
|
||||||
|
|
|
@ -484,6 +484,29 @@ module Arel
|
||||||
manager = Arel::SelectManager.new Table.engine
|
manager = Arel::SelectManager.new Table.engine
|
||||||
manager.order(table[:id]).must_equal manager
|
manager.order(table[:id]).must_equal manager
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
describe 'on' do
|
describe 'on' do
|
||||||
|
|
Loading…
Reference in a new issue