-
-
Save joeldrapper/cb39af2350dfc9c223b78fc15bff51ae to your computer and use it in GitHub Desktop.
Simple Table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
class Components::SimpleTable < Components::Base | |
Column = Data.define(:name, :align, :content) | |
def initialize(resources) | |
@resources = resources | |
@columns = [] | |
end | |
def before_template | |
# Does an early yield so the public method `column` can be used to build up the table. | |
vanish { yield(self) } | |
end | |
def view_template | |
table do | |
thead do | |
tr do | |
@columns.each do |column| | |
th { column.name } | |
end | |
end | |
end | |
tbody do | |
@resources.each do |resource| | |
tr do | |
@columns.each do |column| | |
td( | |
class: [ | |
("text-right" if column.align == :right), | |
("text-center" if column.align == :center), | |
("text-left" if column.align == :left), | |
] | |
) { column.content.call(resource) } | |
end | |
end | |
end | |
end | |
end | |
end | |
def column(name, align: :left, &content) | |
align => :left | :right | :center | |
@columns << Column.new(name:, align:, content:) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment