Move table actions partial to a component
This partial was going to get too complex since in some places we've got different texts, different URLs or different confirmation messages. While we should probably try to be more consistent and that would make the partial work in most cases, there'll always be some exceptions, and using a partial (with, perhaps, some helper methods) will become messy really quickly.
This commit is contained in:
8
app/components/admin/table_actions_component.rb
Normal file
8
app/components/admin/table_actions_component.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
class Admin::TableActionsComponent < ApplicationComponent
|
||||
attr_reader :record, :actions
|
||||
|
||||
def initialize(record, actions: [:edit, :destroy])
|
||||
@record = record
|
||||
@actions = actions
|
||||
end
|
||||
end
|
||||
@@ -30,7 +30,7 @@
|
||||
</td>
|
||||
<td>
|
||||
<% if admin_notification.draft? %>
|
||||
<%= render "shared/table_actions", record: admin_notification, actions: [:edit, :destroy] %>
|
||||
<%= render Admin::TableActionsComponent.new(admin_notification) %>
|
||||
<%= link_to t("admin.admin_notifications.index.preview"),
|
||||
admin_admin_notification_path(admin_notification),
|
||||
class: "button" %>
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
<%= administrator.description %>
|
||||
</td>
|
||||
<td>
|
||||
<%= render "shared/table_actions", record: administrator, actions: [:edit, :destroy] %>
|
||||
<%= render Admin::TableActionsComponent.new(administrator) %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
<td><%= user.email %></td>
|
||||
<td class="text-right">
|
||||
<% if user.administrator? && user.administrator.persisted? %>
|
||||
<%= render "shared/table_actions", record: user.administrator, actions: [:destroy] %>
|
||||
<%= render Admin::TableActionsComponent.new(user.administrator, actions: [:destroy]) %>
|
||||
<% else %>
|
||||
<%= link_to t("admin.administrators.administrator.add"),
|
||||
{ controller: "admin/administrators",
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
<td><%= link_to t("admin.budget_groups.headings_manage"),
|
||||
admin_budget_group_headings_path(@budget, group) %></td>
|
||||
<td>
|
||||
<%= render "shared/table_actions", record: group, actions: [:edit, :destroy] %>
|
||||
<%= render Admin::TableActionsComponent.new(group) %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
<%= heading.allow_custom_content ? t("admin.shared.true_value") : t("admin.shared.false_value") %>
|
||||
</td>
|
||||
<td>
|
||||
<%= render "shared/table_actions", record: heading, actions: [:edit, :destroy] %>
|
||||
<%= render Admin::TableActionsComponent.new(heading) %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
<td class="text-center"><%= number_with_delimiter(action.required_supports, delimiter: ".") %></td>
|
||||
<td class="text-center"><%= action.order %></td>
|
||||
<td class="text-right">
|
||||
<%= render "shared/table_actions", record: action, actions: [:edit, :destroy] %>
|
||||
<%= render Admin::TableActionsComponent.new(action) %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
<td><%= geozone.census_code %></td>
|
||||
<td class="break"><%= geozone.html_map_coordinates %></td>
|
||||
<td>
|
||||
<%= render "shared/table_actions", record: geozone, actions: [:edit, :destroy] %>
|
||||
<%= render Admin::TableActionsComponent.new(geozone) %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
|
||||
@@ -17,6 +17,6 @@
|
||||
<% end %>
|
||||
</td>
|
||||
<td>
|
||||
<%= render "shared/table_actions", record: card, actions: [:edit, :destroy] %>
|
||||
<%= render Admin::TableActionsComponent.new(card) %>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
<td class="text-center"><%= I18n.l process.end_date %></td>
|
||||
<td class="text-center"><%= process.total_comments %></td>
|
||||
<td>
|
||||
<%= render "shared/table_actions", record: process, actions: [:destroy] %>
|
||||
<%= render Admin::TableActionsComponent.new(process, actions: [:destroy]) %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
|
||||
@@ -12,6 +12,6 @@
|
||||
<%= local_census_record.postal_code %>
|
||||
</td>
|
||||
<td>
|
||||
<%= render "shared/table_actions", record: local_census_record, actions: [:edit, :destroy] %>
|
||||
<%= render Admin::TableActionsComponent.new(local_census_record) %>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
<%= manager.email %>
|
||||
</td>
|
||||
<td>
|
||||
<%= render "shared/table_actions", record: manager, actions: [:destroy] %>
|
||||
<%= render Admin::TableActionsComponent.new(manager, actions: [:destroy]) %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
<td><%= user.email %></td>
|
||||
<td>
|
||||
<% if user.manager? && user.manager.persisted? %>
|
||||
<%= render "shared/table_actions", record: user.manager, actions: [:destroy] %>
|
||||
<%= render Admin::TableActionsComponent.new(user.manager, actions: [:destroy]) %>
|
||||
<% else %>
|
||||
<%= link_to t("admin.managers.manager.add"),
|
||||
{ controller: "admin/managers",
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
<%= status.description %>
|
||||
</td>
|
||||
<td>
|
||||
<%= render "shared/table_actions", record: status, actions: [:edit, :destroy] %>
|
||||
<%= render Admin::TableActionsComponent.new(status) %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
<%= moderator.email %>
|
||||
</td>
|
||||
<td>
|
||||
<%= render "shared/table_actions", record: moderator, actions: [:destroy] %>
|
||||
<%= render Admin::TableActionsComponent.new(moderator, actions: [:destroy]) %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
<td><%= user.email %></td>
|
||||
<td>
|
||||
<% if user.moderator? && user.moderator.persisted? %>
|
||||
<%= render "shared/table_actions", record: user.moderator, actions: [:destroy] %>
|
||||
<%= render Admin::TableActionsComponent.new(user.moderator, actions: [:destroy]) %>
|
||||
<% else %>
|
||||
<%= link_to t("admin.moderators.moderator.add"),
|
||||
{ controller: "admin/moderators",
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
<% end %>
|
||||
</td>
|
||||
<td>
|
||||
<%= render "shared/table_actions", record: newsletter, actions: [:edit, :destroy] %>
|
||||
<%= render Admin::TableActionsComponent.new(newsletter) %>
|
||||
|
||||
<%= link_to t("admin.newsletters.index.preview"), admin_newsletter_path(newsletter),
|
||||
class: "button" %>
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
<td>
|
||||
<%= link_to t("admin.polls.show.edit_answers"), admin_question_path(question),
|
||||
class: "button hollow" %>
|
||||
<%= render "shared/table_actions", record: question, actions: [:edit, :destroy] %>
|
||||
<%= render Admin::TableActionsComponent.new(question) %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
<% end %>
|
||||
</td>
|
||||
<td>
|
||||
<%= render "shared/table_actions", record: question, actions: [:edit, :destroy] %>
|
||||
<%= render Admin::TableActionsComponent.new(question) %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
<td><%= video.title %></td>
|
||||
<td><%= link_to "#{video.url}", video.url %></td>
|
||||
<td class="text-right">
|
||||
<%= render "shared/table_actions", record: video, actions: [:edit, :destroy] %>
|
||||
<%= render Admin::TableActionsComponent.new(video) %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
|
||||
@@ -36,7 +36,7 @@
|
||||
<%= number_to_percentage(progress_bar.percentage, strip_insignificant_zeros: true) %>
|
||||
</td>
|
||||
<td>
|
||||
<%= render "shared/table_actions", record: progress_bar, actions: [:edit, :destroy] %>
|
||||
<%= render Admin::TableActionsComponent.new(progress_bar) %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
|
||||
@@ -6,6 +6,6 @@
|
||||
<%= group.valuators.count %>
|
||||
</td>
|
||||
<td>
|
||||
<%= render "shared/table_actions", record: group, actions: [:edit, :destroy] %>
|
||||
<%= render Admin::TableActionsComponent.new(group) %>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
@@ -19,6 +19,6 @@
|
||||
<%= valuator_abilities(valuator) %>
|
||||
</td>
|
||||
<td>
|
||||
<%= render "shared/table_actions", record: valuator, actions: [:edit, :destroy] %>
|
||||
<%= render Admin::TableActionsComponent.new(valuator) %>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
29
spec/components/admin/table_actions_component_spec.rb
Normal file
29
spec/components/admin/table_actions_component_spec.rb
Normal file
@@ -0,0 +1,29 @@
|
||||
require "rails_helper"
|
||||
|
||||
describe Admin::TableActionsComponent, type: :component do
|
||||
let(:record) { create(:banner) }
|
||||
|
||||
it "renders links to edit and destroy a record by default" do
|
||||
render_inline Admin::TableActionsComponent.new(record)
|
||||
|
||||
expect(page).to have_css "a", count: 2
|
||||
expect(page).to have_css "a[href*='edit']", text: "Edit"
|
||||
expect(page).to have_css "a[data-method='delete']", text: "Delete"
|
||||
end
|
||||
|
||||
context "actions parameter is passed" do
|
||||
it "renders a link to edit a record if passed" do
|
||||
render_inline Admin::TableActionsComponent.new(record, actions: [:edit])
|
||||
|
||||
expect(page).to have_link "Edit"
|
||||
expect(page).not_to have_link "Delete"
|
||||
end
|
||||
|
||||
it "renders a link to destroy a record if passed" do
|
||||
render_inline Admin::TableActionsComponent.new(record, actions: [:destroy])
|
||||
|
||||
expect(page).to have_link "Delete"
|
||||
expect(page).not_to have_link "Edit"
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user