Browse Source

CLDC-812 rent type validation (#391)

* intermediate_rent_product_name validation

* add wrapper method

* include in case log

* review changes
routes-to-csv
Dushan 3 years ago committed by GitHub
parent
commit
8d861f4d20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      app/models/case_log.rb
  2. 13
      app/models/validations/setup_validations.rb
  3. 4
      config/locales/en.yml
  4. 4
      spec/models/case_log_spec.rb
  5. 33
      spec/models/validations/setup_validations_spec.rb

1
app/models/case_log.rb

@ -1,6 +1,7 @@
class CaseLogValidator < ActiveModel::Validator class CaseLogValidator < ActiveModel::Validator
# Validations methods need to be called 'validate_' to run on model save # Validations methods need to be called 'validate_' to run on model save
# or form page submission # or form page submission
include Validations::SetupValidations
include Validations::HouseholdValidations include Validations::HouseholdValidations
include Validations::PropertyValidations include Validations::PropertyValidations
include Validations::FinancialValidations include Validations::FinancialValidations

13
app/models/validations/setup_validations.rb

@ -0,0 +1,13 @@
module Validations::SetupValidations
def validate_intermediate_rent_product_name(record)
if intermediate_product_rent_type?(record) && record.intermediate_rent_product_name.blank?
record.errors.add :intermediate_rent_product_name, I18n.t("validations.setup.intermediate_rent_product_name.blank")
end
end
private
def intermediate_product_rent_type?(record)
record.rent_type == 5
end
end

4
config/locales/en.yml

@ -47,6 +47,10 @@ en:
taken: "Email already exists" taken: "Email already exists"
invalid: "Enter an email address in the correct format, like name@example.com" invalid: "Enter an email address in the correct format, like name@example.com"
blank: "Enter an email address" blank: "Enter an email address"
setup:
intermediate_rent_product_name:
blank: "Enter name of other intermediate rent product"
property: property:
mrcdate: mrcdate:

4
spec/models/case_log_spec.rb

@ -56,6 +56,10 @@ RSpec.describe CaseLog do
expect(validator).to receive(:validate_startdate) expect(validator).to receive(:validate_startdate)
end end
it "validates intermediate rent product name" do
expect(validator).to receive(:validate_intermediate_rent_product_name)
end
it "validates other household member details" do it "validates other household member details" do
expect(validator).to receive(:validate_household_number_of_other_members) expect(validator).to receive(:validate_household_number_of_other_members)
end end

33
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
Loading…
Cancel
Save