Module: Documents::Container

Extended by:
ActiveSupport::Concern
Included in:
Table
Defined in:
app/models/concerns/documents/container.rb

Instance Method Summary collapse

Instance Method Details

#load_elements_from(configuration) ⇒ Object

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/concerns/documents/container.rb', line 8

def load_elements_from configuration
  result = Documents::DocumentDefinition.new.call(configuration)
  raise ArgumentError.new(result.errors.to_h.to_json) if result.errors.any?

  configuration["elements"].each do |element_config|
    case element_config["element"]
    when "paragraph"
      elements.create!(
        type: "Documents::Paragraph",
        position: :last,
        html: element_config["html"],
        description: element_config["description"] || ""
      )
    when "form"
      form = elements.create!(
        type: "Documents::Form",
        position: :last,
        section_type: element_config["section_type"],
        display_type: element_config["display_type"],
        description: element_config["description"] || ""
      )

      # Create field values for the first section
      section = form.sections.first
      element_config["fields"]&.each do |field_config|
        section.field_values.create!(
          type: field_config["field_type"],
          name: field_config["name"],
          description: field_config["description"],
          required: field_config["required"] || false,
          position: :last
        )
      end
    end
  end
end