Browse Source

Invert form definition routing syntax

pull/98/head
baarkerlounger 4 years ago
parent
commit
d7ba42d318
  1. 17
      app/models/form.rb
  2. 28
      config/forms/2021_2022.json
  3. 18
      spec/fixtures/forms/test_aboutthislog.json
  4. 14
      spec/fixtures/forms/test_form.json
  5. 3
      spec/fixtures/forms/test_validator.json

17
app/models/form.rb

@ -108,22 +108,15 @@ class Form
end end
def page_routed_to?(page, case_log) 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.all? do |question, value|
conditions.select { |k, _condition| k == page } case_log[question].present? && case_log[question] == value
}.flat_map(&:values)
applicable_conditions.all? do |condition|
condition.all? { |k, v| case_log[k].present? && case_log[k] == v }
end end
end end
def pages_with_routing_conditions def page_dependencies(page)
@pages_with_routing_conditions ||= all_routing_conditions.flat_map(&:keys) all_pages[page]["depends_on"]
end
def all_routing_conditions
@all_routing_conditions ||= all_pages.map { |_page_key, page| page["conditional_route_to"] }.compact
end end
def condition_not_met(case_log, question_key, question, condition) def condition_not_met(case_log, question_key, question, condition)

28
config/forms/2021_2022.json

@ -23,9 +23,6 @@
"1": "No" "1": "No"
} }
} }
},
"conditional_route_to": {
"organisation_details": { "gdpr_acceptance": "Yes" }
} }
}, },
"gdpr_declined": { "gdpr_declined": {
@ -33,8 +30,8 @@
"hint_text": "", "hint_text": "",
"description": "We cannot accept data about a tenant or buyer unless they’ve seen the DLUHC privacy notice.", "description": "We cannot accept data about a tenant or buyer unless they’ve seen the DLUHC privacy notice.",
"questions": { "questions": {
},
} "depends_on": { "gdpr_acceptance": "Yes" }
}, },
"organisation_details": { "organisation_details": {
"header": "About this log", "header": "About this log",
@ -76,9 +73,6 @@
"1": "Letting" "1": "Letting"
} }
} }
},
"conditional_route_to": {
"sale_completion_date": { "sale_or_letting": "Sale" }
} }
}, },
"tenant_same_property_renewal": { "tenant_same_property_renewal": {
@ -95,7 +89,8 @@
"1": "No" "1": "No"
} }
} }
} },
"depends_on": { "sale_or_letting": "Letting" }
}, },
"startdate": { "startdate": {
"header": "About this log", "header": "About this log",
@ -107,7 +102,8 @@
"hint_text": "For example, 27 3 2007", "hint_text": "For example, 27 3 2007",
"type": "date" "type": "date"
} }
} },
"depends_on": { "sale_or_letting": "Letting" }
}, },
"about_this_letting": { "about_this_letting": {
"header": "Tell us about this letting", "header": "Tell us about this letting",
@ -145,7 +141,8 @@
"1": "General Needs" "1": "General Needs"
} }
} }
} },
"depends_on": { "sale_or_letting": "Letting" }
}, },
"tenant_code": { "tenant_code": {
"header": "", "header": "",
@ -157,7 +154,8 @@
"hint_text": "", "hint_text": "",
"type": "text" "type": "text"
} }
} },
"depends_on": { "sale_or_letting": "Letting" }
}, },
"sale_completion_date": { "sale_completion_date": {
"header": "Sale Completion Date", "header": "Sale Completion Date",
@ -169,7 +167,8 @@
"hint_text": "For example, 27 3 2007", "hint_text": "For example, 27 3 2007",
"type": "date" "type": "date"
} }
} },
"depends_on": { "sale_or_letting": "Sale" }
}, },
"purchaser_code": { "purchaser_code": {
"header": "About this log", "header": "About this log",
@ -181,7 +180,8 @@
"hint_text": "", "hint_text": "",
"type": "text" "type": "text"
} }
} },
"depends_on": { "sale_or_letting": "Sale" }
} }
} }
} }

18
spec/fixtures/forms/test_aboutthislog.json vendored

@ -74,9 +74,6 @@
"1": "Letting" "1": "Letting"
} }
} }
},
"conditional_route_to": {
"tenant_same_property_renewal": { "sale_or_letting": "Letting" }
} }
}, },
"tenant_same_property_renewal": { "tenant_same_property_renewal": {
@ -93,7 +90,8 @@
"1": "No" "1": "No"
} }
} }
} },
"depends_on": { "sale_or_letting": "Letting" }
}, },
"tenancy_start_date": { "tenancy_start_date": {
"header": "About this log", "header": "About this log",
@ -105,7 +103,8 @@
"hint_text": "For example, 27 3 2007", "hint_text": "For example, 27 3 2007",
"type": "date" "type": "date"
} }
} },
"depends_on": { "sale_or_letting": "Letting" }
}, },
"letting_type": { "letting_type": {
"header": "About this log", "header": "About this log",
@ -143,7 +142,8 @@
"1": "General Needs" "1": "General Needs"
} }
} }
} },
"depends_on": { "sale_or_letting": "Letting" }
}, },
"sale_completion_date": { "sale_completion_date": {
"header": "About this log", "header": "About this log",
@ -155,7 +155,8 @@
"hint_text": "For example, 27 3 2007", "hint_text": "For example, 27 3 2007",
"type": "date" "type": "date"
} }
} },
"depends_on": { "sale_or_letting": "Sale" }
}, },
"purchaser_code": { "purchaser_code": {
"header": "About this log", "header": "About this log",
@ -167,7 +168,8 @@
"hint_text": "", "hint_text": "",
"type": "text" "type": "text"
} }
} },
"depends_on": { "sale_or_letting": "Sale" }
} }
} }
} }

14
spec/fixtures/forms/test_form.json vendored

@ -244,11 +244,6 @@
"1": "No" "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": { "conditional_question_yes_page": {
@ -262,7 +257,8 @@
"1": "No" "1": "No"
} }
} }
} },
"depends_on": { "preg_occ": "Yes" }
}, },
"conditional_question_no_page": { "conditional_question_no_page": {
"questions": { "questions": {
@ -275,7 +271,8 @@
"1": "No" "1": "No"
} }
} }
} },
"depends_on": { "preg_occ": "No" }
}, },
"conditional_question_no_second_page": { "conditional_question_no_second_page": {
"questions": { "questions": {
@ -288,7 +285,8 @@
"1": "No" "1": "No"
} }
} }
} },
"depends_on": { "preg_occ": "No", "sex1": "Male" }
} }
} }
} }

3
spec/fixtures/forms/test_validator.json vendored

@ -22,9 +22,8 @@
"type": "text" "type": "text"
} }
}, },
"conditional_route_to": {"test": "Yes"} "depends_on": {"test": "Yes"}
}, },
"conditional_route_to": {"test": "Yes"},
"person_1_age": { "person_1_age": {
"header": "", "header": "",
"description": "", "description": "",

Loading…
Cancel
Save