Browse Source

Enable multiple inferred check answers values

pull/1077/head
Kat 4 years ago
parent
commit
76030fb1f6
  1. 13
      app/models/form/question.rb
  2. 4
      app/models/form/sales/questions/age1.rb
  3. 4
      app/models/form/sales/questions/age2.rb
  4. 4
      app/models/form/sales/questions/buyer1_ethnic_group.rb
  5. 44
      config/forms/2021_2022.json
  6. 44
      config/forms/2022_2023.json
  7. 4
      config/forms/schema/generic.json
  8. 4
      docs/form/builder.md
  9. 4
      docs/form/question.md
  10. 8
      spec/fixtures/forms/2021_2022.json
  11. 4
      spec/models/form/sales/questions/age1_spec.rb
  12. 4
      spec/models/form/sales/questions/age2_spec.rb

13
app/models/form/question.rb

@ -53,7 +53,7 @@ class Form::Question
answer = label_from_value(log[id]) if log[id].present? answer = label_from_value(log[id]) if log[id].present?
answer_label = [prefix, format_value(answer), suffix_label(log)].join("") if answer answer_label = [prefix, format_value(answer), suffix_label(log)].join("") if answer
inferred = inferred_check_answers_value["value"] if inferred_check_answers_value && has_inferred_check_answers_value?(log) inferred = get_inferred_answer_value(log)
return inferred if inferred.present? return inferred if inferred.present?
answer_label answer_label
@ -104,11 +104,18 @@ class Form::Question
def has_inferred_check_answers_value?(log) def has_inferred_check_answers_value?(log)
return true if selected_answer_option_is_derived?(log) return true if selected_answer_option_is_derived?(log)
return inferred_check_answers_value["condition"].values[0] == log[inferred_check_answers_value["condition"].keys[0]] if inferred_check_answers_value.present? return inferred_check_answers_value&.any? { |inferred_value| inferred_value["condition"].values[0] == log[inferred_value["condition"].keys[0]] } if inferred_check_answers_value.present?
false false
end end
def get_inferred_answer_value(log)
return unless inferred_check_answers_value
inferred_answer = inferred_check_answers_value.find { |inferred_value| inferred_value["condition"].values[0] == log[inferred_value["condition"].keys[0]] }
inferred_answer.present?? inferred_answer["value"] : ""
end
def displayed_answer_options(log, _current_user = nil) def displayed_answer_options(log, _current_user = nil)
answer_options.select do |_key, val| answer_options.select do |_key, val|
!val.is_a?(Hash) || !val["depends_on"] || form.depends_on_met(val["depends_on"], log) !val.is_a?(Hash) || !val["depends_on"] || form.depends_on_met(val["depends_on"], log)
@ -271,7 +278,7 @@ private
end end
def has_inferred_display_value?(log) def has_inferred_display_value?(log)
inferred_check_answers_value.present? && log[inferred_check_answers_value["condition"].keys.first] == inferred_check_answers_value["condition"].values.first inferred_check_answers_value.present? && inferred_check_answers_value.any? { |inferred_value| log[inferred_value["condition"].keys.first] == inferred_value["condition"].values.first}
end end
def checkbox_answer_label(log) def checkbox_answer_label(log)

4
app/models/form/sales/questions/age1.rb

@ -6,12 +6,12 @@ class Form::Sales::Questions::Age1 < ::Form::Question
@header = "Age" @header = "Age"
@type = "numeric" @type = "numeric"
@width = 2 @width = 2
@inferred_check_answers_value = { @inferred_check_answers_value = [{
"condition" => { "condition" => {
"age1_known" => 1, "age1_known" => 1,
}, },
"value" => "Not known", "value" => "Not known",
} }]
@check_answers_card_number = 1 @check_answers_card_number = 1
end end
end end

4
app/models/form/sales/questions/age2.rb

@ -6,10 +6,10 @@ class Form::Sales::Questions::Age2 < ::Form::Question
@header = "Age" @header = "Age"
@type = "numeric" @type = "numeric"
@width = 2 @width = 2
@inferred_check_answers_value = { @inferred_check_answers_value = [{
"condition" => { "age2_known" => 1 }, "condition" => { "age2_known" => 1 },
"value" => "Not known", "value" => "Not known",
} }]
@check_answers_card_number = 2 @check_answers_card_number = 2
end end
end end

4
app/models/form/sales/questions/buyer1_ethnic_group.rb

@ -7,12 +7,12 @@ class Form::Sales::Questions::Buyer1EthnicGroup < ::Form::Question
@type = "radio" @type = "radio"
@answer_options = ANSWER_OPTIONS @answer_options = ANSWER_OPTIONS
@hint_text = "Buyer 1 is the person in the household who does the most paid work. If it’s a joint purchase and the buyers do the same amount of paid work, buyer 1 is whoever is the oldest." @hint_text = "Buyer 1 is the person in the household who does the most paid work. If it’s a joint purchase and the buyers do the same amount of paid work, buyer 1 is whoever is the oldest."
@inferred_check_answers_value = { @inferred_check_answers_value = [{
"condition" => { "condition" => {
"ethnic_group" => 17, "ethnic_group" => 17,
}, },
"value" => "Prefers not to say", "value" => "Prefers not to say",
} }]
@check_answers_card_number = 1 @check_answers_card_number = 1
end end

44
config/forms/2021_2022.json

@ -59,12 +59,12 @@
"is_la_inferred": true "is_la_inferred": true
} }
}, },
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"condition": { "condition": {
"postcode_known": 0 "postcode_known": 0
}, },
"value": "Not known" "value": "Not known"
} }]
} }
}, },
"depends_on": [ "depends_on": [
@ -1298,12 +1298,12 @@
"max": 120, "max": 120,
"step": 1, "step": 1,
"width": 2, "width": 2,
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"condition": { "condition": {
"age1_known": 1 "age1_known": 1
}, },
"value": "Not known" "value": "Not known"
} }]
} }
}, },
"depends_on": [ "depends_on": [
@ -2077,12 +2077,12 @@
"max": 120, "max": 120,
"step": 1, "step": 1,
"width": 2, "width": 2,
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"condition": { "condition": {
"age2_known": 1 "age2_known": 1
}, },
"value": "Not known" "value": "Not known"
} }]
} }
}, },
"depends_on": [ "depends_on": [
@ -2612,12 +2612,12 @@
"max": 120, "max": 120,
"step": 1, "step": 1,
"width": 2, "width": 2,
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"condition": { "condition": {
"age3_known": 1 "age3_known": 1
}, },
"value": "Not known" "value": "Not known"
} }]
} }
}, },
"depends_on": [ "depends_on": [
@ -3144,12 +3144,12 @@
"max": 120, "max": 120,
"step": 1, "step": 1,
"width": 2, "width": 2,
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"condition": { "condition": {
"age4_known": 1 "age4_known": 1
}, },
"value": "Not known" "value": "Not known"
} }]
} }
}, },
"depends_on": [ "depends_on": [
@ -3673,12 +3673,12 @@
"max": 120, "max": 120,
"step": 1, "step": 1,
"width": 2, "width": 2,
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"condition": { "condition": {
"age5_known": 1 "age5_known": 1
}, },
"value": "Not known" "value": "Not known"
} }]
} }
}, },
"depends_on": [ "depends_on": [
@ -4199,12 +4199,12 @@
"max": 120, "max": 120,
"step": 1, "step": 1,
"width": 2, "width": 2,
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"condition": { "condition": {
"age6_known": 1 "age6_known": 1
}, },
"value": "Not known" "value": "Not known"
} }]
} }
}, },
"depends_on": [ "depends_on": [
@ -4722,12 +4722,12 @@
"max": 120, "max": 120,
"step": 1, "step": 1,
"width": 2, "width": 2,
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"condition": { "condition": {
"age7_known": 1 "age7_known": 1
}, },
"value": "Not known" "value": "Not known"
} }]
} }
}, },
"depends_on": [ "depends_on": [
@ -5242,12 +5242,12 @@
"max": 120, "max": 120,
"step": 1, "step": 1,
"width": 2, "width": 2,
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"condition": { "condition": {
"age8_known": 1 "age8_known": 1
}, },
"value": "Not known" "value": "Not known"
} }]
} }
}, },
"depends_on": [ "depends_on": [
@ -6517,12 +6517,12 @@
"is_previous_la_inferred": true "is_previous_la_inferred": true
} }
}, },
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"condition": { "condition": {
"ppcodenk": 0 "ppcodenk": 0
}, },
"value": "Not known" "value": "Not known"
} }]
} }
} }
}, },
@ -6950,12 +6950,12 @@
"W92000004": "Wales", "W92000004": "Wales",
"9300000XX": "Outside UK" "9300000XX": "Outside UK"
}, },
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"condition": { "condition": {
"previous_la_known": 0 "previous_la_known": 0
}, },
"value": "Not known" "value": "Not known"
} }]
} }
}, },
"depends_on": [ "depends_on": [

44
config/forms/2022_2023.json

@ -59,12 +59,12 @@
"is_la_inferred": true "is_la_inferred": true
} }
}, },
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"condition": { "condition": {
"postcode_known": 0 "postcode_known": 0
}, },
"value": "Not known" "value": "Not known"
} }]
} }
}, },
"depends_on": [ "depends_on": [
@ -1333,12 +1333,12 @@
"max": 120, "max": 120,
"step": 1, "step": 1,
"width": 2, "width": 2,
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"condition": { "condition": {
"age1_known": 1 "age1_known": 1
}, },
"value": "Not known" "value": "Not known"
} }]
} }
}, },
"depends_on": [ "depends_on": [
@ -2076,12 +2076,12 @@
"max": 120, "max": 120,
"step": 1, "step": 1,
"width": 2, "width": 2,
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"condition": { "condition": {
"age2_known": 1 "age2_known": 1
}, },
"value": "Not known" "value": "Not known"
} }]
} }
}, },
"depends_on": [ "depends_on": [
@ -2611,12 +2611,12 @@
"max": 120, "max": 120,
"step": 1, "step": 1,
"width": 2, "width": 2,
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"condition": { "condition": {
"age3_known": 1 "age3_known": 1
}, },
"value": "Not known" "value": "Not known"
} }]
} }
}, },
"depends_on": [ "depends_on": [
@ -3143,12 +3143,12 @@
"max": 120, "max": 120,
"step": 1, "step": 1,
"width": 2, "width": 2,
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"condition": { "condition": {
"age4_known": 1 "age4_known": 1
}, },
"value": "Not known" "value": "Not known"
} }]
} }
}, },
"depends_on": [ "depends_on": [
@ -3672,12 +3672,12 @@
"max": 120, "max": 120,
"step": 1, "step": 1,
"width": 2, "width": 2,
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"condition": { "condition": {
"age5_known": 1 "age5_known": 1
}, },
"value": "Not known" "value": "Not known"
} }]
} }
}, },
"depends_on": [ "depends_on": [
@ -4198,12 +4198,12 @@
"max": 120, "max": 120,
"step": 1, "step": 1,
"width": 2, "width": 2,
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"condition": { "condition": {
"age6_known": 1 "age6_known": 1
}, },
"value": "Not known" "value": "Not known"
} }]
} }
}, },
"depends_on": [ "depends_on": [
@ -4721,12 +4721,12 @@
"max": 120, "max": 120,
"step": 1, "step": 1,
"width": 2, "width": 2,
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"condition": { "condition": {
"age7_known": 1 "age7_known": 1
}, },
"value": "Not known" "value": "Not known"
} }]
} }
}, },
"depends_on": [ "depends_on": [
@ -5241,12 +5241,12 @@
"max": 120, "max": 120,
"step": 1, "step": 1,
"width": 2, "width": 2,
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"condition": { "condition": {
"age8_known": 1 "age8_known": 1
}, },
"value": "Not known" "value": "Not known"
} }]
} }
}, },
"depends_on": [ "depends_on": [
@ -6482,12 +6482,12 @@
"is_previous_la_inferred": true "is_previous_la_inferred": true
} }
}, },
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"condition": { "condition": {
"ppcodenk": 0 "ppcodenk": 0
}, },
"value": "Not known" "value": "Not known"
} }]
} }
} }
}, },
@ -6915,12 +6915,12 @@
"W92000004": "Wales", "W92000004": "Wales",
"9300000XX": "Outside UK" "9300000XX": "Outside UK"
}, },
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"condition": { "condition": {
"previous_la_known": 0 "previous_la_known": 0
}, },
"value": "Not known" "value": "Not known"
} }]
} }
}, },
"depends_on": [ "depends_on": [

4
config/forms/schema/generic.json

@ -115,7 +115,7 @@
"description": "fields that get inferred based on the value of the current field", "description": "fields that get inferred based on the value of the current field",
"type": "object" "type": "object"
}, },
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"description": "value that gets displayed in the check answers for this field if the given condition is met", "description": "value that gets displayed in the check answers for this field if the given condition is met",
"type": "object", "type": "object",
"properties": { "properties": {
@ -128,7 +128,7 @@
"type": "object" "type": "object"
} }
} }
} }]
}, },
"minProperties": 1 "minProperties": 1
} }

4
docs/form/builder.md

@ -82,10 +82,10 @@ The JSON should follow the structure:
"[snake_case_question_to_enable_2_name_string]": ["condition-that-enables"] "[snake_case_question_to_enable_2_name_string]": ["condition-that-enables"]
}, },
"inferred_answers": { "field_that_gets_inferred_from_current_field": { "is_that_field_inferred": true } }, "inferred_answers": { "field_that_gets_inferred_from_current_field": { "is_that_field_inferred": true } },
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"condition": { "field_name_for_inferred_check_answers_condition": "field_value_for_inferred_check_answers_condition" }, "condition": { "field_name_for_inferred_check_answers_condition": "field_value_for_inferred_check_answers_condition" },
"value": "Inferred value that gets displayed if condition is met" "value": "Inferred value that gets displayed if condition is met"
} }]
} }
}, },
"depends_on": [{ "question_key": "answer_value_required_for_this_page_to_be_shown" }] "depends_on": [{ "question_key": "answer_value_required_for_this_page_to_be_shown" }]

4
docs/form/question.md

@ -74,12 +74,12 @@ The answer the data inputter provides to some questions allows us to infer the v
"is_la_inferred": true "is_la_inferred": true
} }
}, },
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"condition": { "condition": {
"postcode_known": 0 "postcode_known": 0
}, },
"value": "Not known" "value": "Not known"
} }]
} }
``` ```

8
spec/fixtures/forms/2021_2022.json vendored

@ -342,12 +342,12 @@
1 1
] ]
}, },
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"condition": { "condition": {
"armedforces": 3 "armedforces": 3
}, },
"value": "Prefers not to say" "value": "Prefers not to say"
} }]
}, },
"leftreg": { "leftreg": {
"header": "Are they still serving?", "header": "Are they still serving?",
@ -526,12 +526,12 @@
"is_la_inferred": true "is_la_inferred": true
} }
}, },
"inferred_check_answers_value": { "inferred_check_answers_value": [{
"condition": { "condition": {
"postcode_known": 0 "postcode_known": 0
}, },
"value": "Not known" "value": "Not known"
} }]
} }
} }
}, },

4
spec/models/form/sales/questions/age1_spec.rb

@ -40,10 +40,10 @@ RSpec.describe Form::Sales::Questions::Age1, type: :model do
end end
it "has the correct inferred check answers value" do it "has the correct inferred check answers value" do
expect(question.inferred_check_answers_value).to eq({ expect(question.inferred_check_answers_value).to eq([{
"condition" => { "age1_known" => 1 }, "condition" => { "age1_known" => 1 },
"value" => "Not known", "value" => "Not known",
}) }])
end end
it "has the correct check_answers_card_number" do it "has the correct check_answers_card_number" do

4
spec/models/form/sales/questions/age2_spec.rb

@ -40,12 +40,12 @@ RSpec.describe Form::Sales::Questions::Age2, type: :model do
end end
it "has the correct inferred check answers value" do it "has the correct inferred check answers value" do
expect(question.inferred_check_answers_value).to eq({ expect(question.inferred_check_answers_value).to eq([{
"condition" => { "condition" => {
"age2_known" => 1, "age2_known" => 1,
}, },
"value" => "Not known", "value" => "Not known",
}) }])
end end
it "has the correct check_answers_card_number" do it "has the correct check_answers_card_number" do

Loading…
Cancel
Save