Class: Katalyst::SummaryTableComponent

Inherits:
TableComponent
  • Object
show all
Defined in:
app/components/katalyst/summary_table_component.rb

Overview

A component for rendering a summary table for a model. Generates:

<table>
  <tr><th>Name</th><td><a href="/people/1">Aaron</a></td></tr>
  <tr><th>Email</th><td>[email protected]</td></tr>
</table>

Examples:

<%= Katalyst::SummaryTableComponent.new(model: @person) do |row, person| %>
  <%= row.text :name do |cell| %>
    <%= link_to cell.value, person %>
  <% end %>
  <%= row.text :email %>
<% end %>

Instance Attribute Summary

Attributes inherited from TableComponent

#collection, #object_name

Instance Method Summary collapse

Methods inherited from TableComponent

#before_render, #boolean, #currency, #date, #datetime, #enum, #html_attributes=, #inspect, #number, #record, #rich_text, #row, #text

Methods included from Tables::HasTableContent

#before_render, #model_name

Constructor Details

#initialize(model:) ⇒ SummaryTableComponent

Returns a new instance of SummaryTableComponent.



20
21
22
23
24
25
26
# File 'app/components/katalyst/summary_table_component.rb', line 20

def initialize(model:, **)
  super(collection: [model], **)

  @summary_rows = []

  update_html_attributes(class: "summary-table")
end

Instance Method Details

#with_cell(cell) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/components/katalyst/summary_table_component.rb', line 28

def with_cell(cell, &)
  if row.header?
    @summary_rows << with_summary_row do |row|
      row.with_header do |header|
        header.with_cell(cell)
      end
    end
    @index = 0
  else
    @summary_rows[@index].with_body do |body|
      body.with_cell(cell, &)
    end
    @index += 1
  end
end