From d2f38fd276a79c0298634d935cdd413d2a5640d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sun, 27 Jun 2021 18:10:23 +0200 Subject: [PATCH] Reduce duplication in moderation sections We had five almost-identical views. Now we've removed most of the duplication, although the tables are still similar. We might refactor them in the future. --- .../shared/index_component.html.erb | 39 +++++++++++++++++++ .../moderation/shared/index_component.rb | 39 +++++++++++++++++++ .../budgets/investments/index.html.erb | 39 +------------------ app/views/moderation/comments/index.html.erb | 39 +------------------ app/views/moderation/debates/index.html.erb | 39 +------------------ .../proposal_notifications/index.html.erb | 39 +------------------ app/views/moderation/proposals/index.html.erb | 39 +------------------ 7 files changed, 83 insertions(+), 190 deletions(-) create mode 100644 app/components/moderation/shared/index_component.html.erb create mode 100644 app/components/moderation/shared/index_component.rb diff --git a/app/components/moderation/shared/index_component.html.erb b/app/components/moderation/shared/index_component.html.erb new file mode 100644 index 000000000..ca984eaa9 --- /dev/null +++ b/app/components/moderation/shared/index_component.html.erb @@ -0,0 +1,39 @@ +

<%= t("moderation.#{i18n_namespace}.index.title") %>

+ +<%= render "shared/filter_subnav", i18n_namespace: "moderation.#{i18n_namespace}.index" %> + +

<%= page_entries_info records %>

+
+ <%= t("moderation.#{i18n_namespace}.index.order") %> + <%= render "shared/order_selector", i18n_namespace: "moderation.#{i18n_namespace}.index" %> +
+ +<%= form_tag form_path, method: :put do %> +

+ <%= t("shared.check") %>: + <%= link_to t("shared.check_all"), "#", data: { check_all: field_name } %> + | + <%= link_to t("shared.check_none"), "#", data: { check_none: field_name } %> +

+ + <%= content %> + + <%= submit_tag t("moderation.#{i18n_namespace}.index.block_authors"), + name: "block_authors", + class: "button hollow alert", + data: { confirm: t("moderation.#{i18n_namespace}.index.confirm") } %> + +
+ <%= submit_tag t("moderation.#{i18n_namespace}.index.hide"), + name: "hide_#{table_name}", + class: "button hollow alert", + data: { confirm: t("moderation.#{i18n_namespace}.index.confirm") } %> + + <%= submit_tag t("moderation.#{i18n_namespace}.index.ignore_flags"), + name: "ignore_flags", + class: "button hollow", + data: { confirm: t("moderation.#{i18n_namespace}.index.confirm") } %> +
+ + <%= paginate records %> +<% end %> diff --git a/app/components/moderation/shared/index_component.rb b/app/components/moderation/shared/index_component.rb new file mode 100644 index 000000000..c70e4cd30 --- /dev/null +++ b/app/components/moderation/shared/index_component.rb @@ -0,0 +1,39 @@ +class Moderation::Shared::IndexComponent < ApplicationComponent + attr_reader :records + + def initialize(records) + @records = records + end + + private + + def i18n_namespace + table_name + end + + def field_name + "#{records.model.model_name.singular}_ids[]" + end + + def form_path + url_for( + request.query_parameters.merge( + controller: "moderation/#{section_name}", + action: "moderate", + only_path: true + ) + ) + end + + def table_name + records.model.table_name + end + + def section_name + if table_name == "budget_investments" + "budgets/investments" + else + table_name + end + end +end diff --git a/app/views/moderation/budgets/investments/index.html.erb b/app/views/moderation/budgets/investments/index.html.erb index a67eec50d..a587c4e52 100644 --- a/app/views/moderation/budgets/investments/index.html.erb +++ b/app/views/moderation/budgets/investments/index.html.erb @@ -1,21 +1,4 @@ -

<%= t("moderation.budget_investments.index.title") %>

- -<%= render "shared/filter_subnav", i18n_namespace: "moderation.budget_investments.index" %> - -

<%= page_entries_info @budget_investments %>

-
- <%= t("moderation.budget_investments.index.order") %> - <%= render "shared/order_selector", i18n_namespace: "moderation.budget_investments.index" %> -
- -<%= form_tag moderate_moderation_budget_investments_path(request.query_parameters), method: :put do %> -

- <%= t("shared.check") %>: - <%= link_to t("shared.check_all"), "#", data: { check_all: "budget_investment_ids[]" } %> - | - <%= link_to t("shared.check_none"), "#", data: { check_none: "budget_investment_ids[]" } %> -

- +<%= render Moderation::Shared::IndexComponent.new(@budget_investments) do %> @@ -50,24 +33,4 @@ <% end %>
- - <%= submit_tag t("moderation.budget_investments.index.block_authors"), - name: "block_authors", - class: "button hollow alert", - data: { confirm: t("moderation.budget_investments.index.confirm") } %> - -
- <%= submit_tag t("moderation.budget_investments.index.hide"), - name: "hide_budget_investments", - class: "button hollow alert", - data: { confirm: t("moderation.budget_investments.index.confirm") } %> - - <%= submit_tag t("moderation.budget_investments.index.ignore_flags"), - name: "ignore_flags", - class: "button hollow", - data: { confirm: t("moderation.budget_investments.index.confirm") } %> -
- - <%= paginate @budget_investments %> - <% end %> diff --git a/app/views/moderation/comments/index.html.erb b/app/views/moderation/comments/index.html.erb index 5dd2f7562..03cd66679 100644 --- a/app/views/moderation/comments/index.html.erb +++ b/app/views/moderation/comments/index.html.erb @@ -1,21 +1,4 @@ -

<%= t("moderation.comments.index.title") %>

- -<%= render "shared/filter_subnav", i18n_namespace: "moderation.comments.index" %> - -

<%= page_entries_info @comments %>

-
- <%= t("moderation.comments.index.order") %> - <%= render "shared/order_selector", i18n_namespace: "moderation.comments.index" %> -
- -<%= form_tag moderate_moderation_comments_path(request.query_parameters), method: :put do %> -

- <%= t("shared.check") %>: - <%= link_to t("shared.check_all"), "#", data: { check_all: "comment_ids[]" } %> - | - <%= link_to t("shared.check_none"), "#", data: { check_none: "comment_ids[]" } %> -

- +<%= render Moderation::Shared::IndexComponent.new(@comments) do %> @@ -48,24 +31,4 @@ <% end %>
- - <%= submit_tag t("moderation.comments.index.block_authors"), - name: "block_authors", - class: "button hollow alert", - data: { confirm: t("moderation.comments.index.confirm") } %> - -
- <%= submit_tag t("moderation.comments.index.hide"), - name: "hide_comments", - class: "button hollow alert", - data: { confirm: t("moderation.comments.index.confirm") } %> - - <%= submit_tag t("moderation.comments.index.ignore_flags"), - name: "ignore_flags", - class: "button hollow", - data: { confirm: t("moderation.comments.index.confirm") } %> -
- - <%= paginate @comments %> - <% end %> diff --git a/app/views/moderation/debates/index.html.erb b/app/views/moderation/debates/index.html.erb index 60d9f2d18..fef01f71a 100644 --- a/app/views/moderation/debates/index.html.erb +++ b/app/views/moderation/debates/index.html.erb @@ -1,21 +1,4 @@ -

<%= t("moderation.debates.index.title") %>

- -<%= render "shared/filter_subnav", i18n_namespace: "moderation.debates.index" %> - -

<%= page_entries_info @debates %>

-
- <%= t("moderation.debates.index.order") %> - <%= render "shared/order_selector", i18n_namespace: "moderation.debates.index" %> -
- -<%= form_tag moderate_moderation_debates_path(request.query_parameters), method: :put do %> -

- <%= t("shared.check") %>: - <%= link_to t("shared.check_all"), "#", data: { check_all: "debate_ids[]" } %> - | - <%= link_to t("shared.check_none"), "#", data: { check_none: "debate_ids[]" } %> -

- +<%= render Moderation::Shared::IndexComponent.new(@debates) do %> @@ -47,24 +30,4 @@ <% end %>
- - <%= submit_tag t("moderation.debates.index.block_authors"), - name: "block_authors", - class: "button hollow alert", - data: { confirm: t("moderation.debates.index.confirm") } %> - -
- <%= submit_tag t("moderation.debates.index.hide"), - name: "hide_debates", - class: "button hollow alert", - data: { confirm: t("moderation.debates.index.confirm") } %> - - <%= submit_tag t("moderation.debates.index.ignore_flags"), - name: "ignore_flags", - class: "button hollow", - data: { confirm: t("moderation.debates.index.confirm") } %> -
- - <%= paginate @debates %> - <% end %> diff --git a/app/views/moderation/proposal_notifications/index.html.erb b/app/views/moderation/proposal_notifications/index.html.erb index 4becd470f..5cd563776 100644 --- a/app/views/moderation/proposal_notifications/index.html.erb +++ b/app/views/moderation/proposal_notifications/index.html.erb @@ -1,21 +1,4 @@ -

<%= t("moderation.proposal_notifications.index.title") %>

- -<%= render "shared/filter_subnav", i18n_namespace: "moderation.proposal_notifications.index" %> - -

<%= page_entries_info @proposal_notifications %>

-
- <%= t("moderation.proposal_notifications.index.order") %> - <%= render "shared/order_selector", i18n_namespace: "moderation.proposal_notifications.index" %> -
- -<%= form_tag moderate_moderation_proposal_notifications_path(request.query_parameters), method: :put do %> -

- <%= t("shared.check") %>: - <%= link_to t("shared.check_all"), "#", data: { check_all: "proposal_notification_ids[]" } %> - | - <%= link_to t("shared.check_none"), "#", data: { check_none: "proposal_notification_ids[]" } %> -

- +<%= render Moderation::Shared::IndexComponent.new(@proposal_notifications) do %> @@ -46,24 +29,4 @@ <% end %>
- - <%= submit_tag t("moderation.proposal_notifications.index.block_authors"), - name: "block_authors", - class: "button hollow alert", - data: { confirm: t("moderation.proposal_notifications.index.confirm") } %> - -
- <%= submit_tag t("moderation.proposal_notifications.index.hide"), - name: "hide_proposal_notifications", - class: "button hollow alert", - data: { confirm: t("moderation.proposal_notifications.index.confirm") } %> - - <%= submit_tag t("moderation.proposal_notifications.index.ignore_flags"), - name: "ignore_flags", - class: "button hollow", - data: { confirm: t("moderation.proposal_notifications.index.confirm") } %> -
- - <%= paginate @proposal_notifications %> - <% end %> diff --git a/app/views/moderation/proposals/index.html.erb b/app/views/moderation/proposals/index.html.erb index bf97fcd0c..e715a0b91 100644 --- a/app/views/moderation/proposals/index.html.erb +++ b/app/views/moderation/proposals/index.html.erb @@ -1,21 +1,4 @@ -

<%= t("moderation.proposals.index.title") %>

- -<%= render "shared/filter_subnav", i18n_namespace: "moderation.proposals.index" %> - -

<%= page_entries_info @proposals %>

-
- <%= t("moderation.proposals.index.order") %> - <%= render "shared/order_selector", i18n_namespace: "moderation.proposals.index" %> -
- -<%= form_tag moderate_moderation_proposals_path(request.query_parameters), method: :put do %> -

- <%= t("shared.check") %>: - <%= link_to t("shared.check_all"), "#", data: { check_all: "proposal_ids[]" } %> - | - <%= link_to t("shared.check_none"), "#", data: { check_none: "proposal_ids[]" } %> -

- +<%= render Moderation::Shared::IndexComponent.new(@proposals) do %> @@ -47,24 +30,4 @@ <% end %>
- - <%= submit_tag t("moderation.proposals.index.block_authors"), - name: "block_authors", - class: "button hollow alert", - data: { confirm: t("moderation.proposals.index.confirm") } %> - -
- <%= submit_tag t("moderation.proposals.index.hide"), - name: "hide_proposals", - class: "button hollow alert", - data: { confirm: t("moderation.proposals.index.confirm") } %> - - <%= submit_tag t("moderation.proposals.index.ignore_flags"), - name: "ignore_flags", - class: "button hollow", - data: { confirm: t("moderation.proposals.index.confirm") } %> -
- - <%= paginate @proposals %> - <% end %>