mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Support reversal of ARel orderings in reverse_sql_order
This commit is contained in:
parent
81f7bf55c7
commit
08f3f30994
2 changed files with 24 additions and 3 deletions
|
@ -305,9 +305,18 @@ module ActiveRecord
|
||||||
def reverse_sql_order(order_query)
|
def reverse_sql_order(order_query)
|
||||||
order_query = ["#{quoted_table_name}.#{quoted_primary_key} ASC"] if order_query.empty?
|
order_query = ["#{quoted_table_name}.#{quoted_primary_key} ASC"] if order_query.empty?
|
||||||
|
|
||||||
order_query.join(', ').split(',').collect do |s|
|
order_query.map do |o|
|
||||||
s.gsub!(/\sasc\Z/i, ' DESC') || s.gsub!(/\sdesc\Z/i, ' ASC') || s.concat(' DESC')
|
case o
|
||||||
end
|
when Arel::Nodes::Ordering
|
||||||
|
o.reverse
|
||||||
|
when String, Symbol
|
||||||
|
o.to_s.split(',').collect do |s|
|
||||||
|
s.gsub!(/\sasc\Z/i, ' DESC') || s.gsub!(/\sdesc\Z/i, ' ASC') || s.concat(' DESC')
|
||||||
|
end
|
||||||
|
else
|
||||||
|
o
|
||||||
|
end
|
||||||
|
end.flatten
|
||||||
end
|
end
|
||||||
|
|
||||||
def array_of_strings?(o)
|
def array_of_strings?(o)
|
||||||
|
|
|
@ -145,6 +145,18 @@ class RelationTest < ActiveRecord::TestCase
|
||||||
assert_equal topics(:first).title, topics.first.title
|
assert_equal topics(:first).title, topics.first.title
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
def test_finding_with_arel_order
|
||||||
|
topics = Topic.order(Topic.arel_table[:id].asc)
|
||||||
|
assert_equal 4, topics.to_a.size
|
||||||
|
assert_equal topics(:first).title, topics.first.title
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_finding_last_with_arel_order
|
||||||
|
topics = Topic.order(Topic.arel_table[:id].asc)
|
||||||
|
assert_equal topics(:fourth).title, topics.last.title
|
||||||
|
end
|
||||||
|
|
||||||
def test_finding_with_order_concatenated
|
def test_finding_with_order_concatenated
|
||||||
topics = Topic.order('author_name').order('title')
|
topics = Topic.order('author_name').order('title')
|
||||||
assert_equal 4, topics.to_a.size
|
assert_equal 4, topics.to_a.size
|
||||||
|
|
Loading…
Reference in a new issue