From fb611d91cadb9b728244b4461f7226e2c954de3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Sun, 31 Jan 2021 13:43:22 +0100 Subject: [PATCH] Fix legislation proposal crash with SDG enabled MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now we check the given record or name is a relatable instance or class to avoid trying to render goals for records which don't have a goals association. Note for now we are ignoring the case where we pass a controller_path for an unsupported class (for example, `legislation/proposals` or `budgets/headings`) because we never use it. We might need to revisit this case in the future. Co-Authored-By: Javi Martín --- app/models/sdg/process_enabled.rb | 8 +++++++- spec/models/sdg/process_enabled_spec.rb | 6 ++++++ spec/system/legislation/proposals_spec.rb | 5 ++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/models/sdg/process_enabled.rb b/app/models/sdg/process_enabled.rb index 67a17902a..29b567bcf 100644 --- a/app/models/sdg/process_enabled.rb +++ b/app/models/sdg/process_enabled.rb @@ -7,7 +7,7 @@ class SDG::ProcessEnabled end def enabled? - feature?("sdg") && feature?(process_name) && setting["sdg.process.#{process_name}"] + feature?("sdg") && feature?(process_name) && setting["sdg.process.#{process_name}"] && relatable? end def name @@ -39,4 +39,10 @@ class SDG::ProcessEnabled def module_name name.split("::").first end + + def relatable? + return true if controller_path_name? + + (SDG::Related::RELATABLE_TYPES & [record_or_name.class.name, record_or_name]).any? + end end diff --git a/spec/models/sdg/process_enabled_spec.rb b/spec/models/sdg/process_enabled_spec.rb index 9463e9ba8..5094b1d42 100644 --- a/spec/models/sdg/process_enabled_spec.rb +++ b/spec/models/sdg/process_enabled_spec.rb @@ -31,6 +31,12 @@ describe SDG::ProcessEnabled do expect(process).to be_enabled end + + it "returns false when record or name are not a relatable type" do + expect(SDG::ProcessEnabled.new(build(:legislation_proposal))).not_to be_enabled + expect(SDG::ProcessEnabled.new("Legislation::Proposal")).not_to be_enabled + expect(SDG::ProcessEnabled.new("officing/booth")).not_to be_enabled + end end context "SDG feature is disabled" do diff --git a/spec/system/legislation/proposals_spec.rb b/spec/system/legislation/proposals_spec.rb index 22c83211d..50162f2ae 100644 --- a/spec/system/legislation/proposals_spec.rb +++ b/spec/system/legislation/proposals_spec.rb @@ -226,7 +226,10 @@ describe "Legislation Proposals" do expect(page).to have_content "Open concert" end - scenario "Show proposal tags on show", :js do + scenario "Show proposal tags on show when SDG is enabled", :js do + Setting["feature.sdg"] = true + Setting["sdg.process.legislation"] = true + proposal = create(:legislation_proposal, process: process, tag_list: "Culture") visit legislation_process_proposal_path(proposal.process, proposal)