Class: Components::Bulma::Table

Inherits:
Base
  • Object
show all
Defined in:
lib/components/bulma/table.rb

Overview

Table component for data display

This component implements the [Bulma table](bulma.io/documentation/elements/table/) interface, providing a way to display data in rows and columns with customizable headers and formatting options.

## Example

“‘ruby users = User.all

Bulma::Table(users) do |table|

table.column "Name" do |user|
  user.full_name
end

table.column "Email" do |user|
  user.email
end

table.column "Actions" do |user|
  link_to "Edit", edit_user_path(user), class: "button is-small"
end

end “‘

Instance Method Summary collapse

Constructor Details

#initialize(rows, id_or_options = nil, **options) ⇒ Table

Returns a new instance of Table.



32
33
34
35
36
# File 'lib/components/bulma/table.rb', line 32

def initialize(rows, id_or_options = nil, **options)
  @rows = rows
  @id, @table_class = parse_id_and_options(id_or_options, options, rows)
  @columns = []
end

Instance Method Details

#column(header, **html_attributes, &content) ⇒ Object



62
63
64
# File 'lib/components/bulma/table.rb', line 62

def column(header, **html_attributes, &content)
  @columns << { header:, html_attributes:, content: }
end

#conditional_icon(header, icon_class: "fas fa-check", **html_attributes, &content) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/components/bulma/table.rb', line 72

def conditional_icon(header, icon_class: "fas fa-check", **html_attributes, &content)
  html_attributes[:class] = [html_attributes[:class], "has-text-centered"].compact.join(" ")

  column(header, **html_attributes) do |row|
    icon_span(icon_class) if content.call(row)
  end
end

#date_column(header, format: "%Y-%m-%d", **html_attributes, &content) ⇒ Object



66
67
68
69
70
# File 'lib/components/bulma/table.rb', line 66

def date_column(header, format: "%Y-%m-%d", **html_attributes, &content)
  column(header, **html_attributes) do |row|
    content.call(row)&.strftime(format)
  end
end

#paginate(&path_builder) ⇒ Object



80
81
82
# File 'lib/components/bulma/table.rb', line 80

def paginate(&path_builder)
  @path_builder = path_builder
end

#view_templateObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/components/bulma/table.rb', line 38

def view_template(&)
  vanish(&)

  table(id: @id, class: @table_class) do
    thead do
      @columns.each do |column|
        table_header(column)
      end
    end

    tbody do
      @rows.each do |row|
        tr do
          @columns.each do |column|
            td(**column[:html_attributes]) { column[:content].call(row) }
          end
        end
      end
    end

    pagination
  end
end