diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index b00d453e9..615377e01 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -145,8 +145,7 @@ class LocationsController < ApplicationController def check_answers; end def update_check_answers - @location.confirmed = true - @location.save! + @location.confirmed! flash[:notice] = "#{@location.postcode} #{@location.startdate < Time.zone.now ? 'has been' : 'will be'} added to this scheme" redirect_to scheme_locations_path(@scheme) end diff --git a/app/models/location.rb b/app/models/location.rb index 26413d4d0..2a1dd5c7f 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -7,6 +7,7 @@ class Location < ApplicationRecord validates :type_of_unit, on: :type_of_unit, presence: { message: I18n.t("validations.location.type_of_unit") } validates :mobility_type, on: :mobility_type, presence: { message: I18n.t("validations.location.mobility_standards") } validate :validate_startdate, on: :startdate + validate :validate_confirmed belongs_to :scheme has_many :lettings_logs, class_name: "LettingsLog" has_many :location_deactivation_periods, class_name: "LocationDeactivationPeriod" @@ -428,10 +429,20 @@ class Location < ApplicationRecord end end + def validate_confirmed + if confirmed == true && incomplete? + self.confirmed = false + end + end + def incomplete? [postcode, name, location_admin_district, location_code, units, type_of_unit, mobility_type, startdate].any?(&:blank?) end + def confirmed! + update!(confirmed: true) + end + private PIO = PostcodeService.new