Hi,
I'm opening this issue to track an issue I've found with order clause on multiple columns
Context
ActiveRecord 7.0
Mobility 1.2.6
Issue 1 - 1st parameter is not translatable
Expected Behavior
Orders by translated attribute
Foo.i18n.order(:priority, :name).to_sql
=> "SELECT \"foos\".* FROM \"foos\" ORDER BY \"foos\".\"priority\" ASC, \"foos\".\"name_en\" ASC"
Actual Behavior
Does not order by translated attribute
Foo.i18n.order(:priority, :name).to_sql
=> "SELECT \"foos\".* FROM \"foos\" ORDER BY \"foos\".\"priority\" ASC, \"foos\".\"name\" ASC"
Issue 2 - 1st parameter is translatable
Expected Behavior
Keeps in account nth parameter where n >= 2
Foo.i18n.order(:name, :priority).to_sql
=> "SELECT \"foos\".* FROM \"foos\" ORDER BY \"foos\".\"name_en\" ASC, \"foos\".\"priority\" ASC"
Actual Behavior
Discards nth parameter where n >= 2
Foo.i18n.order(:name, :priority).to_sql
=> "SELECT \"foos\".* FROM \"foos\" ORDER BY \"foos\".\"name_en\" ASC"
Workaround
Concatenate order clauses
Foo.i18n.order(:priority).order(:name).to_sql
=> "SELECT \"foos\".* FROM \"foos\" ORDER BY \"foos\".\"priority\" ASC, \"foos\".\"name_en\" ASC"
Possible Fix
|
case opts |
|
when Symbol, String |
|
@klass.mobility_attribute?(opts) ? order({ opts => :asc }, *rest) : super |
|
when ::Hash |
|
i18n_keys, keys = opts.keys.partition(&@klass.method(:mobility_attribute?)) |
|
return super if i18n_keys.empty? |
|
|
|
base = keys.empty? ? self : super(opts.slice(keys)) |
|
|
|
i18n_keys.inject(base) do |query, key| |
|
backend_class = @klass.mobility_backend_class(key) |
|
dir, node = opts[key], backend_node(key) |
|
backend_class.apply_scope(query, node).order(node.send(dir.downcase)) |
|
end |
|
else |
|
super |
|
end |
Add support to array of fields change the implementation to correctly manage args
Hi,
I'm opening this issue to track an issue I've found with
orderclause on multiple columnsContext
ActiveRecord 7.0
Mobility 1.2.6
Issue 1 - 1st parameter is not translatable
Expected Behavior
Orders by translated attribute
Actual Behavior
Does not order by translated attribute
Issue 2 - 1st parameter is translatable
Expected Behavior
Keeps in account nth parameter where n >= 2
Actual Behavior
Discards nth parameter where n >= 2
Workaround
Concatenate order clauses
Possible Fix
mobility/lib/mobility/plugins/active_record/query.rb
Lines 142 to 158 in 7683433
Add support to array of fieldschange the implementation to correctly manageargs