Module: Recourse::Helpers::Fields

Included in:
Recourse::Helpers
Defined in:
lib/recourse/helpers/fields.rb

Overview

Chooses the form field a column deserves, and labels it.

Instance Method Summary collapse

Instance Method Details

#field(name, **options) ⇒ Object

One labelled field in the form's grid. label: overrides the heading and type: overrides the input the column would otherwise have chosen.



7
8
9
10
11
12
13
14
15
# File 'lib/recourse/helpers/fields.rb', line 7

def field(name, **options)
  column = name.to_s
  label = options.fetch :label, reference_title(column, belongs_to_association(column))

  tag.div class: 'mb-3 lg:col-6' do
    safe_join [@recourse_form.label(column, label, class: 'form-label'),
               resource_field(@recourse_form, column, type: options[:type])]
  end
end

#resource_field(form, column, type: nil) ⇒ Object

A field typed by what the column holds, not merely a text box.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/recourse/helpers/fields.rb', line 18

def resource_field(form, column, type: nil)
  association = belongs_to_association column
  return reference_field form, column, association if association

  # Rails mirrors `maxlength` into `size`, which would shrink the box to it.
  options = { class: 'form-control', size: nil }.merge field_html(column, type)

  return form.text_field column, **options, type: type if type
  return form.password_field column, **options if encrypted_column? column
  return form.email_field column, **options if column == 'email'
  return form.color_field column, **options if column == 'color'

  dated_field form, column, **options
end