Skip to content

Commit

Permalink
Merge pull request #33027 from bkuhlmann/reverse_sql_order_patch-51
Browse files Browse the repository at this point in the history
Fixed `reverse_order` Arel SQL literal issue.
  • Loading branch information
tenderlove committed Jun 5, 2018
2 parents 039dfd1 + b3b17d2 commit 819b515
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions actioncable/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Back port Rails 5.2 `reverse_order` Arel SQL literal fix.

*Matt Jones*, *Brooke Kuhlmann*

## Rails 5.1.6 (March 29, 2018) ##

* No changes.
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/relation/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ def reverse_sql_order(order_query)
end
o.split(",").map! do |s|
s.strip!
s.gsub!(/\sasc\Z/i, " DESC") || s.gsub!(/\sdesc\Z/i, " ASC") || s.concat(" DESC")
s.gsub!(/\sasc\Z/i, " DESC") || s.gsub!(/\sdesc\Z/i, " ASC") || (s << " DESC")
end
else
o
Expand Down
14 changes: 14 additions & 0 deletions activerecord/test/cases/relations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,20 @@ def test_reverse_order_with_function_other_predicates
assert_equal topics(:fifth).title, topics.first.title
end

def test_reverse_order_with_string
result = Topic.order("id").reverse_order
proof = [5, 4, 3, 2, 1]

assert_equal result.pluck(:id), proof
end

def test_reverse_order_with_arel_literal
result = Topic.order(Arel::Nodes::SqlLiteral.new("id")).reverse_order
proof = [5, 4, 3, 2, 1]

assert_equal result.pluck(:id), proof
end

def test_reverse_order_with_multiargument_function
assert_raises(ActiveRecord::IrreversibleOrderError) do
Topic.order("concat(author_name, title)").reverse_order
Expand Down

0 comments on commit 819b515

Please sign in to comment.