Add goal filter to advanced search

This commit is contained in:
Javi Martín
2021-01-08 18:40:54 +01:00
parent 8024c4bb69
commit 75fc4315ba
9 changed files with 87 additions and 4 deletions

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.order(:code), :code, :code_and_title, params[:goal_code])
super(params[:goal_code])
end
def target_options

View File

@@ -20,6 +20,14 @@
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>

View File

@@ -1,4 +1,5 @@
class Shared::AdvancedSearchComponent < ApplicationComponent
include SDG::Goals::OptionsForSelect
private
@@ -28,4 +29,12 @@ class Shared::AdvancedSearchComponent < ApplicationComponent
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

@@ -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

@@ -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,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