Class: Decidim::FormBuilder

Inherits:
FoundationRailsHelper::FormBuilder
  • Object
show all
Includes:
ActionView::Context
Defined in:
lib/decidim/form_builder.rb

Overview

This custom FormBuilder adds fields needed to deal with translatable fields, following the conventions set on ‘Decidim::TranslatableAttributes`.

Direct Known Subclasses

AuthorizationFormBuilder, FilterFormBuilder

Instance Method Summary collapse

Instance Method Details

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

Public: Generates a select field with the categories. Only leaf categories can be set as selected.

name - The name of the field (usually category_id) collection - A collection of categories. options - An optional Hash with options:

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

  • disable_parents - A Boolean to disable parent categories. Defaults to ‘true`.

Returns a String.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/decidim/form_builder.rb', line 88

def categories_select(name, collection, options = {})
  options = {
    disable_parents: true
  }.merge(options)

  disable_parents = options[:disable_parents]

  selected = object.send(name)
  categories = categories_for_select(collection)
  disabled = if disable_parents
               disabled_categories_for(collection)
             else
               []
             end

  select(name, @template.options_for_select(categories, selected: selected, disabled: disabled), options)
end

#editor(name, options = {}) ⇒ Object

Public: generates a hidden field and a container for WYSIWYG editor

name - The name of the field options - The set of options to send to the field

:label   - The Boolean value to create or not the input label (optional) (default: true)
:toolbar - The String value to configure WYSIWYG toolbar. It should be 'basic' or
           or 'full' (optional) (default: 'basic')
:lines   - The Integer to indicate how many lines should editor have (optional) (default: 10)

Renders a container with both hidden field and editor container



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/decidim/form_builder.rb', line 63

def editor(name, options = {})
  options[:toolbar] ||= "basic"
  options[:lines] ||= 10

  (:div, class: "editor") do
    template = ""
    template += label(name) if options[:label] != false
    template += hidden_field(name, options)
    template += (:div, nil, class: "editor-container", data: {
                              toolbar: options[:toolbar]
                            }, style: "height: #{options[:lines]}rem")
    template += error_for(name, options) if error?(name)
    template.html_safe
  end
end

#translated(type, name, options = {}) ⇒ Object

Public: Generates an form field for each locale.

type - The form field’s type, like ‘text_area` or `text_input` name - The name of the field options - The set of options to send to the field

Renders form fields for each locale.



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
51
# File 'lib/decidim/form_builder.rb', line 17

def translated(type, name, options = {})
  if locales.count == 1
    return send(
      type,
      "#{name}_#{locales.first.to_s.gsub("-", "__")}",
      options.merge(label: options[:label] || label_for(name))
    )
  end

  field_label = label_i18n(name, options[:label] || label_for(name))

  tabs_panels = "".html_safe
  if options[:label] != false
    tabs_panels = (:ul, class: "tabs", id: "#{name}-tabs", data: { tabs: true }) do
      locales.each_with_index.inject("".html_safe) do |string, (locale, index)|
        string + (:li, class: tab_element_class_for("title", index)) do
          title = I18n.t(locale, scope: "locales")
          element_class = ""
          element_class += "alert" if error?(name_with_locale(name, locale))
          (:a, title, href: "##{name}-panel-#{index}", class: element_class)
        end
      end
    end
  end

  tabs_content = (:div, class: "tabs-content", data: { tabs_content: "#{name}-tabs" }) do
    locales.each_with_index.inject("".html_safe) do |string, (locale, index)|
      string + (:div, class: tab_element_class_for("panel", index), id: "#{name}-panel-#{index}") do
        send(type, name_with_locale(name, locale), options.merge(label: false))
      end
    end
  end

  safe_join [field_label, tabs_panels, tabs_content]
end