From fbbefaa285cb6badc95347ead07db27f4b4cdc3a Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Fri, 10 Jun 2022 13:55:29 +0100 Subject: [PATCH] Enable answer options to be created from a model lookup --- app/models/form/question.rb | 10 +++++++++- config/forms/2021_2022.json | 9 +++++---- config/forms/2022_2023.json | 9 +++++---- spec/models/form/question_spec.rb | 30 ++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 9 deletions(-) diff --git a/app/models/form/question.rb b/app/models/form/question.rb index fd92d21f7..979258c27 100644 --- a/app/models/form/question.rb +++ b/app/models/form/question.rb @@ -19,7 +19,6 @@ class Form::Question @fields_to_add = hsh["fields-to-add"] @result_field = hsh["result-field"] @readonly = hsh["readonly"] - @answer_options = hsh["answer_options"] @conditional_for = hsh["conditional_for"] @inferred_answers = hsh["inferred_answers"] @inferred_check_answers_value = hsh["inferred_check_answers_value"] @@ -29,6 +28,7 @@ class Form::Question @requires_js = hsh["requires_js"] @fields_added = hsh["fields_added"] @page = page + @answer_options = hsh["answer_options_lookup"] ? answer_options_from_lookup(hsh["answer_options_lookup"]) : hsh["answer_options"] end delegate :subsection, to: :page @@ -164,6 +164,14 @@ class Form::Question private + def answer_options_from_lookup(lookup) + values = {} + values[""] = "Select an option" + records = Object.const_get((lookup["class"]).to_s).send((lookup["scope"]).to_s) + records.each { |record| values[record.send((lookup["id"]).to_s)] = record.send((lookup["label"]).to_s) } + values + end + def selected_answer_option_is_derived?(case_log) selected_option = answer_options&.dig(case_log[id].to_s.presence) selected_option.is_a?(Hash) && selected_option["depends_on"] && form.depends_on_met(selected_option["depends_on"], case_log) diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json index de5bbf3d7..f429a6ab7 100644 --- a/config/forms/2021_2022.json +++ b/config/forms/2021_2022.json @@ -17,10 +17,11 @@ "header": "Which organisation owns this log?", "hint_text": "", "type": "select", - "answer_options": { - "": "Select an option", - "2": "Believe", - "3": "Barrow" + "answer_options_lookup": { + "class": "Organisation", + "scope": "all", + "id": "id", + "label": "name" }, "hidden_in_check_answers": true } diff --git a/config/forms/2022_2023.json b/config/forms/2022_2023.json index 0eb09aa6b..568da5f09 100644 --- a/config/forms/2022_2023.json +++ b/config/forms/2022_2023.json @@ -21,10 +21,11 @@ "header": "Which organisation owns this log?", "hint_text": "", "type": "select", - "answer_options": { - "": "Select an option", - "2": "Believe", - "3": "Barrow" + "answer_options_lookup": { + "class": "Organisation", + "scope": "all", + "id": "id", + "label": "name" }, "hidden_in_check_answers": true } diff --git a/spec/models/form/question_spec.rb b/spec/models/form/question_spec.rb index 4057e4687..5aec2a778 100644 --- a/spec/models/form/question_spec.rb +++ b/spec/models/form/question_spec.rb @@ -166,6 +166,36 @@ RSpec.describe Form::Question, type: :model do expect(question.label_from_value(9999)).to eq("9999") end end + + context "when the answer options are derived from model data" do + let(:question_definition) do + { + "header" => "Which organisation owns this log?", + "type" => "select", + "answer_options_lookup" => { + "class" => "Organisation", + "scope" => "all", + "id" => "id", + "label" => "name", + }, + } + end + let(:question_id) { "owning_organisation_id" } + let(:page) { nil } + let!(:org1) { FactoryBot.create(:organisation, name: "first organisation") } + let!(:org2) { FactoryBot.create(:organisation, name: "second organisation") } + let(:expected_answer_options) do + { + "" => "Select an option", + org1.id => org1.name, + org2.id => org2.name, + } + end + + it "can correctly lookup the data" do + expect(question.answer_options).to eq(expected_answer_options) + end + end end context "when type is checkbox" do