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 for translated fields #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ Post.with_title_translation("This database rocks!") # => #<ActiveRecord::Relatio
Post.with_title_translation("אתר זה טוב", :he) # => #<ActiveRecord::Relation ...>
```

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 ...>
```

Comment on lines +76 to +84
Copy link
Contributor

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?

In order to make this work, you'll need to define an JSON or JSONB column for each of
your translated attributes, using the suffix "_translations":

Expand Down
10 changes: 10 additions & 0 deletions lib/json_translate/translates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Copy link
Contributor

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.

quoted_translation_store = connection.quote_column_name("#{attr_name}#{SUFFIX}")

if MYSQL_ADAPTERS.include?(connection.adapter_name)
order(Arel.sql("JSON_EXTRACT(#{quoted_translation_store}, '$.\"#{locale}\"') #{direction}"))
else
order(Arel.sql("#{quoted_translation_store} ->> '#{locale}' #{direction}"))
end
end
end
end

Expand Down
11 changes: 11 additions & 0 deletions test/translates_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,17 @@ def test_with_translation_relation
end
end

def test_order_by_translation
p = Post.create!(:title_translations => { "en" => "Alice in Wonderland", "fr" => "Alice au pays des merveilles" }, :body_1_translations => { "en" => "English Body", "fr" => "Corps anglais" })
p2 = Post.create!(:title_translations => { "en" => "Zoolander", "fr" => "Zoolandia" }, :body_1_translations => { "en" => "English Body", "fr" => "Corps anglais" })
I18n.with_locale(:en) do
assert_equal p.title_en, Post.order_by_title_translation.first.try(:title)
assert_equal p.title_en, Post.order_by_title_translation(:asc).first.try(:title)
assert_equal p2.title_en, Post.order_by_title_translation(:desc).first.try(:title)
assert_equal p2.title_en, Post.order_by_title_translation(:desc, :fr).first.try(:title)
end
end

def test_with_interpolation_arguments
p = Post.create!(:title_translations => { "en" => "Alice in %{where}" })
I18n.with_locale(:en) do
Expand Down