From 9daba81e4cd13de723daa811eeaeb783a62a9a79 Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Fri, 24 Mar 2023 16:21:56 +0000 Subject: [PATCH] feat: confirm locations when complete even if save button not clicked, and update tests --- app/controllers/locations_controller.rb | 5 ----- app/models/location.rb | 5 +++++ spec/models/form/lettings/questions/location_id_spec.rb | 4 ++-- spec/models/validations/setup_validations_spec.rb | 4 ++-- spec/requests/locations_controller_spec.rb | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index b67701176..8f41a0644 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -134,17 +134,12 @@ class LocationsController < ApplicationController def check_answers; end def confirm - confirm_location if @location.confirmed flash[:notice] = "#{@location.postcode} #{@location.startdate.blank? || @location.startdate < Time.zone.now ? 'has been' : 'will be'} added to this scheme" end redirect_to scheme_locations_path(@scheme) end - def confirm_location - @location.update!(confirmed: [@location.postcode, @location.location_admin_district, @location.location_code, @location.units, @location.type_of_unit, @location.mobility_type].all?(&:present?)) - end - def show; end def new_deactivation diff --git a/app/models/location.rb b/app/models/location.rb index 4281b9e9b..7e853496e 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -7,6 +7,7 @@ class Location < ApplicationRecord validates :mobility_type, on: :mobility_type, presence: { message: I18n.t("validations.location.mobility_standards") } validates :startdate, on: :startdate, presence: { message: I18n.t("validations.location.startdate_invalid") } validate :validate_startdate, on: :startdate, if: proc { |model| model.startdate.presence } + validate :validate_confirmed belongs_to :scheme has_many :lettings_logs, class_name: "LettingsLog" has_many :location_deactivation_periods, class_name: "LocationDeactivationPeriod" @@ -127,6 +128,10 @@ class Location < ApplicationRecord end end + def validate_confirmed + self.confirmed = [postcode, location_admin_district, location_code, units, type_of_unit, mobility_type].all?(&:present?) + end + def linked_local_authorities la = LocalAuthority.find_by(code: location_code) return LocalAuthority.none unless la diff --git a/spec/models/form/lettings/questions/location_id_spec.rb b/spec/models/form/lettings/questions/location_id_spec.rb index ef8c5fe88..fbe580970 100644 --- a/spec/models/form/lettings/questions/location_id_spec.rb +++ b/spec/models/form/lettings/questions/location_id_spec.rb @@ -118,8 +118,8 @@ RSpec.describe Form::Lettings::Questions::LocationId, type: :model do context "and some locations are not confirmed" do before do - FactoryBot.create(:location, scheme:, confirmed: false) - FactoryBot.create(:location, scheme:, confirmed: true) + FactoryBot.create(:location, scheme:, postcode: nil) + FactoryBot.create(:location, scheme:) lettings_log.update!(scheme:) end diff --git a/spec/models/validations/setup_validations_spec.rb b/spec/models/validations/setup_validations_spec.rb index ded900c7a..5bb180aa2 100644 --- a/spec/models/validations/setup_validations_spec.rb +++ b/spec/models/validations/setup_validations_spec.rb @@ -374,7 +374,7 @@ RSpec.describe Validations::SetupValidations do context "with a scheme that has no confirmed locations" do before do - create(:location, scheme:, confirmed: false) + create(:location, scheme:, postcode: nil) scheme.reload end @@ -388,7 +388,7 @@ RSpec.describe Validations::SetupValidations do context "with a scheme that has confirmed locations" do before do - create(:location, scheme:, confirmed: true) + create(:location, scheme:) scheme.reload end diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index 6e8fc7207..ab9dcd268 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -1278,7 +1278,7 @@ RSpec.describe LocationsController, type: :request do context "when location is not complete" do let(:location) { FactoryBot.create(:location, scheme:, startdate: Time.zone.local(2000, 1, 1), postcode: nil) } - it "confirms location" do + it "does not confirm location" do expect(Location.last.confirmed).to eq(false) end @@ -1339,7 +1339,7 @@ RSpec.describe LocationsController, type: :request do context "when location is not complete" do let(:location) { FactoryBot.create(:location, scheme:, startdate: Time.zone.local(2000, 1, 1), postcode: nil) } - it "confirms location" do + it "does not confirm location" do expect(Location.last.confirmed).to eq(false) end