mobility icon indicating copy to clipboard operation
mobility copied to clipboard

AR - Order clause has issues with multiple parameters

Open tagliala opened this issue 2 years ago • 5 comments

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

https://github.com/shioyama/mobility/blob/7683433b80768e1d40cb916f706ce84e9cd4ae83/lib/mobility/plugins/active_record/query.rb#L142-L158

~Add support to array of fields~ change the implementation to correctly manage args

tagliala avatar Apr 14 '22 12:04 tagliala

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

shioyama avatar May 02 '22 03:05 shioyama

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.

shioyama avatar May 02 '22 03:05 shioyama

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:

https://github.com/shioyama/mobility/blob/7683433b80768e1d40cb916f706ce84e9cd4ae83/lib/mobility/plugins/active_record/query.rb#L146

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 https://github.com/tagliala/mobility/commit/91d827a

tagliala avatar May 02 '22 08:05 tagliala

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

tagliala avatar Jun 08 '22 14:06 tagliala

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

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.

shioyama avatar Jun 18 '22 13:06 shioyama