From c19bd07b8d6f23ab15fd0c24b8b1d10e8473c12b Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 27 Oct 2021 14:02:03 +0100 Subject: [PATCH] Extract private validdate_other_field_method --- app/models/case_log.rb | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 9c17b742f..8b9d59767 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -30,13 +30,7 @@ class CaseLogValidator < ActiveModel::Validator end def validate_other_reason_for_leaving_last_settled_home(record) - if record.reason_for_leaving_last_settled_home == "Other" && record.other_reason_for_leaving_last_settled_home.blank? - record.errors.add :other_reason_for_leaving_last_settled_home, "If reason for leaving settled home is other then the other reason must be provided" - end - - if record.reason_for_leaving_last_settled_home != "Other" && record.other_reason_for_leaving_last_settled_home.present? - record.errors.add :other_reason_for_leaving_last_settled_home, "The other reason must not be provided if the reason for leaving settled home was not other" - end + validate_other_field(record, "reason_for_leaving_last_settled_home", "other_reason_for_leaving_last_settled_home") end def validate_reason_for_leaving_last_settled_home(record) @@ -126,6 +120,18 @@ class CaseLogValidator < ActiveModel::Validator private + def validate_other_field(record, main_field, other_field) + main_field_label = main_field.humanize(capitalize: false) + other_field_label = other_field.humanize(capitalize: false) + if record[main_field] == "Other" && record[other_field].blank? + record.errors.add other_field.to_sym, "If #{main_field_label} is other then #{other_field_label} must be provided" + end + + if record[main_field] != "Other" && record[other_field].present? + record.errors.add other_field.to_sym, "#{other_field_label} must not be provided if #{main_field_label} was not other" + end + end + def women_of_child_bearing_age_in_household(record) (1..8).any? do |n| next if record["person_#{n}_gender"].nil? || record["person_#{n}_age"].nil?