Module: BulmaPhlex::Rails::NestedForms

Extended by:
ActiveSupport::Concern
Included in:
FormBuilder
Defined in:
lib/bulma_phlex/rails/concerns/nested_forms.rb

Overview

# Nested Forms

This modules provides support for nested forms. It overrides the ‘fields_for` method to capture the block for nested attributes then creates a template for adding new nested records dynamically when the `nested_form_add_button` method is called.

The nested form can also include a delete button for each row using the ‘nested_form_delete_button` method.

Instance Method Summary collapse

Instance Method Details

#fields_for(record_name, record_object = nil, fields_options = {}, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bulma_phlex/rails/concerns/nested_forms.rb', line 20

def fields_for(record_name, record_object = nil, fields_options = {}, &block)
  output = super

  if (record_name.is_a?(Symbol) || record_name.is_a?(String)) && nested_attributes_association?(record_name)
    if @nested_forms_add_buttons&.include?(record_name)
      output += build_fields_for_template(record_name, fields_options, &block)
    else
      (@nested_forms_templates ||= {}).store(record_name, [fields_options, block])
    end
  end

  output
end

#nested_form_add_button(record_name, container:, label: nil, icon: nil, icon_left: nil, icon_right: nil, **html_attributes) ⇒ Object

Add a button to add new nested form rows dynamically.

#### Arguments

  • ‘record_name`: Symbol or String - The name of the nested association (e.g., :tasks).

  • ‘container`: String - A CSS selector that identifies the container element where new rows should be added.

  • ‘label`: String (optional) - The text label to display on the button.

  • ‘icon`: String (optional) - The name of an icon to display on the button (appears on the left by default).

  • ‘icon_left`: String (optional) - The name of an icon to display on the left side of the button.

  • ‘icon_right`: String (optional) - The name of an icon to display on the right side of the button.

  • Additional HTML attributes can be passed via ‘html_attributes`.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bulma_phlex/rails/concerns/nested_forms.rb', line 46

def nested_form_add_button(record_name, # rubocop:disable Metrics/ParameterLists
                           container:,
                           label: nil,
                           icon: nil,
                           icon_left: nil,
                           icon_right: nil,
                           **html_attributes)
  button_html = build_nested_form_add_button(record_name, container, label, icon_left || icon, icon_right,
                                             html_attributes)

  if @nested_forms_templates&.key?(record_name)
    field_options, block = @nested_forms_templates[record_name]
    button_html + build_fields_for_template(record_name, field_options, &block)
  else
    (@nested_forms_add_buttons ||= []) << record_name
    button_html
  end
end

#nested_form_delete_button(row_selector:, label: nil, icon: nil, icon_left: nil, icon_right: nil, **html_attributes) ⇒ Object

Add a button to delete a nested form row.

#### Arguments

  • ‘row_selector`: String - A CSS selector that identifies the row to be deleted. This is passed to the element’s ‘closest` method to find the row element.

  • ‘label`: String (optional) - The text label to display on the button.

  • ‘icon`: String (optional) - The name of an icon to display on the button (appears on the left by default).

  • ‘icon_left`: String (optional) - The name of an icon to display on the left side of the button.

  • ‘icon_right`: String (optional) - The name of an icon to display on the right side of the button.

  • Additional HTML attributes can be passed via ‘html_attributes`.



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bulma_phlex/rails/concerns/nested_forms.rb', line 76

def nested_form_delete_button(row_selector:, # rubocop:disable Metrics/ParameterLists
                              label: nil,
                              icon: nil,
                              icon_left: nil,
                              icon_right: nil,
                              **html_attributes)
  action = object.persisted? ? "markForDestruction" : "remove"

  build_nested_form_delete_button(label, icon_left || icon, icon_right, row_selector, action, html_attributes) +
    hidden_field(:_destroy)
end