@ -1,53 +1,4 @@
class CaseLogValidator < ActiveModel :: Validator
# Methods to be used on save and continue need to be named 'validate_'
# followed by field name this is how the metaprogramming of the method
# name being call in the validate method works.
def validate_property_number_of_times_relet ( record )
if record . property_number_of_times_relet && ! / ^[1-9]$|^0[1-9]$|^1[0-9]$|^20$ / . match? ( record . property_number_of_times_relet . to_s )
record . errors . add :property_number_of_times_relet , " Must be between 0 and 20 "
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_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
# Validations methods need to be called 'validate_' to run on model save
include HouseholdValidations
include PropertyValidations