Browse Source

Start date validation

pull/299/head
baarkerlounger 3 years ago
parent
commit
95d8a029b0
  1. 11
      app/models/validations/date_validations.rb
  2. 4
      config/locales/en.yml
  3. 28
      spec/models/validations/date_validations_spec.rb

11
app/models/validations/date_validations.rb

@ -29,7 +29,11 @@ module Validations::DateValidations
end end
def validate_startdate(record) def validate_startdate(record)
date_valid?("startdate", record) return unless record.startdate && date_valid?("startdate", record)
if record.startdate < Time.zone.local(2021, 0o4, 0o1) || record.startdate > Time.zone.local(2023, 0o6, 30)
record.errors.add :startdate, I18n.t("validations.date.outside_collection_window")
end
end end
def validate_sale_completion_date(record) def validate_sale_completion_date(record)
@ -40,7 +44,10 @@ private
def date_valid?(question, record) def date_valid?(question, record)
if record[question].is_a?(ActiveSupport::TimeWithZone) && record[question].year.zero? if record[question].is_a?(ActiveSupport::TimeWithZone) && record[question].year.zero?
record.errors.add question, I18n.t("validations.date") record.errors.add question, I18n.t("validations.date.invalid_date")
false
else
true
end end
end end

4
config/locales/en.yml

@ -35,7 +35,9 @@ en:
updated: "Organisation details updated" updated: "Organisation details updated"
validations: validations:
date: "Please enter a valid date" date:
invalid_date: "Please enter a valid date"
outside_collection_window: "Date must be within the current collection windows"
postcode: "Enter a postcode in the correct format, for example AA1 1AA" postcode: "Enter a postcode in the correct format, for example AA1 1AA"
property: property:

28
spec/models/validations/date_validations_spec.rb

@ -0,0 +1,28 @@
require "rails_helper"
RSpec.describe Validations::DateValidations do
subject(:date_validator) { validator_class.new }
let(:validator_class) { Class.new { include Validations::DateValidations } }
let(:record) { FactoryBot.create(:case_log) }
describe "tenancy start date" do
it "cannot be before the first collection window start date" do
record.startdate = Time.zone.local(2020, 1, 1)
date_validator.validate_startdate(record)
expect(record.errors["startdate"]).to include(match I18n.t("validations.date.outside_collection_window"))
end
it "cannot be after the second collection window end date" do
record.startdate = Time.zone.local(2023, 7, 1)
date_validator.validate_startdate(record)
expect(record.errors["startdate"]).to include(match I18n.t("validations.date.outside_collection_window"))
end
it "must be a valid date" do
record.startdate = Time.zone.local(0, 7, 1)
date_validator.validate_startdate(record)
expect(record.errors["startdate"]).to include(match I18n.t("validations.date.invalid_date"))
end
end
end
Loading…
Cancel
Save