From 301ae82a597bd7ddc003068429cfcb3d5918a633 Mon Sep 17 00:00:00 2001 From: Matthew Phelan Date: Mon, 11 Oct 2021 11:23:48 +0100 Subject: [PATCH] review comments update --- app/controllers/case_logs_controller.rb | 2 +- app/models/case_log.rb | 35 +++++++++++++++---------- config/forms/2021_2022.json | 3 +-- db/schema.rb | 1 + 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb index 515275f09..c4619b169 100644 --- a/app/controllers/case_logs_controller.rb +++ b/app/controllers/case_logs_controller.rb @@ -26,7 +26,7 @@ class CaseLogsController < ApplicationController previous_page = params[:case_log][:previous_page] questions_for_page = form.questions_for_page(previous_page).keys answers_for_page = page_params(questions_for_page).select { |k, _v| questions_for_page.include?(k) } - @case_log.custom_validator_options = { previous_page: previous_page, validation: form.questions_for_page(previous_page)[previous_page]["regex_validation"] } + @case_log.previous_page(previous_page) if @case_log.update(answers_for_page) redirect_path = form.next_page_redirect_path(previous_page) redirect_to(send(redirect_path, @case_log)) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 13c7a2f7a..5e4f99c48 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -1,32 +1,39 @@ class CaseLogValidator < ActiveModel::Validator - def validate_tenant_age(record, validation_regex) - regexp = Regexp.new validation_regex - if !record.tenant_age? + # Methods 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_tenant_code(record) + if record.tenant_code.blank? + record.errors.add :tenant_code, "Tenant code can't be blank" + end + end + + def validate_tenant_age(record) + if record.tenant_age.blank? record.errors.add :tenant_age, "Tenant age can't be blank" - elsif !(record.tenant_age.to_s =~ regexp) + elsif !(record.tenant_age.to_s =~ /^[1-9][0-9]?$|^100$/) record.errors.add :tenant_age, "Tenant age must be between 0 and 100" end end def validate(record) question_to_validate = options[:previous_page] - validation_regex = options[:validation] - if question_to_validate == "tenant_code" - if !record.tenant_code? - record.errors.add :tenant_code, "Tenant code can't be blank" - end - elsif question_to_validate == "tenant_age" - validate_tenant_age(record, validation_regex) - end + public_send("validate_#{question_to_validate}", record) end end class CaseLog < ApplicationRecord validate :instance_validations - attr_accessor :custom_validator_options + @previous_page enum status: { "in progress" => 0, "submitted" => 1 } + + def previous_page(value) + @previous_page = value + end + def instance_validations - validates_with CaseLogValidator, (custom_validator_options || {}) + validates_with CaseLogValidator, ({ previous_page: @previous_page } || {}) end end diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json index 1001224ad..eb91f81e3 100644 --- a/config/forms/2021_2022.json +++ b/config/forms/2021_2022.json @@ -32,8 +32,7 @@ "type": "numeric", "min": 0, "max": 150, - "step": 1, - "regex_validation" : "^[1-9][0-9]?$|^100$" + "step": 1 } } }, diff --git a/db/schema.rb b/db/schema.rb index 648ded320..706deaf3b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -109,6 +109,7 @@ ActiveRecord::Schema.define(version: 2021_10_05_115813) do t.string "chr_letting" t.string "cap_letting" t.string "outstanding_rent_or_charges" + t.string "other_reason_for_leaving_last_settled_home" end end