Browse Source

Do not display deleted schemes as an option

pull/2286/head
Kat 2 years ago
parent
commit
6b5fb10dff
  1. 10
      app/models/form/lettings/questions/scheme_id.rb
  2. 13
      spec/models/form/lettings/questions/scheme_id_spec.rb

10
app/models/form/lettings/questions/scheme_id.rb

@ -20,8 +20,8 @@ class Form::Lettings::Questions::SchemeId < ::Form::Question
answer_opts = { "" => "Select an option" } answer_opts = { "" => "Select an option" }
return answer_opts unless ActiveRecord::Base.connected? return answer_opts unless ActiveRecord::Base.connected?
Scheme.select(:id, :service_name, :primary_client_group, Scheme.visible.select(:id, :service_name, :primary_client_group,
:secondary_client_group).each_with_object(answer_opts) do |scheme, hsh| :secondary_client_group).each_with_object(answer_opts) do |scheme, hsh|
hsh[scheme.id.to_s] = scheme hsh[scheme.id.to_s] = scheme
hsh hsh
end end
@ -30,10 +30,10 @@ class Form::Lettings::Questions::SchemeId < ::Form::Question
def displayed_answer_options(lettings_log, _user = nil) def displayed_answer_options(lettings_log, _user = nil)
organisation = lettings_log.owning_organisation || lettings_log.created_by&.organisation organisation = lettings_log.owning_organisation || lettings_log.created_by&.organisation
schemes = if organisation schemes = if organisation
Scheme.includes(:locations).select(:id).where(owning_organisation_id: organisation.id, Scheme.visible.includes(:locations).select(:id).where(owning_organisation_id: organisation.id,
confirmed: true) confirmed: true)
else else
Scheme.includes(:locations).select(:id).where(confirmed: true) Scheme.visible.includes(:locations).select(:id).where(confirmed: true)
end end
filtered_scheme_ids = schemes.joins(:locations).merge(Location.started_in_2_weeks).map(&:id) filtered_scheme_ids = schemes.joins(:locations).merge(Location.started_in_2_weeks).map(&:id)
answer_options.select do |k, _v| answer_options.select do |k, _v|

13
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) expect(question.displayed_answer_options(lettings_log)).to eq(expected_answer)
end end
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 end
context "when there are no schemes with locations" do context "when there are no schemes with locations" do

Loading…
Cancel
Save