Module: CoreFormCheckboxHelper

Defined in:
app/helpers/core_form_checkbox_helper.rb

Instance Method Summary collapse

Instance Method Details

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

This method is abstract.

Render a text field

Parameters:

  • model (Mongoid::Document)
    • The model to render the field for

  • field (Symbol)
    • The field to render

  • options (Hash) (defaults to: {})
    • Options to pass to the field



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/helpers/core_form_checkbox_helper.rb', line 6

def form_checkbox(model, field, options = {})
  classes = options[:classes] || []
  value = model.send(field)
  options[:disabled] ||= false
  properties = {
    class: 'form-check-input',
    id: form_field_id(model, field, options),
    name: form_field_name(model, field, options),
    type: :checkbox,
    disabled: options[:disabled]
  }
  properties[:checked] = true if model.send(field)
  checkbox_tag = tag(:input, properties)
  (:div, class: (%w[mb-3 align-items-center d-flex] + classes).join(' ')) do
    concat((:label, class: 'form-check form-switch align-middle') do
      concat(checkbox_tag)
      concat(form_checkbox_label_tag(model, field, value, input_classes: classes))
    end)
  end
end

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/helpers/core_form_checkbox_helper.rb', line 27

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

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

  error = model.errors[field]
  label = input_label(model, field)
  classes = value.nil? && place_holder.blank? ? '' : 'active'
  classes += error.present? ? ' invalid red-text' : ' valid'
  options[:class] = classes
  options['data-error'] = error.join(', ') if error.present?
  (:span, options) { label}
end