Browse Source

Ensure case log always returns a form

pull/302/head
baarkerlounger 3 years ago
parent
commit
7aff64d0a3
  1. 4
      app/models/case_log.rb
  2. 4
      app/models/form.rb
  3. 4
      spec/fixtures/forms/test_validator.json
  4. 15
      spec/models/case_log_spec.rb

4
app/models/case_log.rb

@ -150,11 +150,11 @@ class CaseLog < ApplicationRecord
AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at].freeze AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at].freeze
def form def form
FormHandler.instance.get_form(form_name) FormHandler.instance.get_form(form_name) || FormHandler.instance.forms.first.second
end end
def form_name def form_name
return "2021_2022" unless startdate return unless startdate
window_end_date = Time.zone.local(startdate.year, 4, 1) window_end_date = Time.zone.local(startdate.year, 4, 1)
if startdate < window_end_date if startdate < window_end_date

4
app/models/form.rb

@ -7,8 +7,8 @@ class Form
@form_definition = JSON.parse(File.open(form_path).read) @form_definition = JSON.parse(File.open(form_path).read)
@name = name @name = name
@start_date = form_definition["start_date"] @start_date = Time.iso8601(form_definition["start_date"])
@end_date = form_definition["end_date"] @end_date = Time.iso8601(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)

4
spec/fixtures/forms/test_validator.json vendored

@ -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": {
"household": { "household": {
"label": "About the household", "label": "About the household",

15
spec/models/case_log_spec.rb

@ -10,10 +10,21 @@ RSpec.describe CaseLog do
let(:case_log_year_2) { FactoryBot.build(:case_log, startdate: Time.zone.local(2023, 5, 1)) } let(:case_log_year_2) { FactoryBot.build(:case_log, startdate: Time.zone.local(2023, 5, 1)) }
it "has returns the correct form based on the start date" do it "has returns the correct form based on the start date" do
expect(case_log.form_name).to eq("2021_2022") expect(case_log.form_name).to be_nil
expect(case_log.form).to be_a(Form)
expect(case_log_2.form_name).to eq("2021_2022") expect(case_log_2.form_name).to eq("2021_2022")
expect(case_log_2.form).to be_a(Form)
expect(case_log_year_2.form_name).to eq("2023_2024") expect(case_log_year_2.form_name).to eq("2023_2024")
expect(case_log.form).to be_a(Form) expect(case_log_year_2.form).to be_a(Form)
end
context "when a date outside the collection window is passed" do
let(:case_log) { FactoryBot.build(:case_log, startdate: Time.zone.local(2015, 1, 1)) }
it "returns the first form" do
expect(case_log.form).to be_a(Form)
expect(case_log.form.start_date.year).to eq(2021)
end
end end
end end

Loading…
Cancel
Save