activerecord-hackery / ransack

Object-based searching.
https://activerecord-hackery.github.io/ransack/
MIT License
5.65k stars 790 forks source link

Translation when using with SimpleForm #1160

Open eric-hemasystems opened 3 years ago

eric-hemasystems commented 3 years ago

When using Ransack with SimpleForm the translation is not working. If I do:

<%= f.input :status_eq, collection: %w[Active Inactive] %>

Then my label ends up being "Status eq" rather than "Status equal". This is due to not using the translation support in Ransack. This could be an issue for SimpleForm rather than Ransack. But since the existing integration between the two systems has Ransack modifying itself for SimpleForm rather than the other way around it seemed perhaps start with an issue here.

I do have a monkey-patch to fix it up but it involves monkey-patching a SimpleForm method so not sure it is a suitable strategy. If you don't see another strategy from the Ransack side I can try opening a ticket with SimpleForm to see if they would be willing to modify their method.

My monkey-patch:

module SimpleFormRansackTranslate
  # Adapted from:
  #   https://github.com/activerecord-hackery/ransack/blob/master/lib/ransack/helpers/form_builder.rb#L35-L37
  #
  # Decorating:
  #   https://github.com/heartcombo/simple_form/blob/master/lib/simple_form/components/labels.rb#L75-L83
  #
  # NOTE: Decorated method is protected so higher risk of breakage on upgrade
  def label_translation
    if object.respond_to? :translate
      object.translate reflection_or_attribute_name.to_s, include_associations: true
    else
      super
    end
  end
end

SimpleForm::Inputs::Base.prepend SimpleFormRansackTranslate

This related to PR #341 where support for SimpleForm was added.

abinoam commented 4 months ago

Hi @eric-hemasystems

If you still interested in Ransack / SimpleForm integration I would appreciate your feedback in my PR at #1487