Simple_form: Allow to define multiple wrapper mappings

Created on 25 Jan 2019  路  5Comments  路  Source: heartcombo/simple_form

Environment

  • Ruby 2.5.3
  • Rails 5.2.2
  • Simple Form 4.1.0

Proposal

The current wrapper mappings allow to specify how every field type should be mapped and this allows to have a consistent style across your application. However there is a need to have additional styles / wrappers for different kinds of search inputs, a typical example is search forms. For example in our application we have Vertical Forms, using custom bootstrap wrapper mappings but for the search filters (using ransack) above our tables we would more like an inline style where labels are hidden by default.

Current solution is to always pass in the wrapper_mappings in the simple_form_tag but even with a helper method around it, this is a bit error prone. I propose to add the concept of wrapper sets (as originally proposed in #1229).

For backwards compatibility the default configuration would still be possible

config.wrapper_mappings = {
    boolean:       :custom_boolean,
    check_boxes:   :custom_collection,
    date:          :custom_multi_select,
    datetime:      :custom_multi_select,
    file:          :custom_file,
    radio_buttons: :custom_collection,
    range:         :custom_range,
    time:          :custom_multi_select,
    select:        :custom_multi_select
  }

Internally this will be set on wrapper_mappings[:default] and used across the board, advance usage could then also define a new set of wrappers

config.wrapper_mappings[:search] = {
    boolean:       :custom_boolean_search,
    check_boxes:   :custom_collection_search,
    date:          :custom_multi_select_search,
    datetime:      :custom_multi_select_search,
    file:          :custom_file_search,
    radio_buttons: :custom_collection_search,
    range:         :custom_range_search,
    time:          :custom_multi_select_search,
    select:        :custom_multi_select_search
  }

This could then be used in the form asfollowed

simple_form_for(animal, wrapper_mappings: :search)
OR
simple_form_for(animal, wrapper_set: :search)

I'm willing to spend some time on this if it will be accepted as a MR

Regards,

Feature request

Most helpful comment

As a quick workaround, you can create a custom simple_form helper and add some logic there :

# app/helpers/application_helper.rb
module ApplicationHelper
    def my_simple_form_for(record, options, &block)
      if options[:wrapper] == :search && options[:wrapper_mappings].nil?
          options[:wrapper_mappings] = {
            boolean:       :custom_boolean_search,
            check_boxes:   :custom_collection_search,
            .....
          }
      end
      simple_form_for(record, options, &block)
    end
  end

All 5 comments

I'm ok with this proposal. @rafaelfranca any thoughts?

One additional thought on this:
From my point of view it would be very helpful to set the wrapper mapping per form wrapper.

If you have multiple form wrappers defined according to the proposal of @JanStevens it would be still necessary to select the wrapper mapping every time I render a form with an explicit wrapper instead of setting a default once when the form wrapper is defined.

I think it would be helpful to end up with a result like this:

config.wrappers :my_special_form do |b|
  b.wrapper_mapping :my_special_mapping
  b.use :input
  # ...
end

config.wrapper_mappings[:my_special_mapping] = {
  boolean: :my_special_boolean,
  # ....
}

Then when using the form in the view like this it would know which form wrapper to use and also automatically which mapping to use for this wrapper:

simple_form_for(animal, wrapper: :my_special_form)

Multiple wrapper mappings would be really helpful.

@JanStevens Any news on this issue? Are you still planning to submit a pull request?

Sadly no, I've moved on and not actively working with rails anymore 馃槵

As a quick workaround, you can create a custom simple_form helper and add some logic there :

# app/helpers/application_helper.rb
module ApplicationHelper
    def my_simple_form_for(record, options, &block)
      if options[:wrapper] == :search && options[:wrapper_mappings].nil?
          options[:wrapper_mappings] = {
            boolean:       :custom_boolean_search,
            check_boxes:   :custom_collection_search,
            .....
          }
      end
      simple_form_for(record, options, &block)
    end
  end
Was this page helpful?
0 / 5 - 0 ratings