class Formalist::Elements::Many
Attributes
child_template[R]
@api private
Public Class Methods
build(children: [], **args)
click to toggle source
@api private
Calls superclass method
Formalist::Element::build
# File lib/formalist/elements/many.rb, line 22 def self.build(children: [], **args) child_template = children.dup super(child_template: child_template, **args) end
new(child_template:, **args)
click to toggle source
@api private
Calls superclass method
Formalist::Element::new
# File lib/formalist/elements/many.rb, line 28 def initialize(child_template:, **args) @child_template = child_template super(**args) end
Public Instance Methods
fill(input: {}, errors: {})
click to toggle source
@api private
Calls superclass method
Formalist::Element::fill
# File lib/formalist/elements/many.rb, line 34 def fill(input: {}, errors: {}) input = input.fetch(name) { [] } errors = errors[name] || {} # Errors look like this when they are on the array itself: ["size cannot be greater than 2"] # Errors look like this when they are on children: {0=>{:summary=>["must be filled"]} children = input.each_with_index.map { |child_input, index| child_errors = errors.is_a?(Hash) ? errors.fetch(index) { {} } : {} child_template.map { |child| child.fill(input: child_input, errors: child_errors) } } super( input: input, errors: errors, children: children, child_template: child_template, ) end
to_ast()
click to toggle source
Converts a collection of “many” repeating elements into an abstract syntax tree.
It takes the following format:
“‘
- :many, [params]
-
“‘
With the following parameters:
-
Collection name
-
Custom form element type (or ‘:many` otherwise)
-
Collection-level error messages
-
Form
element attributes -
Child element “template” (i.e. the form elements comprising a single entry in the collection of “many” elements, without any user data associated)
-
Child elements, one for each of the entries in the input data (or none, if there is no or empty input data)
@see
Formalist::Element::Attributes#to_ast
“Form element attributes” structure@example “locations” collection
many.to_ast # => [:many, [ :locations, :many, ["locations size cannot be less than 3"], [:object, [ [:allow_create, [:value, [true]]], [:allow_update, [:value, [true]]], [:allow_destroy, [:value, [true]]], [:allow_reorder, [:value, [true]]] ]], [ [:field, [:name, :field, nil, [], [], [:object, []]]], [:field, [:address, :field, nil, [], [], [:object, []]]] ], [ [ [:field, [:name, :field, "Icelab Canberra", [], [], [:object, []]]], [:field, [:address, :field, "Canberra, ACT, Australia", [], [], [:object, []]]] ], [ [:field, [:name, :field, "Icelab Melbourne", [], [], [:object, []]]], [:field, [:address, :field, "Melbourne, VIC, Australia", [], [], [:object, []]]] ] ] ]]
@return [Array] the collection as an abstract syntax tree.
-
# File lib/formalist/elements/many.rb, line 107 def to_ast local_errors = errors.is_a?(Array) ? errors : [] [:many, [ name, type, local_errors, Element::Attributes.new(attributes).to_ast, child_template.map(&:to_ast), children.map { |el_list| el_list.map(&:to_ast) }, ]] end