diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb index 82ea9ec1e..f039e2d56 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) 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 diff --git a/app/helpers/tasklist_helper.rb b/app/helpers/tasklist_helper.rb index 4c831029c..549ef2d22 100644 --- a/app/helpers/tasklist_helper.rb +++ b/app/helpers/tasklist_helper.rb @@ -36,6 +36,15 @@ module TasklistHelper subsections.count { |subsection| get_subsection_status(subsection, case_log, form.questions_for_subsection(subsection).keys) == status } end + def get_first_page_or_check_answers(subsection, case_log, form, questions) + path = if is_started?(subsection, case_log, questions) + "case_log_#{subsection}_check_answers_path" + else + "case_log_#{form.first_page_for_subsection(subsection)}_path" + end + send(path, case_log) + end + private def all_questions_completed(case_log) @@ -46,4 +55,9 @@ private status = get_subsection_status(subsection, case_log, questions) %i[not_started in_progress].include?(status) end + + def is_started?(subsection, case_log, questions) + status = get_subsection_status(subsection, case_log, questions) + %i[in_progress completed].include?(status) + end end diff --git a/app/models/case_log.rb b/app/models/case_log.rb index c05ed887b..aa17eddf6 100644 --- a/app/models/case_log.rb +++ b/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 !/^[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 + 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 diff --git a/app/views/case_logs/_tasklist.html.erb b/app/views/case_logs/_tasklist.html.erb index dbbeb6321..fdf4c5aa9 100644 --- a/app/views/case_logs/_tasklist.html.erb +++ b/app/views/case_logs/_tasklist.html.erb @@ -9,9 +9,10 @@