From 0a8af2969c68e32cd0d4e763b9dc936b2884f031 Mon Sep 17 00:00:00 2001 From: Dushan Despotovic Date: Tue, 15 Mar 2022 16:34:07 +0000 Subject: [PATCH] intermediate_rent_product_name validation --- app/models/validations/setup_validations.rb | 7 ++++ config/locales/en.yml | 4 +++ .../validations/setup_validations_spec.rb | 33 +++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 app/models/validations/setup_validations.rb create mode 100644 spec/models/validations/setup_validations_spec.rb diff --git a/app/models/validations/setup_validations.rb b/app/models/validations/setup_validations.rb new file mode 100644 index 000000000..d0799e1c1 --- /dev/null +++ b/app/models/validations/setup_validations.rb @@ -0,0 +1,7 @@ +module Validations::SetupValidations + def validate_intermediate_rent_product_name(record) + if record.rent_type == 5 && record.intermediate_rent_product_name.blank? + record.errors.add :intermediate_rent_product_name, I18n.t("validations.setup.intermediate_rent_product_name.blank") + end + end +end diff --git a/config/locales/en.yml b/config/locales/en.yml index 9f5d8056f..857604acd 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -47,6 +47,10 @@ en: taken: "Email already exists" invalid: "Enter an email address in the correct format, like name@example.com" blank: "Enter an email address" + + setup: + intermediate_rent_product_name: + blank: "Enter an intermediate rent product name" property: mrcdate: diff --git a/spec/models/validations/setup_validations_spec.rb b/spec/models/validations/setup_validations_spec.rb new file mode 100644 index 000000000..0c333d6d7 --- /dev/null +++ b/spec/models/validations/setup_validations_spec.rb @@ -0,0 +1,33 @@ +require "rails_helper" + +RSpec.describe Validations::SetupValidations do + subject(:setup_validator) { setup_validator_class.new } + + let(:setup_validator_class) { Class.new { include Validations::SetupValidations } } + let(:record) { FactoryBot.create(:case_log) } + + describe "#validate_intermediate_rent_product_name" do + it "adds an error when the intermediate rent product name is not provided but the rent type was given as other intermediate rent product" do + record.rent_type = 5 + record.intermediate_rent_product_name = nil + setup_validator.validate_intermediate_rent_product_name(record) + expect(record.errors["intermediate_rent_product_name"]) + .to include(match I18n.t("validations.setup.intermediate_rent_product_name.blank")) + end + + it "adds an error when the intermediate rent product name is blank but the rent type was given as other intermediate rent product" do + record.rent_type = 5 + record.intermediate_rent_product_name = "" + setup_validator.validate_intermediate_rent_product_name(record) + expect(record.errors["intermediate_rent_product_name"]) + .to include(match I18n.t("validations.setup.intermediate_rent_product_name.blank")) + end + + it "Does not add an error when the intermediate rent product name is provided and the rent type was given as other intermediate rent product" do + record.rent_type = 5 + record.intermediate_rent_product_name = "Example" + setup_validator.validate_intermediate_rent_product_name(record) + expect(record.errors["intermediate_rent_product_name"]).to be_empty + end + end +end