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>
|
||||||
<td>
|
<td>
|
||||||
<% if admin_notification.draft? %>
|
<% 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"),
|
<%= link_to t("admin.admin_notifications.index.preview"),
|
||||||
admin_admin_notification_path(admin_notification),
|
admin_admin_notification_path(admin_notification),
|
||||||
class: "button" %>
|
class: "button" %>
|
||||||
|
|||||||
@@ -29,7 +29,7 @@
|
|||||||
<%= administrator.description %>
|
<%= administrator.description %>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<%= render "shared/table_actions", record: administrator, actions: [:edit, :destroy] %>
|
<%= render Admin::TableActionsComponent.new(administrator) %>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
<td><%= user.email %></td>
|
<td><%= user.email %></td>
|
||||||
<td class="text-right">
|
<td class="text-right">
|
||||||
<% if user.administrator? && user.administrator.persisted? %>
|
<% if user.administrator? && user.administrator.persisted? %>
|
||||||
<%= render "shared/table_actions", record: user.administrator, actions: [:destroy] %>
|
<%= render Admin::TableActionsComponent.new(user.administrator, actions: [:destroy]) %>
|
||||||
<% else %>
|
<% else %>
|
||||||
<%= link_to t("admin.administrators.administrator.add"),
|
<%= link_to t("admin.administrators.administrator.add"),
|
||||||
{ controller: "admin/administrators",
|
{ controller: "admin/administrators",
|
||||||
|
|||||||
@@ -29,7 +29,7 @@
|
|||||||
<td><%= link_to t("admin.budget_groups.headings_manage"),
|
<td><%= link_to t("admin.budget_groups.headings_manage"),
|
||||||
admin_budget_group_headings_path(@budget, group) %></td>
|
admin_budget_group_headings_path(@budget, group) %></td>
|
||||||
<td>
|
<td>
|
||||||
<%= render "shared/table_actions", record: group, actions: [:edit, :destroy] %>
|
<%= render Admin::TableActionsComponent.new(group) %>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|||||||
@@ -34,7 +34,7 @@
|
|||||||
<%= heading.allow_custom_content ? t("admin.shared.true_value") : t("admin.shared.false_value") %>
|
<%= heading.allow_custom_content ? t("admin.shared.true_value") : t("admin.shared.false_value") %>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<%= render "shared/table_actions", record: heading, actions: [:edit, :destroy] %>
|
<%= render Admin::TableActionsComponent.new(heading) %>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
<td class="text-center"><%= number_with_delimiter(action.required_supports, delimiter: ".") %></td>
|
<td class="text-center"><%= number_with_delimiter(action.required_supports, delimiter: ".") %></td>
|
||||||
<td class="text-center"><%= action.order %></td>
|
<td class="text-center"><%= action.order %></td>
|
||||||
<td class="text-right">
|
<td class="text-right">
|
||||||
<%= render "shared/table_actions", record: action, actions: [:edit, :destroy] %>
|
<%= render Admin::TableActionsComponent.new(action) %>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|||||||
@@ -22,7 +22,7 @@
|
|||||||
<td><%= geozone.census_code %></td>
|
<td><%= geozone.census_code %></td>
|
||||||
<td class="break"><%= geozone.html_map_coordinates %></td>
|
<td class="break"><%= geozone.html_map_coordinates %></td>
|
||||||
<td>
|
<td>
|
||||||
<%= render "shared/table_actions", record: geozone, actions: [:edit, :destroy] %>
|
<%= render Admin::TableActionsComponent.new(geozone) %>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|||||||
@@ -17,6 +17,6 @@
|
|||||||
<% end %>
|
<% end %>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<%= render "shared/table_actions", record: card, actions: [:edit, :destroy] %>
|
<%= render Admin::TableActionsComponent.new(card) %>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|||||||
@@ -35,7 +35,7 @@
|
|||||||
<td class="text-center"><%= I18n.l process.end_date %></td>
|
<td class="text-center"><%= I18n.l process.end_date %></td>
|
||||||
<td class="text-center"><%= process.total_comments %></td>
|
<td class="text-center"><%= process.total_comments %></td>
|
||||||
<td>
|
<td>
|
||||||
<%= render "shared/table_actions", record: process, actions: [:destroy] %>
|
<%= render Admin::TableActionsComponent.new(process, actions: [:destroy]) %>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|||||||
@@ -12,6 +12,6 @@
|
|||||||
<%= local_census_record.postal_code %>
|
<%= local_census_record.postal_code %>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<%= render "shared/table_actions", record: local_census_record, actions: [:edit, :destroy] %>
|
<%= render Admin::TableActionsComponent.new(local_census_record) %>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|||||||
@@ -22,7 +22,7 @@
|
|||||||
<%= manager.email %>
|
<%= manager.email %>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<%= render "shared/table_actions", record: manager, actions: [:destroy] %>
|
<%= render Admin::TableActionsComponent.new(manager, actions: [:destroy]) %>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
<td><%= user.email %></td>
|
<td><%= user.email %></td>
|
||||||
<td>
|
<td>
|
||||||
<% if user.manager? && user.manager.persisted? %>
|
<% if user.manager? && user.manager.persisted? %>
|
||||||
<%= render "shared/table_actions", record: user.manager, actions: [:destroy] %>
|
<%= render Admin::TableActionsComponent.new(user.manager, actions: [:destroy]) %>
|
||||||
<% else %>
|
<% else %>
|
||||||
<%= link_to t("admin.managers.manager.add"),
|
<%= link_to t("admin.managers.manager.add"),
|
||||||
{ controller: "admin/managers",
|
{ controller: "admin/managers",
|
||||||
|
|||||||
@@ -23,7 +23,7 @@
|
|||||||
<%= status.description %>
|
<%= status.description %>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<%= render "shared/table_actions", record: status, actions: [:edit, :destroy] %>
|
<%= render Admin::TableActionsComponent.new(status) %>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|||||||
@@ -24,7 +24,7 @@
|
|||||||
<%= moderator.email %>
|
<%= moderator.email %>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<%= render "shared/table_actions", record: moderator, actions: [:destroy] %>
|
<%= render Admin::TableActionsComponent.new(moderator, actions: [:destroy]) %>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
<td><%= user.email %></td>
|
<td><%= user.email %></td>
|
||||||
<td>
|
<td>
|
||||||
<% if user.moderator? && user.moderator.persisted? %>
|
<% if user.moderator? && user.moderator.persisted? %>
|
||||||
<%= render "shared/table_actions", record: user.moderator, actions: [:destroy] %>
|
<%= render Admin::TableActionsComponent.new(user.moderator, actions: [:destroy]) %>
|
||||||
<% else %>
|
<% else %>
|
||||||
<%= link_to t("admin.moderators.moderator.add"),
|
<%= link_to t("admin.moderators.moderator.add"),
|
||||||
{ controller: "admin/moderators",
|
{ controller: "admin/moderators",
|
||||||
|
|||||||
@@ -29,7 +29,7 @@
|
|||||||
<% end %>
|
<% end %>
|
||||||
</td>
|
</td>
|
||||||
<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),
|
<%= link_to t("admin.newsletters.index.preview"), admin_newsletter_path(newsletter),
|
||||||
class: "button" %>
|
class: "button" %>
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
<td>
|
<td>
|
||||||
<%= link_to t("admin.polls.show.edit_answers"), admin_question_path(question),
|
<%= link_to t("admin.polls.show.edit_answers"), admin_question_path(question),
|
||||||
class: "button hollow" %>
|
class: "button hollow" %>
|
||||||
<%= render "shared/table_actions", record: question, actions: [:edit, :destroy] %>
|
<%= render Admin::TableActionsComponent.new(question) %>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|||||||
@@ -27,7 +27,7 @@
|
|||||||
<% end %>
|
<% end %>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<%= render "shared/table_actions", record: question, actions: [:edit, :destroy] %>
|
<%= render Admin::TableActionsComponent.new(question) %>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|||||||
@@ -29,7 +29,7 @@
|
|||||||
<td><%= video.title %></td>
|
<td><%= video.title %></td>
|
||||||
<td><%= link_to "#{video.url}", video.url %></td>
|
<td><%= link_to "#{video.url}", video.url %></td>
|
||||||
<td class="text-right">
|
<td class="text-right">
|
||||||
<%= render "shared/table_actions", record: video, actions: [:edit, :destroy] %>
|
<%= render Admin::TableActionsComponent.new(video) %>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|||||||
@@ -36,7 +36,7 @@
|
|||||||
<%= number_to_percentage(progress_bar.percentage, strip_insignificant_zeros: true) %>
|
<%= number_to_percentage(progress_bar.percentage, strip_insignificant_zeros: true) %>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<%= render "shared/table_actions", record: progress_bar, actions: [:edit, :destroy] %>
|
<%= render Admin::TableActionsComponent.new(progress_bar) %>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|||||||
@@ -6,6 +6,6 @@
|
|||||||
<%= group.valuators.count %>
|
<%= group.valuators.count %>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<%= render "shared/table_actions", record: group, actions: [:edit, :destroy] %>
|
<%= render Admin::TableActionsComponent.new(group) %>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|||||||
@@ -19,6 +19,6 @@
|
|||||||
<%= valuator_abilities(valuator) %>
|
<%= valuator_abilities(valuator) %>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<%= render "shared/table_actions", record: valuator, actions: [:edit, :destroy] %>
|
<%= render Admin::TableActionsComponent.new(valuator) %>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</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