Class: CrudComponents::Presenters::Form
- 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
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#record ⇒ Object
readonly
Returns the value of attribute record.
-
#structure ⇒ Object
readonly
Returns the value of attribute structure.
Attributes inherited from Base
Instance Method Summary collapse
- #any_errors? ⇒ Boolean
-
#display(field) ⇒ Object
Read-only display reuses the value renderer (record surface).
- #editable?(field) ⇒ Boolean
-
#fields ⇒ Object
Visible fields that have a form representation (computed/json skipped).
-
#form_options ⇒ Object
form_with options; nil url/method let Rails infer from the record.
-
#initialize(view:, record:, fieldset: nil, action: nil, url: nil, method: nil) ⇒ Form
constructor
A new instance of Form.
-
#summary_errors ⇒ Object
Errors not attached to a visible field — base errors, or errors on a column the form doesn’t show.
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
#action ⇒ Object (readonly)
Returns the value of attribute action.
11 12 13 |
# File 'lib/crud_components/presenters/form.rb', line 11 def action @action end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
11 12 13 |
# File 'lib/crud_components/presenters/form.rb', line 11 def model @model end |
#record ⇒ Object (readonly)
Returns the value of attribute record.
11 12 13 |
# File 'lib/crud_components/presenters/form.rb', line 11 def record @record end |
#structure ⇒ Object (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
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
30 31 32 |
# File 'lib/crud_components/presenters/form.rb', line 30 def editable?(field) field.editable? && field.editable_permitted?(, record) end |
#fields ⇒ Object
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?(, record) } end |
#form_options ⇒ Object
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 { url: @url, method: @method }.compact end |
#summary_errors ⇒ Object
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 |