diff --git a/app/models/form.rb b/app/models/form.rb index a0c709912..3de2655ad 100644 --- a/app/models/form.rb +++ b/app/models/form.rb @@ -108,22 +108,15 @@ class Form end def page_routed_to?(page, case_log) - return true unless pages_with_routing_conditions.include?(page) + return true unless conditions = page_dependencies(page) - applicable_conditions = all_routing_conditions.flat_map { |conditions| - conditions.select { |k, _condition| k == page } - }.flat_map(&:values) - applicable_conditions.all? do |condition| - condition.all? { |k, v| case_log[k].present? && case_log[k] == v } + conditions.all? do |question, value| + case_log[question].present? && case_log[question] == value end end - def pages_with_routing_conditions - @pages_with_routing_conditions ||= all_routing_conditions.flat_map(&:keys) - end - - def all_routing_conditions - @all_routing_conditions ||= all_pages.map { |_page_key, page| page["conditional_route_to"] }.compact + def page_dependencies(page) + all_pages[page]["depends_on"] end def condition_not_met(case_log, question_key, question, condition) diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json index e56212cbe..667300bf3 100644 --- a/config/forms/2021_2022.json +++ b/config/forms/2021_2022.json @@ -23,9 +23,6 @@ "1": "No" } } - }, - "conditional_route_to": { - "organisation_details": { "gdpr_acceptance": "Yes" } } }, "gdpr_declined": { @@ -33,8 +30,8 @@ "hint_text": "", "description": "We cannot accept data about a tenant or buyer unless they’ve seen the DLUHC privacy notice.", "questions": { - - } + }, + "depends_on": { "gdpr_acceptance": "Yes" } }, "organisation_details": { "header": "About this log", @@ -76,9 +73,6 @@ "1": "Letting" } } - }, - "conditional_route_to": { - "sale_completion_date": { "sale_or_letting": "Sale" } } }, "tenant_same_property_renewal": { @@ -95,7 +89,8 @@ "1": "No" } } - } + }, + "depends_on": { "sale_or_letting": "Letting" } }, "startdate": { "header": "About this log", @@ -107,7 +102,8 @@ "hint_text": "For example, 27 3 2007", "type": "date" } - } + }, + "depends_on": { "sale_or_letting": "Letting" } }, "about_this_letting": { "header": "Tell us about this letting", @@ -145,7 +141,8 @@ "1": "General Needs" } } - } + }, + "depends_on": { "sale_or_letting": "Letting" } }, "tenant_code": { "header": "", @@ -157,7 +154,8 @@ "hint_text": "", "type": "text" } - } + }, + "depends_on": { "sale_or_letting": "Letting" } }, "sale_completion_date": { "header": "Sale Completion Date", @@ -169,7 +167,8 @@ "hint_text": "For example, 27 3 2007", "type": "date" } - } + }, + "depends_on": { "sale_or_letting": "Sale" } }, "purchaser_code": { "header": "About this log", @@ -181,7 +180,8 @@ "hint_text": "", "type": "text" } - } + }, + "depends_on": { "sale_or_letting": "Sale" } } } } diff --git a/spec/fixtures/forms/test_aboutthislog.json b/spec/fixtures/forms/test_aboutthislog.json index 8b206c064..798469487 100644 --- a/spec/fixtures/forms/test_aboutthislog.json +++ b/spec/fixtures/forms/test_aboutthislog.json @@ -74,9 +74,6 @@ "1": "Letting" } } - }, - "conditional_route_to": { - "tenant_same_property_renewal": { "sale_or_letting": "Letting" } } }, "tenant_same_property_renewal": { @@ -93,7 +90,8 @@ "1": "No" } } - } + }, + "depends_on": { "sale_or_letting": "Letting" } }, "tenancy_start_date": { "header": "About this log", @@ -105,7 +103,8 @@ "hint_text": "For example, 27 3 2007", "type": "date" } - } + }, + "depends_on": { "sale_or_letting": "Letting" } }, "letting_type": { "header": "About this log", @@ -143,7 +142,8 @@ "1": "General Needs" } } - } + }, + "depends_on": { "sale_or_letting": "Letting" } }, "sale_completion_date": { "header": "About this log", @@ -155,7 +155,8 @@ "hint_text": "For example, 27 3 2007", "type": "date" } - } + }, + "depends_on": { "sale_or_letting": "Sale" } }, "purchaser_code": { "header": "About this log", @@ -167,7 +168,8 @@ "hint_text": "", "type": "text" } - } + }, + "depends_on": { "sale_or_letting": "Sale" } } } } diff --git a/spec/fixtures/forms/test_form.json b/spec/fixtures/forms/test_form.json index c1660b6df..d60befb1c 100644 --- a/spec/fixtures/forms/test_form.json +++ b/spec/fixtures/forms/test_form.json @@ -244,11 +244,6 @@ "1": "No" } } - }, - "conditional_route_to": { - "conditional_question_yes_page": { "preg_occ": "Yes" }, - "conditional_question_no_page": { "preg_occ": "No" }, - "conditional_question_no_second_page": { "preg_occ": "No", "sex1": "Male" } } }, "conditional_question_yes_page": { @@ -262,7 +257,8 @@ "1": "No" } } - } + }, + "depends_on": { "preg_occ": "Yes" } }, "conditional_question_no_page": { "questions": { @@ -275,7 +271,8 @@ "1": "No" } } - } + }, + "depends_on": { "preg_occ": "No" } }, "conditional_question_no_second_page": { "questions": { @@ -288,7 +285,8 @@ "1": "No" } } - } + }, + "depends_on": { "preg_occ": "No", "sex1": "Male" } } } } diff --git a/spec/fixtures/forms/test_validator.json b/spec/fixtures/forms/test_validator.json index 2f8e770d3..da5e04a8e 100644 --- a/spec/fixtures/forms/test_validator.json +++ b/spec/fixtures/forms/test_validator.json @@ -22,9 +22,8 @@ "type": "text" } }, - "conditional_route_to": {"test": "Yes"} + "depends_on": {"test": "Yes"} }, - "conditional_route_to": {"test": "Yes"}, "person_1_age": { "header": "", "description": "",