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"] %>