Browse Source

Refactor log setup into code

pull/672/head
baarkerlounger 3 years ago
parent
commit
e334728886
  1. 8
      app/models/form.rb
  2. 18
      app/models/form/page.rb
  3. 44
      app/models/form/question.rb
  4. 8
      app/models/form/section.rb
  5. 18
      app/models/form/setup/pages/needs_type.rb
  6. 16
      app/models/form/setup/pages/property_reference.rb
  7. 16
      app/models/form/setup/pages/renewal.rb
  8. 19
      app/models/form/setup/pages/rent_type.rb
  9. 15
      app/models/form/setup/pages/tenancy_start_date.rb
  10. 16
      app/models/form/setup/pages/tenant_code.rb
  11. 10
      app/models/form/setup/questions/irproduct_other.rb
  12. 17
      app/models/form/setup/questions/needs_type.rb
  13. 12
      app/models/form/setup/questions/property_reference.rb
  14. 17
      app/models/form/setup/questions/renewal.rb
  15. 22
      app/models/form/setup/questions/rent_type.rb
  16. 10
      app/models/form/setup/questions/tenancy_start_date.rb
  17. 12
      app/models/form/setup/questions/tenant_code.rb
  18. 10
      app/models/form/setup/sections/setup.rb
  19. 20
      app/models/form/setup/subsections/setup.rb
  20. 8
      app/models/form/subsection.rb
  21. 8
      app/models/form_handler.rb
  22. 138
      config/forms/setup/log_setup.json
  23. 71
      spec/fixtures/forms/setup/log_setup.json
  24. 2
      spec/models/form_handler_spec.rb
  25. 3
      spec/models/rent_period_spec.rb
  26. 4
      spec/requests/case_logs_controller_spec.rb
  27. 3
      spec/services/imports/case_logs_field_import_service_spec.rb
  28. 5
      spec/services/imports/case_logs_import_service_spec.rb
  29. 9
      yarn.lock

8
app/models/form.rb

@ -3,13 +3,13 @@ class Form
:start_date, :end_date, :type, :name, :setup_definition,
:setup_sections, :form_sections
def initialize(form_path, name, setup_path)
raise "No setup definition file exists for given path".freeze unless File.exist?(setup_path)
include Form::Setup
def initialize(form_path, name)
raise "No form definition file exists for given year".freeze unless File.exist?(form_path)
@name = name
@setup_definition = JSON.parse(File.open(setup_path).read)
@setup_sections = setup_definition["sections"].map { |id, s| Form::Section.new(id, s, self) } || []
@setup_sections = [Form::Setup::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) }
@type = form_definition["form_type"]

18
app/models/form/page.rb

@ -4,15 +4,17 @@ class Form::Page
def initialize(id, hsh, subsection)
@id = id
@header = hsh["header"]
@description = hsh["description"]
@questions = hsh["questions"].map { |q_id, q| Form::Question.new(q_id, q, self) }
@depends_on = hsh["depends_on"]
@derived = hsh["derived"]
@title_text = hsh["title_text"]
@informative_text = hsh["informative_text"]
@hide_subsection_label = hsh["hide_subsection_label"]
@subsection = subsection
if hsh
@header = hsh["header"]
@description = hsh["description"]
@questions = hsh["questions"].map { |q_id, q| Form::Question.new(q_id, q, self) }
@depends_on = hsh["depends_on"]
@derived = hsh["derived"]
@title_text = hsh["title_text"]
@informative_text = hsh["informative_text"]
@hide_subsection_label = hsh["hide_subsection_label"]
end
end
delegate :form, to: :subsection

44
app/models/form/question.rb

@ -7,28 +7,30 @@ class Form::Question
def initialize(id, hsh, page)
@id = id
@check_answer_label = hsh["check_answer_label"]
@header = hsh["header"]
@guidance_partial = hsh["guidance_partial"]
@hint_text = hsh["hint_text"]
@type = hsh["type"]
@min = hsh["min"]
@max = hsh["max"]
@step = hsh["step"]
@width = hsh["width"]
@fields_to_add = hsh["fields-to-add"]
@result_field = hsh["result-field"]
@readonly = hsh["readonly"]
@answer_options = hsh["answer_options"]
@conditional_for = hsh["conditional_for"]
@inferred_answers = hsh["inferred_answers"]
@inferred_check_answers_value = hsh["inferred_check_answers_value"]
@hidden_in_check_answers = hsh["hidden_in_check_answers"]
@prefix = hsh["prefix"]
@suffix = hsh["suffix"]
@requires_js = hsh["requires_js"]
@fields_added = hsh["fields_added"]
@page = page
if hsh
@check_answer_label = hsh["check_answer_label"]
@header = hsh["header"]
@guidance_partial = hsh["guidance_partial"]
@hint_text = hsh["hint_text"]
@type = hsh["type"]
@min = hsh["min"]
@max = hsh["max"]
@step = hsh["step"]
@width = hsh["width"]
@fields_to_add = hsh["fields-to-add"]
@result_field = hsh["result-field"]
@readonly = hsh["readonly"]
@answer_options = hsh["answer_options"]
@conditional_for = hsh["conditional_for"]
@inferred_answers = hsh["inferred_answers"]
@inferred_check_answers_value = hsh["inferred_check_answers_value"]
@hidden_in_check_answers = hsh["hidden_in_check_answers"]
@prefix = hsh["prefix"]
@suffix = hsh["suffix"]
@requires_js = hsh["requires_js"]
@fields_added = hsh["fields_added"]
end
end
delegate :subsection, to: :page

8
app/models/form/section.rb

@ -3,9 +3,11 @@ class Form::Section
def initialize(id, hsh, form)
@id = id
@label = hsh["label"]
@description = hsh["description"]
@form = form
@subsections = hsh["subsections"].map { |s_id, s| Form::Subsection.new(s_id, s, self) }
if hsh
@label = hsh["label"]
@description = hsh["description"]
@subsections = hsh["subsections"].map { |s_id, s| Form::Subsection.new(s_id, s, self) }
end
end
end

18
app/models/form/setup/pages/needs_type.rb

@ -0,0 +1,18 @@
class Form::Setup::Pages::NeedsType < ::Form::Page
def initialize(id, hsh, subsection)
super
@id = "needs_type"
@header = ""
@description = ""
@questions = questions
@depends_on = [{ "supported_housing_schemes_enabled?" => true }]
@derived = true
@subsection = subsection
end
def questions
[
Form::Setup::Questions::NeedsType.new(nil, nil, self),
]
end
end

16
app/models/form/setup/pages/property_reference.rb

@ -0,0 +1,16 @@
class Form::Setup::Pages::PropertyReference < ::Form::Page
def initialize(id, hsh, subsection)
super
@id = "property_reference"
@header = ""
@description = ""
@questions = questions
@subsection = subsection
end
def questions
[
Form::Setup::Questions::PropertyReference.new(nil, nil, self),
]
end
end

16
app/models/form/setup/pages/renewal.rb

@ -0,0 +1,16 @@
class Form::Setup::Pages::Renewal < ::Form::Page
def initialize(id, hsh, subsection)
super
@id = "renewal"
@header = ""
@description = ""
@questions = questions
@subsection = subsection
end
def questions
[
Form::Setup::Questions::Renewal.new(nil, nil, self),
]
end
end

19
app/models/form/setup/pages/rent_type.rb

@ -0,0 +1,19 @@
class Form::Setup::Pages::RentType < ::Form::Page
def initialize(id, hsh, subsection)
super
@id = "rent_type"
@header = ""
@description = ""
@questions = questions
@depends_on = [{ "supported_housing_schemes_enabled?" => true }]
@derived = true
@subsection = subsection
end
def questions
[
Form::Setup::Questions::RentType.new(nil, nil, self),
Form::Setup::Questions::IrproductOther.new(nil, nil, self),
]
end
end

15
app/models/form/setup/pages/tenancy_start_date.rb

@ -0,0 +1,15 @@
class Form::Setup::Pages::TenancyStartDate < ::Form::Page
def initialize(id, hsh, subsection)
super
@id = "tenancy_start_date"
@description = ""
@questions = questions
@subsection = subsection
end
def questions
[
Form::Setup::Questions::TenancyStartDate.new(nil, nil, self),
]
end
end

16
app/models/form/setup/pages/tenant_code.rb

@ -0,0 +1,16 @@
class Form::Setup::Pages::TenantCode < ::Form::Page
def initialize(id, hsh, subsection)
super
@id = "tenant_code"
@header = ""
@description = ""
@questions = questions
@subsection = subsection
end
def questions
[
Form::Setup::Questions::TenantCode.new(nil, nil, self),
]
end
end

10
app/models/form/setup/questions/irproduct_other.rb

@ -0,0 +1,10 @@
class Form::Setup::Questions::IrproductOther < ::Form::Question
def initialize(id, hsh, page)
super
@id = "irproduct_other"
@check_answer_label = "Product name"
@header = "Name of rent product"
@type = "text"
@page = page
end
end

17
app/models/form/setup/questions/needs_type.rb

@ -0,0 +1,17 @@
class Form::Setup::Questions::NeedsType < ::Form::Question
def initialize(id, hsh, page)
super
@id = "needstype"
@check_answer_label = "Needs type"
@header = "What is the needs type?"
@hint_text = ""
@type = "radio"
@answer_options = ANSWER_OPTIONS
@page = page
end
ANSWER_OPTIONS = {
"1" => { "value" => "General needs" },
"2" => { "value" => "Supported housing" },
}.freeze
end

12
app/models/form/setup/questions/property_reference.rb

@ -0,0 +1,12 @@
class Form::Setup::Questions::PropertyReference < ::Form::Question
def initialize(id, hsh, page)
super
@id = "propcode"
@check_answer_label = "Property reference"
@header = "What is the property reference?"
@hint_text = "This is how you usually refer to this property on your own systems."
@type = "text"
@width = 10
@page = page
end
end

17
app/models/form/setup/questions/renewal.rb

@ -0,0 +1,17 @@
class Form::Setup::Questions::Renewal < ::Form::Question
def initialize(id, hsh, page)
super
@id = "renewal"
@check_answer_label = "Property renewal"
@header = "Is this letting a renewal?"
@hint_text = ""
@type = "radio"
@answer_options = ANSWER_OPTIONS
@page = page
end
ANSWER_OPTIONS = {
"1" => { "value" => "Yes" },
"0" => { "value" => "No" },
}.freeze
end

22
app/models/form/setup/questions/rent_type.rb

@ -0,0 +1,22 @@
class Form::Setup::Questions::RentType < ::Form::Question
def initialize(id, hsh, page)
super
@id = "rent_type"
@check_answer_label = "Rent type"
@header = "What is the rent type?"
@hint_text = ""
@type = "radio"
@answer_options = ANSWER_OPTIONS
@conditional_for = { "irproduct_other" => [5] }
@page = page
end
ANSWER_OPTIONS = {
"1" => { "value" => "Affordable Rent" },
"2" => { "value" => "London Affordable Rent" },
"4" => { "value" => "London Living Rent" },
"3" => { "value" => "Rent to Buy" },
"0" => { "value" => "Social Rent" },
"5" => { "value" => "Other intermediate rent product" },
}.freeze
end

10
app/models/form/setup/questions/tenancy_start_date.rb

@ -0,0 +1,10 @@
class Form::Setup::Questions::TenancyStartDate < ::Form::Question
def initialize(id, hsh, page)
super
@id = "startdate"
@check_answer_label = "Tenancy start date"
@header = "What is the tenancy start date?"
@type = "date"
@page = page
end
end

12
app/models/form/setup/questions/tenant_code.rb

@ -0,0 +1,12 @@
class Form::Setup::Questions::TenantCode < ::Form::Question
def initialize(id, hsh, page)
super
@id = "tenant_code"
@check_answer_label = "Tenant code"
@header = "What is the tenant code?"
@hint_text = "This is how you usually refer to this tenancy on your own systems."
@type = "text"
@width = 10
@page = page
end
end

10
app/models/form/setup/sections/setup.rb

@ -0,0 +1,10 @@
class Form::Sections::Setup < ::Form::Section
def initialize(id, hsh, form)
super
@id = "setup"
@label = "Before you start"
@description = ""
@form = form
@subsections = [Form::Setup::Subsections::Setup.new(nil, nil, self)]
end
end

20
app/models/form/setup/subsections/setup.rb

@ -0,0 +1,20 @@
class Form::Subsections::Setup < ::Form::Subsection
def initialize(id, hsh, section)
super
@id = "setup"
@label = "Set up this lettings log"
@pages = [pages]
@section = section
end
def pages
[
Form::Setup::Pages::NeedsType.new(nil, nil, self),
Form::Setup::Pages::Renewal.new(nil, nil, self),
Form::Setup::Pages::TenancyStartDate.new(nil, nil, self),
Form::Setup::Pages::RentType.new(nil, nil, self),
Form::Setup::Pages::TenantCode.new(nil, nil, self),
Form::Setup::Pages::PropertyReference.new(nil, nil, self),
]
end
end

8
app/models/form/subsection.rb

@ -3,10 +3,12 @@ class Form::Subsection
def initialize(id, hsh, section)
@id = id
@label = hsh["label"]
@depends_on = hsh["depends_on"]
@pages = hsh["pages"].map { |s_id, p| Form::Page.new(s_id, p, self) }
@section = section
if hsh
@label = hsh["label"]
@depends_on = hsh["depends_on"]
@pages = hsh["pages"].map { |s_id, p| Form::Page.new(s_id, p, self) }
end
end
delegate :form, to: :section

8
app/models/form_handler.rb

@ -21,18 +21,12 @@ private
directories.each do |directory|
Dir.glob("#{directory}/*.json").each do |form_path|
form_name = File.basename(form_path, ".json")
forms[form_name] = Form.new(form_path, form_name, setup_path)
forms[form_name] = Form.new(form_path, form_name)
end
end
forms
end
def setup_path
return "spec/fixtures/forms/setup/log_setup.json" if Rails.env.test?
"config/forms/setup/log_setup.json"
end
def directories
Rails.env.test? ? ["spec/fixtures/forms"] : ["config/forms"]
end

138
config/forms/setup/log_setup.json

@ -1,138 +0,0 @@
{
"form_type": "setup",
"sections": {
"setup": {
"label": "Before you start",
"subsections": {
"setup": {
"label": "Set up this lettings log",
"pages": {
"needs_type": {
"header": "",
"description": "",
"questions": {
"needstype": {
"check_answer_label": "Needs type",
"header": "What is the needs type?",
"hint_text": "",
"type": "radio",
"answer_options": {
"1": {
"value": "General needs"
},
"2": {
"value": "Supported housing"
}
}
}
},
"derived": true,
"depends_on": [
{
"supported_housing_schemes_enabled?" : true
}
]
},
"renewal": {
"header": "",
"description": "",
"questions": {
"renewal": {
"check_answer_label": "Property renewal",
"header": "Is this letting a renewal?",
"hint_text": "A renewal is a letting to the same tenant in the same property.",
"type": "radio",
"answer_options": {
"1": {
"value": "Yes"
},
"0": {
"value": "No"
}
}
}
}
},
"tenancy_start_date": {
"header": "",
"description": "",
"questions": {
"startdate": {
"check_answer_label": "Tenancy start date",
"header": "What is the tenancy start date?",
"type": "date"
}
}
},
"rent_type": {
"header": "",
"description": "",
"questions": {
"rent_type": {
"check_answer_label": "Rent type",
"header": "What is the rent type?",
"hint_text": "",
"type": "radio",
"answer_options": {
"1": {
"value": "Affordable Rent"
},
"2": {
"value": "London Affordable Rent"
},
"4": {
"value": "London Living Rent"
},
"3": {
"value": "Rent to Buy"
},
"0": {
"value": "Social Rent"
},
"5": {
"value": "Other intermediate rent product"
}
},
"conditional_for": {
"irproduct_other": [5]
}
},
"irproduct_other": {
"check_answer_label": "Product name",
"header": "Name of rent product",
"type": "text"
}
}
},
"tenant_code": {
"header": "",
"description": "",
"questions": {
"tenant_code": {
"check_answer_label": "Tenant code",
"header": "What is the tenant code?",
"hint_text": "This is how you usually refer to this tenancy on your own systems.",
"type": "text",
"width": 10
}
}
},
"property_reference": {
"header": "",
"description": "",
"questions": {
"propcode": {
"check_answer_label": "Property reference",
"header": "What is the property reference?",
"hint_text": "This is how you usually refer to this property on your own systems.",
"type": "text",
"width": 10
}
}
}
}
}
}
}
}
}

71
spec/fixtures/forms/setup/log_setup.json vendored

@ -1,71 +0,0 @@
{
"form_type": "setup",
"sections": {
"setup": {
"label": "Before you start",
"subsections": {
"setup": {
"label": "Set up this lettings log",
"pages": {
"renewal": {
"header": "",
"description": "",
"questions": {
"renewal": {
"check_answer_label": "Property renewal",
"header": "Is this a renewal to the same tenant in the same property?",
"hint_text": "",
"type": "radio",
"answer_options": {
"1": "Yes",
"0": "No"
}
}
}
},
"startdate": {
"header": "",
"description": "",
"questions": {
"startdate": {
"check_answer_label": "Tenancy start date",
"header": "What is the tenancy start date?",
"hint_text": "For example, 27 3 2007",
"type": "date"
}
}
},
"about_this_letting": {
"header": "Tell us about this letting",
"description": "",
"questions": {
"rent_type": {
"check_answer_label": "Rent type",
"header": "What is the rent type?",
"hint_text": "",
"type": "radio",
"answer_options": {
"0": "Social rent",
"1": "Affordable rent",
"2": "London Affordable rent",
"3": "Rent to buy",
"4": "London living rent",
"5": "Other intermediate rent product"
},
"conditional_for": {
"intermediate_rent_product_name": [5]
}
},
"intermediate_rent_product_name": {
"check_answer_label": "Product name",
"header": "What is intermediate rent product name?",
"type": "text"
}
}
}
}
}
}
}
}
}

2
spec/models/form_handler_spec.rb

@ -32,7 +32,7 @@ RSpec.describe FormHandler do
it "loads the form once at boot time" do
form_handler = described_class.instance
expect(Form).not_to receive(:new).with(:any, test_form_name, :any)
expect(Form).not_to receive(:new).with(:any, test_form_name)
expect(form_handler.get_form(test_form_name)).to be_a(Form)
end
end

3
spec/models/rent_period_spec.rb

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

4
spec/requests/case_logs_controller_spec.rb

@ -623,7 +623,7 @@ RSpec.describe CaseLogsController, type: :request do
end
it "displays a section status for a case log" do
assert_select ".govuk-tag", text: /Not started/, count: 8
assert_select ".govuk-tag", text: /Not started/, count: 7
assert_select ".govuk-tag", text: /In progress/, count: 1
assert_select ".govuk-tag", text: /Completed/, count: 0
assert_select ".govuk-tag", text: /Cannot start yet/, count: 1
@ -646,7 +646,7 @@ RSpec.describe CaseLogsController, type: :request do
end
it "displays a section status for a case log" do
assert_select ".govuk-tag", text: /Not started/, count: 8
assert_select ".govuk-tag", text: /Not started/, count: 7
assert_select ".govuk-tag", text: /Completed/, count: 1
assert_select ".govuk-tag", text: /Cannot start yet/, count: 1
end

3
spec/services/imports/case_logs_field_import_service_spec.rb

@ -6,8 +6,7 @@ RSpec.describe Imports::CaseLogsFieldImportService do
let(:storage_service) { instance_double(StorageService) }
let(:logger) { instance_double(ActiveSupport::Logger) }
let(:real_setup_path) { "config/forms/setup/log_setup.json" }
let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json", "2021_2022", real_setup_path) }
let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json", "2021_2022") }
let(:fixture_directory) { "spec/fixtures/softwire_imports/case_logs" }
let(:case_log_id) { "0ead17cb-1668-442d-898c-0d52879ff592" }

5
spec/services/imports/case_logs_import_service_spec.rb

@ -6,9 +6,8 @@ RSpec.describe Imports::CaseLogsImportService do
let(:storage_service) { instance_double(StorageService) }
let(:logger) { instance_double(ActiveSupport::Logger) }
let(:real_setup_path) { "config/forms/setup/log_setup.json" }
let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json", "2021_2022", real_setup_path) }
let(:real_2022_2023_form) { Form.new("config/forms/2022_2023.json", "2022_2023", real_setup_path) }
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(:fixture_directory) { "spec/fixtures/softwire_imports/case_logs" }
def open_file(directory, filename)

9
yarn.lock

@ -1489,7 +1489,7 @@ braces@^3.0.2, braces@~3.0.2:
dependencies:
fill-range "^7.0.1"
browserslist@^4.14.5, browserslist@^4.20.2, browserslist@^4.20.3:
browserslist@^4.14.5, browserslist@^4.20.2, browserslist@^4.20.4:
version "4.20.4"
resolved "https://registry.yarnpkg.com/browserslist/-/browserslist-4.20.4.tgz#98096c9042af689ee1e0271333dbc564b8ce4477"
integrity sha512-ok1d+1WpnU24XYN7oC3QWgTyMhY/avPJ/r9T00xxvUOIparA/gc+UPUMaod3i+G6s+nI2nUb9xZ5k794uIwShw==
@ -2402,12 +2402,7 @@ globjoin@^0.1.4:
resolved "https://registry.yarnpkg.com/globjoin/-/globjoin-0.1.4.tgz#2f4494ac8919e3767c5cbb691e9f463324285d43"
integrity sha512-xYfnw62CKG8nLkZBfWbhWwDw02CHty86jfPcc2cr3ZfeuK9ysoVPPEUxf21bAD/rWAgk52SuBrLJlefNy8mvFg==
govuk-frontend@4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/govuk-frontend/-/govuk-frontend-4.0.1.tgz#bceff58ecb399272cba32bd2b357fe16198e3249"
integrity sha512-X+B88mqYHoxAz0ID87Uxo3oHqdKBRnNHd3Cz8+u8nvQUAsrEzROFLK+t7sAu7e+fKqCCrJyIgx6Cmr6dIGnohQ==
govuk-frontend@^4.0.1:
govuk-frontend@4.0.1, govuk-frontend@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/govuk-frontend/-/govuk-frontend-4.0.1.tgz#bceff58ecb399272cba32bd2b357fe16198e3249"
integrity sha512-X+B88mqYHoxAz0ID87Uxo3oHqdKBRnNHd3Cz8+u8nvQUAsrEzROFLK+t7sAu7e+fKqCCrJyIgx6Cmr6dIGnohQ==

Loading…
Cancel
Save