Remove duplication in search results summary

We were using very similar code for proposals, debates and investments,
so we might as well share the code between them.

Note we're using the `proposals.index.search_results` key even for
debates and investments. This will still work because the translations
shared the same text, but IMHO we should rename the key to something
like `shared.search_results_summary`. We aren't doing so because we'd
lose all the existing translations.
This commit is contained in:
Javi Martín
2022-04-09 02:04:18 +02:00
parent de35e93611
commit 5c0aa42351
12 changed files with 62 additions and 74 deletions

View File

@@ -34,6 +34,7 @@
@import "moderation_actions";
@import "notification_item";
@import "community";
@import "search_results_summary";
@import "stats";
@import "sticky_overrides";
@import "tags";

View File

@@ -866,15 +866,6 @@
}
}
.search-results-summary {
@include full-width-background;
background: $highlight;
margin-bottom: $line-height;
margin-top: -$line-height;
padding-bottom: $line-height;
padding-top: $line-height;
}
// 05. Featured
// ------------

View File

@@ -0,0 +1,8 @@
.search-results-summary {
@include full-width-background;
background: $highlight;
margin-bottom: $line-height;
margin-top: -$line-height;
padding-bottom: $line-height;
padding-top: $line-height;
}

View File

@@ -0,0 +1,17 @@
<div class="search-results-summary">
<div class="row">
<div class="small-12 column">
<% if search_terms || advanced_search_terms %>
<h2><%= t("shared.search_results") %></h2>
<p>
<%= page_entries_info results %>
<% if advanced_search_terms.blank? %>
<%= summary %>
<% end %>
</p>
<% else %>
<%= content %>
<% end %>
</div>
</div>
</div>

View File

@@ -0,0 +1,15 @@
class Shared::SearchResultsSummaryComponent < ApplicationComponent
attr_reader :results, :search_terms, :advanced_search_terms
def initialize(results:, search_terms:, advanced_search_terms:)
@results = results
@search_terms = search_terms
@advanced_search_terms = advanced_search_terms
end
private
def summary
sanitize(t("proposals.index.search_results", count: results.size, search_term: search_terms))
end
end

View File

@@ -22,21 +22,11 @@
<% end %>
<% if params[:search].present? || params[:advanced_search].present? %>
<div class="search-results-summary">
<div class="row">
<div class="small-12 column">
<h2><%= t("shared.search_results") %></h2>
<p>
<%= page_entries_info @investments %>
<% if params[:advanced_search].blank? %>
<%= sanitize(t("budgets.investments.index.search_results",
count: @investments.size,
search_term: params[:search])) %>
<% end %>
</p>
</div>
</div>
</div>
<%= render Shared::SearchResultsSummaryComponent.new(
results: @investments,
search_terms: params[:search],
advanced_search_terms: params[:advanced_search]
) %>
<% end %>
<div class="row">

View File

@@ -10,23 +10,11 @@
<main>
<% if @search_terms || @advanced_search_terms %>
<div class="search-results-summary">
<div class="row">
<div class="small-12 column">
<% if @search_terms || @advanced_search_terms %>
<h2><%= t("shared.search_results") %></h2>
<p>
<%= page_entries_info @debates %>
<% if !@advanced_search_terms %>
<%= sanitize(
t("debates.index.search_results", count: @debates.size, search_term: @search_terms)
) %>
<% end %>
</p>
<% end %>
</div>
</div>
</div>
<%= render Shared::SearchResultsSummaryComponent.new(
results: @debates,
search_terms: @search_terms,
advanced_search_terms: @advanced_search_terms
) %>
<% else %>
<%= render "shared/section_header", i18n_namespace: "debates.index.section_header", image: "debates" %>
<% end %>

View File

@@ -15,27 +15,17 @@
params[:retired].present?,
params[:selected].present?
].any? %>
<div class="search-results-summary">
<div class="row">
<div class="small-12 column">
<% if @search_terms || @advanced_search_terms %>
<h2><%= t("shared.search_results") %></h2>
<p>
<%= page_entries_info @proposals %>
<% if !@advanced_search_terms %>
<%= sanitize(
t("proposals.index.search_results", count: @proposals.size, search_term: @search_terms)
) %>
<% end %>
</p>
<% elsif params[:retired].present? %>
<h2><%= t("proposals.index.retired_proposals") %></h2>
<% elsif params[:selected].present? %>
<h2><%= t("proposals.index.selected_proposals") %></h2>
<% end %>
</div>
</div>
</div>
<%= render Shared::SearchResultsSummaryComponent.new(
results: @proposals,
search_terms: @search_terms,
advanced_search_terms: @advanced_search_terms
) do %>
<% if params[:retired].present? %>
<h2><%= t("proposals.index.retired_proposals") %></h2>
<% elsif params[:selected].present? %>
<h2><%= t("proposals.index.selected_proposals") %></h2>
<% end %>
<% end %>
<% else %>
<%= render "shared/section_header", i18n_namespace: "proposals.index.section_header", image: "proposals" %>
<% end %>

View File

@@ -92,9 +92,6 @@ en:
button: Search
placeholder: Search investment projects...
title: Search
search_results:
one: " containing the term <strong>'%{search_term}'</strong>"
other: " containing the term <strong>'%{search_term}'</strong>"
sidebar:
my_ballot: My ballot
voted_info:

View File

@@ -110,9 +110,6 @@ en:
button: Search
placeholder: Search debates...
title: Search
search_results:
one: " containing the term <strong>'%{search_term}'</strong>"
other: " containing the term <strong>'%{search_term}'</strong>"
start_debate: Start a debate
title: Debates
section_header:

View File

@@ -92,9 +92,6 @@ es:
button: Buscar
placeholder: Buscar proyectos de gasto...
title: Buscar
search_results:
one: " que contiene <strong>'%{search_term}'</strong>"
other: " que contienen <strong>'%{search_term}'</strong>"
sidebar:
my_ballot: Mis votos
voted_info:

View File

@@ -110,9 +110,6 @@ es:
button: Buscar
placeholder: Buscar debates...
title: Buscar
search_results:
one: " que contiene <strong>'%{search_term}'</strong>"
other: " que contienen <strong>'%{search_term}'</strong>"
start_debate: Empieza un debate
title: Debates
section_header: