From 6c1ab0d44a602f1c9aca80d820b0b15d1de0cb86 Mon Sep 17 00:00:00 2001 From: Kat Date: Fri, 1 Mar 2024 15:50:13 +0000 Subject: [PATCH] Do not display deleted locationa as an option --- app/models/form/lettings/questions/location_id.rb | 4 ++-- .../form/lettings/questions/location_id_spec.rb | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/models/form/lettings/questions/location_id.rb b/app/models/form/lettings/questions/location_id.rb index cbc9b840a..d79bcab76 100644 --- a/app/models/form/lettings/questions/location_id.rb +++ b/app/models/form/lettings/questions/location_id.rb @@ -20,7 +20,7 @@ class Form::Lettings::Questions::LocationId < ::Form::Question answer_opts = {} return answer_opts unless ActiveRecord::Base.connected? - Location.started_in_2_weeks.select(:id, :postcode, :name).each_with_object(answer_opts) do |location, hsh| + Location.visible.started_in_2_weeks.select(:id, :postcode, :name).each_with_object(answer_opts) do |location, hsh| hsh[location.id.to_s] = { "value" => location.postcode, "hint" => location.name } hsh end @@ -29,7 +29,7 @@ class Form::Lettings::Questions::LocationId < ::Form::Question def displayed_answer_options(lettings_log, _user = nil) return {} unless lettings_log.scheme - scheme_location_ids = lettings_log.scheme.locations.confirmed.pluck(:id) + scheme_location_ids = lettings_log.scheme.locations.visible.confirmed.pluck(:id) answer_options.select { |k, _v| scheme_location_ids.include?(k.to_i) } end diff --git a/spec/models/form/lettings/questions/location_id_spec.rb b/spec/models/form/lettings/questions/location_id_spec.rb index a2c41af80..8406b8119 100644 --- a/spec/models/form/lettings/questions/location_id_spec.rb +++ b/spec/models/form/lettings/questions/location_id_spec.rb @@ -80,6 +80,18 @@ RSpec.describe Form::Lettings::Questions::LocationId, type: :model do end end + context "and all the locations are deleted" do + before do + FactoryBot.create(:location, scheme:, discarded_at: Time.utc(2022, 5, 12)) + FactoryBot.create(:location, scheme:, discarded_at: Time.utc(2022, 5, 12)) + lettings_log.update!(scheme:) + end + + it "the displayed_answer_options is an empty hash" do + expect(question.displayed_answer_options(lettings_log)).to eq({}) + end + end + context "and all but one of the locations have a startdate more than 2 weeks in the future" do before do FactoryBot.create(:location, scheme:, startdate: Time.utc(2022, 5, 13))