Browse Source

Display number of bedrooms validations on bedroom questions and add validation for negative number of bedrooms (#331)

pull/333/head
kosiakkatrina 3 years ago committed by GitHub
parent
commit
ec8a905dbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      app/models/validations/property_validations.rb
  2. 2
      config/locales/en.yml
  3. 13
      spec/models/validations/property_validations_spec.rb

7
app/models/validations/property_validations.rb

@ -66,16 +66,23 @@ module Validations::PropertyValidations
end end
def validate_shared_housing_rooms(record) def validate_shared_housing_rooms(record)
if record.beds.present? && record.beds.negative?
record.errors.add :beds, I18n.t("validations.property.beds.negative")
end
unless record.unittype_gn.nil? unless record.unittype_gn.nil?
if record.unittype_gn == "Bedsit" && record.beds != 1 && record.beds.present? if record.unittype_gn == "Bedsit" && record.beds != 1 && record.beds.present?
record.errors.add :unittype_gn, I18n.t("validations.property.unittype_gn.one_bedroom_bedsit") record.errors.add :unittype_gn, I18n.t("validations.property.unittype_gn.one_bedroom_bedsit")
record.errors.add :beds, I18n.t("validations.property.unittype_gn.one_bedroom_bedsit")
end end
if record.other_hhmemb&.zero? && record.unittype_gn.include?("Shared") && if record.other_hhmemb&.zero? && record.unittype_gn.include?("Shared") &&
!record.beds.to_i.between?(1, 3) && record.beds.present? !record.beds.to_i.between?(1, 3) && record.beds.present?
record.errors.add :unittype_gn, I18n.t("validations.property.unittype_gn.one_three_bedroom_single_tenant_shared") record.errors.add :unittype_gn, I18n.t("validations.property.unittype_gn.one_three_bedroom_single_tenant_shared")
record.errors.add :beds, I18n.t("validations.property.unittype_gn.one_three_bedroom_single_tenant_shared")
elsif record.unittype_gn.include?("Shared") && record.beds.present? && !record.beds.to_i.between?(1, 7) elsif record.unittype_gn.include?("Shared") && record.beds.present? && !record.beds.to_i.between?(1, 7)
record.errors.add :unittype_gn, I18n.t("validations.property.unittype_gn.one_seven_bedroom_shared") record.errors.add :unittype_gn, I18n.t("validations.property.unittype_gn.one_seven_bedroom_shared")
record.errors.add :beds, I18n.t("validations.property.unittype_gn.one_seven_bedroom_shared")
end end
end end
end end

2
config/locales/en.yml

@ -65,6 +65,8 @@ en:
one_bedroom_bedsit: "A bedsit can only have one bedroom" one_bedroom_bedsit: "A bedsit can only have one bedroom"
one_seven_bedroom_shared: "A shared house must have 1 to 7 bedrooms" one_seven_bedroom_shared: "A shared house must have 1 to 7 bedrooms"
one_three_bedroom_single_tenant_shared: "A shared house with less than two tenants must have 1 to 3 bedrooms" one_three_bedroom_single_tenant_shared: "A shared house with less than two tenants must have 1 to 3 bedrooms"
beds:
negative: "Number of bedrooms has to be greater than 0"
financial: financial:
tshortfall: tshortfall:

13
spec/models/validations/property_validations_spec.rb

@ -75,6 +75,7 @@ RSpec.describe Validations::PropertyValidations do
record.unittype_gn = "Bedsit" record.unittype_gn = "Bedsit"
property_validator.validate_shared_housing_rooms(record) property_validator.validate_shared_housing_rooms(record)
expect(record.errors["unittype_gn"]).to include(match(expected_error)) expect(record.errors["unittype_gn"]).to include(match(expected_error))
expect(record.errors["beds"]).to include(I18n.t("validations.property.unittype_gn.one_bedroom_bedsit"))
end end
end end
@ -86,6 +87,7 @@ RSpec.describe Validations::PropertyValidations do
record.unittype_gn = "Bedsit" record.unittype_gn = "Bedsit"
property_validator.validate_shared_housing_rooms(record) property_validator.validate_shared_housing_rooms(record)
expect(record.errors["unittype_gn"]).to include(match(expected_error)) expect(record.errors["unittype_gn"]).to include(match(expected_error))
expect(record.errors["beds"]).to include(I18n.t("validations.property.unittype_gn.one_bedroom_bedsit"))
end end
end end
@ -98,6 +100,7 @@ RSpec.describe Validations::PropertyValidations do
record.other_hhmemb = 2 record.other_hhmemb = 2
property_validator.validate_shared_housing_rooms(record) property_validator.validate_shared_housing_rooms(record)
expect(record.errors["unittype_gn"]).to include(match(expected_error)) expect(record.errors["unittype_gn"]).to include(match(expected_error))
expect(record.errors["beds"]).to include(I18n.t("validations.property.unittype_gn.one_seven_bedroom_shared"))
end end
end end
@ -110,6 +113,7 @@ RSpec.describe Validations::PropertyValidations do
record.other_hhmemb = 2 record.other_hhmemb = 2
property_validator.validate_shared_housing_rooms(record) property_validator.validate_shared_housing_rooms(record)
expect(record.errors["unittype_gn"]).to include(match(expected_error)) expect(record.errors["unittype_gn"]).to include(match(expected_error))
expect(record.errors["beds"]).to include(I18n.t("validations.property.unittype_gn.one_seven_bedroom_shared"))
end end
end end
@ -122,6 +126,15 @@ RSpec.describe Validations::PropertyValidations do
record.other_hhmemb = 0 record.other_hhmemb = 0
property_validator.validate_shared_housing_rooms(record) property_validator.validate_shared_housing_rooms(record)
expect(record.errors["unittype_gn"]).to include(match(expected_error)) expect(record.errors["unittype_gn"]).to include(match(expected_error))
expect(record.errors["beds"]).to include(I18n.t("validations.property.unittype_gn.one_three_bedroom_single_tenant_shared"))
end
end
context "when a negative number of bedrooms is entered" do
it "adds an error" do
record.beds = -4
property_validator.validate_shared_housing_rooms(record)
expect(record.errors["beds"]).to include(I18n.t("validations.property.beds.negative"))
end end
end end
end end

Loading…
Cancel
Save