1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

convert order arg to string before checking if we can reverse it

This commit is contained in:
Ben Toews 2017-10-11 14:15:47 -06:00 committed by Matthew Draper
parent ab03eb9f57
commit c711a27d29
2 changed files with 6 additions and 2 deletions

View file

@ -1097,6 +1097,10 @@ module ActiveRecord
end
def does_not_support_reverse?(order)
# Account for String subclasses like Arel::Nodes::SqlLiteral that
# override methods like #count.
order = String.new(order) unless order.instance_of?(String)
# Uses SQL function with multiple arguments.
(order.include?(",") && order.split(",").find { |section| section.count("(") != section.count(")") }) ||
# Uses "nulls first" like construction.

View file

@ -248,9 +248,9 @@ class RelationTest < ActiveRecord::TestCase
end
def test_reverse_order_with_function_other_predicates
topics = Topic.order("author_name, length(title), id").reverse_order
topics = Topic.order(Arel.sql("author_name, length(title), id")).reverse_order
assert_equal topics(:second).title, topics.first.title
topics = Topic.order("length(author_name), id, length(title)").reverse_order
topics = Topic.order(Arel.sql("length(author_name), id, length(title)")).reverse_order
assert_equal topics(:fifth).title, topics.first.title
end