From f25a08cfc8b1d7a26802e7d629fdd8e6b97bd341 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Thu, 16 Sep 2021 14:26:38 +0100 Subject: [PATCH] Refactoring and navigation --- app/controllers/case_logs_controller.rb | 17 +++------ app/models/form.rb | 38 +++++++++++++++---- app/views/case_logs/_tasklist.html.erb | 24 +++++------- app/views/case_logs/edit.html.erb | 2 +- .../previous_housing_situation.html.erb} | 6 +-- app/views/form/questions/tenant_age.html.erb | 2 +- app/views/form/questions/tenant_code.html.erb | 2 +- .../form/questions/tenant_gender.html.erb | 2 +- config/routes.rb | 5 +-- 9 files changed, 55 insertions(+), 43 deletions(-) rename app/views/form/{previous_housing_situation.erb => questions/previous_housing_situation.html.erb} (96%) diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb index 39338157d..6bae9e7c8 100644 --- a/app/controllers/case_logs_controller.rb +++ b/app/controllers/case_logs_controller.rb @@ -20,20 +20,15 @@ class CaseLogsController < ApplicationController end def next_question - subsection = params[:subsection] @case_log = CaseLog.find(params[:case_log_id]) - next_question = if subsection - Form::FIRST_QUESTION_FOR_SUBSECTION[subsection] - else - previous_question = params[:previous_question] - answer = params[previous_question] - @case_log.update!(previous_question => answer) - Form::QUESTIONS[previous_question] - end - render next_question, locals: { case_log_id: @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)) end - Form::QUESTIONS.keys.each do |question| + Form::QUESTIONS.each_key do |question| define_method(question) do @case_log = CaseLog.find(params[:case_log_id]) render "form/questions/#{question}", locals: { case_log_id: @case_log.id } diff --git a/app/models/form.rb b/app/models/form.rb index e57986aee..6f322cc33 100644 --- a/app/models/form.rb +++ b/app/models/form.rb @@ -1,15 +1,39 @@ class Form < ApplicationRecord self.abstract_class = true - FIRST_QUESTION_FOR_SUBSECTION = { - "Household characteristics" => "form/questions/tenant_code", - "Household situation" => "form/questions/previous_housing_situation" + 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" => "form/questions/tenant_age", - "tenant_age" => "form/questions/tenant_gender", - "tenant_gender" => "form/questions/tenant_ethnic_group", - "tenant_ethnic_group" => "form/questions/tenant_nationality", + "tenant_code" => "tenant_age", + "tenant_age" => "tenant_gender", + "tenant_gender" => "tenant_ethnic_group", + "tenant_ethnic_group" => "tenant_nationality", + "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 end diff --git a/app/views/case_logs/_tasklist.html.erb b/app/views/case_logs/_tasklist.html.erb index af4267f3a..5c3d161d9 100644 --- a/app/views/case_logs/_tasklist.html.erb +++ b/app/views/case_logs/_tasklist.html.erb @@ -1,25 +1,19 @@ -<%# Placeholder until we have a real form schema %> -<% - sections = { - "About the household" => ["Household characteristics", "Household situation", "Household needs"], - "Tenancy and property information" => ["Tenancy information", "Property information"], - "Rent and charges" => ["Income & benefits", "Rent"], - "Local Authority" => ["Local authority"], - "Submission" => ["Declaration"] - } -%> -
    - <% sections.map do |section, subsections| %> + <% Form::SECTIONS.map do |section, subsections| %>
  1. - <%= section %> + + <%= section.gsub('_', ' ').capitalize %> +

      <% subsections.map do |subsection| %>
    • - <%= link_to subsection, case_log_form_path(@case_log, subsection: subsection), class: "task-name" %> - Not started + <% first_question = Form.first_question_for_subsection(subsection) %> + <%= link_to subsection.gsub('_', ' ').capitalize, send("case_log_#{first_question}_path", @case_log), class: "task-name" %> + + Not started +
    • <% end %>
    diff --git a/app/views/case_logs/edit.html.erb b/app/views/case_logs/edit.html.erb index e4ff62322..4dc968a72 100644 --- a/app/views/case_logs/edit.html.erb +++ b/app/views/case_logs/edit.html.erb @@ -1,4 +1,4 @@ -<%= turbo_frame_tag "case_log_form" do %> +<%= turbo_frame_tag "case_log_form", target: "_top" do %>

    Tasklist for log diff --git a/app/views/form/previous_housing_situation.erb b/app/views/form/questions/previous_housing_situation.html.erb similarity index 96% rename from app/views/form/previous_housing_situation.erb rename to app/views/form/questions/previous_housing_situation.html.erb index 5c0719e9c..b57886419 100644 --- a/app/views/form/previous_housing_situation.erb +++ b/app/views/form/questions/previous_housing_situation.html.erb @@ -23,12 +23,12 @@ OpenStruct.new(id: 21, name: "Fixed term PRP General Needs tenancy"), OpenStruct.new(id: 22, name: "Lifetime PRP General Needs tenancy"), ] %> - -<%= turbo_frame_tag "case_log_form" do %> + +<%= turbo_frame_tag "case_log_form", target: "_top" do %> <%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> <%= f.govuk_collection_radio_buttons :previous_housing_situation, situations, :id, :name, 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 :case_log_id, value: case_log_id %> <%= f.govuk_submit "Save and continue" %> <% end %> -<% end %> \ No newline at end of file +<% end %> diff --git a/app/views/form/questions/tenant_age.html.erb b/app/views/form/questions/tenant_age.html.erb index 5c8f02ca0..9a6d028b3 100644 --- a/app/views/form/questions/tenant_age.html.erb +++ b/app/views/form/questions/tenant_age.html.erb @@ -1,4 +1,4 @@ -<%= turbo_frame_tag "case_log_form" do %> +<%= turbo_frame_tag "case_log_form", target: "_top" do %> <%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> <%= f.govuk_number_field :tenant_age, hint: { text: "More detail" }, diff --git a/app/views/form/questions/tenant_code.html.erb b/app/views/form/questions/tenant_code.html.erb index d850ef2a5..251712985 100644 --- a/app/views/form/questions/tenant_code.html.erb +++ b/app/views/form/questions/tenant_code.html.erb @@ -1,4 +1,4 @@ -<%= turbo_frame_tag "case_log_form" do %> +<%= turbo_frame_tag "case_log_form", target: "_top" do %> <%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> <%= f.govuk_text_field :tenant_code, hint: { text: "More detail" }, diff --git a/app/views/form/questions/tenant_gender.html.erb b/app/views/form/questions/tenant_gender.html.erb index 429944959..d5897d8f9 100644 --- a/app/views/form/questions/tenant_gender.html.erb +++ b/app/views/form/questions/tenant_gender.html.erb @@ -5,7 +5,7 @@ OpenStruct.new(id: 3, name: "Prefer not to say") ] %> -<%= turbo_frame_tag "case_log_form" do %> +<%= turbo_frame_tag "case_log_form", target: "_top" do %> <%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> <%= f.govuk_collection_radio_buttons :tenant_gender, genders, :id, :name, legend: { text: "Which of these best describes the tenant's gender identity?", size: "l" } %> <%= f.hidden_field :previous_question, value: :tenant_gender %> diff --git a/config/routes.rb b/config/routes.rb index fe4f59dd3..94f90e5f6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,10 +4,9 @@ Rails.application.routes.draw do get "/", to: "test#index" resources :case_logs do - get "form", to: "case_logs#next_question" - post "form", to: "case_logs#next_question" Form::QUESTIONS.keys.map do |question| - get "#{question}", to: "case_logs##{question}" + get question.to_s, to: "case_logs##{question}" + post question.to_s, to: "case_logs#next_question" end end end