diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb index 48e546236..f75d8c318 100644 --- a/app/controllers/case_logs_controller.rb +++ b/app/controllers/case_logs_controller.rb @@ -57,8 +57,7 @@ class CaseLogsController < ApplicationController def submit_form form = FormHandler.instance.get_form("2021_2022") @case_log = CaseLog.find(params[:id]) - @case_log.page_id = params[:case_log][:page] - page = form.get_page(@case_log.page_id) + page = form.get_page(params[:case_log][:page]) responses_for_page = responses_for_page(page) if @case_log.update(responses_for_page) && @case_log.has_no_unresolved_soft_errors? redirect_path = form.next_page_redirect_path(page, @case_log) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 35150f279..e9aab905d 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -1,6 +1,6 @@ class CaseLogValidator < ActiveModel::Validator - # Validations methods need to be called 'validate_' to run on model save - # or 'validate_' to run on submit as well + # Validations methods need to be called 'validate_' to run on model save + # or form page submission include HouseholdValidations include PropertyValidations include FinancialValidations @@ -8,16 +8,8 @@ class CaseLogValidator < ActiveModel::Validator include DateValidations def validate(record) - # If we've come from the form UI we only want to validate the specific fields - # that have just been submitted. If we're submitting a log via API or Bulk Upload - # we want to validate all data fields. - page_to_validate = record.page_id - if page_to_validate - public_send("validate_#{page_to_validate}", record) if respond_to?("validate_#{page_to_validate}") - else - validation_methods = public_methods.select { |method| method.starts_with?("validate_") } - validation_methods.each { |meth| public_send(meth, record) } - end + validation_methods = public_methods.select { |method| method.starts_with?("validate_") } + validation_methods.each { |meth| public_send(meth, record) } end private @@ -44,8 +36,6 @@ class CaseLog < ApplicationRecord validates_with CaseLogValidator before_save :update_status! - attr_accessor :page_id - enum status: { "not_started" => 0, "in_progress" => 1, "completed" => 2 } enum ethnic: DbEnums.ethnic diff --git a/spec/requests/case_log_controller_spec.rb b/spec/requests/case_log_controller_spec.rb index 06427a4e8..eb8b9c9c9 100644 --- a/spec/requests/case_log_controller_spec.rb +++ b/spec/requests/case_log_controller_spec.rb @@ -311,6 +311,23 @@ RSpec.describe CaseLogsController, type: :request do it "re-renders the same page with errors if validation fails" do expect(response).to have_http_status(:redirect) end + + let(:params) do + { + id: case_log.id, + case_log: { + page: page_id, + age1: answer, + age2: 2000 + }, + } + end + + it "only updates answers that apply to the page being submitted" do + case_log.reload + expect(case_log.age1).to eq(answer) + expect(case_log.age2).to be nil + end end end end