Browse Source

feat: confirm locations when complete even if save button not clicked, and update tests

pull/1455/head
natdeanlewissoftwire 3 years ago
parent
commit
9daba81e4c
  1. 5
      app/controllers/locations_controller.rb
  2. 5
      app/models/location.rb
  3. 4
      spec/models/form/lettings/questions/location_id_spec.rb
  4. 4
      spec/models/validations/setup_validations_spec.rb
  5. 4
      spec/requests/locations_controller_spec.rb

5
app/controllers/locations_controller.rb

@ -134,17 +134,12 @@ class LocationsController < ApplicationController
def check_answers; end def check_answers; end
def confirm def confirm
confirm_location
if @location.confirmed if @location.confirmed
flash[:notice] = "#{@location.postcode} #{@location.startdate.blank? || @location.startdate < Time.zone.now ? 'has been' : 'will be'} added to this scheme" flash[:notice] = "#{@location.postcode} #{@location.startdate.blank? || @location.startdate < Time.zone.now ? 'has been' : 'will be'} added to this scheme"
end end
redirect_to scheme_locations_path(@scheme) redirect_to scheme_locations_path(@scheme)
end 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 show; end
def new_deactivation def new_deactivation

5
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 :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") } 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_startdate, on: :startdate, if: proc { |model| model.startdate.presence }
validate :validate_confirmed
belongs_to :scheme belongs_to :scheme
has_many :lettings_logs, class_name: "LettingsLog" has_many :lettings_logs, class_name: "LettingsLog"
has_many :location_deactivation_periods, class_name: "LocationDeactivationPeriod" has_many :location_deactivation_periods, class_name: "LocationDeactivationPeriod"
@ -127,6 +128,10 @@ class Location < ApplicationRecord
end end
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 def linked_local_authorities
la = LocalAuthority.find_by(code: location_code) la = LocalAuthority.find_by(code: location_code)
return LocalAuthority.none unless la return LocalAuthority.none unless la

4
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 context "and some locations are not confirmed" do
before do before do
FactoryBot.create(:location, scheme:, confirmed: false) FactoryBot.create(:location, scheme:, postcode: nil)
FactoryBot.create(:location, scheme:, confirmed: true) FactoryBot.create(:location, scheme:)
lettings_log.update!(scheme:) lettings_log.update!(scheme:)
end end

4
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 context "with a scheme that has no confirmed locations" do
before do before do
create(:location, scheme:, confirmed: false) create(:location, scheme:, postcode: nil)
scheme.reload scheme.reload
end end
@ -388,7 +388,7 @@ RSpec.describe Validations::SetupValidations do
context "with a scheme that has confirmed locations" do context "with a scheme that has confirmed locations" do
before do before do
create(:location, scheme:, confirmed: true) create(:location, scheme:)
scheme.reload scheme.reload
end end

4
spec/requests/locations_controller_spec.rb

@ -1278,7 +1278,7 @@ RSpec.describe LocationsController, type: :request do
context "when location is not complete" do context "when location is not complete" do
let(:location) { FactoryBot.create(:location, scheme:, startdate: Time.zone.local(2000, 1, 1), postcode: nil) } 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) expect(Location.last.confirmed).to eq(false)
end end
@ -1339,7 +1339,7 @@ RSpec.describe LocationsController, type: :request do
context "when location is not complete" do context "when location is not complete" do
let(:location) { FactoryBot.create(:location, scheme:, startdate: Time.zone.local(2000, 1, 1), postcode: nil) } 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) expect(Location.last.confirmed).to eq(false)
end end

Loading…
Cancel
Save