@ -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 )
@ -55,12 +49,62 @@ class CaseLogValidator < ActiveModel::Validator
end
end
end
end
def validate_outstanding_rent_amount ( record )
if record . outstanding_rent_or_charges == " Yes " && record . outstanding_amount . blank?
record . errors . add :outstanding_amount , " You must answer the oustanding amout question if you have outstanding rent or charges. "
end
if record . outstanding_rent_or_charges == " No " && record . outstanding_amount . present?
record . errors . add :outstanding_amount , " You must not answer the oustanding amout question if you don't have outstanding rent or charges. "
end
end
EMPLOYED_STATUSES = [ " Full-time - 30 hours or more " , " Part-time - Less than 30 hours " ] . freeze
def validate_net_income_uc_proportion ( record )
( 1 .. 8 ) . any? do | n |
economic_status = record [ " person_ #{ n } _economic_status " ]
is_employed = EMPLOYED_STATUSES . include? ( economic_status )
relationship = record [ " person_ #{ n } _relationship " ]
is_partner_or_main = relationship == " Partner " || ( relationship . nil? && economic_status . present? )
if is_employed && is_partner_or_main && record . net_income_uc_proportion == " All "
record . errors . add :net_income_uc_proportion , " income is from Universal Credit, state pensions or benefits cannot be All if the tenant or the partner works part or full time "
end
end
end
def validate_armed_forces_active_response ( record )
if record . armed_forces == " Yes - a regular " && record . armed_forces_active . blank?
record . errors . add :armed_forces_active , " You must answer the armed forces active question if the tenant has served as a regular in the armed forces "
end
if record . armed_forces != " Yes - a regular " && record . armed_forces_active . present?
record . errors . add :armed_forces_active , " You must not answer the armed forces active question if the tenant has not served as a regular in the armed forces "
end
end
def validate_household_pregnancy ( record )
def validate_household_pregnancy ( record )
if ( record . pregnancy == " Yes " || record . pregnancy == " Prefer not to say " ) && ! women_of_child_bearing_age_in_household ( record )
if ( record . pregnancy == " Yes " || record . pregnancy == " Prefer not to say " ) && ! women_of_child_bearing_age_in_household ( record )
record . errors . add :pregnancy , " You must answer no as there are no female tenants aged 16-50 in the property "
record . errors . add :pregnancy , " You must answer no as there are no female tenants aged 16-50 in the property "
end
end
end
end
def validate_fixed_term_tenancy ( record )
is_present = record . fixed_term_tenancy . present?
is_in_range = record . fixed_term_tenancy . to_i . between? ( 2 , 99 )
is_secure = record . tenancy_type == " Fixed term – Secure "
is_ast = record . tenancy_type == " Fixed term – Assured Shorthold Tenancy (AST) "
conditions = [
{ condition : ! ( is_secure || is_ast ) && is_present , error : " You must only answer the fixed term tenancy length question if the tenancy type is fixed term " } ,
{ condition : is_ast && ! is_in_range , error : " Fixed term – Assured Shorthold Tenancy (AST) should be between 2 and 99 years " } ,
{ condition : is_secure && ( ! is_in_range && is_present ) , error : " Fixed term – Secure should be between 2 and 99 years or not specified " } ,
]
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_shared_housing_rooms ( record )
def validate_shared_housing_rooms ( record )
number_of_tenants = people_in_household ( record )
number_of_tenants = people_in_household ( record )
@ -100,6 +144,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?
@ -188,6 +244,14 @@ private
dynamically_not_required << " net_income_frequency "
dynamically_not_required << " net_income_frequency "
end
end
if tenancy_type == " Fixed term – Secure "
dynamically_not_required << " fixed_term_tenancy "
end
if tenancy_type != " Other "
dynamically_not_required << " other_tenancy_type "
end
required . delete_if { | key , _value | dynamically_not_required . include? ( key ) }
required . delete_if { | key , _value | dynamically_not_required . include? ( key ) }
end
end
end
end