mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
deal with Array arguments to #order
This commit is contained in:
parent
c711a27d29
commit
b76cc29865
3 changed files with 22 additions and 4 deletions
|
@ -930,7 +930,19 @@ module ActiveRecord
|
||||||
|
|
||||||
# Extract column names from arguments passed to #order or #reorder.
|
# Extract column names from arguments passed to #order or #reorder.
|
||||||
def column_names_from_order_arguments(args)
|
def column_names_from_order_arguments(args)
|
||||||
args.flat_map { |arg| arg.is_a?(Hash) ? arg.keys : arg }
|
args.flat_map do |arg|
|
||||||
|
case arg
|
||||||
|
when Hash
|
||||||
|
# Tag.order(id: :desc)
|
||||||
|
arg.keys
|
||||||
|
when Array
|
||||||
|
# Tag.order([Arel.sql("field(id, ?)"), [1, 3, 2]])
|
||||||
|
arg.flatten
|
||||||
|
else
|
||||||
|
# Tag.order(:id)
|
||||||
|
arg
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_mutability!
|
def assert_mutability!
|
||||||
|
|
|
@ -63,6 +63,12 @@ module ActiveRecord
|
||||||
# # => "id ASC"
|
# # => "id ASC"
|
||||||
def sanitize_sql_for_order(condition) # :doc:
|
def sanitize_sql_for_order(condition) # :doc:
|
||||||
if condition.is_a?(Array) && condition.first.to_s.include?("?")
|
if condition.is_a?(Array) && condition.first.to_s.include?("?")
|
||||||
|
# Ensure we aren't dealing with a subclass of String that might
|
||||||
|
# override methods we use (eg. Arel::Nodes::SqlLiteral).
|
||||||
|
if condition.first.kind_of?(String) && !condition.first.instance_of?(String)
|
||||||
|
condition = [String.new(condition.first), *condition[1..-1]]
|
||||||
|
end
|
||||||
|
|
||||||
sanitize_sql_array(condition)
|
sanitize_sql_array(condition)
|
||||||
else
|
else
|
||||||
condition
|
condition
|
||||||
|
|
|
@ -389,13 +389,13 @@ class RelationTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_finding_with_sanitized_order
|
def test_finding_with_sanitized_order
|
||||||
query = Tag.order(["field(id, ?)", [1, 3, 2]]).to_sql
|
query = Tag.order([Arel.sql("field(id, ?)"), [1, 3, 2]]).to_sql
|
||||||
assert_match(/field\(id, 1,3,2\)/, query)
|
assert_match(/field\(id, 1,3,2\)/, query)
|
||||||
|
|
||||||
query = Tag.order(["field(id, ?)", []]).to_sql
|
query = Tag.order([Arel.sql("field(id, ?)"), []]).to_sql
|
||||||
assert_match(/field\(id, NULL\)/, query)
|
assert_match(/field\(id, NULL\)/, query)
|
||||||
|
|
||||||
query = Tag.order(["field(id, ?)", nil]).to_sql
|
query = Tag.order([Arel.sql("field(id, ?)"), nil]).to_sql
|
||||||
assert_match(/field\(id, NULL\)/, query)
|
assert_match(/field\(id, NULL\)/, query)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue