Class: Formalist::ChildForms::ChildForm

Inherits:
Element
  • Object
show all
Defined in:
lib/formalist/child_forms/child_form.rb

Direct Known Subclasses

Elements::FormField

Constant Summary collapse

DEFAULT_INPUT_PROCESSOR =
-> input { input }.freeze

Instance Attribute Summary

Attributes inherited from Element

#children, #errors, #input, #name

Instance Method Summary collapse

Methods inherited from Element

#==, build, fill, #initialize, #type

Methods included from Element::ClassInterface

#attribute, #attributes_schema, #type

Constructor Details

This class inherits a constructor from Formalist::Element

Instance Method Details

#attributesObject



18
19
20
# File 'lib/formalist/child_forms/child_form.rb', line 18

def attributes
  super.merge(form: form_attribute_ast)
end

#fill(input: {}, errors: {}) ⇒ Object



14
15
16
# File 'lib/formalist/child_forms/child_form.rb', line 14

def fill(input: {}, errors: {})
  super(input: form_input_ast(input), errors: errors.to_a)
end

#form_attribute_astObject



22
23
24
# File 'lib/formalist/child_forms/child_form.rb', line 22

def form_attribute_ast
  @attributes[:form].to_ast
end

#form_input_ast(data) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/formalist/child_forms/child_form.rb', line 26

def form_input_ast(data)
  # Run the raw data through the validation schema
  validation = @attributes[:schema].(data)

  # And then through the embedded form's input processor (which may add
  # extra system-generated information necessary for the form to render
  # fully)
  input = @attributes[:input_processor].(validation.to_h)

  @attributes[:form].fill(input: input, errors: validation.errors.to_h).to_ast
end

#to_astObject



38
39
40
41
42
43
44
45
# File 'lib/formalist/child_forms/child_form.rb', line 38

def to_ast
  [:child_form, [
    name,
    type,
    input,
    Element::Attributes.new(attributes).to_ast,
  ]]
end