Class: Formalist::Elements::ManyForms

Inherits:
Formalist::Element show all
Defined in:
lib/formalist/elements/many_forms.rb

Instance Attribute Summary

Attributes inherited from Formalist::Element

#children, #errors, #input, #name

Instance Method Summary collapse

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

#attributesObject

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: embeddable_forms_ast)
end

#child_form_builderObject



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_astObject

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 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: {}) ⇒ 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_astArray

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)

Examples:

"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, []]
  ]
]]

Returns:

  • (Array)

    the collection as an abstract syntax tree.

See Also:



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