Browse Source

Merge pull request #52 from communitiesuk/CLDC-487/why-were-they-given-reasonable-preference

Why reasonable preference? validation added
pull/49/head
Matthew J. Phelan 3 years ago committed by GitHub
parent
commit
dcf561150e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      app/javascript/controllers/conditional_question_controller.js
  2. 18
      app/models/case_log.rb
  3. 4
      spec/fixtures/complete_case_log.json
  4. 32
      spec/models/case_log_spec.rb

5
app/javascript/controllers/conditional_question_controller.js

@ -28,7 +28,10 @@ export default class extends Controller {
div.style.display = "block" div.style.display = "block"
} else { } else {
div.style.display = "none" 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]) => { Object.entries(buttons).forEach(([idx, button]) => {
button.checked = false; button.checked = false;
}) })

18
app/models/case_log.rb

@ -15,6 +15,20 @@ class CaseLogValidator < ActiveModel::Validator
end 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 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"
end
end
end
def validate_other_reason_for_leaving_last_settled_home(record) def validate_other_reason_for_leaving_last_settled_home(record)
if record.reason_for_leaving_last_settled_home == "Other" && record.other_reason_for_leaving_last_settled_home.blank? if record.reason_for_leaving_last_settled_home == "Other" && record.other_reason_for_leaving_last_settled_home.blank?
record.errors.add :other_reason_for_leaving_last_settled_home, "If reason for leaving settled home is other then the other reason must be provided" record.errors.add :other_reason_for_leaving_last_settled_home, "If reason for leaving settled home is other then the other reason must be provided"
@ -30,8 +44,10 @@ class CaseLogValidator < ActiveModel::Validator
# that have just been submitted. If we're submitting a log via API or Bulk Upload # that have just been submitted. If we're submitting a log via API or Bulk Upload
# we want to validate all data fields. # we want to validate all data fields.
question_to_validate = options[:previous_page] question_to_validate = options[:previous_page]
if question_to_validate && respond_to?("validate_#{question_to_validate}") if question_to_validate
if respond_to?("validate_#{question_to_validate}")
public_send("validate_#{question_to_validate}", record) public_send("validate_#{question_to_validate}", record)
end
else else
# This assumes that all methods in this class other than this one are # This assumes that all methods in this class other than this one are
# validations to be run # validations to be run

4
spec/fixtures/complete_case_log.json vendored

@ -38,7 +38,7 @@
"person_8_age": 2, "person_8_age": 2,
"person_8_gender": "Prefer not to say", "person_8_gender": "Prefer not to say",
"person_8_economic_status": "Child under 16", "person_8_economic_status": "Child under 16",
"homelessness": "No", "homelessness": "Yes - other homelessness",
"reason_for_leaving_last_settled_home": "Other problems with neighbours", "reason_for_leaving_last_settled_home": "Other problems with neighbours",
"benefit_cap_spare_room_subsidy": "No", "benefit_cap_spare_room_subsidy": "No",
"armed_forces_active": "No", "armed_forces_active": "No",
@ -107,7 +107,7 @@
"condition_effects_mental_health": false, "condition_effects_mental_health": false,
"condition_effects_social_or_behavioral": false, "condition_effects_social_or_behavioral": false,
"condition_effects_other": 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_homeless": false,
"reasonable_preference_reason_unsatisfactory_housing": false, "reasonable_preference_reason_unsatisfactory_housing": false,
"reasonable_preference_reason_medical_grounds": false, "reasonable_preference_reason_medical_grounds": false,

32
spec/models/case_log_spec.rb

@ -26,6 +26,38 @@ RSpec.describe Form, type: :model do
expect { CaseLog.create!(property_number_of_times_relet: 0) }.to raise_error(ActiveRecord::RecordInvalid) expect { CaseLog.create!(property_number_of_times_relet: 0) }.to raise_error(ActiveRecord::RecordInvalid)
end 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 homeless 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
context "other reason for leaving last settled home validation" do context "other reason for leaving last settled home validation" do
it "must be provided if main reason for leaving last settled home was given as other" do it "must be provided if main reason for leaving last settled home was given as other" do
expect { expect {

Loading…
Cancel
Save