Browse Source

Extract private validdate_other_field_method

pull/68/head
Kat 4 years ago
parent
commit
c19bd07b8d
  1. 20
      app/models/case_log.rb

20
app/models/case_log.rb

@ -30,13 +30,7 @@ class CaseLogValidator < ActiveModel::Validator
end end
def validate_other_reason_for_leaving_last_settled_home(record) 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? validate_other_field(record, "reason_for_leaving_last_settled_home", "other_reason_for_leaving_last_settled_home")
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
end end
def validate_reason_for_leaving_last_settled_home(record) def validate_reason_for_leaving_last_settled_home(record)
@ -126,6 +120,18 @@ class CaseLogValidator < ActiveModel::Validator
private 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) def women_of_child_bearing_age_in_household(record)
(1..8).any? do |n| (1..8).any? do |n|
next if record["person_#{n}_gender"].nil? || record["person_#{n}_age"].nil? next if record["person_#{n}_gender"].nil? || record["person_#{n}_age"].nil?

Loading…
Cancel
Save