Class: CrudComponents::Presenters::Form

Inherits:
Base
  • Object
show all
Defined in:
lib/crud_components/presenters/form.rb

Overview

The single ‘form` local of the form partial. Derives a form from the same field metadata everything else uses; the host app’s controller owns saving (with the matching CrudComponents.permitted_attributes list).

Field selection falls back: the action’s fieldset → :form → :default. A visible field that isn’t editable (by type or permission) renders read-only rather than vanishing.

Constant Summary

Constants inherited from Base

Base::GEM_VIEW_ROOT

Instance Attribute Summary collapse

Attributes inherited from Base

#view

Instance Method Summary collapse

Methods inherited from Base

#ability, #config, #css, #permission_context, #render_cell, #render_filter_control

Constructor Details

#initialize(view:, record:, fieldset: nil, action: nil, url: nil, method: nil) ⇒ Form

Returns a new instance of Form.



13
14
15
16
17
18
19
20
21
22
# File 'lib/crud_components/presenters/form.rb', line 13

def initialize(view:, record:, fieldset: nil, action: nil, url: nil, method: nil)
  super(view: view)
  @record = record
  @model = record.class
  @structure = Structure.for(@model)
  @action = (action || (record.persisted? ? :edit : :new)).to_sym
  @fieldset = fieldset ? @structure.fieldset(fieldset) : @structure.form_fieldset(@action)
  @url = url
  @method = method
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



11
12
13
# File 'lib/crud_components/presenters/form.rb', line 11

def action
  @action
end

#modelObject (readonly)

Returns the value of attribute model.



11
12
13
# File 'lib/crud_components/presenters/form.rb', line 11

def model
  @model
end

#recordObject (readonly)

Returns the value of attribute record.



11
12
13
# File 'lib/crud_components/presenters/form.rb', line 11

def record
  @record
end

#structureObject (readonly)

Returns the value of attribute structure.



11
12
13
# File 'lib/crud_components/presenters/form.rb', line 11

def structure
  @structure
end

Instance Method Details

#any_errors?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/crud_components/presenters/form.rb', line 34

def any_errors?
  record.errors.any?
end

#display(field) ⇒ Object

Read-only display reuses the value renderer (record surface).



52
53
54
# File 'lib/crud_components/presenters/form.rb', line 52

def display(field)
  render_cell(field, record, surface: :record)
end

#editable?(field) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/crud_components/presenters/form.rb', line 30

def editable?(field)
  field.editable? && field.editable_permitted?(permission_context, record)
end

#fieldsObject

Visible fields that have a form representation (computed/json skipped).



25
26
27
28
# File 'lib/crud_components/presenters/form.rb', line 25

def fields
  structure.fieldset_fields(@fieldset)
           .select { |f| f.form_control && f.permitted?(permission_context, record) }
end

#form_optionsObject

form_with options; nil url/method let Rails infer from the record.



47
48
49
# File 'lib/crud_components/presenters/form.rb', line 47

def form_options
  { url: @url, method: @method }.compact
end

#summary_errorsObject

Errors not attached to a visible field — base errors, or errors on a column the form doesn’t show. Rendered in the summary so “fix N errors” is never a dead end with nothing to fix.



41
42
43
44
# File 'lib/crud_components/presenters/form.rb', line 41

def summary_errors
  shown = fields.map(&:name)
  record.errors.reject { |error| shown.include?(error.attribute) }.map(&:full_message)
end