Skip to content

Commit d34cf20

Browse files
authored
Extract shared resource toolbar partial (#972)
## Summary - add `shared/resource_toolbar` partial for edit, preview, and destroy actions - replace duplicated action buttons in show views with the shared toolbar - document toolbar usage and add translation for preview action ## Testing - `bundle exec rubocop` *(fails: bundler: command not found: rubocop)* - `bundle exec brakeman -q -w2` *(fails: bundler: command not found: brakeman)* - `bundle exec bundler-audit --update` *(fails: bundler: command not found: bundler-audit)* - `bin/codex_style_guard` *(fails: bundler: command not found: rubocop)* - `bin/ci` *(fails: bundler: command not found: rails)* ------ https://chatgpt.com/codex/tasks/task_e_689a55855c448321b8d8ff076a916415
2 parents 0e995d7 + a9cc24f commit d34cf20

File tree

10 files changed

+124
-70
lines changed

10 files changed

+124
-70
lines changed

app/views/better_together/calls_for_interest/show.html.erb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,12 @@
2222
<% end %>
2323
</div>
2424
<div class="col-auto">
25-
<% if policy(@resource).edit? %>
26-
<%= link_to edit_call_for_interest_path(@resource), class: 'btn btn-outline-primary btn-sm me-2', 'aria-label' => 'Edit Partner' do %>
27-
<i class="fas fa-edit"></i> <%= t('globals.edit') %>
28-
<% end %>
29-
<% end %>
30-
<% if policy(@resource).destroy? %>
31-
<%= link_to call_for_interest_path(@resource), data: { turbo_method: :delete, turbo_confirm: t('globals.confirm_delete') }, class: 'btn btn-outline-danger btn-sm', 'aria-label' => 'Delete Record' do %>
32-
<i class="fas fa-trash-alt"></i> <%= t('globals.delete') %>
33-
<% end %>
34-
<% end %>
25+
<%= render 'shared/resource_toolbar',
26+
edit_path: policy(@resource).edit? ? edit_call_for_interest_path(@resource) : nil,
27+
edit_aria_label: 'Edit Partner',
28+
destroy_path: policy(@resource).destroy? ? call_for_interest_path(@resource) : nil,
29+
destroy_confirm: t('globals.confirm_delete'),
30+
destroy_aria_label: 'Delete Record' %>
3531
</div>
3632
</div>
3733
</div>

app/views/better_together/categories/show.html.erb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,11 @@
1212
<% if policy(resource_class).index? %>
1313
<%= link_to t('better_together.categories.back_to_categories'), categories_path, class: 'btn btn-sm btn-outline-secondary' %>
1414
<% end %>
15-
<% if policy(@resource).edit? %>
16-
<%= link_to edit_category_path(@resource), class: 'btn btn-outline-primary btn-sm me-2', 'aria-label' => 'Edit Partner' do %>
17-
<i class="fas fa-edit"></i> <%= t('globals.edit') %>
18-
<% end %>
19-
<% end %>
20-
<% if policy(@resource).destroy? %>
21-
<%= link_to category_path(@resource), data: { turbo_method: :delete, turbo_confirm: t('globals.confirm_delete') }, class: 'btn btn-outline-danger btn-sm', 'aria-label' => 'Delete Record' do %>
22-
<i class="fas fa-trash-alt"></i> <%= t('globals.delete') %>
23-
<% end %>
24-
<% end %>
15+
<%= render 'shared/resource_toolbar',
16+
edit_path: policy(@resource).edit? ? edit_category_path(@resource) : nil,
17+
edit_aria_label: 'Edit Partner',
18+
destroy_path: policy(@resource).destroy? ? category_path(@resource) : nil,
19+
destroy_confirm: t('globals.confirm_delete'),
20+
destroy_aria_label: 'Delete Record' %>
2521
</div>
2622
</div>

app/views/better_together/communities/show.html.erb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,12 @@
2727
<%= privacy_badge(@resource) %>
2828
</div>
2929
<div class="col-auto">
30-
<% if policy(@resource).edit? %>
31-
<%= link_to edit_community_path(@resource), class: 'btn btn-outline-primary btn-sm me-2', 'aria-label' => 'Edit Partner' do %>
32-
<i class="fas fa-edit"></i> <%= t('globals.edit') %>
33-
<% end %>
34-
<% end %>
35-
<% if policy(@resource).destroy? %>
36-
<%= link_to community_path(@resource), data: { turbo_method: :delete, turbo_confirm: t('partners.confirm_delete') }, class: 'btn btn-outline-danger btn-sm', 'aria-label' => 'Delete Partner' do %>
37-
<i class="fas fa-trash-alt"></i> <%= t('globals.delete') %>
38-
<% end %>
39-
<% end %>
30+
<%= render 'shared/resource_toolbar',
31+
edit_path: policy(@resource).edit? ? edit_community_path(@resource) : nil,
32+
edit_aria_label: 'Edit Partner',
33+
destroy_path: policy(@resource).destroy? ? community_path(@resource) : nil,
34+
destroy_confirm: t('partners.confirm_delete'),
35+
destroy_aria_label: 'Delete Partner' %>
4036
</div>
4137
</div>
4238
</div>

app/views/better_together/events/show.html.erb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,12 @@
2929
<% end %>
3030
</div>
3131
<div class="col-auto">
32-
<% if policy(@resource).edit? %>
33-
<%= link_to edit_event_path(@resource), class: 'btn btn-outline-primary btn-sm me-2', 'aria-label' => 'Edit Partner' do %>
34-
<i class="fas fa-edit"></i> <%= t('globals.edit') %>
35-
<% end %>
36-
<% end %>
37-
<% if policy(@resource).destroy? %>
38-
<%= link_to event_path(@resource), data: { turbo_method: :delete, turbo_confirm: t('globals.confirm_delete') }, class: 'btn btn-outline-danger btn-sm', 'aria-label' => 'Delete Record' do %>
39-
<i class="fas fa-trash-alt"></i> <%= t('globals.delete') %>
40-
<% end %>
41-
<% end %>
32+
<%= render 'shared/resource_toolbar',
33+
edit_path: policy(@resource).edit? ? edit_event_path(@resource) : nil,
34+
edit_aria_label: 'Edit Partner',
35+
destroy_path: policy(@resource).destroy? ? event_path(@resource) : nil,
36+
destroy_confirm: t('globals.confirm_delete'),
37+
destroy_aria_label: 'Delete Record' %>
4238
</div>
4339
</div>
4440
</div>

app/views/better_together/people/show.html.erb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,12 @@
2626
<%= privacy_badge(@person) %>
2727
</div>
2828
<div class="col-auto">
29-
<% if policy(@person).edit? %>
30-
<%= link_to edit_person_path(@person), class: 'btn btn-outline-primary btn-sm me-2', 'aria-label' => 'Edit Profile' do %>
31-
<i class="fas fa-edit"></i> <%= t('globals.edit') %>
32-
<% end %>
33-
<% end %>
34-
<% if policy(@person).destroy? %>
35-
<%= link_to person_path(@person), method: :delete, data: { turbo_method: :delete, confirm: t('people.confirm_delete') }, class: 'btn btn-outline-danger btn-sm', 'aria-label' => 'Delete Profile' do %>
36-
<i class="fas fa-trash-alt"></i> <%= t('globals.delete') %>
37-
<% end %>
38-
<% end %>
29+
<%= render 'shared/resource_toolbar',
30+
edit_path: policy(@person).edit? ? edit_person_path(@person) : nil,
31+
edit_aria_label: 'Edit Profile',
32+
destroy_path: policy(@person).destroy? ? person_path(@person) : nil,
33+
destroy_confirm: t('people.confirm_delete'),
34+
destroy_aria_label: 'Delete Profile' %>
3935
</div>
4036
</div>
4137
</div>

app/views/better_together/platforms/show.html.erb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,12 @@
2424
<p class="profile-description"><%= @platform.description.presence || t('globals.no_description') %></p>
2525
</div>
2626
<div class="col-auto">
27-
<% if policy(@platform).edit? %>
28-
<%= link_to edit_platform_path(@platform), class: 'btn btn-outline-primary btn-sm me-2', 'aria-label' => 'Edit Platform' do %>
29-
<i class="fas fa-edit"></i> <%= t('globals.edit') %>
30-
<% end %>
31-
<% end %>
32-
<% if policy(@platform).destroy? %>
33-
<%= link_to platform_path(@platform), method: :delete, data: { turbo_method: :delete, confirm: t('platforms.confirm_delete') }, class: 'btn btn-outline-danger btn-sm', 'aria-label' => 'Delete Platform' do %>
34-
<i class="fas fa-trash-alt"></i> <%= t('globals.delete') %>
35-
<% end %>
36-
<% end %>
27+
<%= render 'shared/resource_toolbar',
28+
edit_path: policy(@platform).edit? ? edit_platform_path(@platform) : nil,
29+
edit_aria_label: 'Edit Platform',
30+
destroy_path: policy(@platform).destroy? ? platform_path(@platform) : nil,
31+
destroy_confirm: t('platforms.confirm_delete'),
32+
destroy_aria_label: 'Delete Platform' %>
3733
</div>
3834
</div>
3935
</div>

app/views/better_together/posts/show.html.erb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,11 @@
1313
<% if policy(resource_class).index? %>
1414
<%= link_to t('better_together.posts.back_to_posts', default: 'Back to Posts'), posts_path, class: 'btn btn-sm btn-outline-secondary' %>
1515
<% end %>
16-
<% if policy(@post).edit? %>
17-
<%= link_to edit_post_path(@post), class: 'btn btn-outline-primary btn-sm me-2', 'aria-label' => 'Edit Post' do %>
18-
<i class="fas fa-edit"></i> <%= t('globals.edit') %>
19-
<% end %>
20-
<% end %>
21-
<% if policy(@post).destroy? %>
22-
<%= link_to post_path(@post), data: { turbo_method: :delete, turbo_confirm: t('globals.confirm_delete') }, class: 'btn btn-outline-danger btn-sm', 'aria-label' => 'Delete Record' do %>
23-
<i class="fas fa-trash-alt"></i> <%= t('globals.delete') %>
24-
<% end %>
25-
<% end %>
16+
<%= render 'shared/resource_toolbar',
17+
edit_path: policy(@post).edit? ? edit_post_path(@post) : nil,
18+
edit_aria_label: 'Edit Post',
19+
destroy_path: policy(@post).destroy? ? post_path(@post) : nil,
20+
destroy_confirm: t('globals.confirm_delete'),
21+
destroy_aria_label: 'Delete Record' %>
2622
</div>
2723
</div>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<%# Renders a toolbar with optional action buttons for a resource.
2+
Locals:
3+
edit_path: path for edit action (optional)
4+
edit_aria_label: aria-label for edit button (defaults to t('globals.edit'))
5+
view_path: path for view action (optional)
6+
view_aria_label: aria-label for view button (defaults to t('globals.view'))
7+
destroy_path: path for destroy action (optional)
8+
destroy_confirm: confirmation message (defaults to t('globals.confirm_delete'))
9+
destroy_aria_label: aria-label for destroy button (defaults to t('globals.delete'))
10+
%>
11+
<div class="resource-toolbar">
12+
<% if local_assigns[:edit_path] %>
13+
<%= link_to edit_path, class: 'btn btn-outline-primary btn-sm me-2', 'aria-label' => (local_assigns[:edit_aria_label] || t('globals.edit')) do %>
14+
<i class="fas fa-edit"></i> <%= t('globals.edit') %>
15+
<% end %>
16+
<% end %>
17+
<% if local_assigns[:view_path] %>
18+
<%= link_to view_path, class: 'btn btn-outline-secondary btn-sm me-2', 'aria-label' => (local_assigns[:view_aria_label] || t('globals.view')) do %>
19+
<i class="fas fa-eye"></i> <%= t('globals.view') %>
20+
<% end %>
21+
<% end %>
22+
<% if local_assigns[:destroy_path] %>
23+
<%= link_to destroy_path, data: { turbo_method: :delete, turbo_confirm: (local_assigns[:destroy_confirm] || t('globals.confirm_delete')) }, class: 'btn btn-outline-danger btn-sm', 'aria-label' => (local_assigns[:destroy_aria_label] || t('globals.delete')) do %>
24+
<i class="fas fa-trash-alt"></i> <%= t('globals.delete') %>
25+
<% end %>
26+
<% end %>
27+
</div>

docs/resource_toolbar.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Resource Toolbar
2+
3+
A shared partial for rendering edit, view, and destroy buttons for a resource.
4+
5+
## Usage
6+
7+
```
8+
<%= render 'shared/resource_toolbar',
9+
edit_path: edit_post_path(@post),
10+
view_path: post_path(@post),
11+
destroy_path: post_path(@post),
12+
destroy_confirm: t('globals.confirm_delete'),
13+
edit_aria_label: 'Edit Post',
14+
view_aria_label: 'View Post',
15+
destroy_aria_label: 'Delete Post' %>
16+
```
17+
18+
## Locals
19+
20+
- `edit_path` – link for the edit action (optional)
21+
- `view_path` – link for the view action (optional)
22+
- `destroy_path` – link for the destroy action (optional)
23+
- `destroy_confirm` – confirmation text for destroy (defaults to `t('globals.confirm_delete')`)
24+
- `edit_aria_label`, `view_aria_label`, `destroy_aria_label` – ARIA labels for accessibility.
25+
26+
Buttons render only when the corresponding path is provided. Defaults use the global translations for button text and ARIA labels.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe 'shared/resource_toolbar', type: :view do
6+
it 'renders provided action buttons' do
7+
render partial: 'shared/resource_toolbar', locals: {
8+
edit_path: '/edit',
9+
view_path: '/view',
10+
destroy_path: '/destroy',
11+
destroy_confirm: 'Are you sure?'
12+
}
13+
14+
expect(rendered).to include(t('globals.edit'))
15+
expect(rendered).to include(t('globals.view'))
16+
expect(rendered).to include(t('globals.delete'))
17+
expect(rendered).to include('href="/edit"')
18+
expect(rendered).to include('href="/view"')
19+
expect(rendered).to include('href="/destroy"')
20+
end
21+
22+
it 'omits buttons when paths are missing' do
23+
render partial: 'shared/resource_toolbar'
24+
25+
expect(rendered).not_to include(t('globals.edit'))
26+
expect(rendered).not_to include(t('globals.view'))
27+
expect(rendered).not_to include(t('globals.delete'))
28+
end
29+
end

0 commit comments

Comments
 (0)