From a6146c3f7ac787771a72fde90b5e917d973eb714 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Thu, 16 Sep 2021 13:37:17 +0100 Subject: [PATCH] Refactor routes --- app/controllers/case_logs_controller.rb | 27 ++++++++----------- app/models/form.rb | 15 +++++++++++ app/views/case_logs/_tasklist.html.erb | 2 +- .../previous_housing_situation.erb | 0 .../{household => }/tenant_age.html.erb | 0 .../{household => }/tenant_code.html.erb | 0 .../{household => }/tenant_gender.html.erb | 0 config/routes.rb | 10 ++++--- 8 files changed, 34 insertions(+), 20 deletions(-) create mode 100644 app/models/form.rb rename app/views/{case_logs/household_situation => form}/previous_housing_situation.erb (100%) rename app/views/form/questions/{household => }/tenant_age.html.erb (100%) rename app/views/form/questions/{household => }/tenant_code.html.erb (100%) rename app/views/form/questions/{household => }/tenant_gender.html.erb (100%) diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb index 279ba2e6c..39338157d 100644 --- a/app/controllers/case_logs_controller.rb +++ b/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 diff --git a/app/models/form.rb b/app/models/form.rb new file mode 100644 index 000000000..e57986aee --- /dev/null +++ b/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 diff --git a/app/views/case_logs/_tasklist.html.erb b/app/views/case_logs/_tasklist.html.erb index 024835b5d..af4267f3a 100644 --- a/app/views/case_logs/_tasklist.html.erb +++ b/app/views/case_logs/_tasklist.html.erb @@ -18,7 +18,7 @@