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

Add order_by feature #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jandudulski
Copy link

Simple shortcut which allows to order by translated column as described in readme.

@jandudulski
Copy link
Author

ping @robworley ? :)

@robworley
Copy link
Contributor

Firstly, thanks for your work on this 👍. I'm just not sure I want to get into this kind of generic API because it's hard to say where the scope of this translation gem will stop... ordering, querying etc. Somehow it never quite feels like first-class/proper ActiveRecord 😐.

@jandudulski
Copy link
Author

I think that the basic functionality should be easily accessible (find, order) - in this case you have to write quite complex code to make simple order by translation. And for me, I have to always check how can I even do that since I don't use db arrays too often 😉

@hodak
Copy link

hodak commented Oct 11, 2014

Actually, I would use this feature too. It's fairly common requirement and the implementation is not trivial.

@daniel-rikowski
Copy link

I really like that feature too, but in it's current version it is not enough, at least for me.
I'm relying on fallbacks and your code doesn't account for them.

I used your code as a starting point to "coalesce" all fallback translations and use that as order by statement.

Perhaps updating the readme with ordering instructions would be a nice compromise until the scope of hstore_translate is properly defined ⏳

@rept
Copy link

rept commented May 26, 2016

Would like this one too. I mostly add stuff like this:

scope :ordered, -> { order('name') }

Which of course doesn't work in this case. @daniel-rikowski mind sharing how you solved this without using this modification?

@rept
Copy link

rept commented May 26, 2016

Ok found how to do this pretty easily:

scope :ordered, -> { order("name_translations -> '#{I18n.locale}'") }

@daniel-rikowski
Copy link

@rept This still won't work with fallbacks.

Imagine this fallback configuration:

I18n.fallbacks[I18n.locale] == [:'de-DE', :de, :en]

And these database records:

[
  { name_translations: { 'de-DE' => 'Kanarienvogel', 'en-US' => 'Canary' },
  { name_translations: { 'de' => 'März', 'en' => 'March' },
  { name_translations: { 'en' => 'Wikipedia' },
]

Considering I18n.locale = :'de-DE' your SQL would sort these translations

'Kanarienvogel'
NULL      -- should be 'März'
NULL      -- should be 'Wikipedia'

To achive correct sorting with fallbacks you have to generate a dynamic SQL statement which takes I18n.fallbacks into account. Like this:

SELECT ... ORDER BY COALESCE(
    name_translations -> 'de-DE', 
    name_translations -> 'de', 
    name_translations -> 'en')

I've created a Gist with my - rather hacky - implementation. Use at your own risk!
https://gist.github.com/daniel-rikowski/1d94ea943bbcf8935fd0f23a5c8b37f6

Example:

Book.order_translated(:title)
Author.references(:books).order_translated(books: :title)

@rept
Copy link

rept commented Apr 18, 2019

Any plans on getting this integrated in the main branch?

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

Successfully merging this pull request may close these issues.

5 participants