Browse Source

Refactor routes

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

27
app/controllers/case_logs_controller.rb

@ -19,29 +19,24 @@ class CaseLogsController < ApplicationController
@case_log = CaseLog.find(params[:id])
end
FIRST_QUESTION_FOR_SUBSECTION = {
"Household characteristics" => "form/questions/household/tenant_code",
"Household situation" => "case_logs/household_situation/previous_housing_situation"
}.freeze
NEXT_QUESTION = {
"tenant_code" => "form/questions/household/tenant_age",
"tenant_age" => "form/questions/household/tenant_gender",
"tenant_gender" => "form/questions/household/tenant_ethnic_group",
"tenant_ethnic_group" => "form/questions/household/tenant_nationality",
}.freeze
def next_question
subsection = params[:subsection]
@case_log = CaseLog.find(params[:case_log_id])
result = if subsection
FIRST_QUESTION_FOR_SUBSECTION[subsection]
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)
NEXT_QUESTION[previous_question]
Form::QUESTIONS[previous_question]
end
render result, locals: { case_log_id: @case_log.id }
render next_question, locals: { case_log_id: @case_log.id }
end
Form::QUESTIONS.keys.each 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 }
end
end
end

15
app/models/form.rb

@ -0,0 +1,15 @@
class Form < ApplicationRecord
self.abstract_class = true
FIRST_QUESTION_FOR_SUBSECTION = {
"Household characteristics" => "form/questions/tenant_code",
"Household situation" => "form/questions/previous_housing_situation"
}.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",
}.freeze
end

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

@ -18,7 +18,7 @@
<ul class="app-task-list__items">
<% subsections.map do |subsection| %>
<li class="app-task-list__item">
<%= link_to subsection, form_path(subsection: subsection, case_log_id: @case_log.id), class: "task-name" %>
<%= 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>
</li>
<% end %>

0
app/views/case_logs/household_situation/previous_housing_situation.erb → app/views/form/previous_housing_situation.erb

0
app/views/form/questions/household/tenant_age.html.erb → app/views/form/questions/tenant_age.html.erb

0
app/views/form/questions/household/tenant_code.html.erb → app/views/form/questions/tenant_code.html.erb

0
app/views/form/questions/household/tenant_gender.html.erb → app/views/form/questions/tenant_gender.html.erb

10
config/routes.rb

@ -2,8 +2,12 @@ Rails.application.routes.draw do
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
get "about", to: "about#index"
get "/", to: "test#index"
get "form", to: "case_logs#next_question"
post "form", to: "case_logs#next_question"
resources :case_logs
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}"
end
end
end

Loading…
Cancel
Save