Class: Formalist::Elements::ManyForms
- Inherits:
-
Formalist::Element
- Object
- Formalist::Element
- Formalist::Elements::ManyForms
- Defined in:
- lib/formalist/elements/many_forms.rb
Instance Attribute Summary
Attributes inherited from Formalist::Element
#children, #errors, #input, #name
Instance Method Summary collapse
-
#attributes ⇒ Object
FIXME: it would be tidier to have a reader method for each attribute.
- #child_form_builder ⇒ Object
-
#embeddable_forms_ast ⇒ Object
Replace the form objects with their AST.
- #fill(input: {}, errors: {}) ⇒ Object private
-
#to_ast ⇒ Array
Converts a collection of "many" repeating elements into an abstract syntax tree.
Methods inherited from Formalist::Element
#==, build, fill, #initialize, #type
Methods included from Formalist::Element::ClassInterface
#attribute, #attributes_schema, #type
Constructor Details
This class inherits a constructor from Formalist::Element
Instance Method Details
#attributes ⇒ Object
FIXME: it would be tidier to have a reader method for each attribute
21 22 23 |
# File 'lib/formalist/elements/many_forms.rb', line 21 def attributes super.merge(embeddable_forms: ) end |
#child_form_builder ⇒ Object
54 55 56 |
# File 'lib/formalist/elements/many_forms.rb', line 54 def child_form_builder ChildForms::Builder.new(@attributes[:embeddable_forms]) end |
#embeddable_forms_ast ⇒ Object
Replace the form objects with their AST
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/formalist/elements/many_forms.rb', line 40 def @attributes[:embeddable_forms].to_h.map { |key, attrs| template_attrs = attrs.slice(:label, :preview_image_url) [ key, attrs.merge( form: attrs[:form].to_ast, attributes_template: Element::Attributes.new(template_attrs).to_ast ) ] }.to_h end |
#fill(input: {}, errors: {}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/formalist/elements/many_forms.rb', line 26 def fill(input: {}, errors: {}) input = input[name] || [] errors = errors[name].to_a children = child_form_builder.(input) super( input: input, errors: errors, children: children, ) end |
#to_ast ⇒ Array
Converts a collection of "many" repeating elements into an abstract syntax tree.
It takes the following format:
[:many_forms, [params]]
With the following parameters:
- Collection name
- Custom form element type (or
:many_forms
otherwise) - Collection-level error messages
- Form element attributes
- Child elements, one for each of the entries in the input data (or none, if there is no or empty input data)
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/formalist/elements/many_forms.rb', line 101 def to_ast local_errors = errors.is_a?(Array) ? errors : [] [:many_forms, [ name, type, local_errors, Element::Attributes.new(attributes).to_ast, children.map(&:to_ast) ]] end |