Browse Source

Cldc 358 multiple conditions routing (#61)

* Change condition format in json

* Add test for multiple conditions

* Check multiple conditions

* nearly better

* check the condition for route without checcking the question type

* remove a comment and an unused variable

Co-authored-by: magicmilo <milobascombe@gmail.com>
pull/62/head
kosiakkatrina 3 years ago committed by GitHub
parent
commit
ed2c03c253
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      app/controllers/case_logs_controller.rb
  2. 19
      app/helpers/check_answers_helper.rb
  3. 10
      spec/features/case_log_spec.rb
  4. 9
      spec/fixtures/forms/test_form.json
  5. 10
      spec/helpers/check_answers_helper_spec.rb

14
app/controllers/case_logs_controller.rb

@ -61,7 +61,7 @@ class CaseLogsController < ApplicationController
responses_for_page = question_responses(questions_for_page)
@case_log.previous_page = previous_page
if @case_log.update(responses_for_page)
redirect_path = get_next_page_path(form, previous_page, responses_for_page)
redirect_path = get_next_page_path(form, previous_page, @case_log)
redirect_to(send(redirect_path, @case_log))
else
page_info = form.all_pages[previous_page]
@ -129,18 +129,16 @@ private
params.require(:case_log).permit(CaseLog.editable_fields)
end
def get_next_page_path(form, previous_page, responses_for_page = {})
questions_for_page = form.questions_for_page(previous_page)
questions_for_page.each do |question, content|
next unless content.key?("conditional_route_to")
def get_next_page_path(form, previous_page, case_log = {})
content = form.all_pages[previous_page]
content["conditional_route_to"].each do |route, answer|
if responses_for_page[question].present? && answer.include?(responses_for_page[question])
if content.key?("conditional_route_to")
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]) }
return "case_log_#{route}_path"
end
end
end
form.next_page_redirect_path(previous_page)
end
end

19
app/helpers/check_answers_helper.rb

@ -11,9 +11,10 @@ module CheckAnswersHelper
def total_questions(subsection, case_log, form)
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)
question_key = questions.keys[0]
question_value = questions.values[0]
@ -21,7 +22,7 @@ module CheckAnswersHelper
applicable_questions = filter_conditional_questions(questions, case_log)
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
total_questions
@ -40,15 +41,15 @@ module CheckAnswersHelper
applicable_questions
end
def get_next_page_name(form, page_name, applicable_questions, question_key, case_log, question_value)
if applicable_questions[question_key].key?("conditional_route_to")
applicable_questions[question_key]["conditional_route_to"].each do |conditional_page_key, condition|
unless condition_not_met(case_log, question_key, question_value, condition)
return conditional_page_key
def get_next_page_name(form, page_name, case_log)
page = form.all_pages[page_name]
if page.key?("conditional_route_to")
page["conditional_route_to"].each do |conditional_page_name, condition|
unless condition.any? { |field, value| case_log[field].blank? || !value.include?(case_log[field]) }
return conditional_page_name
end
end
end
form.next_page(page_name)
end

10
spec/features/case_log_spec.rb

@ -361,5 +361,15 @@ RSpec.describe "Test Features" do
click_button("Save and continue")
expect(page).to have_current_path("/case_logs/#{id}/conditional_question/check_answers")
end
it "can route based on multiple conditions" do
visit("/case_logs/#{id}/tenant_gender")
choose("case-log-tenant-gender-female-field", allow_label_click: true)
click_button("Save and continue")
visit("/case_logs/#{id}/conditional_question")
choose("case-log-pregnancy-yes-field", allow_label_click: true)
click_button("Save and continue")
expect(page).to have_current_path("/case_logs/#{id}/rent")
end
end
end

9
spec/fixtures/forms/test_form.json vendored

@ -248,13 +248,14 @@
"answer_options": {
"0": "Yes",
"1": "No"
},
"conditional_route_to": {
"conditional_question_yes_page": "Yes",
"conditional_question_no_page": "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"
},
"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
case_log["pregnancy"] = "Yes"
case_log["tenant_gender"] = "Female"
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)
end
end

Loading…
Cancel
Save