Module: CoreFormHelper

Defined in:
app/helpers/core_form_helper.rb

Overview

helpers for creating forms

Instance Method Summary collapse

Instance Method Details

#field_place_holder(model, field) ⇒ Object



290
291
292
293
# File 'app/helpers/core_form_helper.rb', line 290

def 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

#form_checkbox(model, field, classes = %w[s12 m6 l4 xl3],, options = {}) ⇒ Object

Checkbox field



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'app/helpers/core_form_helper.rb', line 173

def form_checkbox(model, field, classes = %w[s12 m6 l4 xl3], options = {})
  value = model.send(field)
  options[:disabled] ||= false
  properties = {
    class: 'validate',
    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[input-field col] + classes).join(' ')) do
    concat((:label) 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

get the label for checkbox



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'app/helpers/core_form_helper.rb', line 196

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 = 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

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



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, 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 = {}) ⇒ Object

Return a consistent form field id



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'app/helpers/core_form_helper.rb', line 322

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

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

Return a consistent form field name



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'app/helpers/core_form_helper.rb', line 298

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



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)
  (:div, class: (%w[input-field col] + classes).join(' ')) do
    concat(file_tag)
    concat(form_label_tag(model, field, value))
  end
end

#form_hint_tag(model, field) ⇒ Object

Get the hint for the field and add it



257
258
259
260
261
262
263
264
# File 'app/helpers/core_form_helper.rb', line 257

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_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
# File 'app/helpers/core_form_helper.rb', line 232

def form_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 = options[:place_holder] || 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[:for] = form_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

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

Password field



87
88
89
90
91
92
93
94
95
96
97
# File 'app/helpers/core_form_helper.rb', line 87

def form_password(model, field, options = {})
  classes = options[:classes] || %w[s12 m6 l4 xl3]
  value = model.send(field)
  options[:type] = :password
  options[:place_holder] ||= mask_value(value)
  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

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



343
344
345
346
347
348
349
350
# File 'app/helpers/core_form_helper.rb', line 343

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_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, options = {})
  value = model.send(field)
  raise 'Prompt needed' if value.blank? && options[:prompt].blank? && !options[: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 + (options[:classes] || %w[s12 m6 l4 xl3])).join(' ')
  (:div, class: classes, data: data) do
    concat(form_select_tag(model, field, options))
    concat(form_label_tag(model, field, value, options))
  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, 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



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, 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_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, options = {})
  classes = options[: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']
  (:div, class: (%w[input-field col] + classes).join(' ')) do
    concat(text_area_tag(model, field, value, options))
    concat(form_label_tag(model, field, value, options))
  end
end

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

Text field



40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/helpers/core_form_helper.rb', line 40

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

#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, 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

#materialize_form_for(options = {}, &block) ⇒ Object



7
8
9
10
11
# File 'app/helpers/core_form_helper.rb', line 7

def materialize_form_for(options = {}, &block)
  options[:form_authenticity_token] = form_authenticity_token
  form = Materialize::Form.new(options)
  form.render_form(&block)
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, options = {})
  tag_options = text_field_options(model, field, options)
  tag_options[:class] += ['materialize-textarea']
  (:textarea, tag_options) do
    concat(value)
  end
end

#text_field_options(model, field, options) ⇒ Object

Add the placeholder option if found in the translations



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'app/helpers/core_form_helper.rb', line 269

def text_field_options(model, field, options)
  hint_key = "ui_form.#{model.class.to_s.underscore}.hints.#{field}"
  if I18n.exists?(hint_key)
    classes = %w[validate tooltipped]
    options[:data] = { tooltip: I18n.t(hint_key), position: :top }
  else
    classes = %w[validate]
  end
  classes += options[:input_classes] if options[:input_classes].present?
  options[:name] = form_field_name(model, field, options)
  options[:id] = form_field_id(model, field, options)
  place_holder = options[:place_holder] || 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