Class: Monolith::Components::Table

Inherits:
Base
  • Object
show all
Defined in:
app/components/monolith/table.rb

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ Table

Returns a new instance of Table.



2
3
4
5
# File 'app/components/monolith/table.rb', line 2

def initialize(collection)
  @collection = collection
  @columns = []
end

Instance Method Details

#row(header, &row) ⇒ Object



7
8
9
# File 'app/components/monolith/table.rb', line 7

def row(header, &row)
  @columns << [ header, row ]
end

#view_templateObject



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
# File 'app/components/monolith/table.rb', line 11

def view_template(&)
  vanish(&)

  headers, rows = @columns.transpose

  div(class: "overflow-x-auto") do
    table class: "table" do
      thead do
        tr do
          headers.each do |header|
            th { header }
          end
        end
      end
      tbody do
        @collection.each do |item|
          tr do
            rows.each do |cell|
              td { cell.call item }
            end
          end
        end
      end
    end
  end
end