Add SDG target filter to advanced search

This commit is contained in:
Javi Martín
2021-01-21 00:56:43 +01:00
parent b8cd758437
commit 3ec628a63b
13 changed files with 98 additions and 15 deletions

View File

@@ -22,10 +22,10 @@
.date-filters {
float: left;
width: 50%;
width: 25%;
.filter {
width: 50%;
width: 100%;
}
.custom-date-filters {

View File

@@ -1,7 +0,0 @@
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

@@ -0,0 +1,13 @@
module SDG::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
def target_options(selected_code = nil)
targets = SDG::Target.all + SDG::LocalTarget.all
options_from_collection_for_select(targets.sort, :code, :code, selected_code)
end
end

View File

@@ -1,6 +1,6 @@
class SDGManagement::Relations::IndexComponent < ApplicationComponent
include Header
include SDG::Goals::OptionsForSelect
include SDG::OptionsForSelect
delegate :valid_filters, :current_filter, to: :helpers
attr_reader :records
@@ -53,8 +53,6 @@ class SDGManagement::Relations::IndexComponent < ApplicationComponent
end
def target_options
targets = SDG::Target.all + SDG::LocalTarget.all
options_from_collection_for_select(targets.sort, :code, :code, params[:target_code])
super(params[:target_code])
end
end

View File

@@ -26,6 +26,11 @@
<%= select_tag("advanced_search[goal]", goal_options,
include_blank: t("shared.advanced_search.goal_blank")) %>
</div>
<div class="filter">
<label for="advanced_search_target"><%= t("shared.advanced_search.target") %></label>
<%= select_tag("advanced_search[target]", target_options,
include_blank: t("shared.advanced_search.target_blank")) %>
</div>
<% end %>
<div class="date-filters">

View File

@@ -1,5 +1,5 @@
class Shared::AdvancedSearchComponent < ApplicationComponent
include SDG::Goals::OptionsForSelect
include SDG::OptionsForSelect
private
@@ -34,6 +34,10 @@ class Shared::AdvancedSearchComponent < ApplicationComponent
super(advanced_search[:goal])
end
def target_options
super(advanced_search[:target])
end
def sdg?
SDG::ProcessEnabled.new(controller_path).enabled?
end

View File

@@ -20,7 +20,7 @@ module Filterable
def allowed_filter?(filter, value)
return if value.blank?
["official_level", "date_range", "goal"].include?(filter)
["official_level", "date_range", "goal", "target"].include?(filter)
end
end
end

View File

@@ -697,6 +697,8 @@ en:
goal: "By SDG"
goal_blank: "Select a goal"
search: "Filter"
target: "By target"
target_blank: "Select a target"
title: "Advanced search"
to: "To"
author_info:

View File

@@ -697,6 +697,8 @@ es:
goal: "Por ODS"
goal_blank: "Elige un objetivo"
search: "Filtrar"
target: "Por meta"
target_blank: "Elige una meta"
title: "Búsqueda avanzada"
to: "Hasta"
author_info:

View File

@@ -17,6 +17,7 @@ describe Shared::AdvancedSearchComponent, type: :component do
render_inline component
expect(page).not_to have_selector "#advanced_search_goal", visible: :all
expect(page).not_to have_selector "#advanced_search_target", visible: :all
end
it "does not render when the SDG process feature is disabled" do
@@ -25,12 +26,14 @@ describe Shared::AdvancedSearchComponent, type: :component do
render_inline component
expect(page).not_to have_selector "#advanced_search_goal", visible: :all
expect(page).not_to have_selector "#advanced_search_target", visible: :all
end
it "renders when both features are enabled" do
render_inline component
expect(page).to have_selector "#advanced_search_goal", visible: :all
expect(page).to have_selector "#advanced_search_target", visible: :all
end
end
end

View File

@@ -500,6 +500,27 @@ describe "Budget Investments" do
end
end
end
scenario "Search by SDG target", :js do
Setting["feature.sdg"] = true
Setting["sdg.process.budgets"] = true
create(:budget_investment, heading: heading, title: "Unrelated")
create(:budget_investment, heading: heading, title: "High school", sdg_targets: [SDG::Target["4.1"]])
create(:budget_investment, heading: heading, title: "Preschool", sdg_targets: [SDG::Target["4.2"]])
visit budget_investments_path(budget)
click_link "Advanced search"
select "4.2", from: "By target"
click_button "Filter"
expect(page).to have_content("There is 1 investment")
within("#budget-investments") do
expect(page).to have_content("Preschool")
expect(page).not_to have_content("High school")
expect(page).not_to have_content("Unrelated")
end
end
end
context("Filters") do

View File

@@ -866,6 +866,27 @@ describe "Debates" do
end
end
end
scenario "Search by SDG target", :js do
Setting["feature.sdg"] = true
Setting["sdg.process.debates"] = true
create(:debate, title: "Unrelated")
create(:debate, title: "High school", sdg_targets: [SDG::Target["4.1"]])
create(:debate, title: "Preschool", sdg_targets: [SDG::Target["4.2"]])
visit debates_path
click_link "Advanced search"
select "4.2", from: "By target"
click_button "Filter"
expect(page).to have_content("There is 1 debate")
within("#debates") do
expect(page).to have_content("Preschool")
expect(page).not_to have_content("High school")
expect(page).not_to have_content("Unrelated")
end
end
end
scenario "Order by relevance by default", :js do

View File

@@ -1482,6 +1482,27 @@ describe "Proposals" do
end
end
end
scenario "Search by SDG target", :js do
Setting["feature.sdg"] = true
Setting["sdg.process.proposals"] = true
create(:proposal, title: "Unrelated")
create(:proposal, title: "High school", sdg_targets: [SDG::Target["4.1"]])
create(:proposal, title: "Preschool", sdg_targets: [SDG::Target["4.2"]])
visit proposals_path
click_link "Advanced search"
select "4.2", from: "By target"
click_button "Filter"
expect(page).to have_content("There is 1 citizen proposal")
within("#proposals") do
expect(page).to have_content("Preschool")
expect(page).not_to have_content("High school")
expect(page).not_to have_content("Unrelated")
end
end
end
scenario "Order by relevance by default", :js do