Class: Decidim::FormBuilder
- Inherits:
-
FoundationRailsHelper::FormBuilder
- Object
- FoundationRailsHelper::FormBuilder
- Decidim::FormBuilder
- Includes:
- ActionView::Context, Map::Autocomplete::FormBuilder, TranslatableAttributes
- Defined in:
- decidim-core/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
Admin::FormBuilder, AuthorizationFormBuilder, FilterFormBuilder
Instance Method Summary collapse
-
#areas_select(name, collection, options = {}, html_options = {}) ⇒ Object
Public: Generates a select field for areas.
-
#attachment(attribute, options = {}) ⇒ Object
Public: Generates a file upload field for Decidim::Attachment type of attachment.
-
#categories_select(name, collection, options = {}, html_options = {}) ⇒ Object
Public: Generates a select field with the categories.
-
#check_box(attribute, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
Public: Override so checkboxes are rendered before the label.
- #choose_button_label(attribute) ⇒ Object
-
#collection_check_boxes(attribute, collection, value_attribute, text_attribute, options = {}, html_options = {}) ⇒ Object
Public: generates a check boxes input from a collection and adds help text and errors.
-
#collection_radio_buttons(attribute, collection, value_attribute, text_attribute, options = {}, html_options = {}) ⇒ Object
Public: generates a radio buttons input from a collection and adds help text and errors.
-
#create_language_selector(locales, tabs_id, name) ⇒ Object
rubocop:enable Metrics/ParameterLists.
-
#data_picker(attribute, options = {}, prompt_params = {}) ⇒ Object
Public: Generates a picker field for selection (either simple or multiselect).
-
#editor(name, options = {}) ⇒ Object
Public: generates a hidden field and a container for WYSIWYG editor.
- #form_field_for(attribute, options = {}) ⇒ Object
-
#hashtaggable_text_field(type, name, locale, options = {}) ⇒ Object
Public: Generates a field for hashtaggable type.
-
#label_for(attribute) ⇒ Object
Public: Returns the translated name for the given attribute.
- #max_file_size(record, attribute) ⇒ Object
- #password_field(attribute, options = {}) ⇒ Object
-
#resources_select(name, collection, options = {}) ⇒ Object
Public: Generates a select field for resource types.
-
#scopes_picker(attribute, options = {}) ⇒ Object
Public: Generates a picker field for scope selection.
-
#social_field(type, name, handlers, options = {}) ⇒ Object
Public: Generates a form field for each social.
-
#text_area(attribute, options = {}) ⇒ Object
Discard the pattern attribute which is not allowed for textarea elements.
-
#translated(type, name, options = {}) ⇒ Object
Public: Generates a form field for each locale.
- #translated_one_locale(type, name, locale, options = {}) ⇒ Object
-
#upload(attribute, options = {}) ⇒ Object
Public: Generates a file upload field and sets the form as multipart.
- #upload_help(record, attribute, options = {}) ⇒ Object
Methods included from Map::Autocomplete::FormBuilder
Methods included from TranslatableAttributes
Instance Method Details
#areas_select(name, collection, options = {}, html_options = {}) ⇒ Object
Public: Generates a select field for areas.
name - The name of the field (usually area_id) collection - A collection of areas or area_types.
If it is areas, we sort the selectable options alphabetically.
Returns a String.
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'decidim-core/lib/decidim/form_builder.rb', line 264 def areas_select(name, collection, = {}, = {}) selectables = if collection.first.is_a?(Decidim::Area) assemblies = collection .map { |a| [a.name[I18n.locale.to_s], a.id] } .sort_by { |arr| arr[0] } @template.( assemblies, selected: [:selected] ) else @template.option_groups_from_collection_for_select( collection, :areas, :translated_name, :id, :translated_name, selected: [:selected] ) end select(name, selectables, , ) end |
#attachment(attribute, options = {}) ⇒ Object
Public: Generates a file upload field for Decidim::Attachment type of attachment. It is similar to upload method, but it changes some options so that attachment can have title and different upload validations.
attribute - The String name of the attribute to build the field. options - A Hash with options to build the field. See upload method for more detailed information.
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'decidim-core/lib/decidim/form_builder.rb', line 392 def (attribute, = {}) = object..present? record = ? object. : object = { titled: [:multiple], resource_class: "Decidim::Attachment", show_current: false, max_file_size: max_file_size(record, :file), label: I18n.t("decidim.forms.upload.labels.add_attachment"), button_edit_label: I18n.t("decidim.forms.upload.labels.edit_image"), extension_allowlist: Decidim.organization_settings(record).upload_allowed_file_extensions }.merge() # Upload help uses extension allowlist from the options so we need to call this AFTER setting the defaults. [:help] = upload_help(record, attribute, ) if [:help].blank? upload(attribute, ) end |
#categories_select(name, collection, options = {}, html_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`.
html_options - HTML options for the select
Returns a String.
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'decidim-core/lib/decidim/form_builder.rb', line 238 def categories_select(name, collection, = {}, = {}) = { disable_parents: true }.merge() disable_parents = [:disable_parents] selected = object.send(name) selected = selected.first if selected.is_a?(Array) && selected.length > 1 categories = categories_for_select(collection) disabled = if disable_parents disabled_categories_for(collection) else [] end select(name, @template.(categories, selected:, disabled:), , ) end |
#check_box(attribute, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
Public: Override so checkboxes are rendered before the label.
377 378 379 380 381 382 383 |
# File 'decidim-core/lib/decidim/form_builder.rb', line 377 def check_box(attribute, = {}, checked_value = "1", unchecked_value = "0") custom_label(attribute, [:label], [:label_options], field_before_label: true) do .delete(:label) .delete(:label_options) @template.check_box(@object_name, attribute, (.except(:help_text)), checked_value, unchecked_value) end + error_and_help_text(attribute, ) end |
#choose_button_label(attribute) ⇒ Object
463 464 465 466 467 468 469 470 471 472 473 |
# File 'decidim-core/lib/decidim/form_builder.rb', line 463 def (attribute) @choose_button_label ||= begin if resource_class(attribute).attached_config[attribute].uploader <= Decidim::ImageUploader I18n.t("decidim.forms.upload.labels.add_image") else I18n.t("decidim.forms.upload.labels.add_file") end rescue NoMethodError I18n.t("decidim.forms.upload.labels.add_file") end end |
#collection_check_boxes(attribute, collection, value_attribute, text_attribute, options = {}, html_options = {}) ⇒ Object
Public: generates a check boxes input from a collection and adds help text and errors.
attribute - the name of the field collection - the collection from which we will render the check boxes value_attribute - a Symbol or a Proc defining how to find the value
attribute
text_attribute - a Symbol or a Proc defining how to find the text
attribute
options - a Hash with options html_options - a Hash with options
Renders a collection of check boxes. rubocop:disable Metrics/ParameterLists
27 28 29 |
# File 'decidim-core/lib/decidim/form_builder.rb', line 27 def collection_check_boxes(attribute, collection, value_attribute, text_attribute, = {}, = {}) error_and_help_text(attribute, ) + super end |
#collection_radio_buttons(attribute, collection, value_attribute, text_attribute, options = {}, html_options = {}) ⇒ Object
Public: generates a radio buttons input from a collection and adds help text and errors.
attribute - the name of the field collection - the collection from which we will render the radio buttons value_attribute - a Symbol or a Proc defining how to find the value attribute text_attribute - a Symbol or a Proc defining how to find the text attribute options - a Hash with options html_options - a Hash with options
Renders a collection of radio buttons. rubocop:disable Metrics/ParameterLists
44 45 46 |
# File 'decidim-core/lib/decidim/form_builder.rb', line 44 def (attribute, collection, value_attribute, text_attribute, = {}, = {}) error_and_help_text(attribute, ) + super end |
#create_language_selector(locales, tabs_id, name) ⇒ Object
rubocop:enable Metrics/ParameterLists
49 50 51 52 53 54 55 |
# File 'decidim-core/lib/decidim/form_builder.rb', line 49 def create_language_selector(locales, tabs_id, name) if locales.count > 4 language_selector_select(locales, tabs_id, name) else language_tabs(locales, tabs_id, name) end end |
#data_picker(attribute, options = {}, prompt_params = {}) ⇒ Object
Public: Generates a picker field for selection (either simple or multiselect).
attribute - The name of the object’s attribute. options - A Hash with options:
-
multiple: Multiple mode, to allow selection of multiple items.
-
label: Show label?
-
name: (optional) The name attribute of the input elements.
prompt_params - Hash with options:
-
url: The url where the ajax endpoint that will fill the content of the selector popup (the prompt).
-
text: Text in the button to open the Data Picker selector.
Also it should receive a block that returns a Hash with :url and :text for each selected scope
Returns an html String.
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'decidim-core/lib/decidim/form_builder.rb', line 358 def data_picker(attribute, = {}, prompt_params = {}) = { id: "#{@object_name}_#{attribute}", class: "picker-#{[:multiple] ? "multiple" : "single"}", name: [:name] || "#{@object_name}[#{attribute}]" } [:class] += " is-invalid-input" if error?(attribute) [:class] += " picker-autosort" if [:autosort] items = object.send(attribute).collect { |item| [item, yield(item)] } template = "" template += label(attribute, label_for(attribute) + required_for_attribute(attribute)) unless [:label] == false template += error_and_help_text(attribute, ) template += @template.render("decidim/widgets/data_picker", picker_options:, prompt_params:, items:) template.html_safe 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)
:help_text - The help text to display
:disabled - Whether the editor should be disabled
Renders a container with both hidden field and editor container
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'decidim-core/lib/decidim/form_builder.rb', line 188 def editor(name, = {}) [:disabled] ||= false = .delete(:toolbar) || "basic" lines = .delete(:lines) || 10 label_text = [:label].to_s label_text = label_for(name) if label_text.blank? .delete(:required) help_text = .delete(:help_text) editor_image = Decidim::EditorImage.new = (editor_image, ) = extract_validations(name, ).merge() @template.append_stylesheet_pack_tag "decidim_editor" @template.append_javascript_pack_tag "decidim_editor", defer: false content_tag( :div, class: "editor #{"hashtags__container" if [:editor]["class"].include?("js-hashtags")}", id: "#{sanitize_for_dom_selector(@object_name)}_#{sanitize_for_dom_selector(name)}" ) do template = "" template += label(name, label_text + required_for_attribute(name), for: nil) if .fetch(:label, true) template += hidden_field(name, .merge(id: nil)) template += content_tag(:span, help_text, class: "help-text") if help_text template += content_tag( :div, nil, class: [:editor].delete("class").join(" "), data: { toolbar:, disabled: [:disabled], options: [:editor] } ) { content_tag(:div, nil, class: "editor-input", style: "height: #{lines}rem") } template += error_for(name, ) if error?(name) template += editor_upload(editor_image, [:upload]) template.html_safe end end |
#form_field_for(attribute, options = {}) ⇒ Object
509 510 511 512 513 514 515 |
# File 'decidim-core/lib/decidim/form_builder.rb', line 509 def form_field_for(attribute, = {}) if attribute == :body text_area(attribute, .merge(rows: 10)) else text_field(attribute, ) end end |
#hashtaggable_text_field(type, name, locale, options = {}) ⇒ Object
Public: Generates a field for hashtaggable type. type - The form field’s type, like ‘text_area` or `text_field` name - The name of the field locale - The locale to be created options - The set of options to send to the field
Renders form fields for each locale.
122 123 124 125 126 127 128 129 130 131 132 |
# File 'decidim-core/lib/decidim/form_builder.rb', line 122 def hashtaggable_text_field(type, name, locale, = {}) [:hashtaggable] = true if type.to_sym == :editor content_tag(:div, class: "hashtags__container") do if [:value] send(type, name_with_locale(name, locale), .merge(label: [:label], value: [:value][locale])) else send(type, name_with_locale(name, locale), .merge(label: [:label])) end end end |
#label_for(attribute) ⇒ Object
Public: Returns the translated name for the given attribute.
attribute - The String name of the attribute to return the name.
501 502 503 504 505 506 507 |
# File 'decidim-core/lib/decidim/form_builder.rb', line 501 def label_for(attribute) if object.class.respond_to?(:human_attribute_name) object.class.human_attribute_name(attribute) else attribute.to_s.humanize end end |
#max_file_size(record, attribute) ⇒ Object
459 460 461 |
# File 'decidim-core/lib/decidim/form_builder.rb', line 459 def max_file_size(record, attribute) Decidim::FileValidatorHumanizer.new(record, attribute).max_file_size end |
#password_field(attribute, options = {}) ⇒ Object
97 98 99 100 101 102 103 |
# File 'decidim-core/lib/decidim/form_builder.rb', line 97 def password_field(attribute, = {}) field attribute, do |opts| opts[:autocomplete] ||= :off opts[:class] ||= "input-group-field" method(__method__).super_method.super_method.call(attribute, opts) end end |
#resources_select(name, collection, options = {}) ⇒ Object
Public: Generates a select field for resource types.
name - The name of the field (usually resource_type) collection - A collection of resource types.
The options are sorted alphabetically.
Returns a String.
295 296 297 298 299 300 301 302 |
# File 'decidim-core/lib/decidim/form_builder.rb', line 295 def resources_select(name, collection, = {}) resources = collection .map { |r| [I18n.t(r.split("::").last.underscore, scope: "decidim.components.component_order_selector.order"), r] } .sort select(name, @template.(resources, selected: [:selected]), ) end |
#scopes_picker(attribute, options = {}) ⇒ Object
Public: Generates a picker field for scope selection.
attribute - The name of the field (usually scope_id) options - An optional Hash with options:
-
multiple - Multiple mode, to allow multiple scopes selection.
-
label - Show label?
-
checkboxes_on_top - Show checked picker values on top (default) or below the picker prompt (only for multiple pickers)
-
namespace - prepend a custom name to the html element’s DOM id.
Also it should receive a block that returns a Hash with :url and :text for each selected scope (and for null scope for prompt)
Returns a String.
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'decidim-core/lib/decidim/form_builder.rb', line 316 def scopes_picker(attribute, = {}) id = if self..has_key?(:namespace) "#{self.[:namespace]}_#{sanitize_for_dom_selector(@object_name)}" else "#{sanitize_for_dom_selector(@object_name)}_#{attribute}" end = { id:, class: "picker-#{[:multiple] ? "multiple" : "single"}", name: "#{@object_name}[#{attribute}]" } [:class] += " is-invalid-input" if error?(attribute) prompt_params = yield(nil) scopes = selected_scopes(attribute).map { |scope| [scope, yield(scope)] } template = "" template += "<label>#{label_for(attribute) + required_for_attribute(attribute)}</label>" unless [:label] == false template += @template.render("decidim/scopes/scopes_picker_input", picker_options:, prompt_params:, scopes:, values_on_top: ![:multiple] || [:checkboxes_on_top]) template += error_and_help_text(attribute, ) template.html_safe end |
#social_field(type, name, handlers, options = {}) ⇒ Object
Public: Generates a form field for each social.
type - The form field’s type, like ‘text_area` or `text_field` name - The name of the field handlers - The social handlers to be created options - The set of options to send to the field
Renders form fields for each locale.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'decidim-core/lib/decidim/form_builder.rb', line 142 def (type, name, handlers, = {}) tabs_id = sanitize_tabs_selector([:tabs_id] || "#{object_name}-#{name}-tabs") label_tabs = content_tag(:div, class: "label--tabs") do field_label = label_i18n(name, [:label] || label_for(name), required: [:required]) tabs_panels = "".html_safe if [:label] != false tabs_panels = content_tag(:ul, class: "tabs tabs--lang", id: tabs_id, data: { tabs: true }) do handlers.each_with_index.inject("".html_safe) do |string, (handler, index)| string + content_tag(:li, class: tab_element_class_for("title", index)) do title = I18n.t(".#{handler}", scope: "activemodel.attributes.#{object_name}") tab_content_id = sanitize_tabs_selector "#{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 handlers.each_with_index.inject("".html_safe) do |string, (handler, index)| tab_content_id = sanitize_tabs_selector "#{tabs_id}-#{name}-panel-#{index}" string + content_tag(:div, class: tab_element_class_for("panel", index), id: tab_content_id) do send(type, "#{handler}_handler", .merge(label: false)) end end end safe_join [label_tabs, tabs_content] end |
#text_area(attribute, options = {}) ⇒ Object
Discard the pattern attribute which is not allowed for textarea elements.
518 519 520 521 522 523 524 525 526 527 528 |
# File 'decidim-core/lib/decidim/form_builder.rb', line 518 def text_area(attribute, = {}) field(attribute, ) do |opts| opts.delete(:pattern) @template.send( :text_area, @object_name, attribute, (opts) ) end end |
#translated(type, name, options = {}) ⇒ Object
Public: Generates a form field for each locale.
type - The form field’s type, like ‘text_area` or `text_field` name - The name of the field options - The set of options to send to the field
Renders form fields for each locale.
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 |
# File 'decidim-core/lib/decidim/form_builder.rb', line 64 def translated(type, name, = {}) return translated_one_locale(type, name, locales.first, .merge(label: ([:label] || label_for(name)))) if locales.count == 1 tabs_id = sanitize_tabs_selector([:tabs_id] || "#{object_name}-#{name}-tabs") label_tabs = content_tag(:div, class: "label--tabs") do field_label = label_i18n(name, [:label] || label_for(name), required: [:required]) language_selector = "".html_safe language_selector = create_language_selector(locales, tabs_id, name) if [:label] != false safe_join [field_label, language_selector] end hashtaggable = .delete(:hashtaggable) 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 if hashtaggable hashtaggable_text_field(type, name, locale, .merge(label: false)) elsif type.to_sym == :editor send(type, name_with_locale(name, locale), .merge(label: false, hashtaggable:)) else send(type, name_with_locale(name, locale), .merge(label: false)) end end end end safe_join [label_tabs, tabs_content] end |
#translated_one_locale(type, name, locale, options = {}) ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'decidim-core/lib/decidim/form_builder.rb', line 105 def translated_one_locale(type, name, locale, = {}) return hashtaggable_text_field(type, name, locale, ) if .delete(:hashtaggable) send( type, "#{name}_#{locale.to_s.gsub("-", "__")}", .merge(label: [:label] || label_for(name)) ) end |
#upload(attribute, options = {}) ⇒ Object
Public: Generates a file upload field and sets the form as multipart. If the file is an image it displays the default image if present or the current one. By default it also generates a button to replace the file.
attribute - The String name of the attribute to build the field. options - A Hash with options to build the field.
* max_file_size: Maximum size for the file (If you really want to change max
file size you should probably change it in validator).
* resouce_name: Name of the resource (e.g. user)
* resource_class: Attribute's resource class (e.g. Decidim::User)
* resouce_class: Class of the resource (e.g. user)
* required: Whether the file is required or not (false by default).
* titled: Whether the file can have title or not.
* show_current: Whether the current file is displayed next to the button.
* help: Array of help messages which are displayed inside of the upload modal.
* dimensions_info: Hash about resize dimensions (e.g. {:medium=>{:processor=>:resize_to_fit, :dimensions=>[600, 160]}})
produces 'Resized to fit 600 x 160 px.'
* extension_allowlist: Array of allowed file extensions (e.g. %w(jpeg jpg png))
* label: Label for the attribute
* button_label: Label for the button
* button_edit_label: Button label when file is already selected.
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'decidim-core/lib/decidim/form_builder.rb', line 432 def upload(attribute, = {}) self.multipart = true max_file_size = [:max_file_size] || max_file_size(object, attribute) = [:button_label] || (attribute) = [:help] || upload_help(object, attribute, ) = { attribute:, resource_name: @object_name, resource_class: [:resource_class]&.to_s || resource_class(attribute), required: false, titled: false, show_current: true, max_file_size:, help: , label: label_for(attribute), button_label:, button_edit_label: I18n.t("decidim.forms.upload.labels.replace") }.merge() ::Decidim::ViewModel.cell( "decidim/upload_modal", self, ).call end |
#upload_help(record, attribute, options = {}) ⇒ Object
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 |
# File 'decidim-core/lib/decidim/form_builder.rb', line 475 def upload_help(record, attribute, = {}) humanizer = FileValidatorHumanizer.new(record, attribute) help_scope = if [:help_i18n_scope].present? [:help_i18n_scope] elsif humanizer.uploader.is_a?(Decidim::ImageUploader) "decidim.forms.file_help.image" else "decidim.forms.file_help.file" end = if [:help_i18n_messages].present? Array([:help_i18n_messages]) else %w(message_1 message_2) end = .each.map { |msg| I18n.t(msg, scope: help_scope) } + humanizer. += extension_allowlist_help([:extension_allowlist]) if [:extension_allowlist] += image_dimensions_help([:dimensions_info]) if [:dimensions_info] end |