From eb3f2bc2ca0fdec8abff5f9477fcca3e0e8fcd70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sun, 14 Jun 2020 18:32:21 +0200 Subject: [PATCH] Extract component for restore and hide actions By doing so, we remove a lot of duplication. --- .../hidden_table_actions_component.html.erb | 12 +++++++ .../admin/hidden_table_actions_component.rb | 34 +++++++++++++++++++ .../hidden_budget_investments/index.html.erb | 12 +------ .../admin/hidden_comments/index.html.erb | 12 +------ app/views/admin/hidden_debates/index.html.erb | 12 +------ .../index.html.erb | 12 +------ .../admin/hidden_proposals/index.html.erb | 12 +------ app/views/admin/hidden_users/index.html.erb | 12 +------ .../hidden_table_actions_component_spec.rb | 14 ++++++++ 9 files changed, 66 insertions(+), 66 deletions(-) create mode 100644 app/components/admin/hidden_table_actions_component.html.erb create mode 100644 app/components/admin/hidden_table_actions_component.rb create mode 100644 spec/components/admin/hidden_table_actions_component_spec.rb diff --git a/app/components/admin/hidden_table_actions_component.html.erb b/app/components/admin/hidden_table_actions_component.html.erb new file mode 100644 index 000000000..7cf58e888 --- /dev/null +++ b/app/components/admin/hidden_table_actions_component.html.erb @@ -0,0 +1,12 @@ +<%= render Admin::TableActionsComponent.new(actions: []) do %> + <%= link_to restore_text, restore_path, + method: :put, + data: { confirm: t("admin.actions.confirm") }, + class: "button hollow warning" %> + + <% unless record.confirmed_hide? %> + <%= link_to confirm_hide_text, confirm_hide_path, + method: :put, + class: "button" %> + <% end %> +<% end %> diff --git a/app/components/admin/hidden_table_actions_component.rb b/app/components/admin/hidden_table_actions_component.rb new file mode 100644 index 000000000..68647d130 --- /dev/null +++ b/app/components/admin/hidden_table_actions_component.rb @@ -0,0 +1,34 @@ +class Admin::HiddenTableActionsComponent < ApplicationComponent + attr_reader :record + + def initialize(record) + @record = record + end + + private + + def restore_text + t("admin.actions.restore") + end + + def restore_path + action_path(:restore) + end + + def confirm_hide_text + t("admin.actions.confirm_hide") + end + + def confirm_hide_path + action_path(:confirm_hide) + end + + def action_path(action) + url_for({ + controller: "admin/hidden_#{record.model_name.plural}", + action: action, + id: record, + only_path: true + }.merge(request.query_parameters)) + end +end diff --git a/app/views/admin/hidden_budget_investments/index.html.erb b/app/views/admin/hidden_budget_investments/index.html.erb index ed89d5cdc..90213a5ed 100644 --- a/app/views/admin/hidden_budget_investments/index.html.erb +++ b/app/views/admin/hidden_budget_investments/index.html.erb @@ -24,17 +24,7 @@ - <%= link_to t("admin.actions.restore"), - restore_admin_hidden_budget_investment_path(investment, request.query_parameters), - method: :put, - data: { confirm: t("admin.actions.confirm") }, - class: "button hollow warning" %> - <% unless investment.confirmed_hide? %> - <%= link_to t("admin.actions.confirm_hide"), - confirm_hide_admin_hidden_budget_investment_path(investment, request.query_parameters), - method: :put, - class: "button" %> - <% end %> + <%= render Admin::HiddenTableActionsComponent.new(investment) %> <% end %> diff --git a/app/views/admin/hidden_comments/index.html.erb b/app/views/admin/hidden_comments/index.html.erb index 0744661b0..d52f4f68a 100644 --- a/app/views/admin/hidden_comments/index.html.erb +++ b/app/views/admin/hidden_comments/index.html.erb @@ -23,17 +23,7 @@ <% end %> - <%= link_to t("admin.actions.restore"), - restore_admin_hidden_comment_path(comment, request.query_parameters), - method: :put, - data: { confirm: t("admin.actions.confirm") }, - class: "button hollow warning" %> - <% unless comment.confirmed_hide? %> - <%= link_to t("admin.actions.confirm_hide"), - confirm_hide_admin_hidden_comment_path(comment, request.query_parameters), - method: :put, - class: "button" %> - <% end %> + <%= render Admin::HiddenTableActionsComponent.new(comment) %> <% end %> diff --git a/app/views/admin/hidden_debates/index.html.erb b/app/views/admin/hidden_debates/index.html.erb index 27473db52..954ecf854 100644 --- a/app/views/admin/hidden_debates/index.html.erb +++ b/app/views/admin/hidden_debates/index.html.erb @@ -24,17 +24,7 @@ - <%= link_to t("admin.actions.restore"), - restore_admin_hidden_debate_path(debate, request.query_parameters), - method: :put, - data: { confirm: t("admin.actions.confirm") }, - class: "button hollow warning" %> - <% unless debate.confirmed_hide? %> - <%= link_to t("admin.actions.confirm_hide"), - confirm_hide_admin_hidden_debate_path(debate, request.query_parameters), - method: :put, - class: "button" %> - <% end %> + <%= render Admin::HiddenTableActionsComponent.new(debate) %> <% end %> diff --git a/app/views/admin/hidden_proposal_notifications/index.html.erb b/app/views/admin/hidden_proposal_notifications/index.html.erb index 61522cf88..5b17b7dce 100644 --- a/app/views/admin/hidden_proposal_notifications/index.html.erb +++ b/app/views/admin/hidden_proposal_notifications/index.html.erb @@ -24,17 +24,7 @@ - <%= link_to t("admin.actions.restore"), - restore_admin_hidden_proposal_notification_path(proposal_notification, request.query_parameters), - method: :put, - data: { confirm: t("admin.actions.confirm") }, - class: "button hollow warning" %> - <% unless proposal_notification.confirmed_hide? %> - <%= link_to t("admin.actions.confirm_hide"), - confirm_hide_admin_hidden_proposal_notification_path(proposal_notification, request.query_parameters), - method: :put, - class: "button" %> - <% end %> + <%= render Admin::HiddenTableActionsComponent.new(proposal_notification) %> <% end %> diff --git a/app/views/admin/hidden_proposals/index.html.erb b/app/views/admin/hidden_proposals/index.html.erb index bf8a2ff6a..b9502d7bc 100644 --- a/app/views/admin/hidden_proposals/index.html.erb +++ b/app/views/admin/hidden_proposals/index.html.erb @@ -28,17 +28,7 @@ - <%= link_to t("admin.actions.restore"), - restore_admin_hidden_proposal_path(proposal, request.query_parameters), - method: :put, - data: { confirm: t("admin.actions.confirm") }, - class: "button hollow warning" %> - <% unless proposal.confirmed_hide? %> - <%= link_to t("admin.actions.confirm_hide"), - confirm_hide_admin_hidden_proposal_path(proposal, request.query_parameters), - method: :put, - class: "button" %> - <% end %> + <%= render Admin::HiddenTableActionsComponent.new(proposal) %> <% end %> diff --git a/app/views/admin/hidden_users/index.html.erb b/app/views/admin/hidden_users/index.html.erb index d02a42e0b..bb2387e9f 100644 --- a/app/views/admin/hidden_users/index.html.erb +++ b/app/views/admin/hidden_users/index.html.erb @@ -19,17 +19,7 @@ - <%= link_to t("admin.actions.restore"), - restore_admin_hidden_user_path(user, request.query_parameters), - method: :put, - data: { confirm: t("admin.actions.confirm") }, - class: "button hollow warning" %> - <% unless user.confirmed_hide? %> - <%= link_to t("admin.actions.confirm_hide"), - confirm_hide_admin_hidden_user_path(user, request.query_parameters), - method: :put, - class: "button" %> - <% end %> + <%= render Admin::HiddenTableActionsComponent.new(user) %> <% end %> diff --git a/spec/components/admin/hidden_table_actions_component_spec.rb b/spec/components/admin/hidden_table_actions_component_spec.rb new file mode 100644 index 000000000..f439cf06c --- /dev/null +++ b/spec/components/admin/hidden_table_actions_component_spec.rb @@ -0,0 +1,14 @@ +require "rails_helper" + +describe Admin::HiddenTableActionsComponent, type: :component do + let(:record) { create(:user) } + let(:component) { Admin::HiddenTableActionsComponent.new(record) } + + it "renders links to restore and confirm hide" do + render_inline component + + expect(page).to have_css "a", count: 2 + expect(page).to have_css "a[href*='restore'][data-method='put']", text: "Restore" + expect(page).to have_css "a[href*='confirm_hide'][data-method='put']", text: "Confirm moderation" + end +end