Skip to content

Commit

Permalink
WIP: Add resource table component
Browse files Browse the repository at this point in the history
Add first try of a resource table. It is pretty raw and does not create the right output, but it is only a proposal.
  • Loading branch information
kulturbande committed Mar 9, 2024
1 parent 63d5474 commit c3a768c
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions app/components/alchemy/admin/resource_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

module Alchemy
module Admin
class ResourceTable < ViewComponent::Base
attr_reader :columns, :collection

erb_template <<~ERB
<div class="resources-table-wrapper">
<table>
<tr>
<% columns.each do |column| %>
<th><%= column.name %></th>
<% end %>
</tr>
<% collection.each do |row| %>
<tr>
<% columns.each do |column| %>
<td>
<%= view_context.capture(row, &column.block) %>
</td>
<% end %>
</tr>
<% end %>
</table>
</div>
ERB

def initialize(collection)
@collection = collection
@columns = []
end

def add_column(name, sortable: false, &block)
@columns << Column.new(name, sortable: sortable, &block)
end

private

##
# the before_render - method is necessary to force ViewComponent to evaluate the add_column - calls
def before_render
content
end

class Column
attr_reader :block, :name, :sortable

def initialize(name, sortable:, &block)
@name = name
@sortable = sortable
@block = block_given? ? block : lambda { |item| item[name] }
end
end
end
end
end

0 comments on commit c3a768c

Please sign in to comment.