Browse Source

Add validation

pull/484/head
baarkerlounger 3 years ago
parent
commit
4ef43c8738
  1. 32
      app/models/validations/local_authority_validations.rb
  2. 1
      config/locales/en.yml
  3. 35
      spec/models/validations/local_authority_validations_spec.rb

32
app/models/validations/local_authority_validations.rb

@ -9,6 +9,26 @@ module Validations::LocalAuthorityValidations
end
end
def validate_la(record)
if record.la.present? && !LONDON_BOROUGHS.include?(record.la) && record.is_london_rent?
record.errors.add :la, I18n.t("validations.property.la.london_rent")
if record.postcode_known? && record.postcode_full.present?
record.errors.add :postcode_full, I18n.t("validations.property.la.london_rent_postcode")
end
end
if record.la_known? && record.la.blank?
record.errors.add :la, I18n.t("validations.property.la.la_known")
end
if record.owning_organisation && record.owning_organisation.local_authorities.present? &&
record.la && !record.owning_organisation.local_authorities.include?(record.la)
la_name = record.form.get_question("la", record).label_from_value(record.la)
org_name = record.owning_organisation.name
record.errors.add :la, I18n.t("validations.property.la.la_invalid_for_org", org_name:, la_name:)
end
end
LONDON_BOROUGHS = %w[E09000001
E09000002
E09000003
@ -42,16 +62,4 @@ module Validations::LocalAuthorityValidations
E09000031
E09000032
E09000033].freeze
def validate_la(record)
if record.la.present? && !LONDON_BOROUGHS.include?(record.la) && record.is_london_rent?
record.errors.add :la, I18n.t("validations.property.la.london_rent")
if record.postcode_known? && record.postcode_full.present?
record.errors.add :postcode_full, I18n.t("validations.property.la.london_rent_postcode")
end
end
if record.la_known? && record.la.blank?
record.errors.add :la, I18n.t("validations.property.la.la_known")
end
end
end

1
config/locales/en.yml

@ -70,6 +70,7 @@ en:
london_rent: "Local authority must be in London"
london_rent_postcode: "The postcode must be a London postcode because you told us the rent type is London Affordable Rent or London Living Rent"
la_known: "Enter name of local authority"
la_invalid_for_org: "%{org_name} does not operate in %{la_name}"
rsnvac:
first_let_not_social: "Reason for vacancy cannot be first let if unit has been previously let as social housing"
first_let_social: "Reason for vacancy must be first let if unit has been previously let as social housing"

35
spec/models/validations/local_authority_validations_spec.rb

@ -77,5 +77,40 @@ RSpec.describe Validations::LocalAuthorityValidations do
.to include(match I18n.t("validations.property.la.la_known"))
end
end
context "when the organisation only operates in specific local authorities" do
let(:organisation) { FactoryBot.create(:organisation) }
let(:record) { FactoryBot.create(:case_log, owning_organisation: organisation) }
before do
FactoryBot.create(:organisation_la, organisation:, ons_code: "E07000178")
FactoryBot.create(:organisation_la, organisation:, ons_code: "E09000033")
end
it "validates that the local authority is one the owning organisation operates in" do
record.la = "E06000014"
local_auth_validator.validate_la(record)
expect(record.errors["la"])
.to include(match I18n.t(
"validations.property.la.la_invalid_for_org",
org_name: organisation.name,
la_name: "York",
))
end
it "expects that the local authority can be one that the owning organisation operates in" do
record.la = "E07000178"
local_auth_validator.validate_la(record)
expect(record.errors["la"]).to be_empty
end
end
context "when the organisation has not listed specific local authorities it operates in" do
it "does not validate the local authority for the organisation" do
record.la = "E06000014"
local_auth_validator.validate_la(record)
expect(record.errors["la"]).to be_empty
end
end
end
end

Loading…
Cancel
Save