Browse Source

Change the FormHandler back to only contain the form objects

pull/875/head
Kat 3 years ago
parent
commit
6c947c1733
  1. 13
      app/models/form_handler.rb
  2. 8
      app/models/validations/date_validations.rb
  3. 8
      config/routes.rb
  4. 10
      spec/models/form_handler_spec.rb

13
app/models/form_handler.rb

@ -7,23 +7,23 @@ class FormHandler
end
def get_form(form)
@forms[form]["form"] if @forms[form].present?
@forms[form]
end
def current_lettings_form
forms["current_lettings"]["form"]
forms["current_lettings"]
end
def current_sales_form
forms["current_sales"]["form"]
forms["current_sales"]
end
def sales_forms
sales_sections = [] # Add section classes here e.g. Form::Sales::Property::Sections::PropertyInformation
current_form = Form.new(nil, current_collection_start_year, sales_sections, "sales")
previous_form = Form.new(nil, current_collection_start_year - 1, sales_sections, "sales")
{ "current_sales" => { "form" => current_form, "type" => "sales", "start_year" => current_form.start_date.year },
"previous_sales" => { "form" => previous_form, "type" => "sales", "start_year" => previous_form.start_date.year } }
{ "current_sales" => current_form,
"previous_sales" => previous_form }
end
def lettings_forms
@ -31,10 +31,9 @@ class FormHandler
directories.each do |directory|
Dir.glob("#{directory}/*.json").each do |form_path|
form = Form.new(form_path)
lettings_form_definition = { "form" => form, "type" => "lettings", "start_year" => form.start_date.year }
form_to_set = form_name_from_start_year(form.start_date.year, "lettings")
forms[form_to_set] = lettings_form_definition if forms[form_to_set].blank?
forms[form_to_set] = form if forms[form_to_set].blank?
end
end
forms

8
app/models/validations/date_validations.rb

@ -60,19 +60,19 @@ module Validations::DateValidations
private
def first_collection_start_date
@first_collection_start_date ||= FormHandler.instance.forms.map { |_name, form| form["form"].start_date }.compact.min
@first_collection_start_date ||= FormHandler.instance.forms.map { |_name, form| form.start_date }.compact.min
end
def first_collection_end_date
@first_collection_end_date ||= FormHandler.instance.forms.map { |_name, form| form["form"].end_date }.compact.min
@first_collection_end_date ||= FormHandler.instance.forms.map { |_name, form| form.end_date }.compact.min
end
def second_collection_start_date
@second_collection_start_date ||= FormHandler.instance.forms.map { |_name, form| form["form"].start_date }.compact.max
@second_collection_start_date ||= FormHandler.instance.forms.map { |_name, form| form.start_date }.compact.max
end
def second_collection_end_date
@second_collection_end_date ||= FormHandler.instance.forms.map { |_name, form| form["form"].end_date }.compact.max
@second_collection_end_date ||= FormHandler.instance.forms.map { |_name, form| form.end_date }.compact.max
end
def date_valid?(question, record)

8
config/routes.rb

@ -93,11 +93,11 @@ Rails.application.routes.draw do
end
FormHandler.instance.lettings_forms.each do |_key, form|
form["form"].pages.map do |page|
form.pages.map do |page|
get page.id.to_s.dasherize, to: "form#show_page"
end
form["form"].subsections.map do |subsection|
form.subsections.map do |subsection|
get "#{subsection.id.to_s.dasherize}/check-answers", to: "form#check_answers"
end
end
@ -105,11 +105,11 @@ Rails.application.routes.draw do
resources :sales_logs, path: "/sales-logs" do
FormHandler.instance.sales_forms.each do |_key, form|
form["form"].pages.map do |page|
form.pages.map do |page|
get page.id.to_s.dasherize, to: "form#show_page"
end
form["form"].subsections.map do |subsection|
form.subsections.map do |subsection|
get "#{subsection.id.to_s.dasherize}/check-answers", to: "form#check_answers"
end
end

10
spec/models/form_handler_spec.rb

@ -3,7 +3,7 @@ require "rails_helper"
RSpec.describe FormHandler do
let(:form_handler) { described_class.instance }
before { Singleton.__init__(described_class) } #reload FormHandler Instance to update form definitions between runs
before { Singleton.__init__(described_class) } # reload FormHandler Instance to update form definitions between runs
context "when accessing a form in a different year" do
before do
@ -31,7 +31,7 @@ RSpec.describe FormHandler do
it "is able to load all the forms" do
all_forms = form_handler.forms
expect(all_forms.count).to be >= 1
expect(all_forms["current_sales"]["form"]).to be_a(Form)
expect(all_forms["current_sales"]).to be_a(Form)
end
end
@ -155,9 +155,9 @@ RSpec.describe FormHandler do
expect(form_handler.get_form("current_sales")).to be_a(Form)
end
it "keeps track of form type and start year" do
it "correctly sets form type and start year" do
form = form_handler.forms["current_lettings"]
expect(form["type"]).to eq("lettings")
expect(form["start_year"]).to eq(2022)
expect(form.type).to eq("lettings")
expect(form.start_date.year).to eq(2022)
end
end

Loading…
Cancel
Save