Mobility Ransack
Search on translated attributes with Mobility and Ransack.
Installation
Just add the gem to your Gemfile:
gem 'mobility-ransack', '~> 0.2.1'
Now enable the ransack
plugin in Mobility's configuration so that it can be
used, and optionally set the value for the :ransack
key in default_options
to true
to enable it for all translated attributes on all models.
Mobility.configure do |config|
# ...
config.plugins += [:ransack]
# config.default_options[:ransack] = true
end
If you left the default_options
line above commented out, you will need to
explicitly enable ransack for each attribute you want to search on with the
ransack
option, like this:
class Post < ApplicationRecord
extend Mobility
translates :foo, ransack: true
end
You can search on foo
with Ransack just like any untranslated attribute, e.g.
if Post
has a title
attribute translated with the Jsonb backend:
Post.ransack(title_cont: "foo").result
#=> SELECT "posts".* FROM "posts" WHERE ("posts"."title" ->> 'en') ILIKE '%foo%'
Other backends work exactly the same way.
License
The gem is available as open source under the terms of the MIT License.