Browse Source

Validate record completion

pull/45/head
baarkerlounger 4 years ago
parent
commit
e32e737f58
  1. 2
      app/controllers/case_logs_controller.rb
  2. 29
      app/models/case_log.rb
  3. 5
      spec/models/case_log_spec.rb
  4. 27
      spec/requests/case_log_controller_spec.rb

2
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

29
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

5
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

27
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

Loading…
Cancel
Save