Browse Source

Why reasonable preference? validation added

pull/52/head
Matthew Phelan 3 years ago
parent
commit
8ad852454f
  1. 5
      app/javascript/controllers/conditional_question_controller.js
  2. 24
      app/models/case_log.rb
  3. 33
      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;
}) })

24
app/models/case_log.rb

@ -15,13 +15,33 @@ class CaseLogValidator < ActiveModel::Validator
end end
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) def validate(record)
# If we've come from the form UI we only want to validate the specific fields # 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 # 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
public_send("validate_#{question_to_validate}", record) if respond_to?("validate_#{question_to_validate}")
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

33
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 it "validates number of relets is over 0" 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 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 end
describe "status" do describe "status" do

Loading…
Cancel
Save