Class: IronAdmin::Form::NestedFormComponent
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- IronAdmin::Form::NestedFormComponent
- Defined in:
- app/components/iron_admin/form/nested_form_component.rb
Overview
ViewComponent for rendering nested association forms inline within a parent form.
Renders existing child records as editable rows and provides a hidden element that JavaScript clones to add new rows dynamically.
Instance Attribute Summary collapse
-
#current_user ⇒ Object?
readonly
The current authenticated user.
-
#form ⇒ ActionView::Helpers::FormBuilder
readonly
The parent form builder.
-
#nested_association ⇒ IronAdmin::NestedAssociation
readonly
The nested association configuration.
-
#record ⇒ ActiveRecord::Base
readonly
The parent record.
Instance Method Summary collapse
-
#child_records ⇒ Array<ActiveRecord::Base>
Returns the child records for the nested association.
-
#initialize(form:, nested_association:, record:, current_user: nil) ⇒ NestedFormComponent
constructor
A new instance of NestedFormComponent.
-
#show_add_button? ⇒ Boolean
Whether to show the "Add" button (has_many only).
-
#sortable? ⇒ Boolean
Whether drag-and-drop sorting is enabled.
-
#template_index ⇒ String
The placeholder index used in template rows.
-
#theme ⇒ IronAdmin::Configuration::Theme
Theme configuration.
-
#title ⇒ String
Human-readable title for the section header.
Constructor Details
#initialize(form:, nested_association:, record:, current_user: nil) ⇒ NestedFormComponent
Returns a new instance of NestedFormComponent.
28 29 30 31 32 33 34 |
# File 'app/components/iron_admin/form/nested_form_component.rb', line 28 def initialize(form:, nested_association:, record:, current_user: nil) super() @form = form @nested_association = nested_association @record = record @current_user = current_user end |
Instance Attribute Details
#current_user ⇒ Object? (readonly)
Returns The current authenticated user.
20 21 22 |
# File 'app/components/iron_admin/form/nested_form_component.rb', line 20 def current_user @current_user end |
#form ⇒ ActionView::Helpers::FormBuilder (readonly)
Returns The parent form builder.
11 12 13 |
# File 'app/components/iron_admin/form/nested_form_component.rb', line 11 def form @form end |
#nested_association ⇒ IronAdmin::NestedAssociation (readonly)
Returns The nested association configuration.
14 15 16 |
# File 'app/components/iron_admin/form/nested_form_component.rb', line 14 def nested_association @nested_association end |
#record ⇒ ActiveRecord::Base (readonly)
Returns The parent record.
17 18 19 |
# File 'app/components/iron_admin/form/nested_form_component.rb', line 17 def record @record end |
Instance Method Details
#child_records ⇒ Array<ActiveRecord::Base>
Returns the child records for the nested association.
For has_one, wraps the record in an array or builds a new instance. For has_many with a position_field, sorts by that field ascending. Returns in-memory objects so validation errors are preserved.
68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/components/iron_admin/form/nested_form_component.rb', line 68 def child_records children = record.public_send(name) if kind == :has_one [children || nested_association.reflection.klass.new] elsif position_field children.to_a.sort_by { |c| c.public_send(position_field) || Float::INFINITY } else children.to_a end end |
#show_add_button? ⇒ Boolean
Returns Whether to show the "Add" button (has_many only).
47 48 49 |
# File 'app/components/iron_admin/form/nested_form_component.rb', line 47 def kind == :has_many end |
#sortable? ⇒ Boolean
Returns Whether drag-and-drop sorting is enabled.
52 53 54 |
# File 'app/components/iron_admin/form/nested_form_component.rb', line 52 def sortable? position_field.present? end |
#template_index ⇒ String
Returns The placeholder index used in template rows.
57 58 59 |
# File 'app/components/iron_admin/form/nested_form_component.rb', line 57 def template_index "NEW_RECORD_INDEX" end |
#theme ⇒ IronAdmin::Configuration::Theme
Returns Theme configuration.
37 38 39 |
# File 'app/components/iron_admin/form/nested_form_component.rb', line 37 def theme IronAdmin.configuration.theme end |
#title ⇒ String
Returns Human-readable title for the section header.
42 43 44 |
# File 'app/components/iron_admin/form/nested_form_component.rb', line 42 def title name.to_s.humanize end |