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

Merge pull request #28191 from eugeneius/string_assoc_order

Allow order to be given expressions as hash keys
This commit is contained in:
Rafael França 2017-03-17 13:22:20 -04:00 committed by GitHub
commit bac40b9cc8
2 changed files with 30 additions and 2 deletions

View file

@ -1130,7 +1130,12 @@ module ActiveRecord
arel_attribute(arg).asc arel_attribute(arg).asc
when Hash when Hash
arg.map { |field, dir| arg.map { |field, dir|
arel_attribute(field).send(dir.downcase) case field
when Arel::Nodes::SqlLiteral
field.send(dir.downcase)
else
arel_attribute(field).send(dir.downcase)
end
} }
else else
arg arg

View file

@ -218,17 +218,34 @@ class RelationTest < ActiveRecord::TestCase
assert_equal topics(:fifth).title, topics.first.title assert_equal topics(:fifth).title, topics.first.title
end end
def test_finding_with_reverted_assoc_order def test_finding_with_arel_assoc_order
topics = Topic.order(Arel.sql("id") => :desc)
assert_equal 5, topics.to_a.size
assert_equal topics(:fifth).title, topics.first.title
end
def test_finding_with_reversed_assoc_order
topics = Topic.order(id: :asc).reverse_order topics = Topic.order(id: :asc).reverse_order
assert_equal 5, topics.to_a.size assert_equal 5, topics.to_a.size
assert_equal topics(:fifth).title, topics.first.title assert_equal topics(:fifth).title, topics.first.title
end end
def test_finding_with_reversed_arel_assoc_order
topics = Topic.order(Arel.sql("id") => :asc).reverse_order
assert_equal 5, topics.to_a.size
assert_equal topics(:fifth).title, topics.first.title
end
def test_reverse_order_with_function def test_reverse_order_with_function
topics = Topic.order("length(title)").reverse_order topics = Topic.order("length(title)").reverse_order
assert_equal topics(:second).title, topics.first.title assert_equal topics(:second).title, topics.first.title
end end
def test_reverse_arel_assoc_order_with_function
topics = Topic.order(Arel.sql("length(title)") => :asc).reverse_order
assert_equal topics(:second).title, topics.first.title
end
def test_reverse_order_with_function_other_predicates def test_reverse_order_with_function_other_predicates
topics = Topic.order("author_name, length(title), id").reverse_order topics = Topic.order("author_name, length(title), id").reverse_order
assert_equal topics(:second).title, topics.first.title assert_equal topics(:second).title, topics.first.title
@ -251,6 +268,12 @@ class RelationTest < ActiveRecord::TestCase
end end
end end
def test_reverse_arel_assoc_order_with_multiargument_function
assert_nothing_raised do
Topic.order(Arel.sql("REPLACE(title, '', '')") => :asc).reverse_order
end
end
def test_reverse_order_with_nulls_first_or_last def test_reverse_order_with_nulls_first_or_last
assert_raises(ActiveRecord::IrreversibleOrderError) do assert_raises(ActiveRecord::IrreversibleOrderError) do
Topic.order("title NULLS FIRST").reverse_order Topic.order("title NULLS FIRST").reverse_order