From dcb16bd77179775a2c02ca15ffbfc3d63e6860eb Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 27 Oct 2021 14:08:19 +0100 Subject: [PATCH] Implement other tenancy type validation --- app/models/case_log.rb | 4 ++++ spec/models/case_log_spec.rb | 27 ++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 8b9d59767..8d912354b 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -101,6 +101,10 @@ class CaseLogValidator < ActiveModel::Validator conditions.each { |condition| condition[:condition] ? (record.errors.add :fixed_term_tenancy, condition[:error]) : nil } end + def validate_other_tenancy_type(record) + validate_other_field(record, "tenancy_type", "other_tenancy_type") + end + def validate(record) # If we've come from the form UI we only want to validate the specific fields # that have just been submitted. If we're submitting a log via API or Bulk Upload diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 0b8d7fa1f..d95f000a7 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -201,7 +201,32 @@ RSpec.describe Form, type: :model do expect { CaseLog.create!(armed_forces: "Yes - a regular", armed_forces_active: "Yes", - armed_forces_injured: "Yes") + armed_forces_injured: "Yes")} + end + end + + context "other tenancy type validation" do + it "must be provided if tenancy type was given as other" do + expect { + CaseLog.create!(tenancy_type: "Other", + other_tenancy_type: nil) + }.to raise_error(ActiveRecord::RecordInvalid) + + expect { + CaseLog.create!(tenancy_type: "Other", + other_tenancy_type: "type") + }.not_to raise_error + end + + it "must not be provided if tenancy type is not other" do + expect { + CaseLog.create!(tenancy_type: "Fixed", + other_tenancy_type: "the other reason provided") + }.to raise_error(ActiveRecord::RecordInvalid) + + expect { + CaseLog.create!(tenancy_type: "Fixed", + other_tenancy_type: nil) }.not_to raise_error end end