Skip to content

Instantly share code, notes, and snippets.

@joeldrapper
Created November 25, 2024 23:10
Show Gist options
  • Save joeldrapper/cb39af2350dfc9c223b78fc15bff51ae to your computer and use it in GitHub Desktop.
Save joeldrapper/cb39af2350dfc9c223b78fc15bff51ae to your computer and use it in GitHub Desktop.
Simple Table
# 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