Module: CustomFormGenerator::Helpers::TableRenderer

Included in:
Generator
Defined in:
lib/custom_form_generator/helpers/table_renderer.rb

Overview

Module CustomFormGenerator::Helpers::TableRenderer helps in gnerating table from yaml using slim template

Instance Method Summary collapse

Instance Method Details

#generate_tabular_view(data:, table_template:) ⇒ Object



32
33
34
35
# File 'lib/custom_form_generator/helpers/table_renderer.rb', line 32

def generate_tabular_view(data:, table_template:)
  template = Slim::Template.new { table_template }
  template.render(self, data: data)
end

#table_templateObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/custom_form_generator/helpers/table_renderer.rb', line 8

def table_template
  <<-SLIM
    table
      tr
        - @table_yaml.each do |field|
          th id=CGI.escapeHTML(field["id"]) class=CGI.escapeHTML(field["class"]) = field['label']
        th Activities
      - data.each do |entry|
        tr
          - @table_yaml.each do |field|
            td id=CGI.escapeHTML(field["id"]) class=CGI.escapeHTML(field["class"])
              - value = entry.dig(*field['key'].split('.'))
              - if field['key'] == 'properties.published_at' && value.nil?
                | false
              - elsif field['key'] == 'properties.image_url' && value.nil?
                img src='/path/to/default_image.jpg' alt='Default Thumbnail'
              - else
                = value
          td
            button class='edit-button' data-id=entry['_id']['$oid'] Edit
            button class='delete-button' data-id=entry['_id']['$oid'] Delete
  SLIM
end