diff --git a/app/models/location.rb b/app/models/location.rb index 85017afad..4333ea27c 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -395,13 +395,17 @@ class Location < ApplicationRecord def deactivation_date_errors return unless implicit_run_deactivation_validations - collection_start_date = FormHandler.instance.current_collection_start_date - if deactivation_date_type.blank? - errors.add(:deactivation_date_type, message: I18n.t("validations.location.deactivation_date.not_selected")) - elsif deactivation_date.blank? - errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.invalid")) - elsif !deactivation_date.between?(collection_start_date, Date.new(2200, 1, 1)) - errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date))) + if deactivation_date.blank? + if deactivation_date_type.blank? + errors.add(:deactivation_date_type, message: I18n.t("validations.location.deactivation_date.not_selected")) + elsif deactivation_date_type == "other" + errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.invalid")) + end + else + collection_start_date = FormHandler.instance.current_collection_start_date + if !deactivation_date.between?(collection_start_date, Date.new(2200, 1, 1)) + errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date))) + end end end diff --git a/app/models/scheme.rb b/app/models/scheme.rb index fe3f40a4a..d87b33fb7 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -237,13 +237,17 @@ class Scheme < ApplicationRecord def deactivation_date_errors return unless implicit_run_deactivation_validations - collection_start_date = FormHandler.instance.current_collection_start_date - if deactivation_date_type.blank? - errors.add(:deactivation_date_type, message: I18n.t("validations.scheme.deactivation_date.not_selected")) - elsif deactivation_date.blank? - errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.invalid")) - elsif !deactivation_date.between?(collection_start_date, Date.new(2200, 1, 1)) - errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date))) + if deactivation_date.blank? + if deactivation_date_type.blank? + errors.add(:deactivation_date_type, message: I18n.t("validations.scheme.deactivation_date.not_selected")) + elsif deactivation_date_type == "other" + errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.invalid")) + end + else + collection_start_date = FormHandler.instance.current_collection_start_date + if !deactivation_date.between?(collection_start_date, Date.new(2200, 1, 1)) + errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date))) + end end end end diff --git a/app/views/schemes/deactivate_confirm.html.erb b/app/views/schemes/deactivate_confirm.html.erb index 95d3896be..45f7d4341 100644 --- a/app/views/schemes/deactivate_confirm.html.erb +++ b/app/views/schemes/deactivate_confirm.html.erb @@ -13,7 +13,7 @@ <%= f.hidden_field :deactivation_date, value: @deactivation_date %> <%= f.hidden_field :deactivation_date_type, value: @deactivation_date_type %>
<% end %> diff --git a/db/schema.rb b/db/schema.rb index cf77313f4..1ff8f157c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -360,12 +360,6 @@ ActiveRecord::Schema[7.0].define(version: 2022_11_11_102656) do t.integer "hholdcount" t.integer "age3" t.integer "age3_known" - t.integer "age4" - t.integer "age4_known" - t.integer "age5" - t.integer "age5_known" - t.integer "age6" - t.integer "age6_known" t.string "la" t.integer "la_known" t.integer "income1" @@ -373,6 +367,12 @@ ActiveRecord::Schema[7.0].define(version: 2022_11_11_102656) do t.integer "details_known_2" t.integer "details_known_3" t.integer "details_known_4" + t.integer "age4" + t.integer "age4_known" + t.integer "age5" + t.integer "age5_known" + t.integer "age6" + t.integer "age6_known" t.index ["created_by_id"], name: "index_sales_logs_on_created_by_id" t.index ["managing_organisation_id"], name: "index_sales_logs_on_managing_organisation_id" t.index ["owning_organisation_id"], name: "index_sales_logs_on_owning_organisation_id" diff --git a/spec/models/location_spec.rb b/spec/models/location_spec.rb index 304339af7..32cab5e8e 100644 --- a/spec/models/location_spec.rb +++ b/spec/models/location_spec.rb @@ -147,4 +147,12 @@ RSpec.describe Location, type: :model do expect(location.status).to eq(:deactivated) end end + + describe "with deactivation_date (but no deactivation_date_type)" do + let(:location) { FactoryBot.create(:location, deactivation_date: Date.new(2022, 4, 1)) } + + it "is valid" do + expect(location.valid?).to be_truthy + end + end end diff --git a/spec/models/scheme_spec.rb b/spec/models/scheme_spec.rb index cb6ef2c8d..561aec2b1 100644 --- a/spec/models/scheme_spec.rb +++ b/spec/models/scheme_spec.rb @@ -127,4 +127,12 @@ RSpec.describe Scheme, type: :model do expect(scheme.status).to eq(:deactivated) end end + + describe "with deactivation_date (but no deactivation_date_type)" do + let(:scheme) { FactoryBot.create(:scheme, deactivation_date: Date.new(2022, 4, 1)) } + + it "is valid" do + expect(scheme.valid?).to be_truthy + end + end end