From 074c90905cab963406666444957173b634121711 Mon Sep 17 00:00:00 2001 From: Sam Seed Date: Fri, 20 Jan 2023 17:22:20 +0000 Subject: [PATCH] refactor: simplify sales property validations into one method --- app/models/validations/sales/property_validations.rb | 9 +-------- .../validations/sales/property_validations_spec.rb | 7 ++----- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/app/models/validations/sales/property_validations.rb b/app/models/validations/sales/property_validations.rb index aced46af5..fbe43224e 100644 --- a/app/models/validations/sales/property_validations.rb +++ b/app/models/validations/sales/property_validations.rb @@ -8,18 +8,11 @@ module Validations::Sales::PropertyValidations end end - def validate_property_unit_type(record) + def validate_bedsit_number_of_beds(record) return if record.proptype.blank? || record.beds.blank? unless record.proptype != 2 || record.beds <= 1 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") end end diff --git a/spec/models/validations/sales/property_validations_spec.rb b/spec/models/validations/sales/property_validations_spec.rb index a44ec5479..6b998129c 100644 --- a/spec/models/validations/sales/property_validations_spec.rb +++ b/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) } it "does not add an error if it's a bedsit" do - property_validator.validate_property_unit_type(record) - property_validator.validate_property_number_of_bedrooms(record) + property_validator.validate_bedsit_number_of_beds(record) expect(record.errors).not_to be_present end @@ -65,10 +64,8 @@ RSpec.describe Validations::Sales::PropertyValidations do let(:record) { FactoryBot.build(:sales_log, beds: 2, proptype: 2) } 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 - - property_validator.validate_property_number_of_bedrooms(record) expect(record.errors.added? :beds, "Bedsits have at most one bedroom").to be true end end