Browse Source

Merge pull request #39 from communitiesuk/CLDC-344/Implement-a-few-validations

Cldc 344/implement a few validations
pull/41/head
Matthew J. Phelan 3 years ago committed by GitHub
parent
commit
ff38c89d54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      app/controllers/case_logs_controller.rb
  2. 34
      app/models/case_log.rb
  3. 1
      app/views/form/page.html.erb
  4. 2
      db/schema.rb

4
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)
responses_for_page = question_responses(questions_for_page)
@case_log.previous_page = previous_page
if @case_log.update(responses_for_page)
redirect_path = form.next_page_redirect_path(previous_page)
redirect_to(send(redirect_path, @case_log))
@ -45,7 +45,7 @@ class CaseLogsController < ApplicationController
form = Form.new(2021, 2022)
form.all_pages.map do |page_key, page_info|
define_method(page_key) do
define_method(page_key) do |errors = {}|
@case_log = CaseLog.find(params[:case_log_id])
render "form/page", locals: { form: form, page_key: page_key, page_info: page_info }
end

34
app/models/case_log.rb

@ -1,5 +1,37 @@
class CaseLogValidator < ActiveModel::Validator
# 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 =~ /^[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]
if respond_to?("validate_#{question_to_validate}")
public_send("validate_#{question_to_validate}", record)
end
end
end
class CaseLog < ApplicationRecord
validate :instance_validations
attr_writer :previous_page
enum status: { "in progress" => 0, "submitted" => 1 }
# validates :tenant_age, presence: true
def instance_validations
validates_with CaseLogValidator, ({ previous_page: @previous_page } || {})
end
end

1
app/views/form/page.html.erb

@ -6,6 +6,7 @@
<% end %>
<%= turbo_frame_tag "case_log_form", target: "_top" do %>
<% if page_info["header"].present? %>
<h1 class="govuk-heading-xl">
<%= page_info["header"] %>

2
db/schema.rb

@ -57,6 +57,7 @@ ActiveRecord::Schema.define(version: 2021_10_11_115946) do
t.integer "person_8_age"
t.string "person_8_gender"
t.string "person_8_economic_status"
t.string "postcode"
t.string "homelessness"
t.string "reason_for_leaving_last_settled_home"
t.string "benefit_cap_spare_room_subsidy"
@ -107,6 +108,7 @@ ActiveRecord::Schema.define(version: 2021_10_11_115946) do
t.string "chr_letting"
t.string "cap_letting"
t.string "outstanding_rent_or_charges"
t.string "other_reason_for_leaving_last_settled_home"
t.boolean "accessibility_requirements_fully_wheelchair_accessible_housing"
t.boolean "accessibility_requirements_wheelchair_access_to_essential_rooms"
t.boolean "accessibility_requirements_level_access_housing"

Loading…
Cancel
Save