From 10c095d821ad470584b248c09cfdec79fe454271 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Tue, 9 Jun 2020 00:47:41 +0200 Subject: [PATCH] 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. --- .../admin/table_actions_component.html.erb} | 0 .../admin/table_actions_component.rb | 8 +++++ .../admin/admin_notifications/index.html.erb | 2 +- app/views/admin/administrators/index.html.erb | 2 +- .../admin/administrators/search.html.erb | 2 +- app/views/admin/budget_groups/index.html.erb | 2 +- .../admin/budget_headings/index.html.erb | 2 +- .../admin/dashboard/actions/index.html.erb | 2 +- app/views/admin/geozones/index.html.erb | 2 +- app/views/admin/homepage/_card.html.erb | 2 +- .../legislation/processes/index.html.erb | 2 +- .../_local_census_record.html.erb | 2 +- app/views/admin/managers/index.html.erb | 2 +- app/views/admin/managers/search.html.erb | 2 +- .../admin/milestone_statuses/index.html.erb | 2 +- app/views/admin/moderators/index.html.erb | 2 +- app/views/admin/moderators/search.html.erb | 2 +- app/views/admin/newsletters/index.html.erb | 2 +- .../admin/poll/polls/_questions.html.erb | 2 +- .../admin/poll/questions/_questions.html.erb | 2 +- .../questions/answers/videos/index.html.erb | 2 +- .../progress_bars/_progress_bars.html.erb | 2 +- .../admin/valuator_groups/_group.html.erb | 2 +- .../admin/valuators/_valuator_row.html.erb | 2 +- .../admin/table_actions_component_spec.rb | 29 +++++++++++++++++++ 25 files changed, 59 insertions(+), 22 deletions(-) rename app/{views/shared/_table_actions.html.erb => components/admin/table_actions_component.html.erb} (100%) create mode 100644 app/components/admin/table_actions_component.rb create mode 100644 spec/components/admin/table_actions_component_spec.rb diff --git a/app/views/shared/_table_actions.html.erb b/app/components/admin/table_actions_component.html.erb similarity index 100% rename from app/views/shared/_table_actions.html.erb rename to app/components/admin/table_actions_component.html.erb diff --git a/app/components/admin/table_actions_component.rb b/app/components/admin/table_actions_component.rb new file mode 100644 index 000000000..c1a95dff7 --- /dev/null +++ b/app/components/admin/table_actions_component.rb @@ -0,0 +1,8 @@ +class Admin::TableActionsComponent < ApplicationComponent + attr_reader :record, :actions + + def initialize(record, actions: [:edit, :destroy]) + @record = record + @actions = actions + end +end diff --git a/app/views/admin/admin_notifications/index.html.erb b/app/views/admin/admin_notifications/index.html.erb index 14c88ab1c..48f132461 100644 --- a/app/views/admin/admin_notifications/index.html.erb +++ b/app/views/admin/admin_notifications/index.html.erb @@ -30,7 +30,7 @@ <% 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" %> diff --git a/app/views/admin/administrators/index.html.erb b/app/views/admin/administrators/index.html.erb index fa70f0bf0..4c5620b3a 100644 --- a/app/views/admin/administrators/index.html.erb +++ b/app/views/admin/administrators/index.html.erb @@ -29,7 +29,7 @@ <%= administrator.description %> - <%= render "shared/table_actions", record: administrator, actions: [:edit, :destroy] %> + <%= render Admin::TableActionsComponent.new(administrator) %> <% end %> diff --git a/app/views/admin/administrators/search.html.erb b/app/views/admin/administrators/search.html.erb index 07cd39cb3..671b6efb2 100644 --- a/app/views/admin/administrators/search.html.erb +++ b/app/views/admin/administrators/search.html.erb @@ -19,7 +19,7 @@ <%= user.email %> <% 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", diff --git a/app/views/admin/budget_groups/index.html.erb b/app/views/admin/budget_groups/index.html.erb index 915635975..3634e4bdc 100644 --- a/app/views/admin/budget_groups/index.html.erb +++ b/app/views/admin/budget_groups/index.html.erb @@ -29,7 +29,7 @@ <%= link_to t("admin.budget_groups.headings_manage"), admin_budget_group_headings_path(@budget, group) %> - <%= render "shared/table_actions", record: group, actions: [:edit, :destroy] %> + <%= render Admin::TableActionsComponent.new(group) %> <% end %> diff --git a/app/views/admin/budget_headings/index.html.erb b/app/views/admin/budget_headings/index.html.erb index bed3d5688..bfb878ec9 100644 --- a/app/views/admin/budget_headings/index.html.erb +++ b/app/views/admin/budget_headings/index.html.erb @@ -34,7 +34,7 @@ <%= heading.allow_custom_content ? t("admin.shared.true_value") : t("admin.shared.false_value") %> - <%= render "shared/table_actions", record: heading, actions: [:edit, :destroy] %> + <%= render Admin::TableActionsComponent.new(heading) %> <% end %> diff --git a/app/views/admin/dashboard/actions/index.html.erb b/app/views/admin/dashboard/actions/index.html.erb index 2bf3a91d9..c6aee28f1 100644 --- a/app/views/admin/dashboard/actions/index.html.erb +++ b/app/views/admin/dashboard/actions/index.html.erb @@ -32,7 +32,7 @@ <%= number_with_delimiter(action.required_supports, delimiter: ".") %> <%= action.order %> - <%= render "shared/table_actions", record: action, actions: [:edit, :destroy] %> + <%= render Admin::TableActionsComponent.new(action) %> <% end %> diff --git a/app/views/admin/geozones/index.html.erb b/app/views/admin/geozones/index.html.erb index 3db62760a..31bae3f73 100644 --- a/app/views/admin/geozones/index.html.erb +++ b/app/views/admin/geozones/index.html.erb @@ -22,7 +22,7 @@ <%= geozone.census_code %> <%= geozone.html_map_coordinates %> - <%= render "shared/table_actions", record: geozone, actions: [:edit, :destroy] %> + <%= render Admin::TableActionsComponent.new(geozone) %> <% end %> diff --git a/app/views/admin/homepage/_card.html.erb b/app/views/admin/homepage/_card.html.erb index 2b1c64453..94025a7b1 100644 --- a/app/views/admin/homepage/_card.html.erb +++ b/app/views/admin/homepage/_card.html.erb @@ -17,6 +17,6 @@ <% end %> - <%= render "shared/table_actions", record: card, actions: [:edit, :destroy] %> + <%= render Admin::TableActionsComponent.new(card) %> diff --git a/app/views/admin/legislation/processes/index.html.erb b/app/views/admin/legislation/processes/index.html.erb index d57e85994..ddef4a150 100644 --- a/app/views/admin/legislation/processes/index.html.erb +++ b/app/views/admin/legislation/processes/index.html.erb @@ -35,7 +35,7 @@ <%= I18n.l process.end_date %> <%= process.total_comments %> - <%= render "shared/table_actions", record: process, actions: [:destroy] %> + <%= render Admin::TableActionsComponent.new(process, actions: [:destroy]) %> <% end %> diff --git a/app/views/admin/local_census_records/_local_census_record.html.erb b/app/views/admin/local_census_records/_local_census_record.html.erb index 22beea26e..85a32e39b 100644 --- a/app/views/admin/local_census_records/_local_census_record.html.erb +++ b/app/views/admin/local_census_records/_local_census_record.html.erb @@ -12,6 +12,6 @@ <%= local_census_record.postal_code %> - <%= render "shared/table_actions", record: local_census_record, actions: [:edit, :destroy] %> + <%= render Admin::TableActionsComponent.new(local_census_record) %> diff --git a/app/views/admin/managers/index.html.erb b/app/views/admin/managers/index.html.erb index 2dc28ffcb..62ca1f189 100644 --- a/app/views/admin/managers/index.html.erb +++ b/app/views/admin/managers/index.html.erb @@ -22,7 +22,7 @@ <%= manager.email %> - <%= render "shared/table_actions", record: manager, actions: [:destroy] %> + <%= render Admin::TableActionsComponent.new(manager, actions: [:destroy]) %> <% end %> diff --git a/app/views/admin/managers/search.html.erb b/app/views/admin/managers/search.html.erb index a03d7b72d..a834a9d80 100644 --- a/app/views/admin/managers/search.html.erb +++ b/app/views/admin/managers/search.html.erb @@ -19,7 +19,7 @@ <%= user.email %> <% 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", diff --git a/app/views/admin/milestone_statuses/index.html.erb b/app/views/admin/milestone_statuses/index.html.erb index e7bf0c720..1d66b8703 100644 --- a/app/views/admin/milestone_statuses/index.html.erb +++ b/app/views/admin/milestone_statuses/index.html.erb @@ -23,7 +23,7 @@ <%= status.description %> - <%= render "shared/table_actions", record: status, actions: [:edit, :destroy] %> + <%= render Admin::TableActionsComponent.new(status) %> <% end %> diff --git a/app/views/admin/moderators/index.html.erb b/app/views/admin/moderators/index.html.erb index 0594fb2d3..1c60e1d6d 100644 --- a/app/views/admin/moderators/index.html.erb +++ b/app/views/admin/moderators/index.html.erb @@ -24,7 +24,7 @@ <%= moderator.email %> - <%= render "shared/table_actions", record: moderator, actions: [:destroy] %> + <%= render Admin::TableActionsComponent.new(moderator, actions: [:destroy]) %> <% end %> diff --git a/app/views/admin/moderators/search.html.erb b/app/views/admin/moderators/search.html.erb index f07ab647b..3cda8819e 100644 --- a/app/views/admin/moderators/search.html.erb +++ b/app/views/admin/moderators/search.html.erb @@ -19,7 +19,7 @@ <%= user.email %> <% 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", diff --git a/app/views/admin/newsletters/index.html.erb b/app/views/admin/newsletters/index.html.erb index a1ac52803..f9cf9b604 100644 --- a/app/views/admin/newsletters/index.html.erb +++ b/app/views/admin/newsletters/index.html.erb @@ -29,7 +29,7 @@ <% end %> - <%= 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" %> diff --git a/app/views/admin/poll/polls/_questions.html.erb b/app/views/admin/poll/polls/_questions.html.erb index 6aacb275e..59964d6e6 100644 --- a/app/views/admin/poll/polls/_questions.html.erb +++ b/app/views/admin/poll/polls/_questions.html.erb @@ -32,7 +32,7 @@ <%= 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) %> <% end %> diff --git a/app/views/admin/poll/questions/_questions.html.erb b/app/views/admin/poll/questions/_questions.html.erb index 0099b3c10..784d3e73b 100644 --- a/app/views/admin/poll/questions/_questions.html.erb +++ b/app/views/admin/poll/questions/_questions.html.erb @@ -27,7 +27,7 @@ <% end %> - <%= render "shared/table_actions", record: question, actions: [:edit, :destroy] %> + <%= render Admin::TableActionsComponent.new(question) %> <% end %> diff --git a/app/views/admin/poll/questions/answers/videos/index.html.erb b/app/views/admin/poll/questions/answers/videos/index.html.erb index d74762dda..1a7d41402 100644 --- a/app/views/admin/poll/questions/answers/videos/index.html.erb +++ b/app/views/admin/poll/questions/answers/videos/index.html.erb @@ -29,7 +29,7 @@ <%= video.title %> <%= link_to "#{video.url}", video.url %> - <%= render "shared/table_actions", record: video, actions: [:edit, :destroy] %> + <%= render Admin::TableActionsComponent.new(video) %> <% end %> diff --git a/app/views/admin/progress_bars/_progress_bars.html.erb b/app/views/admin/progress_bars/_progress_bars.html.erb index e0d87e3ee..3615342b9 100644 --- a/app/views/admin/progress_bars/_progress_bars.html.erb +++ b/app/views/admin/progress_bars/_progress_bars.html.erb @@ -36,7 +36,7 @@ <%= number_to_percentage(progress_bar.percentage, strip_insignificant_zeros: true) %> - <%= render "shared/table_actions", record: progress_bar, actions: [:edit, :destroy] %> + <%= render Admin::TableActionsComponent.new(progress_bar) %> <% end %> diff --git a/app/views/admin/valuator_groups/_group.html.erb b/app/views/admin/valuator_groups/_group.html.erb index ea67ffd76..4de3e2e03 100644 --- a/app/views/admin/valuator_groups/_group.html.erb +++ b/app/views/admin/valuator_groups/_group.html.erb @@ -6,6 +6,6 @@ <%= group.valuators.count %> - <%= render "shared/table_actions", record: group, actions: [:edit, :destroy] %> + <%= render Admin::TableActionsComponent.new(group) %> diff --git a/app/views/admin/valuators/_valuator_row.html.erb b/app/views/admin/valuators/_valuator_row.html.erb index 7b0ce40bf..a93d492d7 100644 --- a/app/views/admin/valuators/_valuator_row.html.erb +++ b/app/views/admin/valuators/_valuator_row.html.erb @@ -19,6 +19,6 @@ <%= valuator_abilities(valuator) %> - <%= render "shared/table_actions", record: valuator, actions: [:edit, :destroy] %> + <%= render Admin::TableActionsComponent.new(valuator) %> diff --git a/spec/components/admin/table_actions_component_spec.rb b/spec/components/admin/table_actions_component_spec.rb new file mode 100644 index 000000000..d4161b2d2 --- /dev/null +++ b/spec/components/admin/table_actions_component_spec.rb @@ -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