Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AR - Order clause has issues with multiple parameters #565

Open
tagliala opened this issue Apr 14, 2022 · 5 comments
Open

AR - Order clause has issues with multiple parameters #565

tagliala opened this issue Apr 14, 2022 · 5 comments
Labels

Comments

@tagliala
Copy link

tagliala commented Apr 14, 2022

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

@tagliala tagliala changed the title AR - Order clause with multiple fields does not detect AR - Order clause has issues with multiple parameters Apr 16, 2022
@shioyama
Copy link
Owner

shioyama commented May 2, 2022

Seems like a genuine issue, I'll try to have a look.

@shioyama shioyama added the bug label May 2, 2022
@shioyama
Copy link
Owner

shioyama commented May 2, 2022

This should be a straightforward fix, if anyone wants to have a go at it I'll review any PRs. Otherwise will probably take me a few weeks, I'm very busy until after RailsConf.

tagliala added a commit to tagliala/mobility that referenced this issue May 2, 2022
@tagliala
Copy link
Author

tagliala commented May 2, 2022

Hi, thanks fro the feedback.

I've tried myself to fix but it isn't so easy for me.

The problem is that multiple symbols are not passed as array, and the previous scope is not maintained in the Hash branch, together with attributes being rearranged by putting untranslated ones at the beginning:

i18n_keys, keys = opts.keys.partition(&@klass.method(:mobility_attribute?))

I'm trying to use the same approach as pluck group select for order too, without success for the moment

I've added some failing specs at tagliala@91d827a

tagliala added a commit to tagliala/mobility that referenced this issue May 2, 2022
tagliala added a commit to tagliala/mobility that referenced this issue May 2, 2022
@tagliala
Copy link
Author

tagliala commented Jun 8, 2022

Hi, from time to time I'm still attempting to fix this issue

I've added more failing specs, and I think that Mobility needs something like preprocess_order_args

@shioyama
Copy link
Owner

Thanks @tagliala, yes this seems to be a bit more complicated than when I originally added support for order... 😭

You're right that preprocessor_order_args is the right reference point. I tried quickly to pull together something now but it's a bit tricky.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants