Module: Layered::Ui::FormHelper

Defined in:
app/helpers/layered/ui/form_helper.rb

Constant Summary collapse

FIELD_TYPES =
%i[
  string text email password number tel url search
  date datetime time month week
  color range file
  select checkbox hidden
].freeze

Instance Method Summary collapse

Instance Method Details

#l_ui_field_error_id(record, attribute) ⇒ Object



60
61
62
# File 'app/helpers/layered/ui/form_helper.rb', line 60

def l_ui_field_error_id(record, attribute)
  "#{record.model_name.param_key}_#{attribute}_error"
end

#l_ui_field_hint_id(record, attribute) ⇒ Object



64
65
66
# File 'app/helpers/layered/ui/form_helper.rb', line 64

def l_ui_field_hint_id(record, attribute)
  "#{record.model_name.param_key}_#{attribute}_hint"
end

#l_ui_form(record, fields:, url:, method: nil, submit: nil, multipart: nil) ⇒ Object

Renders a complete form with all fields, error summary, and submit button.

l_ui_form(@post,
  fields: Post.l_managed_resource_fields,
  url: managed_posts_path)


18
19
20
21
22
# File 'app/helpers/layered/ui/form_helper.rb', line 18

def l_ui_form(record, fields:, url:, method: nil, submit: nil, multipart: nil)
  render partial: "layered/ui/managed_resource/form",
         locals: { record: record, fields: fields, url: url, method: method,
                   submit: submit, multipart: multipart }
end

#l_ui_normalise_field(record, config) ⇒ Object

Normalises a raw field config hash into a canonical form.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/helpers/layered/ui/form_helper.rb', line 25

def l_ui_normalise_field(record, config)
  attribute = config[:attribute]
  as = config[:as] || l_ui_field_type_for(record.class, attribute)

  unless FIELD_TYPES.include?(as)
    raise ArgumentError,
          "Unsupported field type :#{as} for :#{attribute}. " \
          "Supported types: #{FIELD_TYPES.map { |t| ":#{t}" }.join(', ')}"
  end

  if as == :select && config[:collection].nil?
    raise ArgumentError,
          "Field :#{attribute} is declared as :select but has no :collection. " \
          "Provide collection: [['Label', value], ...] or collection: -> { Model.pluck(:name, :id) }"
  end

  label = config[:label] || attribute.to_s.humanize

  extras = config.except(:attribute, :as, :label, :required, :hint,
                         :collection, :placeholder, :prompt, :include_blank)

  {
    attribute: attribute,
    as: as,
    label: label,
    required: config.fetch(:required, false),
    hint: config[:hint],
    collection: config[:collection],
    placeholder: config[:placeholder],
    prompt: config[:prompt],
    include_blank: config[:include_blank],
    extras: extras
  }
end