Module: CoreSelectTwoHelper
- Defined in:
- app/helpers/core_select_two_helper.rb
Overview
Helpers to render select form fields using select 2 framework: select2.org
Instance Method Summary collapse
- #field_placeholder_text(model, field, default: 'Select Option') ⇒ Object
- #select_two_field(model, field, options = {}) ⇒ Object abstract
- #select_two_option_key_values(value, options) ⇒ Object
- #select_two_tag(model, field, options = {}) ⇒ Object
Instance Method Details
#field_placeholder_text(model, field, default: 'Select Option') ⇒ Object
41 42 43 44 |
# File 'app/helpers/core_select_two_helper.rb', line 41 def field_placeholder_text(model, field, default: 'Select Option') hint_key = "ui_form.#{model.class.to_s.underscore}.placeholders.#{field}" I18n.exists?(hint_key) ? I18n.t(hint_key) : default end |
#select_two_field(model, field, options = {}) ⇒ Object
This method is abstract.
Create the select two field for the model and field
TODO:
convert generic options hash to parameters, maybe.…
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'app/helpers/core_select_two_helper.rb', line 7 def select_two_field(model, field, = {}) value = model.send(field) hint_key = "ui_form.#{model.class.to_s.underscore}.hints.#{field}" if I18n.exists?(hint_key) base_classes = %w[form-floating form-floating-outline col tooltipped] data = { tooltip: I18n.t(hint_key), position: :top } else base_classes = %w[form-floating form-floating-outline col mb-3] data = {} end classes = (base_classes + ([:classes] || %w[col-sm-12 col-md-6 col-lg-4 col-xl-3 col-xxl-2])).join(' ') content_tag(:div, class: classes, data: data) do concat(select_two_tag(model, field, )) concat(html5_label_tag(model, field, value, )) end end |
#select_two_option_key_values(value, options) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'app/helpers/core_select_two_helper.rb', line 46 def select_two_option_key_values(value, ) = [] # select_options << { key: '', value: options[:prompt], selected: false } if options[:prompt].present? [:options].each do |option_value| option_value[:display_name] = option_value.display_name if option_value.respond_to?(:display_name) option_value[:display_name] = option_value.user_display_name if option_value.respond_to?(:user_display_name) << case option_value when String, Integer { key: option_value, value: option_value, selected: value.eql?(option_value) } when Array { key: option_value.last, value: option_value.first, selected: value.to_s.eql?(option_value.last.to_s) } when Hash { key: option_value[:key], value: option_value[:value], selected: value.eql?(option_value[:key]) } else { key: option_value[:_id].to_s, value: option_value[:display_name] || option_value[:name], selected: value.eql?(option_value) } end end end |
#select_two_tag(model, field, options = {}) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'app/helpers/core_select_two_helper.rb', line 24 def select_two_tag(model, field, = {}) select_content = { id: html5_field_id(model, field, ), name: html5_field_name(model, field, ), class: [[:input_classes], 'select2', 'form-select', 'form-select-lg'].compact.join(' '), disabled: [:disabled], data: { 'allow-clear': true, placeholder: field_placeholder_text(model, field, default: [:prompt]) } } content_tag(:select, select_content) do select_two_option_key_values(model.send(field), ).each do |value| concat(content_tag(:option, value: value[:key], selected: value[:selected]) do concat(value[:value]) end) end end end |