From 8ad852454f45bb8a3f56c55cf78ebe85ff8dbad6 Mon Sep 17 00:00:00 2001 From: Matthew Phelan Date: Tue, 19 Oct 2021 10:58:09 +0100 Subject: [PATCH 1/4] Why reasonable preference? validation added --- .../conditional_question_controller.js | 5 ++- app/models/case_log.rb | 24 ++++++++++++-- spec/models/case_log_spec.rb | 33 +++++++++++++++++++ 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/app/javascript/controllers/conditional_question_controller.js b/app/javascript/controllers/conditional_question_controller.js index c49ba490a..9aa6fddee 100644 --- a/app/javascript/controllers/conditional_question_controller.js +++ b/app/javascript/controllers/conditional_question_controller.js @@ -28,7 +28,10 @@ export default class extends Controller { div.style.display = "block" } else { div.style.display = "none" - let buttons = document.getElementsByName(`case_log[${targetQuestion}]`) + let buttons = document.getElementsByName(`case_log[${targetQuestion}]`); + if (buttons.length == 0){ + buttons = document.getElementsByName(`case_log[${targetQuestion}][]`); + } Object.entries(buttons).forEach(([idx, button]) => { button.checked = false; }) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 46d766ccc..9e8716569 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -15,13 +15,33 @@ class CaseLogValidator < ActiveModel::Validator end end + def validate_api_reasonable_preference(record) + if record.reasonable_preference == "No" + if record.reasonable_preference_reason_homeless || record.reasonable_preference_reason_unsatisfactory_housing || record.reasonable_preference_reason_medical_grounds || record.reasonable_preference_reason_avoid_hardship || record.reasonable_preference_reason_do_not_know + record.errors.add :reasonable_preference_reason, "- no resasons can be set to true, if reasonable preference is No" + end + end + end + + def validate_reasonable_preference(record) + if record.homelessness == "No" && record.reasonable_preference == "Yes" + record.errors.add :reasonable_preference, "can not be Yes if Not Homesless imediately prior to this letting has been selected" + elsif record.reasonable_preference == "Yes" + if !record.reasonable_preference_reason_homeless && !record.reasonable_preference_reason_unsatisfactory_housing && !record.reasonable_preference_reason_medical_grounds && !record.reasonable_preference_reason_avoid_hardship && !record.reasonable_preference_reason_do_not_know + record.errors.add :reasonable_preference_reason, "- if reasonable preference is Yes, a reason must be given" + end + end + end + def validate(record) # If we've come from the form UI we only want to validate the specific fields # that have just been submitted. If we're submitting a log via API or Bulk Upload # we want to validate all data fields. question_to_validate = options[:previous_page] - if question_to_validate && respond_to?("validate_#{question_to_validate}") - public_send("validate_#{question_to_validate}", record) + if question_to_validate + if respond_to?("validate_#{question_to_validate}") + public_send("validate_#{question_to_validate}", record) + end else # This assumes that all methods in this class other than this one are # validations to be run diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 6b0805250..236ddf7dc 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -25,6 +25,39 @@ RSpec.describe Form, type: :model do it "validates number of relets is over 0" do expect { CaseLog.create!(property_number_of_times_relet: 0) }.to raise_error(ActiveRecord::RecordInvalid) end + + describe "reasonable preference validation" do + it "if given reasonable preference is yes a reason must be selected" do + expect { + CaseLog.create!(reasonable_preference: "Yes", + reasonable_preference_reason_homeless: nil, + reasonable_preference_reason_unsatisfactory_housing: nil, + reasonable_preference_reason_medical_grounds: nil, + reasonable_preference_reason_avoid_hardship: nil, + reasonable_preference_reason_do_not_know: nil + ) + }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "if not previously homesless reasonable preference should not be selected" do + expect { + CaseLog.create!( + homelessness: "No", + reasonable_preference: "Yes" + ) + }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "if not given reasonable preference a reason should not be selected" do + expect { + CaseLog.create!( + homelessness: "Yes", + reasonable_preference: "No", + reasonable_preference_reason_homeless: true + ) + }.to raise_error(ActiveRecord::RecordInvalid) + end + end end describe "status" do From b87f1cbdeed39c955e461e0a2428a69393ebeb6a Mon Sep 17 00:00:00 2001 From: Matthew Phelan Date: Wed, 20 Oct 2021 07:41:11 +0100 Subject: [PATCH 2/4] CLDC-487 - Code review comments update --- app/models/case_log.rb | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 9e8716569..519485580 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -15,14 +15,6 @@ class CaseLogValidator < ActiveModel::Validator end end - def validate_api_reasonable_preference(record) - if record.reasonable_preference == "No" - if record.reasonable_preference_reason_homeless || record.reasonable_preference_reason_unsatisfactory_housing || record.reasonable_preference_reason_medical_grounds || record.reasonable_preference_reason_avoid_hardship || record.reasonable_preference_reason_do_not_know - record.errors.add :reasonable_preference_reason, "- no resasons can be set to true, if reasonable preference is No" - end - end - end - def validate_reasonable_preference(record) if record.homelessness == "No" && record.reasonable_preference == "Yes" record.errors.add :reasonable_preference, "can not be Yes if Not Homesless imediately prior to this letting has been selected" @@ -30,6 +22,10 @@ class CaseLogValidator < ActiveModel::Validator if !record.reasonable_preference_reason_homeless && !record.reasonable_preference_reason_unsatisfactory_housing && !record.reasonable_preference_reason_medical_grounds && !record.reasonable_preference_reason_avoid_hardship && !record.reasonable_preference_reason_do_not_know record.errors.add :reasonable_preference_reason, "- if reasonable preference is Yes, a reason must be given" end + elsif record.reasonable_preference == "No" + if record.reasonable_preference_reason_homeless || record.reasonable_preference_reason_unsatisfactory_housing || record.reasonable_preference_reason_medical_grounds || record.reasonable_preference_reason_avoid_hardship || record.reasonable_preference_reason_do_not_know + record.errors.add :reasonable_preference_reason, "- if reasonable preference is no, no reasons should be given" + end end end From f25603bd7dd4812e087f12f55804a0ffa71e5513 Mon Sep 17 00:00:00 2001 From: Matthew Phelan Date: Wed, 20 Oct 2021 08:05:34 +0100 Subject: [PATCH 3/4] test fix for reasonable preference --- spec/fixtures/complete_case_log.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/fixtures/complete_case_log.json b/spec/fixtures/complete_case_log.json index 11f073714..4cf667b6a 100644 --- a/spec/fixtures/complete_case_log.json +++ b/spec/fixtures/complete_case_log.json @@ -38,7 +38,7 @@ "person_8_age": 2, "person_8_gender": "Prefer not to say", "person_8_economic_status": "Child under 16", - "homelessness": "No", + "homelessness": "Yes - other homelessness", "reason_for_leaving_last_settled_home": "Other problems with neighbours", "benefit_cap_spare_room_subsidy": "No", "armed_forces_active": "No", @@ -107,7 +107,7 @@ "condition_effects_mental_health": false, "condition_effects_social_or_behavioral": false, "condition_effects_other": false, - "condition_effects_prefer_not_to_say": false, + "condition_effects_prefer_not_to_say": true, "reasonable_preference_reason_homeless": false, "reasonable_preference_reason_unsatisfactory_housing": false, "reasonable_preference_reason_medical_grounds": false, From 30b985dfe57a41cad1743fff8869c36e1409350f Mon Sep 17 00:00:00 2001 From: Matthew Phelan Date: Wed, 20 Oct 2021 08:28:06 +0100 Subject: [PATCH 4/4] CLDC-487 - typo fixes --- app/models/case_log.rb | 4 ++-- spec/models/case_log_spec.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 519485580..5a390976b 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -17,14 +17,14 @@ class CaseLogValidator < ActiveModel::Validator def validate_reasonable_preference(record) if record.homelessness == "No" && record.reasonable_preference == "Yes" - record.errors.add :reasonable_preference, "can not be Yes if Not Homesless imediately prior to this letting has been selected" + record.errors.add :reasonable_preference, "can not be Yes if Not Homeless immediately prior to this letting has been selected" elsif record.reasonable_preference == "Yes" if !record.reasonable_preference_reason_homeless && !record.reasonable_preference_reason_unsatisfactory_housing && !record.reasonable_preference_reason_medical_grounds && !record.reasonable_preference_reason_avoid_hardship && !record.reasonable_preference_reason_do_not_know record.errors.add :reasonable_preference_reason, "- if reasonable preference is Yes, a reason must be given" end elsif record.reasonable_preference == "No" if record.reasonable_preference_reason_homeless || record.reasonable_preference_reason_unsatisfactory_housing || record.reasonable_preference_reason_medical_grounds || record.reasonable_preference_reason_avoid_hardship || record.reasonable_preference_reason_do_not_know - record.errors.add :reasonable_preference_reason, "- if reasonable preference is no, no reasons should be given" + record.errors.add :reasonable_preference_reason, "- if reasonable preference is No, no reasons should be given" end end end diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 236ddf7dc..b27b0594e 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -39,7 +39,7 @@ RSpec.describe Form, type: :model do }.to raise_error(ActiveRecord::RecordInvalid) end - it "if not previously homesless reasonable preference should not be selected" do + it "if not previously homeless reasonable preference should not be selected" do expect { CaseLog.create!( homelessness: "No",