-
-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
63d5474
commit c3a768c
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
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
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 |