You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
189 lines
6.6 KiB
189 lines
6.6 KiB
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 |
|
include FinancialValidations |
|
include TenancyValidations |
|
|
|
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 |
|
# we want to validate all data fields. |
|
question_to_validate = options[:previous_page] |
|
if question_to_validate |
|
if respond_to?("validate_#{question_to_validate}") |
|
public_send("validate_#{question_to_validate}", record) |
|
end |
|
else |
|
validation_methods = public_methods.select { |method| method.starts_with?("validate_") } |
|
validation_methods.each { |meth| public_send(meth, record) } |
|
end |
|
end |
|
|
|
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 |
|
end |
|
|
|
class CaseLog < ApplicationRecord |
|
include Discard::Model |
|
default_scope -> { kept } |
|
scope :not_started, -> { where(status: "not_started") } |
|
scope :in_progress, -> { where(status: "in_progress") } |
|
scope :not_completed, -> { where.not(status: "completed") } |
|
scope :completed, -> { where(status: "completed") } |
|
|
|
validate :instance_validations |
|
before_save :update_status! |
|
|
|
attr_writer :previous_page |
|
|
|
enum status: { "not_started" => 0, "in_progress" => 1, "completed" => 2 } |
|
|
|
AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at].freeze |
|
|
|
def instance_validations |
|
validates_with CaseLogValidator, ({ previous_page: @previous_page } || {}) |
|
end |
|
|
|
def self.editable_fields |
|
attribute_names - AUTOGENERATED_FIELDS |
|
end |
|
|
|
def completed? |
|
status == "completed" |
|
end |
|
|
|
def not_started? |
|
status == "not_started" |
|
end |
|
|
|
def in_progress? |
|
status == "in_progress" |
|
end |
|
|
|
def weekly_net_income |
|
case net_income_frequency |
|
when "Weekly" |
|
net_income |
|
when "Monthly" |
|
((net_income * 12) / 52.0).round(0) |
|
when "Yearly" |
|
(net_income / 12.0).round(0) |
|
end |
|
end |
|
|
|
def applicable_income_range |
|
return unless person_1_economic_status |
|
|
|
IncomeRange::ALLOWED[person_1_economic_status.to_sym] |
|
end |
|
|
|
private |
|
|
|
def update_status! |
|
self.status = if all_fields_completed? && errors.empty? |
|
"completed" |
|
elsif all_fields_nil? |
|
"not_started" |
|
else |
|
"in_progress" |
|
end |
|
end |
|
|
|
def all_fields_completed? |
|
mandatory_fields.none? { |_key, val| val.nil? } |
|
end |
|
|
|
def all_fields_nil? |
|
mandatory_fields.all? { |_key, val| val.nil? } |
|
end |
|
|
|
def mandatory_fields |
|
required = attributes.except(*AUTOGENERATED_FIELDS) |
|
|
|
dynamically_not_required = [] |
|
|
|
if reason_for_leaving_last_settled_home != "Other" |
|
dynamically_not_required << "other_reason_for_leaving_last_settled_home" |
|
end |
|
|
|
if net_income.to_i.zero? |
|
dynamically_not_required << "net_income_frequency" |
|
end |
|
|
|
if tenancy_type == "Fixed term – Secure" |
|
dynamically_not_required << "fixed_term_tenancy" |
|
end |
|
|
|
unless tenancy_type == "Other" |
|
dynamically_not_required << "other_tenancy_type" |
|
end |
|
|
|
unless net_income_known == "Yes" |
|
dynamically_not_required << "net_income" |
|
dynamically_not_required << "net_income_frequency" |
|
end |
|
|
|
required.delete_if { |key, _value| dynamically_not_required.include?(key) } |
|
end |
|
end
|
|
|