Browse Source

feat: update deactivation_date validation and add tests

pull/980/head
natdeanlewissoftwire 2 years ago
parent
commit
9914a3b301
  1. 18
      app/models/location.rb
  2. 18
      app/models/scheme.rb
  3. 2
      app/views/schemes/deactivate_confirm.html.erb
  4. 12
      db/schema.rb
  5. 8
      spec/models/location_spec.rb
  6. 8
      spec/models/scheme_spec.rb

18
app/models/location.rb

@ -395,13 +395,17 @@ class Location < ApplicationRecord
def deactivation_date_errors def deactivation_date_errors
return unless implicit_run_deactivation_validations return unless implicit_run_deactivation_validations
collection_start_date = FormHandler.instance.current_collection_start_date if deactivation_date.blank?
if deactivation_date_type.blank? if deactivation_date_type.blank?
errors.add(:deactivation_date_type, message: I18n.t("validations.location.deactivation_date.not_selected")) errors.add(:deactivation_date_type, message: I18n.t("validations.location.deactivation_date.not_selected"))
elsif deactivation_date.blank? elsif deactivation_date_type == "other"
errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.invalid")) errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.invalid"))
elsif !deactivation_date.between?(collection_start_date, Date.new(2200, 1, 1)) end
errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date))) 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
end end

18
app/models/scheme.rb

@ -237,13 +237,17 @@ class Scheme < ApplicationRecord
def deactivation_date_errors def deactivation_date_errors
return unless implicit_run_deactivation_validations return unless implicit_run_deactivation_validations
collection_start_date = FormHandler.instance.current_collection_start_date if deactivation_date.blank?
if deactivation_date_type.blank? if deactivation_date_type.blank?
errors.add(:deactivation_date_type, message: I18n.t("validations.scheme.deactivation_date.not_selected")) errors.add(:deactivation_date_type, message: I18n.t("validations.scheme.deactivation_date.not_selected"))
elsif deactivation_date.blank? elsif deactivation_date_type == "other"
errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.invalid")) errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.invalid"))
elsif !deactivation_date.between?(collection_start_date, Date.new(2200, 1, 1)) end
errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date))) 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 end
end end

2
app/views/schemes/deactivate_confirm.html.erb

@ -13,7 +13,7 @@
<%= f.hidden_field :deactivation_date, value: @deactivation_date %> <%= f.hidden_field :deactivation_date, value: @deactivation_date %>
<%= f.hidden_field :deactivation_date_type, value: @deactivation_date_type %> <%= f.hidden_field :deactivation_date_type, value: @deactivation_date_type %>
<div class="govuk-button-group"> <div class="govuk-button-group">
<%= f.govuk_submit "#{action.humanize} this scheme" %> <%= f.govuk_submit "Deactivate this scheme" %>
<%= govuk_button_link_to "Cancel", scheme_details_path(@scheme), html: { method: :get }, secondary: true %> <%= govuk_button_link_to "Cancel", scheme_details_path(@scheme), html: { method: :get }, secondary: true %>
</div> </div>
<% end %> <% end %>

12
db/schema.rb

@ -360,12 +360,6 @@ ActiveRecord::Schema[7.0].define(version: 2022_11_11_102656) do
t.integer "hholdcount" t.integer "hholdcount"
t.integer "age3" t.integer "age3"
t.integer "age3_known" 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.string "la"
t.integer "la_known" t.integer "la_known"
t.integer "income1" 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_2"
t.integer "details_known_3" t.integer "details_known_3"
t.integer "details_known_4" 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 ["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 ["managing_organisation_id"], name: "index_sales_logs_on_managing_organisation_id"
t.index ["owning_organisation_id"], name: "index_sales_logs_on_owning_organisation_id" t.index ["owning_organisation_id"], name: "index_sales_logs_on_owning_organisation_id"

8
spec/models/location_spec.rb

@ -147,4 +147,12 @@ RSpec.describe Location, type: :model do
expect(location.status).to eq(:deactivated) expect(location.status).to eq(:deactivated)
end end
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 end

8
spec/models/scheme_spec.rb

@ -127,4 +127,12 @@ RSpec.describe Scheme, type: :model do
expect(scheme.status).to eq(:deactivated) expect(scheme.status).to eq(:deactivated)
end end
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 end

Loading…
Cancel
Save