Browse Source

Pass in form to view from controller for check_answers

pull/46/head
Kat 4 years ago committed by MadeTech Dushan
parent
commit
31237afe9a
  1. 3
      app/controllers/case_logs_controller.rb
  2. 28
      app/helpers/check_answers_helper.rb
  3. 4
      app/models/form.rb
  4. 6
      app/views/form/check_answers.html.erb
  5. 21
      spec/helpers/check_answers_helper_spec.rb

3
app/controllers/case_logs_controller.rb

@ -49,10 +49,11 @@ class CaseLogsController < ApplicationController
end
def check_answers
form = ENV["RAILS_ENV"] == "test" ? Form.new("test", "form") : Form.new(2021, 2022)
@case_log = CaseLog.find(params[:case_log_id])
current_url = request.env["PATH_INFO"]
subsection = current_url.split("/")[-2]
render "form/check_answers", locals: { case_log: @case_log, subsection: subsection }
render "form/check_answers", locals: { case_log: @case_log, subsection: subsection, form: form }
end
form = ENV["RAILS_ENV"] == "test" ? Form.new("test", "form") : Form.new(2021, 2022)

28
app/helpers/check_answers_helper.rb

@ -1,16 +1,15 @@
module CheckAnswersHelper
def total_answered_questions(subsection, case_log)
total_questions(subsection, case_log).keys.count do |question_key|
def total_answered_questions(subsection, case_log, form)
total_questions(subsection, case_log, form).keys.count do |question_key|
case_log[question_key].present?
end
end
def total_number_of_questions(subsection, case_log)
total_questions(subsection, case_log).count
def total_number_of_questions(subsection, case_log, form)
total_questions(subsection, case_log, form).count
end
def total_questions(subsection, case_log)
form = ENV["RAILS_ENV"] == "test" ? Form.new("test", "form") : Form.new(2021, 2022) #form should be passed in
def total_questions(subsection, case_log, form)
questions = form.questions_for_subsection(subsection)
questions_not_applicable = []
questions.reject do |question_key, question|
@ -36,19 +35,14 @@ module CheckAnswersHelper
end
end
def subsection_pages(subsection)
form = ENV["RAILS_ENV"] == "test" ? Form.new("test", "form") : Form.new(2021, 2022) #form should be passed in
form.pages_for_subsection(subsection)
end
def create_update_answer_link(case_log_answer, case_log_id, page)
link_name = case_log_answer.blank? ? "Answer" : "Change"
link_to(link_name, "/case_logs/#{case_log_id}/#{page}", class: "govuk-link").html_safe
end
def create_next_missing_question_link(case_log_id, subsection, case_log)
def create_next_missing_question_link(case_log_id, subsection, case_log, form)
pages_to_fill_in = []
subsection_pages(subsection).each do |page_title, page_info|
form.pages_for_subsection(subsection).each do |page_title, page_info|
page_info["questions"].any? { |question| case_log[question].blank? }
pages_to_fill_in << page_title
end
@ -56,12 +50,12 @@ module CheckAnswersHelper
link_to("Answer the missing questions", url, class: "govuk-link").html_safe
end
def display_answered_questions_summary(subsection, case_log)
if total_answered_questions(subsection, case_log) == total_number_of_questions(subsection, case_log)
def display_answered_questions_summary(subsection, case_log, form)
if total_answered_questions(subsection, case_log, form) == total_number_of_questions(subsection, case_log, form)
'<p class="govuk-body govuk-!-margin-bottom-7">You answered all the questions</p>'.html_safe
else
"<p class=\"govuk-body govuk-!-margin-bottom-7\">You answered #{total_answered_questions(subsection, case_log)} of #{total_number_of_questions(subsection, case_log)} questions</p>
#{create_next_missing_question_link(case_log['id'], subsection, case_log)}".html_safe
"<p class=\"govuk-body govuk-!-margin-bottom-7\">You answered #{total_answered_questions(subsection, case_log, form)} of #{total_number_of_questions(subsection, case_log, form)} questions</p>
#{create_next_missing_question_link(case_log['id'], subsection, case_log, form)}".html_safe
end
end
end

4
app/models/form.rb

@ -2,10 +2,8 @@ class Form
attr_reader :form_definition
def initialize(start_year, end_year)
ENV["RAILS_ENV"]
form_json = "config/forms/#{start_year}_#{end_year}.json"
# binding.pry
# raise "No form definition file exists for given year".freeze unless File.exist?(form_json)
raise "No form definition file exists for given year".freeze unless File.exist?(form_json)
@form_definition = JSON.parse(File.open(form_json).read)
end

6
app/views/form/check_answers.html.erb

@ -2,10 +2,10 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds-from-desktop">
<h1 class="govuk-heading-l">Check the answers you gave for <%= subsection.humanize(capitalize: false) %></h1>
<%= display_answered_questions_summary(subsection, case_log) %>
<% subsection_pages(subsection).each do |page, page_info| %>
<%= display_answered_questions_summary(subsection, case_log, form) %>
<% form.pages_for_subsection(subsection).each do |page, page_info| %>
<% page_info["questions"].each do |question_title, question_info| %>
<% if total_questions(subsection, case_log).include?(question_title) %>
<% if total_questions(subsection, case_log, form).include?(question_title) %>
<%= render partial: 'form/check_answers_table', locals: { question_title: question_title, question_info: question_info, case_log: case_log, page: page } %>
<%end %>
<%end %>

21
spec/helpers/check_answers_helper_spec.rb

@ -16,32 +16,34 @@ RSpec.describe CheckAnswersHelper do
let(:subsection) { "income_and_benefits" }
let(:subsection_with_numeric_conditionals) { "household_characteristics" }
let(:subsection_with_radio_conditionals) { "household_needs" }
let(:form) { Form.new("test", "form") }
describe "Get answered questions total" do
it "returns 0 if no questions are answered" do
expect(total_answered_questions(subsection, case_log)).to equal(0)
expect(total_answered_questions(subsection, case_log, form)).to equal(0)
end
it "returns 1 if 1 question gets answered" do
case_log["net_income"] = "123"
expect(total_answered_questions(subsection, case_log)).to equal(1)
expect(total_answered_questions(subsection, case_log, form)).to equal(1)
end
it "ignores questions with unmet numeric conditions" do
case_log["tenant_code"] = "T1234"
expect(total_answered_questions(subsection_with_numeric_conditionals, case_log)).to equal(1)
expect(total_answered_questions(subsection_with_numeric_conditionals, case_log, form)).to equal(1)
end
it "includes conditional questions with met numeric conditions" do
expect(total_answered_questions(
subsection_with_numeric_conditionals,
case_log_with_met_numeric_condition,
form,
)).to equal(4)
end
it "ignores questions with unmet radio conditions" do
case_log["armed_forces"] = "No"
expect(total_answered_questions(subsection_with_radio_conditionals, case_log)).to equal(1)
expect(total_answered_questions(subsection_with_radio_conditionals, case_log, form)).to equal(1)
end
it "includes conditional questions with met radio conditions" do
@ -50,34 +52,37 @@ RSpec.describe CheckAnswersHelper do
expect(total_answered_questions(
subsection_with_radio_conditionals,
case_log_with_met_radio_condition,
form,
)).to equal(3)
end
end
describe "Get total number of questions" do
it "returns the total number of questions for a subsection" do
expect(total_number_of_questions(subsection, case_log)).to eq(4)
expect(total_number_of_questions(subsection, case_log, form)).to eq(4)
end
it "ignores questions with unmet numeric conditions" do
expect(total_number_of_questions(subsection_with_numeric_conditionals, case_log)).to eq(7)
expect(total_number_of_questions(subsection_with_numeric_conditionals, case_log, form)).to eq(7)
end
it "includes conditional questions with met numeric conditions" do
expect(total_number_of_questions(
subsection_with_numeric_conditionals,
case_log_with_met_numeric_condition,
form,
)).to eq(15)
end
it "ignores questions with unmet radio conditions" do
expect(total_number_of_questions(subsection_with_radio_conditionals, case_log)).to eq(6)
expect(total_number_of_questions(subsection_with_radio_conditionals, case_log, form)).to eq(6)
end
it "includes conditional questions with met radio conditions" do
expect(total_number_of_questions(
subsection_with_radio_conditionals,
case_log_with_met_radio_condition,
form,
)).to eq(8)
end
@ -99,7 +104,7 @@ RSpec.describe CheckAnswersHelper do
it "raises an error" do
allow_any_instance_of(Form).to receive(:questions_for_subsection).and_return(unimplemented_conditional)
expect { total_number_of_questions(subsection, case_log) }.to raise_error(RuntimeError, "Not implemented yet")
expect { total_number_of_questions(subsection, case_log, form) }.to raise_error(RuntimeError, "Not implemented yet")
end
end
end

Loading…
Cancel
Save