Browse Source

added armed forces active response validation

pull/64/head
magicmilo 3 years ago
parent
commit
4520b3a1cc
  1. 11
      app/models/case_log.rb
  2. 6
      spec/helpers/check_answers_helper_spec.rb
  3. 25
      spec/models/case_log_spec.rb

11
app/models/case_log.rb

@ -55,6 +55,17 @@ class CaseLogValidator < ActiveModel::Validator
end
end
def validate_armed_forces_active_response(record)
# binding.pry
if record.armed_forces == "Yes - a regular" && record.armed_forces_active.blank?
record.errors.add :armed_forces_active, "You must answer the armed forces active question if the tenant has served as a regular in the armed forces"
end
if record.armed_forces != "Yes - a regular" && !record.armed_forces_active.blank?
record.errors.add :armed_forces_active, "You must not answer the armed forces active question if the tenant has not served as a regular in the armed forces"
end
end
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

6
spec/helpers/check_answers_helper_spec.rb

@ -11,7 +11,9 @@ RSpec.describe CheckAnswersHelper do
)
end
let(:case_log_with_met_radio_condition) do
FactoryBot.create(:case_log, armed_forces: "Yes - a regular", armed_forces_injured: "No")
FactoryBot.create(:case_log, armed_forces: "Yes - a regular",
armed_forces_injured: "No",
armed_forces_active: "Yes")
end
let(:subsection) { "income_and_benefits" }
let(:subsection_with_numeric_conditionals) { "household_characteristics" }
@ -56,7 +58,7 @@ RSpec.describe CheckAnswersHelper do
subsection_with_radio_conditionals,
case_log_with_met_radio_condition,
form,
)).to equal(3)
)).to equal(4)
end
end

25
spec/models/case_log_spec.rb

@ -96,6 +96,31 @@ RSpec.describe Form, type: :model do
}.to raise_error(ActiveRecord::RecordInvalid)
end
end
context "armed forces active validation" do
it "must be answered if ever served in the forces as a regular" do
expect {
CaseLog.create!(armed_forces: "Yes - a regular",
armed_forces_active: nil)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "must not be answered if not ever served as a regular" do
expect {
CaseLog.create!(armed_forces: "No",
armed_forces_active: "Yes")
}.to raise_error()
end
#Crossover over tests here as injured must be answered as well for no error
it "must be answered if ever served in the forces as a regular" do
expect {
CaseLog.create!(armed_forces: "Yes - a regular",
armed_forces_active: "Yes",
armed_forces_injured: "Yes")
}.not_to raise_error()
end
end
end
describe "status" do

Loading…
Cancel
Save