Browse Source

Merge pull request #54 from communitiesuk/CLDC-476-left-settled-home-validation

add other reason left settled home validation
pull/52/head^2
Dushan 3 years ago committed by GitHub
parent
commit
5b75e9ff70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      app/models/case_log.rb
  2. 2
      spec/fixtures/complete_case_log.json
  3. 16
      spec/models/case_log_spec.rb

20
app/models/case_log.rb

@ -15,6 +15,16 @@ class CaseLogValidator < ActiveModel::Validator
end end
end end
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?
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"
end
if record.reason_for_leaving_last_settled_home != "Other" && record.other_reason_for_leaving_last_settled_home.present?
record.errors.add :other_reason_for_leaving_last_settled_home, "The other reason must not be provided if the reason for leaving settled home was not other"
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
@ -87,6 +97,14 @@ private
end end
def mandatory_fields def mandatory_fields
attributes.except(*AUTOGENERATED_FIELDS) required = attributes.except(*AUTOGENERATED_FIELDS)
dynamically_not_required = []
if reason_for_leaving_last_settled_home != "Other"
dynamically_not_required << "other_reason_for_leaving_last_settled_home"
end
required.delete_if { |key, _value| dynamically_not_required.include?(key) }
end end
end end

2
spec/fixtures/complete_case_log.json vendored

@ -89,7 +89,7 @@
"chr_letting": false, "chr_letting": false,
"cap_letting": false, "cap_letting": false,
"outstanding_rent_or_charges": 25, "outstanding_rent_or_charges": 25,
"other_reason_for_leaving_last_settled_home": "Other reason", "other_reason_for_leaving_last_settled_home": null,
"accessibility_requirements_fully_wheelchair_accessible_housing": true, "accessibility_requirements_fully_wheelchair_accessible_housing": true,
"accessibility_requirements_wheelchair_access_to_essential_rooms": false, "accessibility_requirements_wheelchair_access_to_essential_rooms": false,
"accessibility_requirements_level_access_housing": false, "accessibility_requirements_level_access_housing": false,

16
spec/models/case_log_spec.rb

@ -25,6 +25,22 @@ 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
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
expect {
CaseLog.create!(reason_for_leaving_last_settled_home: "Other",
other_reason_for_leaving_last_settled_home: nil)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "must not be provided if the main reason for leaving settled home is not other" do
expect {
CaseLog.create!(reason_for_leaving_last_settled_home: "Repossession",
other_reason_for_leaving_last_settled_home: "the other reason provided")
}.to raise_error(ActiveRecord::RecordInvalid)
end
end
end end
describe "status" do describe "status" do

Loading…
Cancel
Save