Browse Source

nearly better

pull/61/head
magicmilo 4 years ago
parent
commit
bc45855434
  1. 6
      app/controllers/case_logs_controller.rb
  2. 19
      app/helpers/check_answers_helper.rb
  3. 10
      spec/fixtures/forms/test_form.json
  4. 10
      spec/helpers/check_answers_helper_spec.rb

6
app/controllers/case_logs_controller.rb

@ -131,16 +131,16 @@ private
def get_next_page_path(form, previous_page, case_log = {}) def get_next_page_path(form, previous_page, case_log = {})
questions_for_page = form.questions_for_page(previous_page) questions_for_page = form.questions_for_page(previous_page)
questions_for_page.each do |_question, content| content = form.all_pages[previous_page]
next unless content.key?("conditional_route_to")
if content.key?("conditional_route_to")
content["conditional_route_to"].each do |route, conditions| content["conditional_route_to"].each do |route, conditions|
if conditions.keys.all? { |x| case_log[x].present? } && conditions.all? { |k, v| v.include?(case_log[k]) } if conditions.keys.all? { |x| case_log[x].present? } && conditions.all? { |k, v| v.include?(case_log[k]) }
return "case_log_#{route}_path" return "case_log_#{route}_path"
end end
# raise ""
end end
end end
form.next_page_redirect_path(previous_page) form.next_page_redirect_path(previous_page)
end end
end end

19
app/helpers/check_answers_helper.rb

@ -11,9 +11,10 @@ module CheckAnswersHelper
def total_questions(subsection, case_log, form) def total_questions(subsection, case_log, form)
total_questions = {} total_questions = {}
page_name = form.pages_for_subsection(subsection).keys.first subsection_keys = form.pages_for_subsection(subsection).keys
page_name = subsection_keys.first
while page_name.to_s != "check_answers" while page_name.to_s != "check_answers" && subsection_keys.include?(page_name)
questions = form.questions_for_page(page_name) questions = form.questions_for_page(page_name)
question_key = questions.keys[0] question_key = questions.keys[0]
question_value = questions.values[0] question_value = questions.values[0]
@ -21,7 +22,7 @@ module CheckAnswersHelper
applicable_questions = filter_conditional_questions(questions, case_log) applicable_questions = filter_conditional_questions(questions, case_log)
total_questions = total_questions.merge(applicable_questions) total_questions = total_questions.merge(applicable_questions)
page_name = get_next_page_name(form, page_name, applicable_questions, question_key, case_log, question_value) page_name = get_next_page_name(form, page_name, case_log)
end end
total_questions total_questions
@ -40,15 +41,15 @@ module CheckAnswersHelper
applicable_questions applicable_questions
end end
def get_next_page_name(form, page_name, applicable_questions, question_key, case_log, question_value) def get_next_page_name(form, page_name, case_log)
if applicable_questions[question_key].key?("conditional_route_to") page = form.all_pages[page_name]
applicable_questions[question_key]["conditional_route_to"].each do |conditional_page_key, condition| if page.key?("conditional_route_to")
unless condition.any? { |k, v| condition_not_met(case_log, k, question_value, v) } page["conditional_route_to"].each do |conditional_page_name, condition|
return conditional_page_key unless condition.any? { |field, value| condition_not_met(case_log, field, form.questions_for_page[field], value) }
return conditional_page_name
end end
end end
end end
form.next_page(page_name) form.next_page(page_name)
end end

10
spec/fixtures/forms/test_form.json vendored

@ -248,14 +248,14 @@
"answer_options": { "answer_options": {
"0": "Yes", "0": "Yes",
"1": "No" "1": "No"
},
"conditional_route_to": {
"rent": { "pregnancy": "Yes", "tenant_gender": "Female" },
"conditional_question_yes_page": { "pregnancy": "Yes" },
"conditional_question_no_page": { "pregnancy": "No" }
} }
} }
}, },
"conditional_route_to": {
"rent": { "pregnancy": "Yes", "tenant_gender": "Female" },
"conditional_question_yes_page": { "pregnancy": "Yes" },
"conditional_question_no_page": { "pregnancy": "No" }
},
"default_next_page": "check_answers" "default_next_page": "check_answers"
}, },
"conditional_question_yes_page": { "conditional_question_yes_page": {

10
spec/helpers/check_answers_helper_spec.rb

@ -153,8 +153,16 @@ RSpec.describe CheckAnswersHelper do
it "it includes conditional pages and questions that were displayed" do it "it includes conditional pages and questions that were displayed" do
case_log["pregnancy"] = "Yes" case_log["pregnancy"] = "Yes"
case_log["tenant_gender"] = "Female"
result = total_questions(conditional_routing_subsection, case_log, form) result = total_questions(conditional_routing_subsection, case_log, form)
expected_keys = %w[pregnancy cbl_letting] expected_keys = %w[pregnancy]
expect(result.keys).to match_array(expected_keys)
end
it "it includes conditional pages and questions that were displayed" do
case_log["pregnancy"] = "No"
result = total_questions(conditional_routing_subsection, case_log, form)
expected_keys = %w[pregnancy conditional_question_no_question conditional_question_no_second_question]
expect(result.keys).to match_array(expected_keys) expect(result.keys).to match_array(expected_keys)
end end
end end

Loading…
Cancel
Save