Class: PhlexUI::Table::Builder

Inherits:
Base
  • Object
show all
Includes:
Phlex::DeferredRender
Defined in:
lib/phlex_ui/table/builder.rb

Defined Under Namespace

Classes: Column

Instance Attribute Summary

Attributes inherited from Base

#attrs

Instance Method Summary collapse

Methods inherited from Base

#before_template

Constructor Details

#initialize(records = [], caption: nil, **attrs) ⇒ Builder

Returns a new instance of Builder.



9
10
11
12
13
14
# File 'lib/phlex_ui/table/builder.rb', line 9

def initialize(records = [], caption: nil, **attrs)
  @records = records
  @columns = []
  @caption = caption
  super(**attrs)
end

Instance Method Details

#bodyObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/phlex_ui/table/builder.rb', line 47

def body
  render PhlexUI::Table::Body.new do
    @records.each do |record|
      render PhlexUI::Table::Row.new do
        @columns.each do |column|
          render PhlexUI::Table::Cell.new do
            column.block.call(record)
          end
        end
      end
    end
  end
end

#captionObject



73
74
75
# File 'lib/phlex_ui/table/builder.rb', line 73

def caption
  render PhlexUI::Table::Caption.new { @caption }
end

#column(header, footer = nil, header_attrs: {}, footer_attrs: {}, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/phlex_ui/table/builder.rb', line 25

def column(header, footer = nil, header_attrs: {}, footer_attrs: {}, &block)
  @columns << Column.new(
    header: header,
    header_attrs: header_attrs,
    footer: footer,
    footer_attrs: footer_attrs,
    block: block
  )
end


61
62
63
64
65
66
67
68
69
70
71
# File 'lib/phlex_ui/table/builder.rb', line 61

def footer
  render PhlexUI::Table::Footer.new do
    render PhlexUI::Table::Row.new do
      @columns.each do |column|
        render PhlexUI::Table::Head.new(**column.footer_attrs) do
          column.footer
        end
      end
    end
  end
end

#headerObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/phlex_ui/table/builder.rb', line 35

def header
  render PhlexUI::Table::Header.new do
    render PhlexUI::Table::Row.new do
      @columns.each do |column|
        render PhlexUI::Table::Head.new(**column.header_attrs) do
          column.header
        end
      end
    end
  end
end

#view_template(&block) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/phlex_ui/table/builder.rb', line 16

def view_template(&block)
  render PhlexUI::Table.new(**attrs) do
    header
    body
    caption if @caption
    footer if @columns.any?(&:footer)
  end
end