From e32e737f58fd9a0638fa6b45d8c93d0f27bc03b7 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Wed, 13 Oct 2021 11:28:47 +0100 Subject: [PATCH] Validate record completion --- app/controllers/case_logs_controller.rb | 2 +- app/models/case_log.rb | 29 +++++++++++++---------- spec/models/case_log_spec.rb | 5 ++++ spec/requests/case_log_controller_spec.rb | 27 ++++++++++++++++++++- 4 files changed, 48 insertions(+), 15 deletions(-) diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb index eaaf400a3..eea4f888c 100644 --- a/app/controllers/case_logs_controller.rb +++ b/app/controllers/case_logs_controller.rb @@ -13,7 +13,7 @@ class CaseLogsController < ApplicationController format.html { redirect_to @case_log } format.json do if @case_log.persisted? - render json: @case_log , status: :created + render json: @case_log, status: :created else render json: { errors: @case_log.errors.full_messages }, status: :unprocessable_entity end diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 187d285b9..715055a90 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -3,20 +3,9 @@ class CaseLogValidator < ActiveModel::Validator # 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 !/^[1-9][0-9]?$|^100$/.match?(record.tenant_age.to_s) - # record.errors.add :tenant_age, "Tenant age must be between 0 and 100" - # end - if record.tenant_age && !/^[1-9][0-9]?$|^100$/.match?(record.tenant_age.to_s) - record.errors.add :tenant_age, "Tenant age must be between 0 and 100" + if record.tenant_age && !/^[1-9][0-9]?$|^120$/.match?(record.tenant_age.to_s) + record.errors.add :tenant_age, "must be between 0 and 120" end end @@ -33,6 +22,8 @@ end class CaseLog < ApplicationRecord validate :instance_validations + before_save :update_status! + attr_writer :previous_page enum status: { "in progress" => 0, "submitted" => 1 } @@ -40,4 +31,16 @@ class CaseLog < ApplicationRecord def instance_validations validates_with CaseLogValidator, ({ previous_page: @previous_page } || {}) end + + def update_status! + self.status = if all_fields_completed? && errors.empty? + "submitted" + else + "in progress" + end + end + + def all_fields_completed? + tenant_age.present? && tenant_code.present? && tenant_nationality.present? + end end diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 08b41d6c2..90c548184 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -1,4 +1,9 @@ require "rails_helper" RSpec.describe Form, type: :model do + describe "#new" do + it "validates age is under 120" do + expect { CaseLog.create!(tenant_age: 121) }.to raise_error(ActiveRecord::RecordInvalid) + end + end end diff --git a/spec/requests/case_log_controller_spec.rb b/spec/requests/case_log_controller_spec.rb index 34d9f98bb..a3d685de6 100644 --- a/spec/requests/case_log_controller_spec.rb +++ b/spec/requests/case_log_controller_spec.rb @@ -11,6 +11,8 @@ RSpec.describe CaseLogsController, type: :request do ActionController::HttpAuthentication::Basic .encode_credentials(api_username, api_password) end + let(:in_progress) { "in progress" } + let(:submitted) { "submitted" } let(:headers) do { @@ -57,7 +59,30 @@ RSpec.describe CaseLogsController, type: :request do it "validates case log parameters" do json_response = JSON.parse(response.body) expect(response).to have_http_status(:unprocessable_entity) - expect(json_response["errors"]).to eq(["Tenant age Tenant age must be between 0 and 100"]) + expect(json_response["errors"]).to eq(["Tenant age must be between 0 and 120"]) + end + end + + context "partial case log submission" do + it "marks the record as in_progress" do + json_response = JSON.parse(response.body) + expect(json_response["status"]).to eq(in_progress) + end + end + + context "complete case log submission" do + let(:params) do + { + "tenant_code": tenant_code, + "tenant_age": tenant_age, + "property_postcode": property_postcode, + "tenant_nationality": "Latvian", + } + end + + it "marks the record as submitted" do + json_response = JSON.parse(response.body) + expect(json_response["status"]).to eq(submitted) end end