Browse Source

Refactor form definition to config file

pull/23/head
baarkerlounger 3 years ago committed by Daniel Baark
parent
commit
53d525670c
  1. 25
      app/controllers/case_logs_controller.rb
  2. 101
      app/models/form.rb
  3. 10
      app/views/case_logs/_tasklist.html.erb
  4. 2
      app/views/case_logs/edit.html.erb
  5. 8
      app/views/form/pages/household_number_of_other_members.html.erb
  6. 8
      app/views/form/pages/previous_housing_situation.html.erb
  7. 8
      app/views/form/pages/tenant_age.html.erb
  8. 8
      app/views/form/pages/tenant_code.html.erb
  9. 8
      app/views/form/pages/tenant_economic_status.html.erb
  10. 8
      app/views/form/pages/tenant_ethnic_group.html.erb
  11. 8
      app/views/form/pages/tenant_gender.html.erb
  12. 8
      app/views/form/pages/tenant_nationality.html.erb
  13. 304
      config/forms/2021_2022.json
  14. 6
      config/routes.rb
  15. 32
      spec/models/form_spec.rb

25
app/controllers/case_logs_controller.rb

@ -11,27 +11,30 @@ class CaseLogsController < ApplicationController
# We don't have a dedicated non-editable show view # We don't have a dedicated non-editable show view
def show def show
@case_log = CaseLog.find(params[:id]) edit
render :edit
end end
def edit def edit
@form = Form.new(2021, 2022)
@case_log = CaseLog.find(params[:id]) @case_log = CaseLog.find(params[:id])
render :edit, locals: { form: @form }
end end
def next_question def next_page
form = Form.new(2021, 2022)
@case_log = CaseLog.find(params[:case_log_id]) @case_log = CaseLog.find(params[:case_log_id])
previous_question = params[:previous_question] previous_page = params[:previous_page]
previous_answer = params[previous_question] previous_answer = params[previous_page]
@case_log.update!(previous_question => previous_answer) @case_log.update!(previous_page => previous_answer)
next_question = Form::QUESTIONS[previous_question] next_page = form.next_page(previous_page)
redirect_to(send("case_log_#{next_question}_path", @case_log)) redirect_to(send("case_log_#{next_page}_path", @case_log))
end end
Form::QUESTIONS.each_key do |question| form = Form.new(2021, 2022)
define_method(question) do form.all_pages.keys.map do |page|
define_method(page) do
@case_log = CaseLog.find(params[:case_log_id]) @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 end
end end

101
app/models/form.rb

@ -1,46 +1,59 @@
class Form < ApplicationRecord class Form
self.abstract_class = true
attr_reader :form_definition, :all_sections, :all_subsections, :all_pages
SECTIONS = {
"About the household" => %w[household_characteristics household_situation household_needs], def initialize(start_year, end_year)
"Tenancy and property information" => %w[tenancy_information property_information], form_json = "config/forms/#{start_year}_#{end_year}.json"
"Rent and charges" => %w[income_and_benefits rent], raise "No form definition file exists for given year".freeze unless File.exist?(form_json)
"Local Authority" => %w[local_authority],
"Submission" => %w[declaration], @form_definition = JSON.load(File.new(form_json))
}.freeze end
SUBSECTIONS = { # Returns a hash with sections as keys
"household_characteristics" => "tenant_code", def all_sections
"household_situation" => "previous_housing_situation", @sections ||= @form_definition["sections"]
"household_needs" => "tenant_code", end
"tenancy_information" => "tenant_code",
"property_information" => "tenant_code", # Returns a hash with subsections as keys
"income_and_benefits" => "tenant_code", def all_subsections
"rent" => "tenant_code", @subsections ||= all_sections.map do |section_key, section_value|
"local_authority" => "tenant_code", section_value["subsections"]
"declaration" => "tenant_code", end.reduce(:merge)
}.freeze end
QUESTIONS = { # Returns a hash with pages as keys
"tenant_code" => "tenant_age", def all_pages
"tenant_age" => "tenant_gender", @all_pages ||= all_subsections.map do |subsection_key, subsection_value|
"tenant_gender" => "tenant_ethnic_group", subsection_value["pages"]
"tenant_ethnic_group" => "tenant_nationality", end.reduce(:merge)
"tenant_nationality" => "tenant_economic_status", end
"tenant_economic_status" => "household_number_of_other_members",
"household_number_of_other_members" => "household_number_of_other_members", # Returns a hash with the pages as keys
"previous_housing_situation" => "previous_housing_situation", def pages_for_subsection(subsection)
}.freeze all_subsections[subsection]["pages"]
end
def self.first_question_for_subsection(subsection)
SUBSECTIONS[subsection] def first_page_for_subsection(subsection)
end pages_for_subsection(subsection).keys.first
end
def self.next_question(previous_question)
Form::QUESTIONS[previous_question] def subsection_for_page(page)
end all_subsections.find do |subsection_key, subsection_value|
subsection_value["pages"].keys.include?(page)
def self.previous_question(current_question) end.first
Hash[QUESTIONS.to_a.map(&:reverse)][current_question] 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
end end

10
app/views/case_logs/_tasklist.html.erb

@ -1,16 +1,16 @@
<ol class="app-task-list app-task-list--no-numbers"> <ol class="app-task-list app-task-list--no-numbers">
<% Form::SECTIONS.map do |section, subsections| %> <% @form.all_sections.map do |section_key, section_value| %>
<li> <li>
<h2 class="app-task-list__section"> <h2 class="app-task-list__section">
<span class="app-task-list__section-number"> <span class="app-task-list__section-number">
<%= section.gsub('_', ' ').capitalize %> <%= section_value["label"] %>
</span> </span>
</h2> </h2>
<ul class="app-task-list__items"> <ul class="app-task-list__items">
<% subsections.map do |subsection| %> <% section_value["subsections"].map do |subsection_key, subsection_value| %>
<li class="app-task-list__item"> <li class="app-task-list__item">
<% first_question = Form.first_question_for_subsection(subsection) %> <% first_page = @form.first_page_for_subsection(subsection_key) %>
<%= link_to subsection.gsub('_', ' ').capitalize, send("case_log_#{first_question}_path", @case_log), class: "task-name" %> <%= link_to subsection_value["label"], send("case_log_#{first_page}_path", @case_log), class: "task-name" %>
<strong class="govuk-tag govuk-tag--grey app-task-list__tag"> <strong class="govuk-tag govuk-tag--grey app-task-list__tag">
Not started Not started
</strong> </strong>

2
app/views/case_logs/edit.html.erb

@ -11,7 +11,7 @@
<a href="#">Skip to next incomplete section</a> <a href="#">Skip to next incomplete section</a>
</p> </p>
<%= render "tasklist" %> <%= render "tasklist", locals: { form: @form } %>
</div> </div>
</div> </div>

8
app/views/form/questions/household_number_of_other_members.html.erb → 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 %> <% 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 Back
<% end %> <% end %>
<% end %> <% end %>
<%= turbo_frame_tag "case_log_form", target: "_top" do %> <%= 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, <%= f.govuk_number_field :household_number_of_other_members,
hint: { text: "The maximum number of others is 7" }, hint: { text: "The maximum number of others is 7" },
label: { text: "How many other people are there in the household?", size: "l"}, label: { text: "How many other people are there in the household?", size: "l"},
min: 0, max: 7, step: 1, width: 20 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.hidden_field :case_log_id, value: case_log_id %>
<%= f.govuk_submit "Save and continue" %> <%= f.govuk_submit "Save and continue" %>
<% end %> <% end %>

8
app/views/form/questions/previous_housing_situation.html.erb → app/views/form/pages/previous_housing_situation.html.erb

@ -24,17 +24,17 @@
OpenStruct.new(id: 22, value: "Lifetime PRP General Needs tenancy"), 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 %> <% 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 Back
<% end %> <% end %>
<% end %> <% end %>
<%= turbo_frame_tag "case_log_form", target: "_top" do %> <%= 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.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.hidden_field :case_log_id, value: case_log_id %>
<%= f.govuk_submit "Save and continue" %> <%= f.govuk_submit "Save and continue" %>
<% end %> <% end %>

8
app/views/form/questions/tenant_age.html.erb → 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 %> <% 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 Back
<% end %> <% end %>
<% end %> <% end %>
<%= turbo_frame_tag "case_log_form", target: "_top" do %> <%= 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, <%= f.govuk_number_field :tenant_age,
hint: { text: "More detail" }, hint: { text: "More detail" },
label: { text: "What is the tenant's age?", size: "l"}, label: { text: "What is the tenant's age?", size: "l"},
min: 0, max: 150, step: 1, width: 20 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.hidden_field :case_log_id, value: case_log_id %>
<%= f.govuk_submit "Save and continue" %> <%= f.govuk_submit "Save and continue" %>
<% end %> <% end %>

8
app/views/form/questions/tenant_code.html.erb → 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 %> <% 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 Back
<% end %> <% end %>
<% end %> <% end %>
<%= turbo_frame_tag "case_log_form", target: "_top" do %> <%= 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, <%= f.govuk_text_field :tenant_code,
hint: { text: "More detail" }, hint: { text: "More detail" },
label: { text: "What is the tenant code?", size: "l"}, label: { text: "What is the tenant code?", size: "l"},
width: 20 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.hidden_field :case_log_id, value: case_log_id %>
<%= f.govuk_submit "Save and continue" %> <%= f.govuk_submit "Save and continue" %>
<% end %> <% end %>

8
app/views/form/questions/tenant_economic_status.html.erb → app/views/form/pages/tenant_economic_status.html.erb

@ -12,17 +12,17 @@
OpenStruct.new(id: 10, value: "Prefer not to say") 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 %> <% 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 Back
<% end %> <% end %>
<% end %> <% end %>
<%= turbo_frame_tag "case_log_form", target: "_top" do %> <%= 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.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.hidden_field :case_log_id, value: case_log_id %>
<%= f.govuk_submit "Save and continue" %> <%= f.govuk_submit "Save and continue" %>
<% end %> <% end %>

8
app/views/form/questions/tenant_ethnic_group.html.erb → app/views/form/pages/tenant_ethnic_group.html.erb

@ -20,18 +20,18 @@
OpenStruct.new(id: 18, value: "Prefer not to say") 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 %> <% 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 Back
<% end %> <% end %>
<% end %> <% end %>
<%= turbo_frame_tag "case_log_form", target: "_top" do %> <%= 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.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.hidden_field :case_log_id, value: case_log_id %>
<%= f.govuk_submit "Save and continue" %> <%= f.govuk_submit "Save and continue" %>
<% end %> <% end %>

8
app/views/form/questions/tenant_gender.html.erb → app/views/form/pages/tenant_gender.html.erb

@ -5,17 +5,17 @@
OpenStruct.new(id: 3, value: "Prefer not to say") 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 %> <% 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 Back
<% end %> <% end %>
<% end %> <% end %>
<%= turbo_frame_tag "case_log_form", target: "_top" do %> <%= 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.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.hidden_field :case_log_id, value: case_log_id %>
<%= f.govuk_submit "Save and continue" %> <%= f.govuk_submit "Save and continue" %>
<% end %> <% end %>

8
app/views/form/questions/tenant_nationality.html.erb → app/views/form/pages/tenant_nationality.html.erb

@ -17,17 +17,17 @@
OpenStruct.new(id: 15, value: "Prefer not to say") 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 %> <% 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 Back
<% end %> <% end %>
<% end %> <% end %>
<%= turbo_frame_tag "case_log_form", target: "_top" do %> <%= 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.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.hidden_field :case_log_id, value: case_log_id %>
<%= f.govuk_submit "Save and continue" %> <%= f.govuk_submit "Save and continue" %>
<% end %> <% end %>

304
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"
}
}
}
}
}
}
}
}
}

6
config/routes.rb

@ -4,9 +4,9 @@ Rails.application.routes.draw do
get "/", to: "test#index" get "/", to: "test#index"
resources :case_logs do resources :case_logs do
Form::QUESTIONS.keys.map do |question| Form.new(2021, 2022).all_pages.keys.map do |page|
get question.to_s, to: "case_logs##{question}" get page.to_s, to: "case_logs##{page}"
post question.to_s, to: "case_logs#next_question" post page.to_s, to: "case_logs#next_page"
end end
end end
end end

32
spec/models/form_spec.rb

@ -1,32 +1,34 @@
require "rails_helper" require "rails_helper"
RSpec.describe Form, type: :model do RSpec.describe Form, type: :model do
describe ".next_question" do let(:form) { Form.new(2021, 2022) }
let(:previous_question) { "tenant_age" }
it "returns the next question given the previous" do describe ".next_page" do
expect(Form.next_question(previous_question)).to eq("tenant_gender") 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
end end
describe ".first_question_for_subsection" do describe ".first_page_for_subsection" do
let(:subsection) { "household_characteristics" } let(:subsection) { "household_characteristics" }
it "returns the next question given the previous" do it "returns the first page given a subsection" do
expect(Form.first_question_for_subsection(subsection)).to eq("tenant_code") expect(form.first_page_for_subsection(subsection)).to eq("tenant_code")
end end
end end
describe ".previous_question" do describe ".previous_page" do
context "given a question in the middle of a subsection" do context "given a page in the middle of a subsection" do
let(:current_question) { "tenant_age" } let(:current_page) { "tenant_age" }
it "returns the previous question given the current" do it "returns the previous page given the current" do
expect(Form.previous_question(current_question)).to eq("tenant_code") expect(form.previous_page(current_page)).to eq("tenant_code")
end end
end end
context "given the first question in a subsection" do context "given the first page in a subsection" do
let(:current_question) { "tenant_code" } let(:current_page) { "tenant_code" }
it "returns empty string" do 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 end
end end

Loading…
Cancel
Save