Browse Source

Add next page path method with conditional routing

pull/49/head
MadeTech Dushan 4 years ago
parent
commit
1e17727687
  1. 14
      app/controllers/case_logs_controller.rb
  2. 45
      spec/controllers/case_logs_controller_spec.rb
  3. 15
      spec/features/case_log_spec.rb
  4. 48
      spec/fixtures/forms/test_form.json

14
app/controllers/case_logs_controller.rb

@ -51,6 +51,20 @@ class CaseLogsController < ApplicationController
end end
end end
def self.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|
if(content.key?("conditional_route_to"))
content["conditional_route_to"].each do |route, answer|
if responses_for_page[question.to_sym] == answer
return "case_log_#{route.to_s}_path"
end
end
end
end
form.next_page_redirect_path(previous_page)
end
def check_answers def check_answers
form = @@form_handler.get_form("2021_2022") form = @@form_handler.get_form("2021_2022")
@case_log = CaseLog.find(params[:case_log_id]) @case_log = CaseLog.find(params[:case_log_id])

45
spec/controllers/case_logs_controller_spec.rb

@ -122,5 +122,50 @@ RSpec.describe CaseLogsController, type: :controller do
expect(case_log.tenant_code).to eq(tenant_code) expect(case_log.tenant_code).to eq(tenant_code)
end end
end end
context "conditional routing" do
let(:case_log_form_conditional_question_yes_params) do
{
pregnancy: "Yes",
previous_page: "conditional_question",
}
end
let(:case_log_form_conditional_question_no_params) do
{
pregnancy: "No",
previous_page: "conditional_question",
}
end
it "routes to the appropriate conditional page based on the question answer of the current page" do
post :submit_form, params: { id: id, case_log: case_log_form_conditional_question_yes_params }
expect(response).to redirect_to("/case_logs/#{id}/conditional_question_yes_page")
post :submit_form, params: { id: id, case_log: case_log_form_conditional_question_no_params }
expect(response).to redirect_to("/case_logs/#{id}/conditional_question_no_page")
end
end
end
describe "get_next_page_path" do
let(:previous_page) { "net_income" }
let(:last_previous_page) { "housing_benefit" }
let(:previous_conditional_page) { "conditional_question" }
let(:form_handler) { FormHandler.instance }
let(:form) { form_handler.get_form("test_form") }
let(:responses_for_page) { { "pregnancy": "No" } }
it "returns a correct page path if there is no conditional routing" do
expect(CaseLogsController.get_next_page_path(form, previous_page)).to eq("case_log_net_income_uc_proportion_path")
end
it "returns a check answers page if previous page is the last page" do
expect(CaseLogsController.get_next_page_path(form, last_previous_page)).to eq("case_log_income_and_benefits_check_answers_path")
end
it "returns a correct page path if there is conditional routing" do
expect(CaseLogsController.get_next_page_path(form, previous_conditional_page, responses_for_page)).to eq("case_log_conditional_question_no_page_path")
end
end end
end end

15
spec/features/case_log_spec.rb

@ -296,4 +296,19 @@ RSpec.describe "Test Features" do
end end
end end
end end
describe "conditional page routing" do
it "can route the user to a different page based on their answer on the current page" do
visit("case_logs/#{id}/conditional_question")
# using a question name that is already in the db to avoid
# having to add a new column to the db for this test
choose("case-log-pregnancy-yes-field")
click_button("Save and continue")
expect(page).to have_current_path("/case_logs/#{id}/conditional_question_yes_page")
click_link(text: "Back")
choose("case-log-pregnancy-no-field")
click_button("Save and continue")
expect(page).to have_current_path("/case_logs/#{id}/conditional_question_no_page")
end
end
end end

48
spec/fixtures/forms/test_form.json vendored

@ -235,6 +235,54 @@
} }
} }
} }
},
"conditional_question": {
"label": "Conditional question",
"pages": {
"conditional_question": {
"questions": {
"pregnancy": {
"check_answer_label": "Has the condition been met?",
"header": "Has the condition been met?",
"type": "radio",
"answer_options": {
"0": "Yes",
"1": "No"
},
"conditional_route_to": {
"conditional_question_yes_page": "Yes",
"conditional_question_no_page": "No"
}
}
}
},
"conditional_question_yes_page": {
"questions": {
"conditional_question_yes_question": {
"check_answer_label": "Has the next condition been met?",
"header": "Has the next condition been met?",
"type": "radio",
"answer_options": {
"0": "Yes",
"1": "No"
}
}
}
},
"conditional_question_no_page": {
"questions": {
"conditional_question_no_question": {
"check_answer_label": "Has the condition not been met?",
"header": "Has the next condition not been met?",
"type": "radio",
"answer_options": {
"0": "Yes",
"1": "No"
}
}
}
}
}
} }
} }
}, },

Loading…
Cancel
Save