Module: Layered::Ui::FormHelper

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

Constant Summary collapse

FIELD_TYPES =
%i[string text email number date datetime select checkbox hidden].freeze

Instance Method Summary collapse

Instance Method Details

#l_ui_field_error_id(record, attribute) ⇒ Object



52
53
54
# File 'app/helpers/layered/ui/form_helper.rb', line 52

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

#l_ui_field_hint_id(record, attribute) ⇒ Object



56
57
58
# File 'app/helpers/layered/ui/form_helper.rb', line 56

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

#l_ui_form(record, fields:, url:, method: 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)


13
14
15
16
# File 'app/helpers/layered/ui/form_helper.rb', line 13

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

#l_ui_normalise_field(record, config) ⇒ Object

Normalises a raw field config hash into a canonical form.



19
20
21
22
23
24
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
# File 'app/helpers/layered/ui/form_helper.rb', line 19

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)

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