From 53d525670c44829e66b8d9160969f73a622e4321 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Thu, 23 Sep 2021 18:31:16 +0100 Subject: [PATCH] Refactor form definition to config file --- app/controllers/case_logs_controller.rb | 25 +- app/models/form.rb | 101 +++--- app/views/case_logs/_tasklist.html.erb | 10 +- app/views/case_logs/edit.html.erb | 2 +- ...household_number_of_other_members.html.erb | 8 +- .../previous_housing_situation.html.erb | 8 +- .../{questions => pages}/tenant_age.html.erb | 8 +- .../{questions => pages}/tenant_code.html.erb | 8 +- .../tenant_economic_status.html.erb | 8 +- .../tenant_ethnic_group.html.erb | 8 +- .../tenant_gender.html.erb | 8 +- .../tenant_nationality.html.erb | 8 +- config/forms/2021_2022.json | 304 ++++++++++++++++++ config/routes.rb | 6 +- spec/models/form_spec.rb | 32 +- 15 files changed, 433 insertions(+), 111 deletions(-) rename app/views/form/{questions => pages}/household_number_of_other_members.html.erb (65%) rename app/views/form/{questions => pages}/previous_housing_situation.html.erb (86%) rename app/views/form/{questions => pages}/tenant_age.html.erb (66%) rename app/views/form/{questions => pages}/tenant_code.html.erb (64%) rename app/views/form/{questions => pages}/tenant_economic_status.html.erb (80%) rename app/views/form/{questions => pages}/tenant_ethnic_group.html.erb (85%) rename app/views/form/{questions => pages}/tenant_gender.html.erb (71%) rename app/views/form/{questions => pages}/tenant_nationality.html.erb (83%) create mode 100644 config/forms/2021_2022.json diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb index 6bae9e7c8..3f39330c1 100644 --- a/app/controllers/case_logs_controller.rb +++ b/app/controllers/case_logs_controller.rb @@ -11,27 +11,30 @@ class CaseLogsController < ApplicationController # We don't have a dedicated non-editable show view def show - @case_log = CaseLog.find(params[:id]) - render :edit + edit end def edit + @form = Form.new(2021, 2022) @case_log = CaseLog.find(params[:id]) + render :edit, locals: { form: @form } end - def next_question + def next_page + form = Form.new(2021, 2022) @case_log = CaseLog.find(params[:case_log_id]) - previous_question = params[:previous_question] - previous_answer = params[previous_question] - @case_log.update!(previous_question => previous_answer) - next_question = Form::QUESTIONS[previous_question] - redirect_to(send("case_log_#{next_question}_path", @case_log)) + previous_page = params[:previous_page] + previous_answer = params[previous_page] + @case_log.update!(previous_page => previous_answer) + next_page = form.next_page(previous_page) + redirect_to(send("case_log_#{next_page}_path", @case_log)) end - Form::QUESTIONS.each_key do |question| - define_method(question) do + form = Form.new(2021, 2022) + form.all_pages.keys.map do |page| + define_method(page) do @case_log = CaseLog.find(params[:case_log_id]) - render "form/questions/#{question}", locals: { case_log_id: @case_log.id } + render "form/pages/#{page}", locals: { case_log_id: @case_log.id, form: form } end end end diff --git a/app/models/form.rb b/app/models/form.rb index 008ca36d7..a371aadce 100644 --- a/app/models/form.rb +++ b/app/models/form.rb @@ -1,46 +1,59 @@ -class Form < ApplicationRecord - self.abstract_class = true - - SECTIONS = { - "About the household" => %w[household_characteristics household_situation household_needs], - "Tenancy and property information" => %w[tenancy_information property_information], - "Rent and charges" => %w[income_and_benefits rent], - "Local Authority" => %w[local_authority], - "Submission" => %w[declaration], - }.freeze - - SUBSECTIONS = { - "household_characteristics" => "tenant_code", - "household_situation" => "previous_housing_situation", - "household_needs" => "tenant_code", - "tenancy_information" => "tenant_code", - "property_information" => "tenant_code", - "income_and_benefits" => "tenant_code", - "rent" => "tenant_code", - "local_authority" => "tenant_code", - "declaration" => "tenant_code", - }.freeze - - QUESTIONS = { - "tenant_code" => "tenant_age", - "tenant_age" => "tenant_gender", - "tenant_gender" => "tenant_ethnic_group", - "tenant_ethnic_group" => "tenant_nationality", - "tenant_nationality" => "tenant_economic_status", - "tenant_economic_status" => "household_number_of_other_members", - "household_number_of_other_members" => "household_number_of_other_members", - "previous_housing_situation" => "previous_housing_situation", - }.freeze - - def self.first_question_for_subsection(subsection) - SUBSECTIONS[subsection] - end - - def self.next_question(previous_question) - Form::QUESTIONS[previous_question] - end - - def self.previous_question(current_question) - Hash[QUESTIONS.to_a.map(&:reverse)][current_question] +class Form + + attr_reader :form_definition, :all_sections, :all_subsections, :all_pages + + def initialize(start_year, end_year) + form_json = "config/forms/#{start_year}_#{end_year}.json" + raise "No form definition file exists for given year".freeze unless File.exist?(form_json) + + @form_definition = JSON.load(File.new(form_json)) + end + + # Returns a hash with sections as keys + def all_sections + @sections ||= @form_definition["sections"] + end + + # Returns a hash with subsections as keys + def all_subsections + @subsections ||= all_sections.map do |section_key, section_value| + section_value["subsections"] + end.reduce(:merge) + end + + # Returns a hash with pages as keys + def all_pages + @all_pages ||= all_subsections.map do |subsection_key, subsection_value| + subsection_value["pages"] + end.reduce(:merge) + end + + # Returns a hash with the pages as keys + def pages_for_subsection(subsection) + all_subsections[subsection]["pages"] + end + + def first_page_for_subsection(subsection) + pages_for_subsection(subsection).keys.first + end + + def subsection_for_page(page) + all_subsections.find do |subsection_key, subsection_value| + subsection_value["pages"].keys.include?(page) + end.first + end + + def next_page(previous_page) + subsection = subsection_for_page(previous_page) + previous_page_idx = pages_for_subsection(subsection).keys.index(previous_page) + pages_for_subsection(subsection).keys[previous_page_idx + 1] || previous_page # Placeholder until we have a check answers page + end + + def previous_page(current_page) + subsection = subsection_for_page(current_page) + current_page_idx = pages_for_subsection(subsection).keys.index(current_page) + return unless current_page_idx > 0 + + pages_for_subsection(subsection).keys[current_page_idx - 1] end end diff --git a/app/views/case_logs/_tasklist.html.erb b/app/views/case_logs/_tasklist.html.erb index 5c3d161d9..d0e60c396 100644 --- a/app/views/case_logs/_tasklist.html.erb +++ b/app/views/case_logs/_tasklist.html.erb @@ -1,16 +1,16 @@
    - <% Form::SECTIONS.map do |section, subsections| %> + <% @form.all_sections.map do |section_key, section_value| %>
  1. - <%= section.gsub('_', ' ').capitalize %> + <%= section_value["label"] %>

      - <% subsections.map do |subsection| %> + <% section_value["subsections"].map do |subsection_key, subsection_value| %>
    • - <% first_question = Form.first_question_for_subsection(subsection) %> - <%= link_to subsection.gsub('_', ' ').capitalize, send("case_log_#{first_question}_path", @case_log), class: "task-name" %> + <% first_page = @form.first_page_for_subsection(subsection_key) %> + <%= link_to subsection_value["label"], send("case_log_#{first_page}_path", @case_log), class: "task-name" %> Not started diff --git a/app/views/case_logs/edit.html.erb b/app/views/case_logs/edit.html.erb index 4dc968a72..434852f1d 100644 --- a/app/views/case_logs/edit.html.erb +++ b/app/views/case_logs/edit.html.erb @@ -11,7 +11,7 @@ Skip to next incomplete section

      - <%= render "tasklist" %> + <%= render "tasklist", locals: { form: @form } %> diff --git a/app/views/form/questions/household_number_of_other_members.html.erb b/app/views/form/pages/household_number_of_other_members.html.erb similarity index 65% rename from app/views/form/questions/household_number_of_other_members.html.erb rename to app/views/form/pages/household_number_of_other_members.html.erb index 4c70b3f1c..54113f0ba 100644 --- a/app/views/form/questions/household_number_of_other_members.html.erb +++ b/app/views/form/pages/household_number_of_other_members.html.erb @@ -1,18 +1,18 @@ -<% previous_question = Form.previous_question("household_number_of_other_members") %> +<% previous_page = form.previous_page("household_number_of_other_members") %> <% content_for :before_content do %> - <%= govuk_back_link href: "/case_logs/#{case_log_id}/#{previous_question}" do %> + <%= govuk_back_link href: "/case_logs/#{case_log_id}/#{previous_page}" do %> Back <% end %> <% end %> <%= turbo_frame_tag "case_log_form", target: "_top" do %> - <%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> + <%= form_with action: '/case_logs', method: "next_page", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> <%= f.govuk_number_field :household_number_of_other_members, hint: { text: "The maximum number of others is 7" }, label: { text: "How many other people are there in the household?", size: "l"}, min: 0, max: 7, step: 1, width: 20 %> - <%= f.hidden_field :previous_question, value: :household_number_of_other_members %> + <%= f.hidden_field :previous_page, value: :household_number_of_other_members %> <%= f.hidden_field :case_log_id, value: case_log_id %> <%= f.govuk_submit "Save and continue" %> <% end %> diff --git a/app/views/form/questions/previous_housing_situation.html.erb b/app/views/form/pages/previous_housing_situation.html.erb similarity index 86% rename from app/views/form/questions/previous_housing_situation.html.erb rename to app/views/form/pages/previous_housing_situation.html.erb index 564b25626..48cdfe79d 100644 --- a/app/views/form/questions/previous_housing_situation.html.erb +++ b/app/views/form/pages/previous_housing_situation.html.erb @@ -24,17 +24,17 @@ OpenStruct.new(id: 22, value: "Lifetime PRP General Needs tenancy"), ] %> -<% previous_question = Form.previous_question("previous_housing_situation") %> +<% previous_page = form.previous_page("previous_housing_situation") %> <% content_for :before_content do %> - <%= govuk_back_link href: "/case_logs/#{case_log_id}/#{previous_question}" do %> + <%= govuk_back_link href: "/case_logs/#{case_log_id}/#{previous_page}" do %> Back <% end %> <% end %> <%= turbo_frame_tag "case_log_form", target: "_top" do %> - <%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> + <%= form_with action: '/case_logs', method: "next_page", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> <%= f.govuk_collection_radio_buttons :previous_housing_situation, situations, :id, :value, legend: { text: "What was the tenant’s housing situation immediately before this letting?", size: "l" } %> - <%= f.hidden_field :previous_question, value: :previous_housing_situation %> + <%= f.hidden_field :previous_page, value: :previous_housing_situation %> <%= f.hidden_field :case_log_id, value: case_log_id %> <%= f.govuk_submit "Save and continue" %> <% end %> diff --git a/app/views/form/questions/tenant_age.html.erb b/app/views/form/pages/tenant_age.html.erb similarity index 66% rename from app/views/form/questions/tenant_age.html.erb rename to app/views/form/pages/tenant_age.html.erb index aabcd39d9..03b8ea503 100644 --- a/app/views/form/questions/tenant_age.html.erb +++ b/app/views/form/pages/tenant_age.html.erb @@ -1,18 +1,18 @@ -<% previous_question = Form.previous_question("tenant_age") %> +<% previous_page = form.previous_page("tenant_age") %> <% content_for :before_content do %> - <%= govuk_back_link href: "/case_logs/#{case_log_id}/#{previous_question}" do %> + <%= govuk_back_link href: "/case_logs/#{case_log_id}/#{previous_page}" do %> Back <% end %> <% end %> <%= turbo_frame_tag "case_log_form", target: "_top" do %> - <%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> + <%= form_with action: '/case_logs', method: "next_page", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> <%= f.govuk_number_field :tenant_age, hint: { text: "More detail" }, label: { text: "What is the tenant's age?", size: "l"}, min: 0, max: 150, step: 1, width: 20 %> - <%= f.hidden_field :previous_question, value: :tenant_age %> + <%= f.hidden_field :previous_page, value: :tenant_age %> <%= f.hidden_field :case_log_id, value: case_log_id %> <%= f.govuk_submit "Save and continue" %> <% end %> diff --git a/app/views/form/questions/tenant_code.html.erb b/app/views/form/pages/tenant_code.html.erb similarity index 64% rename from app/views/form/questions/tenant_code.html.erb rename to app/views/form/pages/tenant_code.html.erb index d13eed35e..751408b2b 100644 --- a/app/views/form/questions/tenant_code.html.erb +++ b/app/views/form/pages/tenant_code.html.erb @@ -1,18 +1,18 @@ -<% previous_question = Form.previous_question("tenant_code") %> +<% previous_page = form.previous_page("tenant_code") %> <% content_for :before_content do %> - <%= govuk_back_link href: "/case_logs/#{case_log_id}/#{previous_question}" do %> + <%= govuk_back_link href: "/case_logs/#{case_log_id}/#{previous_page}" do %> Back <% end %> <% end %> <%= turbo_frame_tag "case_log_form", target: "_top" do %> - <%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> + <%= form_with action: '/case_logs', method: "next_page", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> <%= f.govuk_text_field :tenant_code, hint: { text: "More detail" }, label: { text: "What is the tenant code?", size: "l"}, width: 20 %> - <%= f.hidden_field :previous_question, value: :tenant_code %> + <%= f.hidden_field :previous_page, value: :tenant_code %> <%= f.hidden_field :case_log_id, value: case_log_id %> <%= f.govuk_submit "Save and continue" %> <% end %> diff --git a/app/views/form/questions/tenant_economic_status.html.erb b/app/views/form/pages/tenant_economic_status.html.erb similarity index 80% rename from app/views/form/questions/tenant_economic_status.html.erb rename to app/views/form/pages/tenant_economic_status.html.erb index abfb48f9b..907066a9d 100644 --- a/app/views/form/questions/tenant_economic_status.html.erb +++ b/app/views/form/pages/tenant_economic_status.html.erb @@ -12,17 +12,17 @@ OpenStruct.new(id: 10, value: "Prefer not to say") ] %> -<% previous_question = Form.previous_question("tenant_value") %> +<% previous_page = form.previous_page("tenant_economic_status") %> <% content_for :before_content do %> - <%= govuk_back_link href: "/case_logs/#{case_log_id}/#{previous_question}" do %> + <%= govuk_back_link href: "/case_logs/#{case_log_id}/#{previous_page}" do %> Back <% end %> <% end %> <%= turbo_frame_tag "case_log_form", target: "_top" do %> - <%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> + <%= form_with action: '/case_logs', method: "next_page", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> <%= f.govuk_collection_radio_buttons :tenant_economic_status, economic_statuses, :id, :value, legend: { text: "Which of these best describes the tenant's working situation?", size: "l" } %> - <%= f.hidden_field :previous_question, value: :tenant_economic_status %> + <%= f.hidden_field :previous_page, value: :tenant_economic_status %> <%= f.hidden_field :case_log_id, value: case_log_id %> <%= f.govuk_submit "Save and continue" %> <% end %> diff --git a/app/views/form/questions/tenant_ethnic_group.html.erb b/app/views/form/pages/tenant_ethnic_group.html.erb similarity index 85% rename from app/views/form/questions/tenant_ethnic_group.html.erb rename to app/views/form/pages/tenant_ethnic_group.html.erb index 6511eaba5..b79093406 100644 --- a/app/views/form/questions/tenant_ethnic_group.html.erb +++ b/app/views/form/pages/tenant_ethnic_group.html.erb @@ -20,18 +20,18 @@ OpenStruct.new(id: 18, value: "Prefer not to say") ] %> -<% previous_question = Form.previous_question("tenant_ethnic_group") %> +<% previous_page = form.previous_page("tenant_ethnic_group") %> <% content_for :before_content do %> - <%= govuk_back_link href: "/case_logs/#{case_log_id}/#{previous_question}" do %> + <%= govuk_back_link href: "/case_logs/#{case_log_id}/#{previous_page}" do %> Back <% end %> <% end %> <%= turbo_frame_tag "case_log_form", target: "_top" do %> - <%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> + <%= form_with action: '/case_logs', method: "next_page", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> <%= f.govuk_collection_radio_buttons :tenant_ethnic_group, ethnic_groups, :id, :value, legend: { text: "What is the tenant’s ethnic group?", size: "l" } %> - <%= f.hidden_field :previous_question, value: :tenant_ethnic_group %> + <%= f.hidden_field :previous_page, value: :tenant_ethnic_group %> <%= f.hidden_field :case_log_id, value: case_log_id %> <%= f.govuk_submit "Save and continue" %> <% end %> diff --git a/app/views/form/questions/tenant_gender.html.erb b/app/views/form/pages/tenant_gender.html.erb similarity index 71% rename from app/views/form/questions/tenant_gender.html.erb rename to app/views/form/pages/tenant_gender.html.erb index c0589b632..be9695ee2 100644 --- a/app/views/form/questions/tenant_gender.html.erb +++ b/app/views/form/pages/tenant_gender.html.erb @@ -5,17 +5,17 @@ OpenStruct.new(id: 3, value: "Prefer not to say") ] %> -<% previous_question = Form.previous_question("tenant_gender") %> +<% previous_page = form.previous_page("tenant_gender") %> <% content_for :before_content do %> - <%= govuk_back_link href: "/case_logs/#{case_log_id}/#{previous_question}" do %> + <%= govuk_back_link href: "/case_logs/#{case_log_id}/#{previous_page}" do %> Back <% end %> <% end %> <%= turbo_frame_tag "case_log_form", target: "_top" do %> - <%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> + <%= form_with action: '/case_logs', method: "next_page", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> <%= f.govuk_collection_radio_buttons :tenant_gender, genders, :id, :value, legend: { text: "Which of these best describes the tenant's gender identity?", size: "l" } %> - <%= f.hidden_field :previous_question, value: :tenant_gender %> + <%= f.hidden_field :previous_page, value: :tenant_gender %> <%= f.hidden_field :case_log_id, value: case_log_id %> <%= f.govuk_submit "Save and continue" %> <% end %> diff --git a/app/views/form/questions/tenant_nationality.html.erb b/app/views/form/pages/tenant_nationality.html.erb similarity index 83% rename from app/views/form/questions/tenant_nationality.html.erb rename to app/views/form/pages/tenant_nationality.html.erb index a6de6a4f9..067dcf747 100644 --- a/app/views/form/questions/tenant_nationality.html.erb +++ b/app/views/form/pages/tenant_nationality.html.erb @@ -17,17 +17,17 @@ OpenStruct.new(id: 15, value: "Prefer not to say") ] %> -<% previous_question = Form.previous_question("tenant_nationality") %> +<% previous_page = form.previous_page("tenant_nationality") %> <% content_for :before_content do %> - <%= govuk_back_link href: "/case_logs/#{case_log_id}/#{previous_question}" do %> + <%= govuk_back_link href: "/case_logs/#{case_log_id}/#{previous_page}" do %> Back <% end %> <% end %> <%= turbo_frame_tag "case_log_form", target: "_top" do %> - <%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> + <%= form_with action: '/case_logs', method: "next_page", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> <%= f.govuk_collection_radio_buttons :tenant_nationality, nationalities, :id, :value, legend: { text: "What is the tenant’s ethnic group?", size: "l" } %> - <%= f.hidden_field :previous_question, value: :tenant_nationality %> + <%= f.hidden_field :previous_page, value: :tenant_nationality %> <%= f.hidden_field :case_log_id, value: case_log_id %> <%= f.govuk_submit "Save and continue" %> <% end %> diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json new file mode 100644 index 000000000..55fec6946 --- /dev/null +++ b/config/forms/2021_2022.json @@ -0,0 +1,304 @@ +{ + "form_type": "lettings", + "start_year": 2021, + "end_year": 2022, + "sections": { + "household": { + "label": "About the household", + "subsections": { + "household_characteristics": { + "label": "Household characteristics", + "pages": { + "tenant_code":{ + "header": "", + "description": "", + "questions":{ + "tenant_code": { + "header": "What is the tenant code?", + "hint_text": "", + "type": "text" + } + } + }, + "tenant_age":{ + "header": "", + "description": "", + "questions":{ + "tenant_age": { + "header": "What is the tenant's age?", + "hint_text": "", + "type": "numeric", + "min": 0, + "max": 150 + } + } + }, + "tenant_gender":{ + "header": "", + "description": "", + "questions":{ + "tenant_gender": { + "header": "Which of these best describes the tenant's gender identity?", + "hint_text": "", + "type": "radio", + "answer_options": { + "1": "Female", + "2": "Male", + "3": "Non-binary", + "4": "Prefer not to say" + } + } + } + }, + "tenant_ethnic_group":{ + "header": "", + "description": "", + "questions":{ + "tenant_ethnic_group": { + "header": "What is the tenant's ethnic group?", + "hint_text": "", + "type": "radio", + "answer_options": { + "0": "White: English/Scottish/Welsh/Northern Irish/British", + "1": "White: Irish", + "2": "White: Gypsy/Irish Traveller", + "3": "White: Other", + "4": "Mixed: White & Black Caribbean", + "5": "Mixed: White & Black African", + "6": "Mixed: White & Asian", + "7": "Mixed: Other", + "8": "Asian or Asian British: Indian", + "9": "Asian or Asian British: Pakistani", + "10": "Asian or Asian British: Bangladeshi", + "11": "Asian or Asian British: Chinese", + "12": "Asian or Asian British: Other", + "13": "Black: Caribbean", + "14": "Black: African", + "15": "Black: Other", + "16": "Other Ethnic Group: Arab", + "17": "Other Ethnic Group: Other", + "18": "Prefer not to say" + } + } + } + }, + "tenant_nationality":{ + "header": "", + "description": "", + "questions":{ + "tenant_nationality": { + "header": "What is the tenant's nationality?", + "hint_text": "", + "type": "radio", + "answer_options": { + "0": "UK national resident in UK", + "1": "A current or former reserve in the UK Armed Forces (exc. National Service)", + "2": "UK national returning from residence overseas", + "3": "Czech Republic", + "4": "Estonia", + "5": "Hungary", + "6": "Latvia", + "7": "Lithuania", + "8": "Poland", + "9": "Slovakia", + "10": "Bulgaria", + "11": "Romania", + "12": "Ireland", + "13": "Other EU Economic Area (EEA country)", + "14": "Any other country", + "15": "Prefer not to say" + } + } + } + }, + "tenant_economic_status":{ + "header": "", + "description": "", + "questions":{ + "tenant_economic_status": { + "header": "Which of these best describes the tenant's working situation?", + "hint_text": "", + "type": "radio", + "answer_options": { + "0": "Part-time - Less than 30 hours", + "1": "Full-time - 30 hours or more", + "2": "In government training into work, such as New Deal", + "3": "Jobseeker", + "4": "Retired", + "5": "Not seeking work", + "6": "Full-time student", + "7": "Unable to work because of long term sick or disability", + "8": "Child under 16", + "9": "Other", + "10": "Prefer not to say" + } + } + } + }, + "household_number_of_other_members":{ + "header": "", + "description": "", + "questions":{ + "household_number_of_other_members": { + "header": "How many other people are there in the household?", + "hint_text": "The maximum number of others is 7", + "type": "numeric", + "min": 0, + "max": 7 + } + } + } + } + }, + "household_situation": { + "label": "Household situation", + "pages": { + "tenant_code":{ + "header": "", + "description": "", + "questions":{ + "tenant_code": { + "header": "What is the tenant code?", + "hint_text": "", + "type": "text" + } + } + } + } + }, + "household_needs": { + "label": "Household needs", + "pages": { + "tenant_code":{ + "header": "", + "description": "", + "questions":{ + "tenant_code": { + "header": "What is the tenant code?", + "hint_text": "", + "type": "text" + } + } + } + } + } + } + }, + "tenancy_and_property": { + "label": "Tenancy and property information", + "subsections": { + "tenancy_information": { + "label": "Tenancy information", + "pages": { + "tenant_code":{ + "header": "", + "description": "", + "questions":{ + "tenant_code": { + "header": "What is the tenant code?", + "hint_text": "", + "type": "text" + } + } + } + } + }, + "property_information": { + "label": "Property information", + "pages": { + "tenant_code":{ + "header": "", + "description": "", + "questions":{ + "tenant_code": { + "header": "What is the tenant code?", + "hint_text": "", + "type": "text" + } + } + } + } + } + } + }, + "rent_and_charges": { + "label": "Rent and charges", + "subsections": { + "income_and_benefits": { + "label": "Income and benefits", + "pages": { + "tenant_code":{ + "header": "", + "description": "", + "questions":{ + "tenant_code": { + "header": "What is the tenant code?", + "hint_text": "", + "type": "text" + } + } + } + } + }, + "rent": { + "label": "Rent", + "pages": { + "tenant_code":{ + "header": "", + "description": "", + "questions":{ + "tenant_code": { + "header": "What is the tenant code?", + "hint_text": "", + "type": "text" + } + } + } + } + } + } + }, + "local_authority": { + "label": "Local authority", + "subsections": { + "local_authority": { + "label": "Local authority", + "pages": { + "tenant_code":{ + "header": "", + "description": "", + "questions":{ + "tenant_code": { + "header": "What is the tenant code?", + "hint_text": "", + "type": "text" + } + } + } + } + } + } + }, + "submission": { + "label": "Submission", + "subsections": { + "declaration": { + "label": "Declaration", + "pages": { + "tenant_code":{ + "header": "", + "description": "", + "questions":{ + "tenant_code": { + "header": "What is the tenant code?", + "hint_text": "", + "type": "text" + } + } + } + } + } + } + } + } +} diff --git a/config/routes.rb b/config/routes.rb index 94f90e5f6..6443b9d35 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,9 +4,9 @@ Rails.application.routes.draw do get "/", to: "test#index" resources :case_logs do - Form::QUESTIONS.keys.map do |question| - get question.to_s, to: "case_logs##{question}" - post question.to_s, to: "case_logs#next_question" + Form.new(2021, 2022).all_pages.keys.map do |page| + get page.to_s, to: "case_logs##{page}" + post page.to_s, to: "case_logs#next_page" end end end diff --git a/spec/models/form_spec.rb b/spec/models/form_spec.rb index c19f3fdd4..912370b1e 100644 --- a/spec/models/form_spec.rb +++ b/spec/models/form_spec.rb @@ -1,32 +1,34 @@ require "rails_helper" RSpec.describe Form, type: :model do - describe ".next_question" do - let(:previous_question) { "tenant_age" } - it "returns the next question given the previous" do - expect(Form.next_question(previous_question)).to eq("tenant_gender") + let(:form) { Form.new(2021, 2022) } + + describe ".next_page" do + let(:previous_page) { "tenant_age" } + it "returns the next page given the previous" do + expect(form.next_page(previous_page)).to eq("tenant_gender") end end - describe ".first_question_for_subsection" do + describe ".first_page_for_subsection" do let(:subsection) { "household_characteristics" } - it "returns the next question given the previous" do - expect(Form.first_question_for_subsection(subsection)).to eq("tenant_code") + it "returns the first page given a subsection" do + expect(form.first_page_for_subsection(subsection)).to eq("tenant_code") end end - describe ".previous_question" do - context "given a question in the middle of a subsection" do - let(:current_question) { "tenant_age" } - it "returns the previous question given the current" do - expect(Form.previous_question(current_question)).to eq("tenant_code") + describe ".previous_page" do + context "given a page in the middle of a subsection" do + let(:current_page) { "tenant_age" } + it "returns the previous page given the current" do + expect(form.previous_page(current_page)).to eq("tenant_code") end end - context "given the first question in a subsection" do - let(:current_question) { "tenant_code" } + context "given the first page in a subsection" do + let(:current_page) { "tenant_code" } it "returns empty string" do - expect(Form.previous_question(current_question)).to be_nil + expect(form.previous_page(current_page)).to be_nil end end end