-
Notifications
You must be signed in to change notification settings - Fork 33
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 for translated fields #12
base: master
Are you sure you want to change the base?
Conversation
I like the idea. Can you please improve it as you proposed! ;) Ping me when it is ready for you! |
Any ETA on when this PR will be merged? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot! Last few things.
To order records using translations without constructing JSON queries by hand: | ||
|
||
```ruby | ||
Post.order_by_title_translation # => #<ActiveRecord::Relation ...> | ||
Post.order_by_title_translation(:asc) # => #<ActiveRecord::Relation ...> | ||
Post.order_by_title_translation(:desc) # => #<ActiveRecord::Relation ...> | ||
Post.order_by_title_translation(:desc, :he) # => #<ActiveRecord::Relation ...> | ||
``` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please specify the defaults (direction
and locale
) in the README please?
@@ -48,6 +48,16 @@ def translates(*attrs) | |||
where("#{quoted_translation_store} @> :translation::jsonb", translation: translation_hash.to_json) | |||
end | |||
end | |||
|
|||
define_singleton_method "order_by_#{attr_name}_translation" do |direction = :asc, locale = I18n.locale| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not "order_by_#{attr_name}#{SUFFIX}"
? Would be more conventional.
End of August probably. |
I still need to review the quoting for the order by (currently it's hard-coded), there's probably a way to quote them properly using AR methods. However just checking would you be interested in accepting this functionality?