Browse Source

Derive collection windows from form definitions

pull/299/head
baarkerlounger 3 years ago
parent
commit
bcab0917b8
  1. 6
      app/models/form.rb
  2. 10
      app/models/validations/date_validations.rb
  3. 4
      config/forms/2021_2022.json
  4. 2
      spec/fixtures/forms/2021_2022.json
  5. 2
      spec/fixtures/forms/2022_2023.json
  6. 2
      spec/models/validations/date_validations_spec.rb

6
app/models/form.rb

@ -1,14 +1,14 @@
class Form class Form
attr_reader :form_definition, :sections, :subsections, :pages, :questions, attr_reader :form_definition, :sections, :subsections, :pages, :questions,
:start_year, :end_year, :type, :name :start_date, :end_date, :type, :name
def initialize(form_path, name) def initialize(form_path, name)
raise "No form definition file exists for given year".freeze unless File.exist?(form_path) raise "No form definition file exists for given year".freeze unless File.exist?(form_path)
@form_definition = JSON.parse(File.open(form_path).read) @form_definition = JSON.parse(File.open(form_path).read)
@name = name @name = name
@start_year = form_definition["start_year"] @start_date = form_definition["start_date"]
@end_year = form_definition["end_year"] @end_date = form_definition["end_date"]
@type = form_definition["form_type"] @type = form_definition["form_type"]
@sections = form_definition["sections"].map { |id, s| Form::Section.new(id, s, self) } @sections = form_definition["sections"].map { |id, s| Form::Section.new(id, s, self) }
@subsections = sections.flat_map(&:subsections) @subsections = sections.flat_map(&:subsections)

10
app/models/validations/date_validations.rb

@ -31,7 +31,7 @@ module Validations::DateValidations
def validate_startdate(record) def validate_startdate(record)
return unless record.startdate && 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) if record.startdate < first_collection_start_date || record.startdate > second_collection_end_date
record.errors.add :startdate, I18n.t("validations.date.outside_collection_window") record.errors.add :startdate, I18n.t("validations.date.outside_collection_window")
end end
end end
@ -42,6 +42,14 @@ module Validations::DateValidations
private private
def first_collection_start_date
@first_collection_start_date ||= FormHandler.instance.forms.map { |form| form.second.start_date }.compact.min
end
def second_collection_end_date
@second_collection_end_date ||= FormHandler.instance.forms.map { |form| form.second.end_date }.compact.max
end
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.invalid_date") record.errors.add question, I18n.t("validations.date.invalid_date")

4
config/forms/2021_2022.json

@ -1,7 +1,7 @@
{ {
"form_type": "lettings", "form_type": "lettings",
"start_year": 2021, "start_date": "2021-04-01T00:00:00.000+01:00",
"end_year": 2022, "end_date": "2022-07-01T00:00:00.000+01:00",
"sections": { "sections": {
"setup": { "setup": {
"label": "Before you start", "label": "Before you start",

2
spec/fixtures/forms/2021_2022.json vendored

@ -1,5 +1,7 @@
{ {
"form_type": "lettings", "form_type": "lettings",
"start_date": "2021-04-01T00:00:00.000+01:00",
"end_date": "2022-07-01T00:00:00.000+01:00",
"sections": { "sections": {
"household": { "household": {
"label": "About the household", "label": "About the household",

2
spec/fixtures/forms/2022_2023.json vendored

@ -1,5 +1,7 @@
{ {
"form_type": "lettings", "form_type": "lettings",
"start_date": "2022-04-01T00:00:00.000+01:00",
"end_date": "2023-07-01T00:00:00.000+01:00",
"sections": { "sections": {
"setup": { "setup": {
"label": "Before you start", "label": "Before you start",

2
spec/models/validations/date_validations_spec.rb

@ -14,7 +14,7 @@ RSpec.describe Validations::DateValidations do
end end
it "cannot be after the second collection window end date" do it "cannot be after the second collection window end date" do
record.startdate = Time.zone.local(2023, 7, 1) record.startdate = Time.zone.local(2023, 7, 1, 6)
date_validator.validate_startdate(record) date_validator.validate_startdate(record)
expect(record.errors["startdate"]).to include(match I18n.t("validations.date.outside_collection_window")) expect(record.errors["startdate"]).to include(match I18n.t("validations.date.outside_collection_window"))
end end

Loading…
Cancel
Save