Browse Source

refactor: simplify sales property validations into one method

pull/1188/head
Sam Seed 3 years ago
parent
commit
074c90905c
  1. 9
      app/models/validations/sales/property_validations.rb
  2. 7
      spec/models/validations/sales/property_validations_spec.rb

9
app/models/validations/sales/property_validations.rb

@ -8,18 +8,11 @@ module Validations::Sales::PropertyValidations
end end
end end
def validate_property_unit_type(record) def validate_bedsit_number_of_beds(record)
return if record.proptype.blank? || record.beds.blank? return if record.proptype.blank? || record.beds.blank?
unless record.proptype != 2 || record.beds <= 1 unless record.proptype != 2 || record.beds <= 1
record.errors.add :proptype, I18n.t("validations.property.proptype.bedsits_have_max_one_bedroom") record.errors.add :proptype, I18n.t("validations.property.proptype.bedsits_have_max_one_bedroom")
end
end
def validate_property_number_of_bedrooms(record)
return if record.proptype.blank? || record.beds.blank?
unless record.proptype != 2 || record.beds <= 1
record.errors.add :beds, I18n.t("validations.property.beds.bedsits_have_max_one_bedroom") record.errors.add :beds, I18n.t("validations.property.beds.bedsits_have_max_one_bedroom")
end end
end end

7
spec/models/validations/sales/property_validations_spec.rb

@ -54,8 +54,7 @@ RSpec.describe Validations::Sales::PropertyValidations do
let(:record) { FactoryBot.build(:sales_log, beds: 1, proptype: 2) } let(:record) { FactoryBot.build(:sales_log, beds: 1, proptype: 2) }
it "does not add an error if it's a bedsit" do it "does not add an error if it's a bedsit" do
property_validator.validate_property_unit_type(record) property_validator.validate_bedsit_number_of_beds(record)
property_validator.validate_property_number_of_bedrooms(record)
expect(record.errors).not_to be_present expect(record.errors).not_to be_present
end end
@ -65,10 +64,8 @@ RSpec.describe Validations::Sales::PropertyValidations do
let(:record) { FactoryBot.build(:sales_log, beds: 2, proptype: 2) } let(:record) { FactoryBot.build(:sales_log, beds: 2, proptype: 2) }
it "does add an error if it's a bedsit" do it "does add an error if it's a bedsit" do
property_validator.validate_property_unit_type(record) property_validator.validate_bedsit_number_of_beds(record)
expect(record.errors.added? :proptype, "Properties with 2 or more bedrooms cannot be bedsits").to be true expect(record.errors.added? :proptype, "Properties with 2 or more bedrooms cannot be bedsits").to be true
property_validator.validate_property_number_of_bedrooms(record)
expect(record.errors.added? :beds, "Bedsits have at most one bedroom").to be true expect(record.errors.added? :beds, "Bedsits have at most one bedroom").to be true
end end
end end

Loading…
Cancel
Save