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

Fix order with custom attributes

This follows up 0ee96d13de.
This commit is contained in:
Ryuta Kamizono 2019-02-17 01:56:18 +09:00
parent 263cfd5cad
commit 311f001167
4 changed files with 21 additions and 4 deletions

View file

@ -1157,14 +1157,20 @@ module ActiveRecord
order_args.map! do |arg|
case arg
when Symbol
arel_attribute(arg).asc
field = arg.to_s
arel_column(field) {
Arel.sql(connection.quote_table_name(field))
}.asc
when Hash
arg.map { |field, dir|
case field
when Arel::Nodes::SqlLiteral
field.send(dir.downcase)
else
arel_attribute(field).send(dir.downcase)
field = field.to_s
arel_column(field) {
Arel.sql(connection.quote_table_name(field))
}.send(dir.downcase)
end
}
else

View file

@ -26,7 +26,7 @@ module ActiveRecord
assert relation.order!(:name).equal?(relation)
node = relation.order_values.first
assert_predicate node, :ascending?
assert_equal :name, node.expr.name
assert_equal "name", node.expr.name
assert_equal "posts", node.expr.relation.name
end
@ -89,7 +89,7 @@ module ActiveRecord
node = relation.order_values.first
assert_predicate node, :ascending?
assert_equal :name, node.expr.name
assert_equal "name", node.expr.name
assert_equal "posts", node.expr.relation.name
end

View file

@ -1852,6 +1852,13 @@ class RelationTest < ActiveRecord::TestCase
assert_equal contract.metadata, company.metadata
end
test "joins with order by custom attribute" do
companies = Company.create!([{ name: "test1" }, { name: "test2" }])
companies.each { |company| company.contracts.create! }
assert_equal companies, Company.joins(:contracts).order(:metadata)
assert_equal companies.reverse, Company.joins(:contracts).order(metadata: :desc)
end
test "delegations do not leak to other classes" do
Topic.all.by_lifo
assert Topic.all.class.method_defined?(:by_lifo)

View file

@ -328,6 +328,10 @@ class FakeKlass
# noop
end
def columns_hash
{ "name" => nil }
end
def arel_table
Post.arel_table
end