Module: CoreHtml5FormHelper

Defined in:
app/helpers/core_html5_form_helper.rb

Overview

Helpful methods HTML5 elements in a form

Instance Method Summary collapse

Instance Method Details

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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/helpers/core_html5_form_helper.rb', line 124

def html5_checkbox(model, field, options = {})
  classes = options[:classes] || %w[col-sm-12 col-lg-6]
  value = model.send(field)
  options[:disabled] ||= false
  properties = {
    class: 'form-check-input',
    id: html5_field_id(model, field, options),
    name: html5_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[form-check mt-4] + classes).join(' ')) do
    concat((:label) do
      concat(checkbox_tag)
      concat(html5_checkbox_label_tag(model, field, value, input_classes: classes))
    end)
  end
end

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



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/helpers/core_html5_form_helper.rb', line 145

def html5_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 = html5_field_place_holder(model, field)
  return if place_holder.blank? && value.blank? && options[:prompt].present?

  error = model.errors[field]
  key = "ui_form.#{model.class.to_s.underscore}.labels.#{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) do
    concat(I18n.exists?(key) ? I18n.t(key) : field.to_s.humanize)
  end
end

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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/helpers/core_html5_form_helper.rb', line 98

def html5_field_id(model, field, options = {})
  return options[:form_id] if options[:form_id].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[:form_id_prefix]}#{options[:base_name]}_#{options[:array_name]}_#{options[:index]}_#{field}"
      else
        "#{options[:form_id_prefix]}#{options[:array_name]}_#{options[:index]}_#{field}"
      end
    else
      "#{options[:form_id_prefix]}#{model.class.to_s.underscore}_#{options[:index]}_#{field}"
    end
  else
    "#{options[:form_id_prefix]}#{model.class.to_s.underscore}_#{field}"
  end
end

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/helpers/core_html5_form_helper.rb', line 77

def html5_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

#html5_field_place_holder(model, field) ⇒ Object



119
120
121
122
# File 'app/helpers/core_html5_form_helper.rb', line 119

def html5_field_place_holder(model, field)
  place_holder_key = "ui_form.#{model.class.to_s.underscore}.placeholders.#{field}"
  I18n.exists?(place_holder_key) ? I18n.t(place_holder_key) : nil
end

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/helpers/core_html5_form_helper.rb', line 34

def html5_label_tag(model, field, value, options = {})
  # dont 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')

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

  error = model.errors[field]
  key = "ui_form.#{model.class.to_s.underscore}.labels.#{field}"
  classes = value.nil? && place_holder.blank? ? '' : 'active'
  classes += error.present? ? ' invalid red-text' : ' valid'
  options[:class] = classes
  options[:for] = html5_field_id(model, field, options)
  options['data-error'] = error.join(', ') if error.present?
  (:label, options) do
    concat(I18n.exists?(key) ? I18n.t(key) : field.to_s.humanize)
    concat(" #{error.join(', ')}") if error.present?
  end
end

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

This method is abstract.

Render a password field

TODO:

Combine this with the html5_text_field, the only thing different is the field type



9
10
11
12
13
14
15
16
17
18
19
# File 'app/helpers/core_html5_form_helper.rb', line 9

def html5_password(model, field, options = {})
  classes = options[:classes] || %w[col-sm-12 col-lg-6]
  value = model.send(field)
  options[:type] = :password
  options[:place_holder] ||= mask_value(value)
  tag_options = html5_text_field_options(model, field, options)
  (:div, class: (%w[form-floating form-floating-outline mb-3 col] + classes).join(' ')) do
    concat(tag(:input, tag_options))
    concat(form_label_tag(model, field, value, options))
  end
end

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



21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/helpers/core_html5_form_helper.rb', line 21

def html5_text_field(model, field, options = {})
  classes = options[:classes] || %w[col-sm-12 col-lg-6]
  value = model.send(field)
  options[:type] ||= :text
  options[:value] = value
  options[:disabled] ||= false
  tag_options = html5_text_field_options(model, field, options)
  (:div, class: (%w[form-floating form-floating-outline mb-3 col] + classes).join(' ')) do
    concat(tag(:input, tag_options))
    concat(html5_label_tag(model, field, value, options))
  end
end

#html5_text_field_options(model, field, options) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/helpers/core_html5_form_helper.rb', line 56

def html5_text_field_options(model, field, options)
  hint_key = "ui_form.#{model.class.to_s.underscore}.hints.#{field}"
  if I18n.exists?(hint_key)
    classes = %w[form-control validate tooltipped]
    options[:data] = { tooltip: I18n.t(hint_key), position: :top }
  else
    classes = %w[form-control validate]
  end
  classes += options[:input_classes] if options[:input_classes].present?
  options[:name] = html5_field_name(model, field, options)
  options[:id] = html5_field_id(model, field, options)
  place_holder = options[:place_holder] || html5_field_place_holder(model, field)
  if place_holder.present?
    classes << 'active'
    options[:placeholder] = place_holder
  end
  classes << 'active' if options[:value].present?
  options[:class] = classes.uniq
  options
end