Class: Kithe::FormBuilder

Inherits:
SimpleForm::FormBuilder
  • Object
show all
Defined in:
app/simple_form_enhancements/kithe/form_builder.rb

Overview

A SimpleForm::FormBuilder sub-class with custom kithe features:

  • repeatable_model_input

If you use the ‘kithe_form_for` helper instead of `simple_form_for`, you get a custom Kithe::FormBuilder in your simple_form building.

Instance Method Summary collapse

Instance Method Details

#repeatable_attr_input(attr_name, html_attributes: nil, build: nil) {|builder| ... } ⇒ Object

Produce a form input for a repeatable attr_json field. Takes care of the add/remove UI apparatus – generated HTML assumes Bootstrap 4, and simple_form Bootstrap config with a :vertical_collection wrapper.

See the [Forms Guide](../../../guides/forms.md) for more usage information.

Actual implementation code is over in Kithe::RepeatableInputGenerator

This is a method rather than a simple form component mostly becuase simple form components don’t allow a block param like this, and that works out so well here.

FUTURE: Provide options to customize classes and labels on generated wrapping UI apparatus.

Parameters:

  • attr_name (Symbol)

    Model attribute to generate input for, same one you’d pass to a form builder.

  • build (Boolean) (defaults to: nil)

    nil default, or set to :at_least_one to have an empty input generated even if the model includes no elements.

  • html_attributes (Hash) (defaults to: nil)

    hash of additional attributes to add to a generated simple primitive <input> tag. Useful for data- attributes for custom JS behavior. Only valid if the generator will be generating a simple primtiive input for you, otherwise will raise ArgumentError if you try.

Yields:

  • (builder)

    For model-type attributes (not primitives), yields a sub-builder similar to ‘fields_for`.

  • (input_name, value)

    For primitive-type attributes, different yield.

Yield Parameters:

  • input_name (String)

    that should be used as HTML “name” attribute on input

  • value (String)

    that should be used as existing value when generating input for primitive, usually by passing to ‘value` attribute in some input builder.



37
38
39
40
# File 'app/simple_form_enhancements/kithe/form_builder.rb', line 37

def repeatable_attr_input(attr_name, html_attributes: nil, build: nil, &block)
  #repeatable_main_content(attr_name, &block)
  Kithe::RepeatableInputGenerator.new(self, attr_name, block, html_attributes: html_attributes, build: build).render
end