Module: Decidim::DecidimFormHelper
- Defined in:
- app/helpers/decidim/decidim_form_helper.rb
Overview
A heper to expose an easy way to add authorization forms in a view.
Instance Method Summary collapse
-
#decidim_form_for(record, options = {}, &block) ⇒ Object
A custom form for that injects client side validations with Abide.
-
#editor_field_tag(name, value, options = {}) ⇒ Object
A custom helper to include an editor field without requiring a form object.
-
#name_with_locale(name, locale) ⇒ Object
Helper method used by ‘translated_field_tag`.
-
#tab_element_class_for(type, index) ⇒ Object
Helper method used by ‘translated_field_tag`.
-
#translated_field_tag(type, object_name, name, value = {}, options = {}) ⇒ Object
A custom helper to include a translated field without requiring a form object.
Instance Method Details
#decidim_form_for(record, options = {}, &block) ⇒ Object
A custom form for that injects client side validations with Abide.
record - The object to build the form for. options - A Hash of options to pass to the form builder. &block - The block to execute as content of the form.
Returns a String.
13 14 15 16 17 |
# File 'app/helpers/decidim/decidim_form_helper.rb', line 13 def decidim_form_for(record, = {}, &block) [:data] ||= {} [:data].update(abide: true, "live-validate" => true, "validate-on-blur" => true) form_for(record, , &block) end |
#editor_field_tag(name, value, options = {}) ⇒ Object
A custom helper to include an editor field without requiring a form object
name - The input name value - The input value 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)
Returns a rich editor to be included in a html template.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'app/helpers/decidim/decidim_form_helper.rb', line 30 def editor_field_tag(name, value, = {}) [:toolbar] ||= "basic" [:lines] ||= 10 content_tag(:div, class: "editor") do template = "" template += label_tag(name, [:label]) if [:label] != false template += hidden_field_tag(name, value, ) template += content_tag(:div, nil, class: "editor-container", data: { toolbar: [:toolbar] }, style: "height: #{options[:lines]}rem") template.html_safe end end |
#name_with_locale(name, locale) ⇒ Object
Helper method used by ‘translated_field_tag`
111 112 113 |
# File 'app/helpers/decidim/decidim_form_helper.rb', line 111 def name_with_locale(name, locale) "#{name}_#{locale.to_s.gsub("-", "__")}" end |
#tab_element_class_for(type, index) ⇒ Object
Helper method used by ‘translated_field_tag`
104 105 106 107 108 |
# File 'app/helpers/decidim/decidim_form_helper.rb', line 104 def tab_element_class_for(type, index) element_class = "tabs-#{type}" element_class += " is-active" if index.zero? element_class end |
#translated_field_tag(type, object_name, name, value = {}, options = {}) ⇒ Object
A custom helper to include a translated field without requiring a form object.
type - The type of the translated input field. object_name - The object name used to identify the Foundation tabs. name - The name of the input which will be suffixed with the corresponding locales. value - A hash containing the value for each locale. options - An optional hash of options.
* enable_tabs: Adds the data-tabs attribute so Foundation picks up automatically.
* tabs_id: The id to identify the Foundation tabs element.
* label: The label used for the field.
Returns a Foundation tabs element with the translated input field.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'app/helpers/decidim/decidim_form_helper.rb', line 57 def translated_field_tag(type, object_name, name, value = {}, = {}) locales = available_locales tabs_id = [:tabs_id] || "#{object_name}-#{name}-tabs" enabled_tabs = [:enable_tabs].nil? ? true : [:enable_tabs] tabs_panels_data = enabled_tabs ? { tabs: true } : {} if locales.count == 1 return send( type, "#{name}_#{locales.first.to_s.gsub("-", "__")}", .merge(label: [:label]) ) end label_tabs = content_tag(:div, class: "label--tabs") do field_label = label_tag(name, [:label]) tabs_panels = "".html_safe if [:label] != false tabs_panels = content_tag(:ul, class: "tabs tabs--lang", id: tabs_id, data: tabs_panels_data) 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.with_locale(locale) { I18n.t("name", scope: "locale") } tab_content_id = "#{tabs_id}-#{name}-panel-#{index}" content_tag(:a, title, href: "##{tab_content_id}") end end end end safe_join [field_label, tabs_panels] end tabs_content = content_tag(:div, class: "tabs-content", data: { tabs_content: tabs_id }) do locales.each_with_index.inject("".html_safe) do |string, (locale, index)| tab_content_id = "#{tabs_id}-#{name}-panel-#{index}" string + content_tag(:div, class: tab_element_class_for("panel", index), id: tab_content_id) do send(type, "#{object_name}[#{name_with_locale(name, locale)}]", value[locale.to_s], .merge(id: "#{tabs_id}_#{name}_#{locale}", label: false)) end end end safe_join [label_tabs, tabs_content] end |