Browse Source

Enable answer options to be created from a model lookup

pull/653/head
baarkerlounger 3 years ago
parent
commit
fbbefaa285
  1. 10
      app/models/form/question.rb
  2. 9
      config/forms/2021_2022.json
  3. 9
      config/forms/2022_2023.json
  4. 30
      spec/models/form/question_spec.rb

10
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)

9
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
}

9
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
}

30
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

Loading…
Cancel
Save