|
|
@ -1,7 +1,7 @@ |
|
|
|
class CaseLogValidator < ActiveModel::Validator |
|
|
|
class CaseLogValidator < ActiveModel::Validator |
|
|
|
# Methods need to be named 'validate_' followed by field name |
|
|
|
# Methods to be used on save and continue need to be named 'validate_' |
|
|
|
# this is how the metaprogramming of the method name being |
|
|
|
# followed by field name this is how the metaprogramming of the method |
|
|
|
# call in the validate method works. |
|
|
|
# name being call in the validate method works. |
|
|
|
|
|
|
|
|
|
|
|
def validate_tenant_age(record) |
|
|
|
def validate_tenant_age(record) |
|
|
|
if record.tenant_age && !/^[1-9][0-9]?$|^120$/.match?(record.tenant_age.to_s) |
|
|
|
if record.tenant_age && !/^[1-9][0-9]?$|^120$/.match?(record.tenant_age.to_s) |
|
|
@ -9,13 +9,19 @@ class CaseLogValidator < ActiveModel::Validator |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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(record) |
|
|
|
def validate(record) |
|
|
|
# If we've come from the form UI we only want to validate the specific fields |
|
|
|
# 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 |
|
|
|
# that have just been submitted. If we're submitting a log via API or Bulk Upload |
|
|
|
# we want to validate all data fields. |
|
|
|
# we want to validate all data fields. |
|
|
|
question_to_validate = options[:previous_page] |
|
|
|
question_to_validate = options[:previous_page] |
|
|
|
if question_to_validate && respond_to?("validate_#{question_to_validate}") |
|
|
|
if question_to_validate && respond_to?("validate_#{question_to_validate}") |
|
|
|
public_send("validate_#{question_to_validate}", record) |
|
|
|
public_send("validate_#{question_to_validate}", record) |
|
|
|
else |
|
|
|
else |
|
|
|
# This assumes that all methods in this class other than this one are |
|
|
|
# This assumes that all methods in this class other than this one are |
|
|
|
# validations to be run |
|
|
|
# validations to be run |
|
|
@ -30,7 +36,7 @@ class CaseLog < ApplicationRecord |
|
|
|
before_save :update_status! |
|
|
|
before_save :update_status! |
|
|
|
|
|
|
|
|
|
|
|
attr_writer :previous_page |
|
|
|
attr_writer :previous_page |
|
|
|
|
|
|
|
|
|
|
|
enum status: { "in progress" => 0, "submitted" => 1 } |
|
|
|
enum status: { "in progress" => 0, "submitted" => 1 } |
|
|
|
|
|
|
|
|
|
|
|
AUTOGENERATED_FIELDS = %w[status created_at updated_at id].freeze |
|
|
|
AUTOGENERATED_FIELDS = %w[status created_at updated_at id].freeze |
|
|
|