diff --git a/app/components/concerns/sdg/goals/options_for_select.rb b/app/components/concerns/sdg/goals/options_for_select.rb new file mode 100644 index 000000000..559168a3a --- /dev/null +++ b/app/components/concerns/sdg/goals/options_for_select.rb @@ -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 diff --git a/app/components/sdg_management/relations/index_component.rb b/app/components/sdg_management/relations/index_component.rb index c6f136ff4..4af870992 100644 --- a/app/components/sdg_management/relations/index_component.rb +++ b/app/components/sdg_management/relations/index_component.rb @@ -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 diff --git a/app/components/shared/advanced_search_component.html.erb b/app/components/shared/advanced_search_component.html.erb index a0f5006d8..b03a371fc 100644 --- a/app/components/shared/advanced_search_component.html.erb +++ b/app/components/shared/advanced_search_component.html.erb @@ -20,6 +20,14 @@ include_blank: t("shared.advanced_search.author_type_blank")) %> + <% if sdg? %> +
+ + <%= select_tag("advanced_search[goal]", goal_options, + include_blank: t("shared.advanced_search.goal_blank")) %> +
+ <% end %> +
diff --git a/app/components/shared/advanced_search_component.rb b/app/components/shared/advanced_search_component.rb index c961aae19..a03425305 100644 --- a/app/components/shared/advanced_search_component.rb +++ b/app/components/shared/advanced_search_component.rb @@ -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 diff --git a/app/models/sdg/process_enabled.rb b/app/models/sdg/process_enabled.rb index 5be06dc80..67a17902a 100644 --- a/app/models/sdg/process_enabled.rb +++ b/app/models/sdg/process_enabled.rb @@ -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 diff --git a/config/locales/en/general.yml b/config/locales/en/general.yml index 878928629..60b145e49 100644 --- a/config/locales/en/general.yml +++ b/config/locales/en/general.yml @@ -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" diff --git a/config/locales/es/general.yml b/config/locales/es/general.yml index 2a93e8bfb..a01da18f4 100644 --- a/config/locales/es/general.yml +++ b/config/locales/es/general.yml @@ -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" diff --git a/spec/components/shared/advanced_search_component_spec.rb b/spec/components/shared/advanced_search_component_spec.rb new file mode 100644 index 000000000..1f4966d17 --- /dev/null +++ b/spec/components/shared/advanced_search_component_spec.rb @@ -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 diff --git a/spec/system/sdg/goals_spec.rb b/spec/system/sdg/goals_spec.rb index 3314b5f53..9854b609f 100644 --- a/spec/system/sdg/goals_spec.rb +++ b/spec/system/sdg/goals_spec.rb @@ -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