Class: Decidim::FormBuilder
- Inherits:
-
FoundationRailsHelper::FormBuilder
- Object
- FoundationRailsHelper::FormBuilder
- Decidim::FormBuilder
- 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
Instance Method Summary collapse
-
#categories_select(name, collection, options = {}) ⇒ Object
Public: Generates a select field with the categories.
-
#editor(name, options = {}) ⇒ Object
Public: generates a hidden field and a container for WYSIWYG editor.
-
#translated(type, name, options = {}) ⇒ Object
Public: Generates an form field for each locale.
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, = {}) = { disable_parents: true }.merge() disable_parents = [:disable_parents] selected = object.send(name) categories = categories_for_select(collection) disabled = if disable_parents disabled_categories_for(collection) else [] end select(name, @template.(categories, selected: selected, disabled: disabled), ) 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 . 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, = {}) [:toolbar] ||= "basic" [:lines] ||= 10 content_tag(:div, class: "editor") do template = "" template += label(name) if [:label] != false template += hidden_field(name, ) template += content_tag(:div, nil, class: "editor-container", data: { toolbar: [:toolbar] }, style: "height: #{options[:lines]}rem") template += error_for(name, ) 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, = {}) if locales.count == 1 return send( type, "#{name}_#{locales.first.to_s.gsub("-", "__")}", .merge(label: [:label] || label_for(name)) ) end field_label = label_i18n(name, [:label] || label_for(name)) tabs_panels = "".html_safe if [:label] != false tabs_panels = content_tag(:ul, class: "tabs", id: "#{name}-tabs", data: { tabs: true }) do locales.each_with_index.inject("".html_safe) do |string, (locale, index)| string + content_tag(: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)) content_tag(:a, title, href: "##{name}-panel-#{index}", class: element_class) end end end end tabs_content = content_tag(:div, class: "tabs-content", data: { tabs_content: "#{name}-tabs" }) do locales.each_with_index.inject("".html_safe) do |string, (locale, index)| string + content_tag(:div, class: tab_element_class_for("panel", index), id: "#{name}-panel-#{index}") do send(type, name_with_locale(name, locale), .merge(label: false)) end end end safe_join [field_label, tabs_panels, tabs_content] end |