Skip to content

Commit

Permalink
Allow to pass icon to resource table
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdeyen committed Dec 7, 2023
1 parent ce4fd08 commit e511eb1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
3 changes: 3 additions & 0 deletions app/views/alchemy/admin/resources/_resource.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<tr class="<%= cycle('even', 'odd') %>">
<% if local_assigns[:icon] %>
<td class="icon"><%= render_icon(local_assigns[:icon], size: "xl") %></td>
<% end %>
<% resource_handler.sorted_attributes.each do |attribute| %>
<td class="<%= attribute[:type] %> <%= attribute[:name] %>">
<% if attribute[:type] == :boolean %>
Expand Down
5 changes: 4 additions & 1 deletion app/views/alchemy/admin/resources/_table.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<table class="list" id="<%= resource_handler.resources_name %>_list">
<thead>
<tr>
<% if local_assigns[:icon] %>
<th class="icon"></th>
<% end %>
<% resource_handler.sorted_attributes.each do |attribute| %>
<th class="<%= attribute[:type] %> <%= attribute[:name] %>">
<%= sort_link [:resource_url_proxy, @query],
Expand All @@ -14,7 +17,7 @@
</tr>
</thead>
<tbody>
<%= render_resources %>
<%= render_resources(icon: local_assigns[:icon]) %>
</tbody>
</table>
<% elsif search_filter_params[:q].present? %>
Expand Down
6 changes: 3 additions & 3 deletions lib/alchemy/resources_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ def sortable_resource_header_column(attribute)
#
# NOTE: Alchemy gives you a local variable named like your resource
#
def render_resources
render partial: resource_name, collection: resources_instance_variable
def render_resources(icon: nil)
render partial: resource_name, collection: resources_instance_variable, locals: {icon: icon}
rescue ActionView::MissingTemplate
render partial: "resource", collection: resources_instance_variable
render partial: "resource", collection: resources_instance_variable, locals: {icon: icon}
end

def resource_has_tags
Expand Down
12 changes: 12 additions & 0 deletions spec/libraries/resources_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,18 @@ def resource_handler
end
end

describe "#render_resources" do
it "renders a resource collection" do
expect(controller).to receive(:render).with(collection: [resource_item], partial: "my_resource", locals: {icon: nil})
controller.render_resources
end

it "allows to pass an icon" do
expect(controller).to receive(:render).with(collection: [resource_item], partial: "my_resource", locals: {icon: "global"})
controller.render_resources(icon: "global")
end
end

describe "#resource_attribute_field_options" do
subject { controller.resource_attribute_field_options(attribute) }

Expand Down

0 comments on commit e511eb1

Please sign in to comment.