Browse Source

Add check answers helper, display number of questions answered and a link to the next unanswered question

pull/25/head
Kat 3 years ago committed by MadeTech Dushan
parent
commit
a436a6cae2
  1. 23
      app/helpers/check_answers_helper.rb
  2. 9
      app/views/form/check_answers.html.erb
  3. 30
      spec/features/case_log_spec.rb

23
app/helpers/check_answers_helper.rb

@ -0,0 +1,23 @@
module CheckAnswersHelper
def get_answered_questions_total(subsection_pages, case_log)
questions = []
subsection_pages.keys.each do |page|
questions << page
end
return questions.count {|question| !(case_log[question].blank?)}
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, subsections, case_log)
empty_question = subsections.keys.find{|x| case_log[x].blank? }
url = "/case_logs/#{case_log_id}/#{empty_question}"
link_to('Answer the missing questions', url, class: "govuk-link").html_safe
end
end

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

@ -1,5 +1,7 @@
<%= turbo_frame_tag "case_log_form", target: "_top" do %> <%= turbo_frame_tag "case_log_form", target: "_top" do %>
<h1 class="govuk-heading-l">Check the answers you gave for household characteristics</h1> <h1 class="govuk-heading-l">Check the answers you gave for household characteristics</h1>
<p>You answered <%= get_answered_questions_total(subsection_pages, case_log) %> of <%= subsection_pages.count %> questions</p>
<%= create_next_missing_question_link(case_log_id, subsection_pages, case_log) %>
<% subsection_pages.each do |page, page_info| %> <% subsection_pages.each do |page, page_info| %>
<dl class="govuk-summary-list govuk-!-margin-bottom-9"> <dl class="govuk-summary-list govuk-!-margin-bottom-9">
<div class="govuk-summary-list__row"> <div class="govuk-summary-list__row">
@ -10,11 +12,7 @@
<%= case_log[page] %> <%= case_log[page] %>
</dd> </dd>
<dd class="govuk-summary-list__actions"> <dd class="govuk-summary-list__actions">
<% if case_log[page].blank? %> <%= create_update_answer_link(case_log[page], case_log_id, page)%>
<%= link_to "Answer", "/case_logs/#{case_log_id}/#{page}", class: "govuk-link" %>
<% else %>
<%= link_to "Change", "/case_logs/#{case_log_id}/#{page}", class: "govuk-link" %>
<% end %>
</dd> </dd>
</div> </div>
</dl> </dl>
@ -23,3 +21,4 @@
<%= f.govuk_submit "Save and continue" %> <%= f.govuk_submit "Save and continue" %>
<% end %> <% end %>
<% end %> <% end %>

30
spec/features/case_log_spec.rb

@ -124,9 +124,9 @@ RSpec.describe "Test Features" do
question_labels = ["Tenant code", "Tenant's age", "Tenant's gender", "Ethnicity", "Nationality", "Work", "Number of Other Household Members"] question_labels = ["Tenant code", "Tenant's age", "Tenant's gender", "Ethnicity", "Nationality", "Work", "Number of Other Household Members"]
question_labels.each do |label| question_labels.each do |label|
expect(page).to have_content(label) expect(page).to have_content(label)
end end
end end
it "should display answers given by the user for the question in the subsection" do it "should display answers given by the user for the question in the subsection" do
fill_in_number_question(empty_case_log.id, "tenant_age", 28) fill_in_number_question(empty_case_log.id, "tenant_age", 28)
choose("tenant-gender-non-binary-field") choose("tenant-gender-non-binary-field")
@ -134,22 +134,36 @@ RSpec.describe "Test Features" do
visit("/case_logs/#{empty_case_log.id}/#{subsection}/check_answers") visit("/case_logs/#{empty_case_log.id}/#{subsection}/check_answers")
expect(page).to have_content("28") expect(page).to have_content("28")
expect(page).to have_content("Non-binary") expect(page).to have_content("Non-binary")
end end
it "should have an answer link for questions missing an answer" do it "should have an answer link for questions missing an answer" do
visit("case_logs/#{empty_case_log.id}/#{subsection}/check_answers") visit("case_logs/#{empty_case_log.id}/#{subsection}/check_answers")
assert_selector "a", text: "Answer", count: 7 assert_selector "a", text: /Answer\z/, count: 7
assert_selector "a", text: "Change", count: 0 assert_selector "a", text: "Change", count: 0
expect(page).to have_link('Answer', href: "/case_logs/#{empty_case_log.id}/tenant_age") expect(page).to have_link('Answer', href: "/case_logs/#{empty_case_log.id}/tenant_age")
end end
it "should have a change link for answered questions" do it "should have a change link for answered questions" do
fill_in_number_question(empty_case_log.id, "tenant_age", 28) fill_in_number_question(empty_case_log.id, "tenant_age", 28)
visit("/case_logs/#{empty_case_log.id}/#{subsection}/check_answers") visit("/case_logs/#{empty_case_log.id}/#{subsection}/check_answers")
assert_selector "a", text: "Answer", count: 6 assert_selector "a", text: /Answer\z/, count: 6
assert_selector "a", text: "Change", count: 1 assert_selector "a", text: "Change", count: 1
expect(page).to have_link('Change', href: "/case_logs/#{empty_case_log.id}/tenant_age") expect(page).to have_link('Change', href: "/case_logs/#{empty_case_log.id}/tenant_age")
end end
it "should have a link pointing to the first question if no questions are answered" do
visit("/case_logs/#{empty_case_log.id}/#{subsection}/check_answers")
expect(page).to have_content('You answered 0 of 7 questions')
expect(page).to have_link('Answer the missing questions', href: "/case_logs/#{empty_case_log.id}/tenant_code")
end
it "should have a link pointing to the next empty question if some questions are answered" do
fill_in_number_question(empty_case_log.id, "tenant_code", 0)
visit("/case_logs/#{empty_case_log.id}/#{subsection}/check_answers")
expect(page).to have_content('You answered 1 of 7 questions')
expect(page).to have_link('Answer the missing questions', href: "/case_logs/#{empty_case_log.id}/tenant_age")
end
end end
end end
end end

Loading…
Cancel
Save