Browse Source

Refactoring and navigation

pull/21/head
baarkerlounger 3 years ago
parent
commit
f25a08cfc8
  1. 15
      app/controllers/case_logs_controller.rb
  2. 38
      app/models/form.rb
  3. 24
      app/views/case_logs/_tasklist.html.erb
  4. 2
      app/views/case_logs/edit.html.erb
  5. 2
      app/views/form/questions/previous_housing_situation.html.erb
  6. 2
      app/views/form/questions/tenant_age.html.erb
  7. 2
      app/views/form/questions/tenant_code.html.erb
  8. 2
      app/views/form/questions/tenant_gender.html.erb
  9. 5
      config/routes.rb

15
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_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 }

38
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

24
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"]
}
%>
<ol class="app-task-list app-task-list--no-numbers">
<% sections.map do |section, subsections| %>
<% Form::SECTIONS.map do |section, subsections| %>
<li>
<h2 class="app-task-list__section">
<span class="app-task-list__section-number"><%= section %></span>
<span class="app-task-list__section-number">
<%= section.gsub('_', ' ').capitalize %>
</span>
</h2>
<ul class="app-task-list__items">
<% subsections.map do |subsection| %>
<li class="app-task-list__item">
<%= link_to subsection, case_log_form_path(@case_log, subsection: subsection), class: "task-name" %>
<strong class="govuk-tag govuk-tag--grey app-task-list__tag">Not started</strong>
<% first_question = Form.first_question_for_subsection(subsection) %>
<%= link_to subsection.gsub('_', ' ').capitalize, send("case_log_#{first_question}_path", @case_log), class: "task-name" %>
<strong class="govuk-tag govuk-tag--grey app-task-list__tag">
Not started
</strong>
</li>
<% end %>
</ul>

2
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 %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-xl">Tasklist for log

2
app/views/form/previous_housing_situation.erb → app/views/form/questions/previous_housing_situation.html.erb

@ -24,7 +24,7 @@
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 %>

2
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" },

2
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" },

2
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 %>

5
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

Loading…
Cancel
Save