Browse Source

Set unselected checkbox options to 0

pull/2931/head
Kat 3 months ago
parent
commit
816b09e1f7
  1. 6
      app/controllers/form_controller.rb
  2. 15
      app/models/derived_variables/lettings_log_variables.rb
  3. 62
      spec/services/bulk_upload/lettings/year2024/row_parser_spec.rb

6
app/controllers/form_controller.rb

@ -155,9 +155,9 @@ private
next unless question_params
if %w[checkbox validation_override].include?(question.type)
if %w[checkbox].include?(question.type)
question.answer_keys_without_dividers.each do |option|
result[option] = question_params.include?(option) ? 1 : 0
result[option] = 1 if question_params.include?(option)
end
elsif question.type != "date"
result[question.id] = question_params
@ -347,7 +347,7 @@ private
end
def question_missing_response?(responses_for_page, question)
if %w[checkbox validation_override].include?(question.type)
if %w[checkbox].include?(question.type)
answered = question.answer_keys_without_dividers.map do |option|
session["fields"][option] = @log[option] = params[@log.log_type][question.id].include?(option) ? 1 : 0
params[@log.log_type][question.id].exclude?(option)

15
app/models/derived_variables/lettings_log_variables.rb

@ -142,6 +142,8 @@ module DerivedVariables::LettingsLogVariables
end
reset_address_fields! if is_supported_housing?
set_checkbox_values!
end
private
@ -362,4 +364,17 @@ private
return 2 if rent_type == 4
return 3 if rent_type == 5
end
def set_checkbox_values!
form.questions.select { |q| q.type == "checkbox" }.each do |question|
options = question.answer_keys_without_dividers
next unless options.any? { |option| self[option] == 1 }
options.each do |option|
if self[option].nil?
self[option] = 0
end
end
end
end
end

62
spec/services/bulk_upload/lettings/year2024/row_parser_spec.rb

@ -1246,6 +1246,32 @@ RSpec.describe BulkUpload::Lettings::Year2024::RowParser do
expect(parser.errors[:field_111]).to be_present
end
end
context "when some reasonable preference options are seleceted" do
let(:attributes) { setup_section_params.merge({ bulk_upload:, field_106: "1", field_107: "1", field_108: nil, field_109: "1", field_110: nil, field_111: nil }) }
it "sets the rest of the options to 0" do
parser.valid?
expect(parser.log.rp_homeless).to eq(1)
expect(parser.log.rp_insan_unsat).to eq(0)
expect(parser.log.rp_medwel).to eq(1)
expect(parser.log.rp_hardship).to eq(0)
expect(parser.log.rp_dontknow).to eq(0)
end
end
context "when some reasonable preference options are seleceted but reasonpref is No" do
let(:attributes) { setup_section_params.merge({ bulk_upload:, field_106: "2", field_107: "1", field_108: nil, field_109: "1", field_110: nil, field_111: nil }) }
it "sets the options to nil" do
parser.valid?
expect(parser.log.rp_homeless).to be_nil
expect(parser.log.rp_insan_unsat).to be_nil
expect(parser.log.rp_medwel).to be_nil
expect(parser.log.rp_hardship).to be_nil
expect(parser.log.rp_dontknow).to be_nil
end
end
end
describe "#field_116" do # referral
@ -2484,6 +2510,42 @@ RSpec.describe BulkUpload::Lettings::Year2024::RowParser do
end
end
end
context "when some illness type values are seleceted" do
let(:attributes) { setup_section_params.merge({ bulk_upload:, field_85: "1", field_94: "1", field_87: "1" }) }
it "sets the rest of the values to 0" do
parser.valid?
expect(parser.log.illness_type_1).to eq(1)
expect(parser.log.illness_type_2).to eq(0)
expect(parser.log.illness_type_3).to eq(0)
expect(parser.log.illness_type_4).to eq(0)
expect(parser.log.illness_type_5).to eq(1)
expect(parser.log.illness_type_6).to eq(0)
expect(parser.log.illness_type_7).to eq(0)
expect(parser.log.illness_type_8).to eq(0)
expect(parser.log.illness_type_9).to eq(0)
expect(parser.log.illness_type_10).to eq(0)
end
end
context "when none of the illness type values are seleceted" do
let(:attributes) { setup_section_params.merge({ bulk_upload:, field_85: "1" }) }
it "sets the values to nil" do
parser.valid?
expect(parser.log.illness_type_1).to be_nil
expect(parser.log.illness_type_2).to be_nil
expect(parser.log.illness_type_3).to be_nil
expect(parser.log.illness_type_4).to be_nil
expect(parser.log.illness_type_5).to be_nil
expect(parser.log.illness_type_6).to be_nil
expect(parser.log.illness_type_7).to be_nil
expect(parser.log.illness_type_8).to be_nil
expect(parser.log.illness_type_9).to be_nil
expect(parser.log.illness_type_10).to be_nil
end
end
end
describe "#irproduct_other" do

Loading…
Cancel
Save