Browse Source

Remove name from form initializer and add an optional start year

pull/875/head
Kat 3 years ago
parent
commit
4749aacda5
  1. 10
      app/models/form.rb
  2. 7
      app/models/form_handler.rb
  3. 2
      spec/features/form/check_answers_page_spec.rb
  4. 2
      spec/features/form/conditional_questions_spec.rb
  5. 2
      spec/features/form/form_navigation_spec.rb
  6. 2
      spec/features/form/validations_spec.rb
  7. 2
      spec/helpers/conditional_questions_helper_spec.rb
  8. 2
      spec/helpers/question_attribute_helper_spec.rb
  9. 2
      spec/helpers/tasklist_helper_spec.rb
  10. 2
      spec/jobs/email_csv_job_spec.rb
  11. 2
      spec/models/form/page_spec.rb
  12. 2
      spec/models/form/question_spec.rb
  13. 2
      spec/models/form/section_spec.rb
  14. 2
      spec/models/form/subsection_spec.rb
  15. 4
      spec/models/form_spec.rb
  16. 4
      spec/models/lettings_log_spec.rb
  17. 2
      spec/models/rent_period_spec.rb
  18. 2
      spec/models/validations/financial_validations_spec.rb
  19. 2
      spec/models/validations/household_validations_spec.rb
  20. 2
      spec/models/validations/shared_validations_spec.rb
  21. 2
      spec/requests/form_controller_spec.rb
  22. 2
      spec/requests/lettings_logs_controller_spec.rb
  23. 4
      spec/services/exports/lettings_log_export_service_spec.rb
  24. 2
      spec/services/imports/lettings_logs_field_import_service_spec.rb
  25. 4
      spec/services/imports/lettings_logs_import_service_spec.rb

10
app/models/form.rb

@ -3,9 +3,11 @@ class Form
:start_date, :end_date, :type, :name, :setup_definition,
:setup_sections, :form_sections
include Form::Setup
def initialize(form_path, name, sections_in_form = [], type = "lettings")
if type == "sales"
@name = name
@name = "#{start_year}_#{start_year + 1}_sales"
@setup_sections = [Form::Sales::Sections::Setup.new(nil, nil, self)]
@form_sections = sections_in_form.map { |sec| sec.new(nil, nil, self) }
@type = "sales"
@ -13,8 +15,8 @@ class Form
@subsections = sections.flat_map(&:subsections)
@pages = subsections.flat_map(&:pages)
@questions = pages.flat_map(&:questions)
@start_date = Time.zone.local(name[0..3], 4, 1)
@end_date = Time.zone.local(start_date.year + 1, 7, 1)
@start_date = Time.zone.local(start_year, 4, 1)
@end_date = Time.zone.local(start_year + 1, 7, 1)
@form_definition = {
"form_type" => type,
"start_date" => start_date,
@ -24,7 +26,6 @@ class Form
else
raise "No form definition file exists for given year".freeze unless File.exist?(form_path)
@name = name
@setup_sections = [Form::Lettings::Sections::Setup.new(nil, nil, self)]
@form_definition = JSON.parse(File.open(form_path).read)
@form_sections = form_definition["sections"].map { |id, s| Form::Section.new(id, s, self) }
@ -35,6 +36,7 @@ class Form
@questions = pages.flat_map(&:questions)
@start_date = Time.iso8601(form_definition["start_date"])
@end_date = Time.iso8601(form_definition["end_date"])
@name = "#{start_date.year}_#{end_date.year}"
end
end

7
app/models/form_handler.rb

@ -20,8 +20,8 @@ class FormHandler
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}_#{current_collection_start_year + 1}_sales", sales_sections, "sales")
previous_form = Form.new(nil, "#{current_collection_start_year - 1}_#{current_collection_start_year}_sales", sales_sections, "sales")
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 } }
end
@ -30,8 +30,7 @@ class FormHandler
forms = {}
directories.each do |directory|
Dir.glob("#{directory}/*.json").each do |form_path|
form_name = File.basename(form_path, ".json")
form = Form.new(form_path, form_name)
form = Form.new(form_path)
lettings_form_definition = { "form" => form, "type" => "lettings", "start_year" => form.start_date.year }
form_mappings = { 0 => "current_lettings", 1 => "previous_lettings", -1 => "next_lettings" }

2
spec/features/form/check_answers_page_spec.rb

@ -39,7 +39,7 @@ RSpec.describe "Form Check Answers Page" do
)
end
let(:id) { lettings_log.id }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json", "2021_2022") }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
sign_in user

2
spec/features/form/conditional_questions_spec.rb

@ -13,7 +13,7 @@ RSpec.describe "Form Conditional Questions" do
)
end
let(:id) { lettings_log.id }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json", "2021_2022") }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
sign_in user

2
spec/features/form/form_navigation_spec.rb

@ -32,7 +32,7 @@ RSpec.describe "Form Navigation" do
hhmemb: { type: "numeric", answer: 1, path: "household-number-of-members" },
}
end
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json", "2021_2022") }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
sign_in user

2
spec/features/form/validations_spec.rb

@ -2,7 +2,7 @@ require "rails_helper"
require_relative "helpers"
RSpec.describe "validations" do
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json", "2021_2022") }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
let(:user) { FactoryBot.create(:user) }
let(:lettings_log) do
FactoryBot.create(

2
spec/helpers/conditional_questions_helper_spec.rb

@ -3,7 +3,7 @@ require "rails_helper"
RSpec.describe ConditionalQuestionsHelper do
let(:lettings_log) { FactoryBot.build(:lettings_log) }
let(:page) { lettings_log.form.get_page("armed_forces") }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json", "2021_2022") }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)

2
spec/helpers/question_attribute_helper_spec.rb

@ -4,7 +4,7 @@ RSpec.describe QuestionAttributeHelper do
let(:lettings_log) { FactoryBot.build(:lettings_log) }
let(:form) { lettings_log.form }
let(:questions) { form.get_page("rent").questions }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json", "2021_2022") }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)

2
spec/helpers/tasklist_helper_spec.rb

@ -3,7 +3,7 @@ require "rails_helper"
RSpec.describe TasklistHelper do
let(:empty_lettings_log) { FactoryBot.create(:lettings_log) }
let(:lettings_log) { FactoryBot.create(:lettings_log, :in_progress, needstype: 1) }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json", "2021_2022") }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)

2
spec/jobs/email_csv_job_spec.rb

@ -9,7 +9,7 @@ describe EmailCsvJob do
let(:user) { FactoryBot.create(:user) }
let(:organisation) { user.organisation }
let(:other_organisation) { FactoryBot.create(:organisation) }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json", "2021_2022") }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)

2
spec/models/form/page_spec.rb

@ -14,7 +14,7 @@ RSpec.describe Form::Page, type: :model do
let(:subsection) { Form::Subsection.new(subsection_id, subsection_definition, section) }
let(:page_id) { "net_income" }
let(:page_definition) { subsection_definition["pages"][page_id] }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json", "2021_2022") }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)

2
spec/models/form/question_spec.rb

@ -16,7 +16,7 @@ RSpec.describe Form::Question, type: :model do
let(:page) { Form::Page.new(page_id, page_definition, subsection) }
let(:question_id) { "earnings" }
let(:question_definition) { page_definition["questions"][question_id] }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json", "2021_2022") }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)

2
spec/models/form/section_spec.rb

@ -7,7 +7,7 @@ RSpec.describe Form::Section, type: :model do
let(:form) { lettings_log.form }
let(:section_id) { "household" }
let(:section_definition) { form.form_definition["sections"][section_id] }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json", "2021_2022") }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)

2
spec/models/form/subsection_spec.rb

@ -11,7 +11,7 @@ RSpec.describe Form::Subsection, type: :model do
let(:section) { Form::Section.new(section_id, section_definition, form) }
let(:subsection_id) { "household_characteristics" }
let(:subsection_definition) { section_definition["subsections"][subsection_id] }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json", "2021_2022") }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
RequestHelper.stub_http_requests

4
spec/models/form_spec.rb

@ -209,9 +209,9 @@ RSpec.describe Form, type: :model do
describe "when creating a sales log" do
it "creates a valid sales form" do
sections = []
form = described_class.new(nil, "2022_23_sales", sections, "sales")
form = described_class.new(nil, 2022, sections, "sales")
expect(form.type).to eq("sales")
expect(form.name).to eq("2022_23_sales")
expect(form.name).to eq("2022_2023_sales")
expect(form.setup_sections.count).to eq(1)
expect(form.setup_sections[0].class).to eq(Form::Sales::Sections::Setup)
expect(form.sections.count).to eq(1)

4
spec/models/lettings_log_spec.rb

@ -4,7 +4,7 @@ RSpec.describe LettingsLog do
let(:owning_organisation) { FactoryBot.create(:organisation) }
let(:different_managing_organisation) { FactoryBot.create(:organisation) }
let(:created_by_user) { FactoryBot.create(:user) }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json", "2021_2022") }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)
@ -1652,7 +1652,7 @@ RSpec.describe LettingsLog do
end
context "when a lettings log is a supported housing log" do
let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json", "2021_2022") }
let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json") }
before do
lettings_log.needstype = 2

2
spec/models/rent_period_spec.rb

@ -2,7 +2,7 @@ require "rails_helper"
RSpec.describe RentPeriod, type: :model do
describe "rent period mapping" do
let(:form) { Form.new("spec/fixtures/forms/2021_2022.json", "2021_2022") }
let(:form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(form)

2
spec/models/validations/financial_validations_spec.rb

@ -5,7 +5,7 @@ RSpec.describe Validations::FinancialValidations do
let(:validator_class) { Class.new { include Validations::FinancialValidations } }
let(:record) { FactoryBot.create(:lettings_log) }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json", "2021_2022") }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)

2
spec/models/validations/household_validations_spec.rb

@ -5,7 +5,7 @@ RSpec.describe Validations::HouseholdValidations do
let(:validator_class) { Class.new { include Validations::HouseholdValidations } }
let(:record) { FactoryBot.create(:lettings_log) }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json", "2021_2022") }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)

2
spec/models/validations/shared_validations_spec.rb

@ -5,7 +5,7 @@ RSpec.describe Validations::SharedValidations do
let(:validator_class) { Class.new { include Validations::SharedValidations } }
let(:record) { FactoryBot.create(:lettings_log) }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json", "2021_2022") }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
describe "numeric min max validations" do
before do

2
spec/requests/form_controller_spec.rb

@ -31,7 +31,7 @@ RSpec.describe FormController, type: :request do
)
end
let(:headers) { { "Accept" => "text/html" } }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json", "2021_2022") }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)

2
spec/requests/lettings_logs_controller_spec.rb

@ -18,7 +18,7 @@ RSpec.describe LettingsLogsController, type: :request do
"Authorization" => basic_credentials,
}
end
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json", "2021_2022") }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
allow(ENV).to receive(:[])

4
spec/services/exports/lettings_log_export_service_spec.rb

@ -8,8 +8,8 @@ RSpec.describe Exports::LettingsLogExportService do
let(:xml_export_file) { File.open("spec/fixtures/exports/general_needs_log.xml", "r:UTF-8") }
let(:local_manifest_file) { File.open("spec/fixtures/exports/manifest.xml", "r:UTF-8") }
let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json", "2021_2022") }
let(:real_2022_2023_form) { Form.new("config/forms/2022_2023.json", "2022_2023") }
let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json") }
let(:real_2022_2023_form) { Form.new("config/forms/2022_2023.json") }
let(:expected_master_manifest_filename) { "Manifest_2022_05_01_0001.csv" }
let(:expected_master_manifest_rerun) { "Manifest_2022_05_01_0002.csv" }

2
spec/services/imports/lettings_logs_field_import_service_spec.rb

@ -6,7 +6,7 @@ RSpec.describe Imports::LettingsLogsFieldImportService do
let(:storage_service) { instance_double(Storage::S3Service) }
let(:logger) { instance_double(ActiveSupport::Logger) }
let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json", "2021_2022") }
let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json") }
let(:fixture_directory) { "spec/fixtures/imports/logs" }
let(:lettings_log_id) { "0ead17cb-1668-442d-898c-0d52879ff592" }

4
spec/services/imports/lettings_logs_import_service_spec.rb

@ -6,8 +6,8 @@ RSpec.describe Imports::LettingsLogsImportService do
let(:storage_service) { instance_double(Storage::S3Service) }
let(:logger) { instance_double(ActiveSupport::Logger) }
let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json", "2021_2022") }
let(:real_2022_2023_form) { Form.new("config/forms/2022_2023.json", "2022_2023") }
let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json") }
let(:real_2022_2023_form) { Form.new("config/forms/2022_2023.json") }
let(:fixture_directory) { "spec/fixtures/imports/logs" }
let(:organisation) { FactoryBot.create(:organisation, old_visible_id: "1", provider_type: "PRP") }

Loading…
Cancel
Save