diff options
author | Ernie Miller <ernie@metautonomo.us> | 2011-06-20 14:15:19 -0400 |
---|---|---|
committer | Ernie Miller <ernie@metautonomo.us> | 2011-06-20 14:15:19 -0400 |
commit | 08f3f30994d37f6f44acfac801f82fc43127fc78 (patch) | |
tree | 01775f711e3eea882287303fb7c1490c239018c5 | |
parent | 81f7bf55c7e15f05015e480505897a82b4038242 (diff) | |
download | rails-08f3f30994d37f6f44acfac801f82fc43127fc78.tar.gz rails-08f3f30994d37f6f44acfac801f82fc43127fc78.tar.bz2 rails-08f3f30994d37f6f44acfac801f82fc43127fc78.zip |
Support reversal of ARel orderings in reverse_sql_order
-rw-r--r-- | activerecord/lib/active_record/relation/query_methods.rb | 15 | ||||
-rw-r--r-- | activerecord/test/cases/relations_test.rb | 12 |
2 files changed, 24 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 739363415c..7fbbefeea2 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -305,9 +305,18 @@ module ActiveRecord def reverse_sql_order(order_query) order_query = ["#{quoted_table_name}.#{quoted_primary_key} ASC"] if order_query.empty? - order_query.join(', ').split(',').collect do |s| - s.gsub!(/\sasc\Z/i, ' DESC') || s.gsub!(/\sdesc\Z/i, ' ASC') || s.concat(' DESC') - end + order_query.map do |o| + case o + 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 def array_of_strings?(o) diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index fc9df8c7a3..aa94c9b87e 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -145,6 +145,18 @@ class RelationTest < ActiveRecord::TestCase assert_equal topics(:first).title, topics.first.title 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 topics = Topic.order('author_name').order('title') assert_equal 4, topics.to_a.size |