diff --git a/app/models/scheme.rb b/app/models/scheme.rb index f9596a710..997c3b3fd 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -235,7 +235,7 @@ class Scheme < ApplicationRecord end def status_at(date) - return :incomplete unless confirmed && has_completed_locations? + return :incomplete unless confirmed && has_confirmed_locations? return :deactivated if open_deactivation&.deactivation_date.present? && date >= open_deactivation.deactivation_date return :deactivating_soon if open_deactivation&.deactivation_date.present? && date < open_deactivation.deactivation_date return :reactivating_soon if recent_deactivation&.reactivation_date.present? && date < recent_deactivation.reactivation_date @@ -255,14 +255,14 @@ class Scheme < ApplicationRecord status == :deactivated end - def has_completed_locations? - completed_locations_count.positive? + def has_confirmed_locations? + confirmed_locations_count.positive? end private - def completed_locations_count - locations.count { |location| location.status != :incomplete } + def confirmed_locations_count + locations.confirmed.size end def unconfirmed_locations_count diff --git a/spec/models/form/lettings/questions/location_id_spec.rb b/spec/models/form/lettings/questions/location_id_spec.rb index afb39ce11..ab843b673 100644 --- a/spec/models/form/lettings/questions/location_id_spec.rb +++ b/spec/models/form/lettings/questions/location_id_spec.rb @@ -48,11 +48,13 @@ RSpec.describe Form::Lettings::Questions::LocationId, type: :model do context "when getting available locations" do let(:scheme) { FactoryBot.create(:scheme) } let!(:lettings_log) do - FactoryBot.create(:lettings_log, owning_organisation: scheme.owning_organisation, scheme:, needstype: 2) + FactoryBot.create(:lettings_log, owning_organisation: scheme.owning_organisation, needstype: 2) end context "when there are no locations" do - it "the displayed_answer_options is an empty hash" do + it "the displayed_answer_options is an empty hash (the scheme should have failed validation anyway so don't validate here)" do + lettings_log.scheme = scheme + lettings_log.save!(validate: false) expect(question.displayed_answer_options(lettings_log)).to eq({}) end end @@ -70,6 +72,7 @@ RSpec.describe Form::Lettings::Questions::LocationId, type: :model do before do FactoryBot.create(:location, scheme:, startdate: Time.utc(2022, 5, 13)) FactoryBot.create(:location, scheme:, startdate: Time.utc(2023, 1, 1)) + lettings_log.update!(scheme:) end it "the displayed_answer_options is an empty hash" do @@ -77,10 +80,11 @@ RSpec.describe Form::Lettings::Questions::LocationId, type: :model do end end - context "and the locations have a no startdate" do + context "and the locations have no startdate" do before do FactoryBot.create(:location, scheme:, startdate: nil) FactoryBot.create(:location, scheme:, startdate: nil) + lettings_log.update!(scheme:) end it "the displayed_answer_options shows the locations" do @@ -92,6 +96,7 @@ RSpec.describe Form::Lettings::Questions::LocationId, type: :model do before do FactoryBot.create(:location, scheme:, startdate: Time.utc(2022, 4, 10)) FactoryBot.create(:location, scheme:, startdate: Time.utc(2022, 5, 12)) + lettings_log.update!(scheme:) end it "the displayed_answer_options shows the locations" do @@ -103,6 +108,7 @@ RSpec.describe Form::Lettings::Questions::LocationId, type: :model do before do FactoryBot.create(:location, scheme:, startdate: Time.utc(2022, 10, 10)) FactoryBot.create(:location, scheme:, startdate: Time.utc(2022, 5, 12)) + lettings_log.update!(scheme:) end it "the displayed_answer_options shows the active location" do