Merge pull request #4300 from consul/sdg_filters

Add SDG filters to advanced search
This commit is contained in:
Javi Martín
2021-01-11 13:11:09 +01:00
committed by GitHub
18 changed files with 245 additions and 126 deletions

View File

@@ -0,0 +1,50 @@
.advanced-search {
float: left;
margin: $line-height 0;
position: inherit;
@include breakpoint(medium) {
float: right;
margin-bottom: 0;
margin-top: $line-height / 4;
position: absolute;
right: 0;
}
}
.advanced-search-form {
@include breakpoint(large) {
.filter {
@include grid-column;
width: 25%;
}
.date-filters {
float: left;
width: 50%;
.filter {
width: 50%;
}
.custom-date-filters {
clear: both;
}
}
.submit {
width: 25%;
}
}
.general-search,
.filter,
.submit {
@include grid-column-gutter;
}
select {
height: $line-height * 2;
}
}

View File

@@ -6,6 +6,7 @@
@import "icons";
@import "mixins";
@import "admin";
@import "advanced_search";
@import "layout";
@import "participation";
@import "milestones";

View File

@@ -8,7 +8,6 @@
// 06. Forms
// 07. Callout
// 08. User account
// 09. Search
// 10. Official levels
// 11. Tables
// 12. Social
@@ -1480,40 +1479,6 @@ form {
}
}
// 09. Search
// ----------
.advanced-search {
float: left;
margin: $line-height 0;
position: inherit;
@include breakpoint(medium) {
float: right;
margin-bottom: 0;
margin-top: $line-height / 4;
position: absolute;
right: 0;
}
}
.advanced-search-form {
@include breakpoint(medium) {
> .column {
padding: 0;
}
}
select {
height: $line-height * 2;
}
.column.end.clear {
clear: both;
}
}
// 10. Officials levels
// --------------------

View File

@@ -0,0 +1,7 @@
module SDG::Goals::OptionsForSelect
extend ActiveSupport::Concern
def goal_options(selected_code = nil)
options_from_collection_for_select(SDG::Goal.order(:code), :code, :code_and_title, selected_code)
end
end

View File

@@ -1,5 +1,6 @@
class SDGManagement::Relations::IndexComponent < ApplicationComponent
include Header
include SDG::Goals::OptionsForSelect
attr_reader :records
@@ -47,7 +48,7 @@ class SDGManagement::Relations::IndexComponent < ApplicationComponent
end
def goal_options
options_from_collection_for_select(SDG::Goal.all, :code, :code_and_title, params[:goal_code])
super(params[:goal_code])
end
def target_options

View File

@@ -0,0 +1,65 @@
<div class="relative">
<%= link_to t("shared.advanced_search.title"), "#advanced_search_form", id: "js-advanced-search-title", class: "advanced-search small" %>
</div>
<div class="row advanced-search-form">
<%= form_tag request.path, id: "advanced_search_form", method: :get do %>
<div id="js-advanced-search" data-advanced-search-terms="<%= advanced_search.present? %>" style="display: none">
<div class="general-search">
<label for="search">
<%= t("shared.advanced_search.general") %>
</label>
<%= text_field_tag "search", params[:search],
placeholder: t("shared.advanced_search.general_placeholder") %>
</div>
<div class="filter">
<label for="advanced_search_official_level"><%= t("shared.advanced_search.author_type") %></label>
<%= select_tag("advanced_search[official_level]", official_level_search_options,
include_blank: t("shared.advanced_search.author_type_blank")) %>
</div>
<% if sdg? %>
<div class="filter">
<label for="advanced_search_goal"><%= t("shared.advanced_search.goal") %></label>
<%= select_tag("advanced_search[goal]", goal_options,
include_blank: t("shared.advanced_search.goal_blank")) %>
</div>
<% end %>
<div class="date-filters">
<div class="filter">
<label for="js-advanced-search-date-min"><%= t("shared.advanced_search.date") %></label>
<%= select_tag("advanced_search[date_min]", date_range_options,
include_blank: t("shared.advanced_search.date_range_blank"),
id: "js-advanced-search-date-min") %>
</div>
<div id="js-custom-date" class="custom-date-filters" style="display: none">
<div class="filter">
<label for="advanced_search_date_min">
<%= t("shared.advanced_search.from") %> (<%= t("shared.advanced_search.date_placeholder") %>)
</label>
<%= text_field_tag "advanced_search[date_min]",
advanced_search[:date_min],
class: "js-calendar" %>
</div>
<div class="filter">
<label for="advanced_search_date_max">
<%= t("shared.advanced_search.to") %> (<%= t("shared.advanced_search.date_placeholder") %>)
</label>
<%= text_field_tag "advanced_search[date_max]",
advanced_search[:date_max],
class: "js-calendar" %>
</div>
</div>
</div>
<div class="submit">
<%= submit_tag t("shared.advanced_search.search"), class: "button expanded" %>
</div>
</div>
<% end %>
</div>

View File

@@ -0,0 +1,40 @@
class Shared::AdvancedSearchComponent < ApplicationComponent
include SDG::Goals::OptionsForSelect
private
def advanced_search
params[:advanced_search] || {}
end
def official_level_search_options
options_for_select((1..5).map { |i| [setting["official_level_#{i}_name"], i] },
advanced_search[:official_level])
end
def date_range_options
options_for_select([
[t("shared.advanced_search.date_1"), 1],
[t("shared.advanced_search.date_2"), 2],
[t("shared.advanced_search.date_3"), 3],
[t("shared.advanced_search.date_4"), 4],
[t("shared.advanced_search.date_5"), "custom"]],
selected_date_range)
end
def selected_date_range
custom_date_range? ? "custom" : advanced_search[:date_min]
end
def custom_date_range?
advanced_search[:date_max].present?
end
def goal_options
super(advanced_search[:goal])
end
def sdg?
SDG::ProcessEnabled.new(controller_path).enabled?
end
end

View File

@@ -1,24 +0,0 @@
module SearchHelper
def official_level_search_options
options_for_select((1..5).map { |i| [setting["official_level_#{i}_name"], i] },
params[:advanced_search].try(:[], :official_level))
end
def date_range_options
options_for_select([
[t("shared.advanced_search.date_1"), 1],
[t("shared.advanced_search.date_2"), 2],
[t("shared.advanced_search.date_3"), 3],
[t("shared.advanced_search.date_4"), 4],
[t("shared.advanced_search.date_5"), "custom"]],
selected_date_range)
end
def selected_date_range
custom_date_range? ? "custom" : params[:advanced_search].try(:[], :date_min)
end
def custom_date_range?
params[:advanced_search].try(:[], :date_max).present?
end
end

View File

@@ -21,13 +21,21 @@ class SDG::ProcessEnabled
private
def process_name
if module_name == "Legislation"
"legislation"
if controller_path_name?
name.split("/").first
else
module_name.constantize.table_name
if module_name == "Legislation"
"legislation"
else
module_name.constantize.table_name
end
end
end
def controller_path_name?
name == name.downcase
end
def module_name
name.split("::").first
end

View File

@@ -55,7 +55,7 @@
</div>
<% end %>
<%= render("shared/advanced_search", search_path: budget_investments_path(@budget)) %>
<%= render Shared::AdvancedSearchComponent.new %>
<% if unfeasible_or_unselected_filter %>
<ul class="no-bullet submenu">

View File

@@ -52,7 +52,7 @@
</div>
</div>
<%= render "shared/advanced_search", search_path: debates_path(page: 1) %>
<%= render Shared::AdvancedSearchComponent.new %>
<%= render "shared/order_links", i18n_namespace: "debates.index" %>

View File

@@ -73,8 +73,7 @@
<% end %>
<% unless params[:retired].present? || params[:selected].present? %>
<%= render "shared/advanced_search",
search_path: proposals_path(page: 1) %>
<%= render Shared::AdvancedSearchComponent.new %>
<% end %>
<% unless params[:selected].present? %>

View File

@@ -1,59 +0,0 @@
<div class="relative">
<%= link_to t("shared.advanced_search.title"), "#advanced_search_form", id: "js-advanced-search-title", class: "advanced-search small" %>
</div>
<div class="row advanced-search-form">
<%= form_tag search_path, id: "advanced_search_form", method: :get do %>
<div id="js-advanced-search" data-advanced-search-terms="<%= @advanced_search_terms.present? %>" style="display: none">
<div class="small-12 column">
<label for="search">
<%= t("shared.advanced_search.general") %>
</label>
<%= text_field_tag "search", params[:search],
placeholder: t("shared.advanced_search.general_placeholder") %>
</div>
<div class="small-12 large-3 column">
<label for="advanced_search_official_level"><%= t("shared.advanced_search.author_type") %></label>
<%= select_tag("advanced_search[official_level]", official_level_search_options,
include_blank: t("shared.advanced_search.author_type_blank")) %>
</div>
<div class="small-12 large-9">
<div class="small-12 large-4 column">
<label for="js-advanced-search-date-min"><%= t("shared.advanced_search.date") %></label>
<%= select_tag("advanced_search[date_min]", date_range_options,
include_blank: t("shared.advanced_search.date_range_blank"),
id: "js-advanced-search-date-min") %>
</div>
<div id="js-custom-date" class="small-12 large-8 column" style="display: none">
<div class="row">
<div class="small-12 large-6 column">
<label for="advanced_search_date_min">
<%= t("shared.advanced_search.from") %> (<%= t("shared.advanced_search.date_placeholder") %>)
</label>
<%= text_field_tag "advanced_search[date_min]",
params[:advanced_search].try(:[], :date_min),
class: "js-calendar" %>
</div>
<div class="small-12 large-6 column">
<label for="advanced_search_date_max">
<%= t("shared.advanced_search.to") %> (<%= t("shared.advanced_search.date_placeholder") %>)
</label>
<%= text_field_tag "advanced_search[date_max]",
params[:advanced_search].try(:[], :date_max),
class: "js-calendar" %>
</div>
</div>
</div>
</div>
<div class="small-12 medium-3 column end clear">
<%= submit_tag t("shared.advanced_search.search"), class: "button expanded" %>
</div>
</div>
<% end %>
</div>

View File

@@ -694,6 +694,8 @@ en:
from: "From"
general: "With the text"
general_placeholder: "Write the text"
goal: "By SDG"
goal_blank: "Select a goal"
search: "Filter"
title: "Advanced search"
to: "To"

View File

@@ -694,6 +694,8 @@ es:
from: "Desde"
general: "Con el texto"
general_placeholder: "Escribe el texto"
goal: "Por ODS"
goal_blank: "Elige un objetivo"
search: "Filtrar"
title: "Búsqueda avanzada"
to: "Hasta"

View File

@@ -0,0 +1,16 @@
require "rails_helper"
describe SDGManagement::Relations::IndexComponent, type: :component do
describe "#goal_options" do
it "orders goals by code in the select" do
component = SDGManagement::Relations::IndexComponent.new(Proposal.none.page(1))
render_inline component
options = page.find("#goal_code").all("option")
expect(options[0]).to have_content "All goals"
expect(options[1]).to have_content "1. "
expect(options[17]).to have_content "17. "
end
end
end

View File

@@ -0,0 +1,36 @@
require "rails_helper"
describe Shared::AdvancedSearchComponent, type: :component do
describe "SDG filter" do
let(:component) { Shared::AdvancedSearchComponent.new }
before do
Setting["feature.sdg"] = true
Setting["sdg.process.proposals"] = true
allow(component).to receive(:controller_path).and_return("proposals")
end
it "does not render when the feature is disabled" do
Setting["feature.sdg"] = false
render_inline component
expect(page).not_to have_selector "#advanced_search_goal", visible: :all
end
it "does not render when the SDG process feature is disabled" do
Setting["sdg.process.proposals"] = false
render_inline component
expect(page).not_to have_selector "#advanced_search_goal", visible: :all
end
it "renders when both features are enabled" do
render_inline component
expect(page).to have_selector "#advanced_search_goal", visible: :all
end
end
end

View File

@@ -3,6 +3,8 @@ require "rails_helper"
describe "SDG Goals", :js do
before do
Setting["feature.sdg"] = true
Setting["sdg.process.debates"] = true
Setting["sdg.process.proposals"] = true
end
describe "SDG navigation link" do
@@ -75,6 +77,10 @@ describe "SDG Goals", :js do
expect(page).not_to have_content "Solar panels"
end
within "#advanced_search_form" do
expect(page).to have_select "By SDG", selected: "15. Life on Land"
end
go_back
click_link "See all proposals"
@@ -83,6 +89,10 @@ describe "SDG Goals", :js do
expect(page).to have_content "Animal farm"
expect(page).not_to have_content "Sea farm"
end
within "#advanced_search_form" do
expect(page).to have_select "By SDG", selected: "15. Life on Land"
end
end
end
end