Browse Source

Run all validations (#112)

pull/113/head
Daniel Baark 3 years ago committed by GitHub
parent
commit
2018813e8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      app/controllers/case_logs_controller.rb
  2. 14
      app/models/case_log.rb
  3. 17
      spec/requests/case_log_controller_spec.rb

3
app/controllers/case_logs_controller.rb

@ -57,8 +57,7 @@ class CaseLogsController < ApplicationController
def submit_form def submit_form
form = FormHandler.instance.get_form("2021_2022") form = FormHandler.instance.get_form("2021_2022")
@case_log = CaseLog.find(params[:id]) @case_log = CaseLog.find(params[:id])
@case_log.page_id = params[:case_log][:page] page = form.get_page(params[:case_log][:page])
page = form.get_page(@case_log.page_id)
responses_for_page = responses_for_page(page) responses_for_page = responses_for_page(page)
if @case_log.update(responses_for_page) && @case_log.has_no_unresolved_soft_errors? if @case_log.update(responses_for_page) && @case_log.has_no_unresolved_soft_errors?
redirect_path = form.next_page_redirect_path(page, @case_log) redirect_path = form.next_page_redirect_path(page, @case_log)

14
app/models/case_log.rb

@ -1,6 +1,6 @@
class CaseLogValidator < ActiveModel::Validator class CaseLogValidator < ActiveModel::Validator
# Validations methods need to be called 'validate_<page_name>' to run on model save # Validations methods need to be called 'validate_' to run on model save
# or 'validate_' to run on submit as well # or form page submission
include HouseholdValidations include HouseholdValidations
include PropertyValidations include PropertyValidations
include FinancialValidations include FinancialValidations
@ -8,17 +8,9 @@ class CaseLogValidator < ActiveModel::Validator
include DateValidations include DateValidations
def validate(record) 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 = public_methods.select { |method| method.starts_with?("validate_") }
validation_methods.each { |meth| public_send(meth, record) } validation_methods.each { |meth| public_send(meth, record) }
end end
end
private private
@ -44,8 +36,6 @@ class CaseLog < ApplicationRecord
validates_with CaseLogValidator validates_with CaseLogValidator
before_save :update_status! before_save :update_status!
attr_accessor :page_id
enum status: { "not_started" => 0, "in_progress" => 1, "completed" => 2 } enum status: { "not_started" => 0, "in_progress" => 1, "completed" => 2 }
enum ethnic: DbEnums.ethnic enum ethnic: DbEnums.ethnic

17
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 it "re-renders the same page with errors if validation fails" do
expect(response).to have_http_status(:redirect) expect(response).to have_http_status(:redirect)
end 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 end
end end

Loading…
Cancel
Save