Browse Source

Fix reasonable preference validation (#105)

pull/107/head
Daniel Baark 3 years ago committed by GitHub
parent
commit
7babd2cb71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      app/validations/household_validations.rb
  2. 19
      spec/models/case_log_spec.rb

8
app/validations/household_validations.rb

@ -5,12 +5,12 @@ module HouseholdValidations
if record.homeless == "No" && record.reasonpref == "Yes"
record.errors.add :reasonpref, "Can not be Yes if Not Homeless immediately prior to this letting has been selected"
elsif record.reasonpref == "Yes"
if !record.rp_homeless && !record.rp_insan_unsat && !record.rp_medwel && !record.rp_hardship && !record.rp_dontknow
record.errors.add :reasonable_preference_reason, "If reasonable preference is Yes, a reason must be given"
if [record.rp_homeless, record.rp_insan_unsat, record.rp_medwel, record.rp_hardship, record.rp_dontknow].none? { |a| a == "Yes" }
record.errors.add :reasonable_preference_reason, 'If reasonable preference is "Yes", a reason must be given'
end
elsif record.reasonpref == "No"
if record.rp_homeless || record.rp_insan_unsat || record.rp_medwel || record.rp_hardship || record.rp_dontknow
record.errors.add :reasonable_preference_reason, "If reasonable preference is No, no reasons should be given"
if [record.rp_homeless, record.rp_insan_unsat, record.rp_medwel, record.rp_hardship, record.rp_dontknow].any? { |a| a == "Yes" }
record.errors.add :reasonable_preference_reason, 'If reasonable preference is "No", no reasons should be given'
end
end
end

19
spec/models/case_log_spec.rb

@ -26,8 +26,8 @@ RSpec.describe Form, type: :model do
expect { CaseLog.create!(offered: 0) }.to raise_error(ActiveRecord::RecordInvalid)
end
context "reasonable preference validation" do
it "if given reasonable preference is yes a reason must be selected" do
context "reasonable preference is yes" do
it "validates a reason must be selected" do
expect {
CaseLog.create!(reasonpref: "Yes",
rp_homeless: nil,
@ -38,7 +38,7 @@ RSpec.describe Form, type: :model do
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "if not previously homeless reasonable preference should not be selected" do
it "validates that previously homeless should be selected" do
expect {
CaseLog.create!(
homeless: "No",
@ -46,17 +46,16 @@ RSpec.describe Form, type: :model do
)
}.to raise_error(ActiveRecord::RecordInvalid)
end
end
it "if not given reasonable preference a reason should not be selected" do
context "reasonable preference is no" do
it "validates no reason is needed" do
expect {
CaseLog.create!(
homeless: "Yes - other homelessness",
reasonpref: "No",
rp_homeless: "Yes",
)
}.to raise_error(ActiveRecord::RecordInvalid)
CaseLog.create!(reasonpref: "No", rp_homeless: "No")
}.not_to raise_error
end
end
context "reason for leaving last settled home validation" do
it "Reason for leaving must be don't know if reason for leaving settled home (Q9a) is don't know." do
expect {

Loading…
Cancel
Save