From 6b5fb10dfff43948f8c16ebdd3f7adaada7b0ea3 Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 4 Mar 2024 14:06:25 +0000 Subject: [PATCH] Do not display deleted schemes as an option --- app/models/form/lettings/questions/scheme_id.rb | 10 +++++----- .../form/lettings/questions/scheme_id_spec.rb | 13 +++++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/app/models/form/lettings/questions/scheme_id.rb b/app/models/form/lettings/questions/scheme_id.rb index 8d906f134..006e549e8 100644 --- a/app/models/form/lettings/questions/scheme_id.rb +++ b/app/models/form/lettings/questions/scheme_id.rb @@ -20,8 +20,8 @@ class Form::Lettings::Questions::SchemeId < ::Form::Question answer_opts = { "" => "Select an option" } return answer_opts unless ActiveRecord::Base.connected? - Scheme.select(:id, :service_name, :primary_client_group, - :secondary_client_group).each_with_object(answer_opts) do |scheme, hsh| + Scheme.visible.select(:id, :service_name, :primary_client_group, + :secondary_client_group).each_with_object(answer_opts) do |scheme, hsh| hsh[scheme.id.to_s] = scheme hsh end @@ -30,10 +30,10 @@ class Form::Lettings::Questions::SchemeId < ::Form::Question def displayed_answer_options(lettings_log, _user = nil) organisation = lettings_log.owning_organisation || lettings_log.created_by&.organisation schemes = if organisation - Scheme.includes(:locations).select(:id).where(owning_organisation_id: organisation.id, - confirmed: true) + Scheme.visible.includes(:locations).select(:id).where(owning_organisation_id: organisation.id, + confirmed: true) else - Scheme.includes(:locations).select(:id).where(confirmed: true) + Scheme.visible.includes(:locations).select(:id).where(confirmed: true) end filtered_scheme_ids = schemes.joins(:locations).merge(Location.started_in_2_weeks).map(&:id) answer_options.select do |k, _v| diff --git a/spec/models/form/lettings/questions/scheme_id_spec.rb b/spec/models/form/lettings/questions/scheme_id_spec.rb index b1e628612..d9b7a5d0d 100644 --- a/spec/models/form/lettings/questions/scheme_id_spec.rb +++ b/spec/models/form/lettings/questions/scheme_id_spec.rb @@ -122,6 +122,19 @@ RSpec.describe Form::Lettings::Questions::SchemeId, type: :model do expect(question.displayed_answer_options(lettings_log)).to eq(expected_answer) end end + + context "when the scheme is deleted" do + let(:scheme) { FactoryBot.create(:scheme, owning_organisation: organisation, discarded_at: Time.zone.yesterday) } + + before do + FactoryBot.create(:location, startdate: Time.zone.tomorrow, scheme:) + end + + it "has the correct answer_options based on the schemes the user's organisation owns or manages" do + expected_answer = { "" => "Select an option" } + expect(question.displayed_answer_options(lettings_log)).to eq(expected_answer) + end + end end context "when there are no schemes with locations" do