class Formalist::Elements::ManyForms

Public Instance Methods

attributes() click to toggle source

FIXME: it would be tidier to have a reader method for each attribute

Calls superclass method
# File lib/formalist/elements/many_forms.rb, line 21
def attributes
  super.merge(embeddable_forms: embeddable_forms_ast)
end
child_form_builder() click to toggle source
# File lib/formalist/elements/many_forms.rb, line 54
def child_form_builder
  ChildForms::Builder.new(@attributes[:embeddable_forms])
end
embeddable_forms_ast() click to toggle source

Replace the form objects with their AST

# File lib/formalist/elements/many_forms.rb, line 40
def embeddable_forms_ast
  @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: {}) click to toggle source

@api private

Calls superclass method Formalist::Element::fill
# 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() click to toggle source

Converts a collection of “many” repeating elements into an abstract syntax tree.

It takes the following format:

“‘

:many_forms, [params]

“‘

With the following parameters:

  1. Collection name

  2. Custom form element type (or ‘:many_forms` otherwise)

  3. Collection-level error messages

  4. Form element attributes

  5. 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 “components” collection

many_forms.to_ast
# => [:many_forms, [
  :components,
  :many_forms,
  ["components size cannot be less than 3"],
  [:object, [
    [:allow_create, [:value, [true]]],
    [:allow_update, [:value, [true]]],
    [:allow_destroy, [:value, [true]]],
    [:allow_reorder, [:value, [true]]]
  ]],
  [
    [
     [:child_form,
      [:image_with_captions,
       :child_form,
         [[:field, [:image_id, :text_field, "", ["must be filled"], [:object, []]]], [:field, [:caption, :text_field, "Large panda", [], [:object, []]]]],
        [:object, []]
  ]
]]

@return [Array] the collection as an abstract syntax tree.

# 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