Class: Decidim::Initiatives::InitiativesFilterFormBuilder

Inherits:
FilterFormBuilder
  • Object
show all
Defined in:
lib/decidim/initiatives/initiatives_filter_form_builder.rb

Overview

This custom Form builder add the fields needed to deal with Initiative types.

Instance Method Summary collapse

Instance Method Details

#initiative_types_select(name, collection, options = {}) ⇒ Object

Public: Generates a select field with the initiative types.

name - The name of the field (usually type_id) collection - A collection of initiative types. options - An optional Hash with options:

  • prompt - An optional String with the text to display as prompt.

Returns a String.



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
# File 'lib/decidim/initiatives/initiatives_filter_form_builder.rb', line 16

def initiative_types_select(name, collection, options = {})
  selected = object.send(name)

  if selected.present?
    if selected == "all"
      types = collection.all.map do |type|
        [type.title[I18n.locale.to_s], type.id]
      end
    else
      selected = selected.values if selected.is_a?(Hash)
      selected = [selected] unless selected.is_a?(Array)
      types = collection.where(id: selected.map(&:to_i)).map do |type|
        [type.title[I18n.locale.to_s], type.id]
      end
    end
  else
    types = []
  end

  prompt = options.delete(:prompt)
  remote_path = options.delete(:remote_path) || false
  multiple = options.delete(:multiple) || false
  html_options = {
    multiple: multiple,
    class: "select2",
    "data-remote-path" => remote_path,
    "data-placeholder" => prompt
  }

  select(name, @template.options_for_select(types, selected: selected), options, html_options)
end