Browse Source

Fix first let validations (#185)

pull/189/head
baarkerlounger 3 years ago committed by GitHub
parent
commit
bbb13e4a24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      app/models/case_log.rb
  2. 18
      app/models/validations/property_validations.rb
  3. 15
      spec/features/form/validations_spec.rb
  4. 2
      spec/fixtures/complete_case_log.json
  5. 80
      spec/models/case_log_spec.rb

4
app/models/case_log.rb

@ -180,6 +180,10 @@ class CaseLog < ApplicationRecord
ALLOWED_INCOME_RANGES[ecstat1.to_sym] ALLOWED_INCOME_RANGES[ecstat1.to_sym]
end end
def first_time_property_let_as_social_housing?
first_time_property_let_as_social_housing == "Yes"
end
private private
PIO = Postcodes::IO.new PIO = Postcodes::IO.new

18
app/models/validations/property_validations.rb

@ -2,6 +2,7 @@ module Validations::PropertyValidations
# Validations methods need to be called 'validate_<page_name>' to run on model save # Validations methods need to be called 'validate_<page_name>' to run on model save
# or 'validate_' to run on submit as well # or 'validate_' to run on submit as well
include Constants::CaseLog include Constants::CaseLog
def validate_property_number_of_times_relet(record) def validate_property_number_of_times_relet(record)
if record.offered && !/^[1-9]$|^0[1-9]$|^1[0-9]$|^20$/.match?(record.offered.to_s) if record.offered && !/^[1-9]$|^0[1-9]$|^1[0-9]$|^20$/.match?(record.offered.to_s)
record.errors.add :offered, "Property number of times relet must be between 0 and 20" record.errors.add :offered, "Property number of times relet must be between 0 and 20"
@ -14,9 +15,22 @@ module Validations::PropertyValidations
end end
end end
FIRST_LET_VACANCY_REASONS = ["First let of newbuild property",
"First let of conversion/rehabilitation/acquired property",
"First let of leased property"].freeze
def validate_rsnvac(record)
if !record.first_time_property_let_as_social_housing? && FIRST_LET_VACANCY_REASONS.include?(record.rsnvac)
record.errors.add :rsnvac, "Reason for vacancy cannot be first let if unit has been previously let as social housing"
end
if record.first_time_property_let_as_social_housing? && record.rsnvac.present? && !FIRST_LET_VACANCY_REASONS.include?(record.rsnvac)
record.errors.add :rsnvac, "Reason for vacancy must be first let if unit has been previously let as social housing"
end
end
def validate_unitletas(record) def validate_unitletas(record)
if record.unitletas.present? && (record.rsnvac == "First let of newbuild property" || record.rsnvac == "First let of conversion/rehabilitation/acquired property" || record.rsnvac == "First let of leased property") if record.first_time_property_let_as_social_housing? && record.unitletas.present?
record.errors.add :unitletas, "Can not be completed if it is the first let of the property" record.errors.add :unitletas, "Property cannot have a previous let type if it is being let as social housing for the first time"
end end
end end
end end

15
spec/features/form/validations_spec.rb

@ -162,19 +162,4 @@ RSpec.describe "validations" do
end end
end end
end end
describe "Property Validations" do
context "first let of property and reason for vacancy completed" do
let(:rsnvac) { "First let of newbuild property" }
let(:rsnvac1) { "First let of conversion/rehabilitation/acquired property" }
let(:rsnvac2) { "First let of leased property" }
let(:unitletas) { "Social rent basis" }
it "throws a validation error", js: true do
expect { case_log.update!(rsnvac: rsnvac, unitletas: unitletas) }.to raise_error(ActiveRecord::RecordInvalid)
expect { case_log.update!(rsnvac: rsnvac1, unitletas: unitletas) }.to raise_error(ActiveRecord::RecordInvalid)
expect { case_log.update!(rsnvac: rsnvac2, unitletas: unitletas) }.to raise_error(ActiveRecord::RecordInvalid)
end
end
end
end end

2
spec/fixtures/complete_case_log.json vendored

@ -139,7 +139,7 @@
"ppostc1": "w3", "ppostc1": "w3",
"ppostc2": "w3", "ppostc2": "w3",
"why_dont_you_know_la": "Forgot", "why_dont_you_know_la": "Forgot",
"first_time_property_let_as_social_housing": "Yes", "first_time_property_let_as_social_housing": "No",
"unitletas": "Affordable rent basis", "unitletas": "Affordable rent basis",
"builtype": "Purpose built", "builtype": "Purpose built",
"property_wheelchair_accessible": "Yes", "property_wheelchair_accessible": "Yes",

80
spec/models/case_log_spec.rb

@ -253,6 +253,86 @@ RSpec.describe Form, type: :model do
end end
end end
context "Property vacancy and let as validations" do
it "cannot have a previously let as type, if it hasn't been let before" do
expect {
CaseLog.create!(
first_time_property_let_as_social_housing: "No",
unitletas: "Social rent basis",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.not_to raise_error
expect {
CaseLog.create!(
first_time_property_let_as_social_housing: "Yes",
unitletas: "Social rent basis",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
CaseLog.create!(
first_time_property_let_as_social_housing: "Yes",
unitletas: "Affordable rent basis",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
CaseLog.create!(
first_time_property_let_as_social_housing: "Yes",
unitletas: "Intermediate rent basis",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
CaseLog.create!(
first_time_property_let_as_social_housing: "Yes",
unitletas: "Do not know",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "must have a first let reason for vacancy if it's being let as social housing for the first time" do
expect {
CaseLog.create!(
first_time_property_let_as_social_housing: "Yes",
rsnvac: "First let of newbuild property",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.not_to raise_error
expect {
CaseLog.create!(
first_time_property_let_as_social_housing: "Yes",
rsnvac: "First let of conversion/rehabilitation/acquired property",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.not_to raise_error
expect {
CaseLog.create!(
first_time_property_let_as_social_housing: "Yes",
rsnvac: "First let of leased property",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.not_to raise_error
expect {
CaseLog.create!(
first_time_property_let_as_social_housing: "Yes",
rsnvac: "Tenant moved to care home",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end
end
context "Shared accomodation bedrooms validation" do context "Shared accomodation bedrooms validation" do
it "you must have more than zero bedrooms" do it "you must have more than zero bedrooms" do
expect { expect {

Loading…
Cancel
Save