Module: CoreFormHelper
- Defined in:
- app/helpers/core_form_helper.rb
Overview
helpers for creating forms
Instance Method Summary collapse
-
#form_date_field(model, field, options = {}) ⇒ Object
Text field.
- #form_field_id(model, field, options = {}) ⇒ HTML abstract
- #form_field_name(model, field, options = {}) ⇒ HTML abstract
-
#form_file(model, field, file_types = '.xlsx', classes = %w[s12 m6 l4 xl3])) ⇒ Object
File field.
- #form_helper_text(model, field, default = nil) ⇒ String? abstract
-
#form_hint_tag(model, field) ⇒ Object
Get the hint for the field and add it.
- #form_hint_text(model, field, default = nil) ⇒ String? abstract
-
#form_label_tag(model, field, value, options = {}) ⇒ Object
get the label for field.
- #form_localized_text(model, field, type, default = nil) ⇒ String? abstract
- #form_place_holder_text(model, field, default = nil) ⇒ String? abstract
- #form_radio_button(model, field, options = {}) ⇒ Object
- #form_required_field?(model, field) ⇒ Boolean abstract
-
#form_select(model, field, options = {}) ⇒ Object
Select field.
-
#form_select_option_key_values(value, options) ⇒ Object
Return an array of hashes for the selection to work easily.
-
#form_select_tag(model, field, options = {}) ⇒ Object
Create the select tag.
-
#form_text_area(model, field, options = {}) ⇒ Object
Text area.
-
#form_time_field(model, field, options = {}) ⇒ Object
Time picker field.
- #input_label(model, field, default = model.class.human_attribute_name(field)) ⇒ String? abstract
- #required_field?(model, field) ⇒ Boolean
- #text_area_tag(model, field, value, options = {}) ⇒ Object
Instance Method Details
#form_date_field(model, field, options = {}) ⇒ Object
Text field
def form_text_field(model, field, options = {})
classes = [:classes] || %w[s12 m6 l4 xl3]
value = model.send(field)
[:type] ||= :text
[:value] = value
[:disabled] ||= false
= (model, field, )
content_tag(:div, class: (%w[input-field col] + classes).join(' ')) do
concat(tag(:input, ))
concat(form_label_tag(model, field, value, ))
end
end
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/helpers/core_form_helper.rb', line 53 def form_date_field(model, field, = {}) classes = [:classes] || %w[s12 m6 l4 xl3] value = model.send(field) [:value] = value.strftime('%d %B, %Y') if value.present? [:type] = :text [:disabled] ||= false = (model, field, ) [:class] = [:date_picker_type] || 'simple-date-picker' content_tag(:div, class: (%w[input-field col] + classes).join(' ')) do concat(tag(:input, )) concat(form_label_tag(model, field, value, )) end end |
#form_field_id(model, field, options = {}) ⇒ HTML
Return a consistent form field id
330 331 332 333 334 335 |
# File 'app/helpers/core_form_helper.rb', line 330 def form_field_id(model, field, = {}) return [:form_id] if [:form_id].present? field = "#{field}_id" if model.class.reflect_on_association(field).present? [[:form_id_prefix], [:base_name], [:array_name] || model.class.to_s.underscore, [:index], field].compact.join('_') end |
#form_field_name(model, field, options = {}) ⇒ HTML
Return a consistent form field name
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'app/helpers/core_form_helper.rb', line 304 def form_field_name(model, field, = {}) return [:form_name] if [:form_name].present? # TODO: Need to handle the other side of the 1:M use case where # the field name needs to end in _ids, not _id. field = "#{field}_id" if model.class.reflect_on_association(field).present? if [:index].present? if [:array_name].present? if [:base_name].present? "#{options[:base_name]}[#{options[:array_name]}[#{options[:index]}][#{field}]]" else "#{options[:array_name]}[#{options[:index]}][#{field}]" end else "#{model.class.to_s.underscore}[#{options[:index]}][#{field}]" end else "#{model.class.to_s.underscore}[#{field}]" end end |
#form_file(model, field, file_types = '.xlsx', classes = %w[s12 m6 l4 xl3])) ⇒ Object
File field
218 219 220 221 222 223 224 225 226 227 |
# File 'app/helpers/core_form_helper.rb', line 218 def form_file(model, field, file_types = '.xlsx', classes = %w[s12 m6 l4 xl3]) form_name = form_field_name(model, field) form_id = "#{model.class.to_s.underscore}_#{field}" value = model.send(field) file_tag = tag(:input, id: form_id, name: form_name, type: :file, accept: file_types) content_tag(:div, class: (%w[input-field col] + classes).join(' ')) do concat(file_tag) concat(form_label_tag(model, field, value)) end end |
#form_helper_text(model, field, default = nil) ⇒ String?
Get the helper text for this input element
370 371 372 |
# File 'app/helpers/core_form_helper.rb', line 370 def form_helper_text(model, field, default = nil) form_localized_text(model, field, :helpers, default) end |
#form_hint_tag(model, field) ⇒ Object
Get the hint for the field and add it
261 262 263 264 265 266 267 268 |
# File 'app/helpers/core_form_helper.rb', line 261 def form_hint_tag(model, field) key = "ui_form.#{model.class.to_s.underscore}.hints.#{field}" return nil unless I18n.exists?(key) content_tag(:p, class: 'form-hint', for: form_field_id(model, field)) do concat(I18n.t(key)) end end |
#form_hint_text(model, field, default = nil) ⇒ String?
Get the hint text for this input element
379 380 381 |
# File 'app/helpers/core_form_helper.rb', line 379 def form_hint_text(model, field, default = nil) form_localized_text(model, field, :hints, default) end |
#form_label_tag(model, field, value, options = {}) ⇒ Object
get the label for field
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'app/helpers/core_form_helper.rb', line 232 def form_label_tag(model, field, value, = {}) # don't do a label if we are in default browser mode return if [:no_label] return if [:input_classes].present? && [:input_classes].include?('browser-default') help_text = form_helper_text(model, field) # or if we have a prompt with now value place_holder = [:place_holder] || form_place_holder_text(model, field) return if place_holder.blank? && value.blank? && [:prompt].present? error = model.errors[field] classes = %w[form-label mb-2] classes << 'text-red' if error.present? [:class] = classes.compact.uniq.join(' ') [:for] = form_field_id(model, field, ) # options['data-error'] = error.join(', ') if error.present? [:required] = form_required_field?(model, field) ? 'required' : nil tag.label(**) do concat(input_label(model, field, field.to_s.humanize)) concat(content_tag(:span, class: 'form-help ms-2', data: { bs_toggle: :popover, bs_content: help_text }) { '?' }) if help_text.present? # @todo CMS add handling specific errors later # concat(" #{error.join(', ')}") if error.present? end end |
#form_localized_text(model, field, type, default = nil) ⇒ String?
Get the localized value for the given key
402 403 404 405 406 407 |
# File 'app/helpers/core_form_helper.rb', line 402 def form_localized_text(model, field, type, default = nil) key = ['ui_form', model.class.to_s.underscore, type, field].join('.') I18n.exists?(key) ? I18n.t(key) : default rescue StandardError default end |
#form_place_holder_text(model, field, default = nil) ⇒ String?
Get the placeholder for this input element
361 362 363 |
# File 'app/helpers/core_form_helper.rb', line 361 def form_place_holder_text(model, field, default = nil) form_localized_text(model, field, :placeholders, default) end |
#form_radio_button(model, field, options = {}) ⇒ Object
347 348 349 350 351 352 353 354 |
# File 'app/helpers/core_form_helper.rb', line 347 def (model, field, = {}) value = model.send(field) classes = (%w[input-field col] + [:classes] || []).join(' ') content_tag(:div, class: classes) do concat(form_select_tag(model, field, )) concat(form_label_tag(model, field, value)) end end |
#form_required_field?(model, field) ⇒ Boolean
Determine if the field is required or not
341 342 343 344 345 |
# File 'app/helpers/core_form_helper.rb', line 341 def form_required_field?(model, field) model.class.validators_on(field).any? { |v| v.is_a?(Mongoid::Validatable::PresenceValidator) } rescue StandardError false end |
#form_select(model, field, options = {}) ⇒ Object
Select field
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'app/helpers/core_form_helper.rb', line 102 def form_select(model, field, = {}) value = model.send(field) raise 'Prompt needed' if value.blank? && [:prompt].blank? && ![:no_label] hint_key = "ui_form.#{model.class.to_s.underscore}.hints.#{field}" if I18n.exists?(hint_key) base_classes = %w[input-field col tooltipped] data = { tooltip: I18n.t(hint_key), position: :top } else base_classes = %w[input-field col] data = {} end classes = (base_classes + ([:classes] || %w[s12 m6 l4 xl3])).join(' ') content_tag(:div, class: classes, data: data) do concat(form_select_tag(model, field, )) concat(form_label_tag(model, field, value, )) end end |
#form_select_option_key_values(value, options) ⇒ Object
Return an array of hashes for the selection to work easily
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 |
# File 'app/helpers/core_form_helper.rb', line 142 def form_select_option_key_values(value, ) = [] << { key: '', value: [:prompt], selected: false } if [: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 |
#form_select_tag(model, field, options = {}) ⇒ Object
Create the select tag
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'app/helpers/core_form_helper.rb', line 124 def form_select_tag(model, field, = {}) select_content = { id: form_field_id(model, field, ), name: form_field_name(model, field, ), class: [[:input_classes], 'materialize'].compact.join(' '), disabled: [:disabled] } content_tag(:select, select_content) do form_select_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 |
#form_text_area(model, field, options = {}) ⇒ Object
Text area
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'app/helpers/core_form_helper.rb', line 16 def form_text_area(model, field, = {}) classes = [:classes] || %w[s12] value = model.send(field) # options[:value] = value # options[:disabled] ||= false # tag_options = {class: []} # text_field_options(model, field, options) # tag_options[:class] += ['materialize-textarea'] content_tag(:div, class: (%w[input-field col] + classes).join(' ')) do concat(text_area_tag(model, field, value, )) concat(form_label_tag(model, field, value, )) end end |
#form_time_field(model, field, options = {}) ⇒ Object
Time picker field
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'app/helpers/core_form_helper.rb', line 70 def form_time_field(model, field, = {}) classes = [:classes] || %w[s12 m6 l4 xl3] value = model.send(field) [:value] = value [:type] = :text [:disabled] ||= false = (model, field, ) [:class] = [:time_picker_type] || 'time-picker' content_tag(:div, class: (%w[input-field col] + classes).join(' ')) do concat(tag(:input, )) concat(form_label_tag(model, field, value, )) end end |
#input_label(model, field, default = model.class.human_attribute_name(field)) ⇒ String?
Get the label for this input element
388 389 390 |
# File 'app/helpers/core_form_helper.rb', line 388 def input_label(model, field, default = model.class.human_attribute_name(field)) form_localized_text(model, field, :labels, default) end |
#required_field?(model, field) ⇒ Boolean
392 393 394 |
# File 'app/helpers/core_form_helper.rb', line 392 def required_field?(model, field) form_required_field?(model, field) end |
#text_area_tag(model, field, value, options = {}) ⇒ Object
29 30 31 32 33 34 35 |
# File 'app/helpers/core_form_helper.rb', line 29 def text_area_tag(model, field, value, = {}) = (model, field, ) [:class] += ['materialize-textarea'] content_tag(:textarea, ) do concat(value) end end |