Module: CoreFormHelper

Defined in:
app/helpers/core_form_helper.rb

Overview

helpers for creating forms

Instance Method Summary collapse

Instance Method Details

#form_date_field(model, field, options = {}) ⇒ Object

Text field

def form_text_field(model, field, options = {})

classes = options[:classes] || %w[s12 m6 l4 xl3]
value = model.send(field)
options[:type] ||= :text
options[:value] = value
options[:disabled] ||= false
tag_options = text_field_options(model, field, options)
(:div, class: (%w[input-field col] + classes).join(' ')) do
  concat(tag(:input, tag_options))
  concat(form_label_tag(model, field, value, options))
end

end



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/helpers/core_form_helper.rb', line 31

def form_date_field(model, field, options = {})
  classes = options[:classes] || %w[s12 m6 l4 xl3]
  value = model.send(field)
  options[:value] = value.strftime('%d %B, %Y') if value.present?
  options[:type] = :text
  options[:disabled] ||= false
  tag_options = text_field_options(model, field, options)
  tag_options[:class] = options[:date_picker_type] || 'simple-date-picker'
  (:div, class: (%w[input-field col] + classes).join(' ')) do
    concat(tag(:input, tag_options))
    concat(form_label_tag(model, field, value, options))
  end
end

#form_field_id(model, field, options = {}) ⇒ HTML

This method is abstract.

Return a consistent form field id

Parameters:

  • model (Mongoid::Document)
  • field (Symbol)
  • options (Hash) (defaults to: {})

Returns:

  • (HTML)


308
309
310
311
312
313
# File 'app/helpers/core_form_helper.rb', line 308

def form_field_id(model, field, options = {})
  return options[:form_id] if options[:form_id].present?

  field = "#{field}_id" if model.class.reflect_on_association(field).present?
  [options[:form_id_prefix], options[:base_name], options[:array_name] || model.class.to_s.underscore, options[:index], field].compact.join('_')
end

#form_field_name(model, field, options = {}) ⇒ HTML

This method is abstract.

Return a consistent form field name

Parameters:

  • model (Mongoid::Document)
  • field (Symbol)
  • options (Hash) (defaults to: {})

Returns:

  • (HTML)


282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'app/helpers/core_form_helper.rb', line 282

def form_field_name(model, field, options = {})
  return options[:form_name] if options[: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 options[:index].present?
    if options[:array_name].present?
      if options[: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



196
197
198
199
200
201
202
203
204
205
# File 'app/helpers/core_form_helper.rb', line 196

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)
  (: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?

This method is abstract.

Get the helper text for this input element

Parameters:

  • model (Mongoid::Document)
  • field (Symbol)
  • default (String, nil) (defaults to: nil)

Returns:

  • (String, nil)


348
349
350
# File 'app/helpers/core_form_helper.rb', line 348

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



239
240
241
242
243
244
245
246
# File 'app/helpers/core_form_helper.rb', line 239

def form_hint_tag(model, field)
  key = "ui_form.#{model.class.to_s.underscore}.hints.#{field}"
  return nil unless I18n.exists?(key)

  (: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?

This method is abstract.

Get the hint text for this input element

Parameters:

  • model (Mongoid::Document)
  • field (Symbol)
  • default (String, nil) (defaults to: nil)

Returns:

  • (String, nil)


357
358
359
# File 'app/helpers/core_form_helper.rb', line 357

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



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'app/helpers/core_form_helper.rb', line 210

def form_label_tag(model, field, value, options = {})
  # don't do a label if we are in default browser mode
  return if options[:no_label]
  return if options[:input_classes].present? && options[:input_classes].include?('browser-default')

  help_text = form_helper_text(model, field)

  # or if we have a prompt with now value
  place_holder = options[:place_holder] || form_place_holder_text(model, field)
  return if place_holder.blank? && value.blank? && options[:prompt].present?

  error = model.errors[field]
  classes = %w[form-label mb-2]
  classes << 'text-red' if error.present?
  options[:class] = classes.compact.uniq.join(' ')
  options[:for] = form_field_id(model, field, options)
  # options['data-error'] = error.join(', ') if error.present?
  options[:required] = form_required_field?(model, field) ? 'required' : nil
  tag.label(**options) do
    concat(input_label(model, field, field.to_s.humanize))
    concat((: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?

This method is abstract.

Get the localized value for the given key

Parameters:

  • model (Mongoid::Document)
  • field (Symbol)
  • type (Symbol)
  • default (String) (defaults to: nil)

Returns:

  • (String, nil)


380
381
382
383
384
385
# File 'app/helpers/core_form_helper.rb', line 380

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?

This method is abstract.

Get the placeholder for this input element

Parameters:

  • model (Mongoid::Document)
  • field (Symbol)
  • default (String, nil) (defaults to: nil)

Returns:

  • (String, nil)


339
340
341
# File 'app/helpers/core_form_helper.rb', line 339

def form_place_holder_text(model, field, default = nil)
  form_localized_text(model, field, :placeholders, default)
end

#form_radio_button(model, field, options = {}) ⇒ Object



325
326
327
328
329
330
331
332
# File 'app/helpers/core_form_helper.rb', line 325

def form_radio_button(model, field, options = {})
  value = model.send(field)
  classes = (%w[input-field col] + options[:classes] || []).join(' ')
  (:div, class: classes) do
    concat(form_select_tag(model, field, options))
    concat(form_label_tag(model, field, value))
  end
end

#form_required_field?(model, field) ⇒ Boolean

This method is abstract.

Determine if the field is required or not

Parameters:

  • model (Mongoid::Document)
  • field (Symbol)

Returns:

  • (Boolean)


319
320
321
322
323
# File 'app/helpers/core_form_helper.rb', line 319

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_option_key_values(value, options) ⇒ Object

Return an array of hashes for the selection to work easily



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/helpers/core_form_helper.rb', line 120

def form_select_option_key_values(value, options)
  select_options = []
  select_options << { key: '', value: options[:prompt], selected: false } if options[:prompt].present?
  options[: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)
    select_options << 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
  select_options
end

#form_select_tag(model, field, options = {}) ⇒ Object

Create the select tag



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/helpers/core_form_helper.rb', line 102

def form_select_tag(model, field, options = {})
  select_content = {
    id: form_field_id(model, field, options),
    name: form_field_name(model, field, options),
    class: [options[:input_classes], 'materialize'].compact.join(' '),
    disabled: options[:disabled]
  }
  (:select, select_content) do
    form_select_option_key_values(model.send(field), options).each do |value|
      concat((:option, value: value[:key], selected: value[:selected]) do
        concat(value[:value])
      end)
    end
  end
end

#form_time_field(model, field, options = {}) ⇒ Object

Time picker field



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/helpers/core_form_helper.rb', line 48

def form_time_field(model, field, options = {})
  classes = options[:classes] || %w[s12 m6 l4 xl3]
  value = model.send(field)
  options[:value] = value
  options[:type] = :text
  options[:disabled] ||= false
  tag_options = text_field_options(model, field, options)
  tag_options[:class] = options[:time_picker_type] || 'time-picker'
  (:div, class: (%w[input-field col] + classes).join(' ')) do
    concat(tag(:input, tag_options))
    concat(form_label_tag(model, field, value, options))
  end
end

#input_label(model, field, default = model.class.human_attribute_name(field)) ⇒ String?

This method is abstract.

Get the label for this input element

Parameters:

  • model (Mongoid::Document)
  • field (Symbol)
  • default (String, nil) (defaults to: model.class.human_attribute_name(field))

Returns:

  • (String, nil)


366
367
368
# File 'app/helpers/core_form_helper.rb', line 366

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

Returns:

  • (Boolean)


370
371
372
# File 'app/helpers/core_form_helper.rb', line 370

def required_field?(model, field)
  form_required_field?(model, field)
end