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" 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" record.errors.add :reasonpref, "Can not be Yes if Not Homeless immediately prior to this letting has been selected"
elsif record.reasonpref == "Yes" elsif record.reasonpref == "Yes"
if !record.rp_homeless && !record.rp_insan_unsat && !record.rp_medwel && !record.rp_hardship && !record.rp_dontknow 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" record.errors.add :reasonable_preference_reason, 'If reasonable preference is "Yes", a reason must be given'
end end
elsif record.reasonpref == "No" elsif record.reasonpref == "No"
if record.rp_homeless || record.rp_insan_unsat || record.rp_medwel || record.rp_hardship || record.rp_dontknow 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" record.errors.add :reasonable_preference_reason, 'If reasonable preference is "No", no reasons should be given'
end end
end 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) expect { CaseLog.create!(offered: 0) }.to raise_error(ActiveRecord::RecordInvalid)
end end
context "reasonable preference validation" do context "reasonable preference is yes" do
it "if given reasonable preference is yes a reason must be selected" do it "validates a reason must be selected" do
expect { expect {
CaseLog.create!(reasonpref: "Yes", CaseLog.create!(reasonpref: "Yes",
rp_homeless: nil, rp_homeless: nil,
@ -38,7 +38,7 @@ RSpec.describe Form, type: :model do
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
it "if not previously homeless reasonable preference should not be selected" do it "validates that previously homeless should be selected" do
expect { expect {
CaseLog.create!( CaseLog.create!(
homeless: "No", homeless: "No",
@ -46,17 +46,16 @@ RSpec.describe Form, type: :model do
) )
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end 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 { expect {
CaseLog.create!( CaseLog.create!(reasonpref: "No", rp_homeless: "No")
homeless: "Yes - other homelessness", }.not_to raise_error
reasonpref: "No",
rp_homeless: "Yes",
)
}.to raise_error(ActiveRecord::RecordInvalid)
end end
end end
context "reason for leaving last settled home validation" do 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 it "Reason for leaving must be don't know if reason for leaving settled home (Q9a) is don't know." do
expect { expect {

Loading…
Cancel
Save