Module: StimulusPlumbers::Form::Fields::Inputs::Select::Grouped

Included in:
Builder
Defined in:
lib/stimulus_plumbers/form/fields/inputs/select/grouped.rb

Instance Method Summary collapse

Instance Method Details

#grouped_collection_select(attribute, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/stimulus_plumbers/form/fields/inputs/select/grouped.rb', line 9

def grouped_collection_select(
  attribute,
  collection,
  group_method,
  group_label_method,
  option_key_method,
  option_value_method,
  options = {},
  html_options = {}
)
  html_native = options.delete(:html_native) { false }
  Field.new(@template, **options).render(
    object,
    attribute,
    input_id: field_id(attribute)
  ) do |html_opts, opts, error|
    merged_html_opts = merge_html_options(html_options, html_opts, field_theme(:form_select, error: error))
    if html_native
      super(
        attribute,
        collection,
        group_method,
        group_label_method,
        option_key_method,
        option_value_method,
        opts,
        merged_html_opts
      )
    else
      render_select_dropdown(attribute, opts, merged_html_opts, err: error) do
        collection.map do |group|
          {
            label:   group.public_send(group_label_method),
            options: group.public_send(group_method).map do |item|
              [item.public_send(option_value_method), item.public_send(option_key_method)]
            end
          }
        end
      end
    end
  end
end