diff --git a/app/models/form/question.rb b/app/models/form/question.rb index d98fe5388..0a2bc9998 100644 --- a/app/models/form/question.rb +++ b/app/models/form/question.rb @@ -136,7 +136,11 @@ class Form::Question labels = answer_options[value.to_s] labels["value"] if labels when "select" - answer_options[value.to_s] + if answer_options[value.to_s].respond_to?(:service_name) + answer_options[value.to_s].service_name + else + answer_options[value.to_s] + end else value.to_s end @@ -191,23 +195,22 @@ class Form::Question label end - def answer_option_synonyms(answer_id) - if id == "scheme_id" - Scheme.find(answer_id).locations.map(&:postcode).join(",") - end + def answer_option_synonyms(resource) + return unless resource.respond_to?(:synonyms) + + resource.synonyms end - def answer_option_append(answer_id) - if id == "scheme_id" - "(" + Scheme.find(answer_id).locations.count.to_s + " locations)" - end + def answer_option_append(resource) + return unless resource.respond_to?(:appended_text) + + resource.appended_text end - def answer_option_hint(answer_id) - if id == "scheme_id" - scheme = Scheme.find(answer_id) - [scheme.primary_client_group, scheme.secondary_client_group].filter { |x| x.present? }.join(", ") - end + def answer_option_hint(resource) + return unless resource.respond_to?(:hint) + + resource.hint end private diff --git a/app/models/form/setup/questions/scheme_id.rb b/app/models/form/setup/questions/scheme_id.rb index d17cbc806..e69cd05a3 100644 --- a/app/models/form/setup/questions/scheme_id.rb +++ b/app/models/form/setup/questions/scheme_id.rb @@ -13,8 +13,8 @@ class Form::Setup::Questions::SchemeId < ::Form::Question answer_opts = {} return answer_opts unless ActiveRecord::Base.connected? - Scheme.select(:id, :service_name).each_with_object(answer_opts) do |scheme, hsh| - hsh[scheme.id.to_s] = scheme.service_name + Scheme.select(:id, :service_name, :primary_client_group, :secondary_client_group).each_with_object(answer_opts) do |scheme, hsh| + hsh[scheme.id.to_s] = scheme hsh end end diff --git a/app/models/scheme.rb b/app/models/scheme.rb index bafa7d395..2b09ab6b6 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -142,4 +142,16 @@ class Scheme < ApplicationRecord { name: "Intended length of stay", value: intended_stay }, ] end + + def synonyms + locations.map(&:postcode).join(",") + end + + def appended_text + "(" + locations.count.to_s + " locations)" + end + + def hint + [primary_client_group, secondary_client_group].filter { |x| x.present? }.join(", ") + end end diff --git a/app/views/form/_select_question.html.erb b/app/views/form/_select_question.html.erb index bd25041ab..9f83c3fde 100644 --- a/app/views/form/_select_question.html.erb +++ b/app/views/form/_select_question.html.erb @@ -1,7 +1,7 @@ <%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %> <% selected = @case_log.public_send(question.id) || "" %> -<% answers = question.displayed_answer_options(@case_log).map { |key, value| OpenStruct.new(id: key, name: value) } %> +<% answers = question.displayed_answer_options(@case_log).map { |key, value| OpenStruct.new(id: key, name: value.respond_to?(:service_name) ? value.service_name : nil, resource: value) } %> <%= f.govuk_select(question.id.to_sym, label: legend(question, page_header, conditional), "data-controller": "accessible-autocomplete", @@ -9,10 +9,10 @@ hint: { text: question.hint_text&.html_safe }) do %> <% answers.each do |answer| %> + data-synonyms="<%= question.answer_option_synonyms(answer.resource) %>" + data-append="<%= question.answer_option_append(answer.resource) %>" + data-hint="<%= question.answer_option_hint(answer.resource) %>" + <%= @case_log[question.id] == answer.name || @case_log[question.id] == answer.resource || @case_log[question.id].to_s == answer.id ? "selected" : "" %> + <%= answer.id == "" ? "disabled" : "" %>><%= answer.name || answer.resource %> <% end %> <% end %> diff --git a/spec/features/form/accessible_autocomplete_spec.rb b/spec/features/form/accessible_autocomplete_spec.rb index 2601a2bb6..03d35ef40 100644 --- a/spec/features/form/accessible_autocomplete_spec.rb +++ b/spec/features/form/accessible_autocomplete_spec.rb @@ -24,11 +24,11 @@ RSpec.describe "Accessible Automcomplete" do context "when using accessible autocomplete" do before do allow_any_instance_of(Form::Question).to receive(:answer_option_synonyms).and_call_original - allow_any_instance_of(Form::Question).to receive(:answer_option_synonyms).with("E08000003").and_return("synonym") + allow_any_instance_of(Form::Question).to receive(:answer_option_synonyms).with("Manchester").and_return("synonym") allow_any_instance_of(Form::Question).to receive(:answer_option_append).and_call_original - allow_any_instance_of(Form::Question).to receive(:answer_option_append).with("E08000003").and_return(" (append)") + allow_any_instance_of(Form::Question).to receive(:answer_option_append).with("Manchester").and_return(" (append)") allow_any_instance_of(Form::Question).to receive(:answer_option_hint).and_call_original - allow_any_instance_of(Form::Question).to receive(:answer_option_hint).with("E08000003").and_return("hint") + allow_any_instance_of(Form::Question).to receive(:answer_option_hint).with("Manchester").and_return("hint") visit("/logs/#{case_log.id}/accessible-select") end diff --git a/spec/models/form/setup/questions/scheme_id_spec.rb b/spec/models/form/setup/questions/scheme_id_spec.rb index c40d5302c..9dde00b84 100644 --- a/spec/models/form/setup/questions/scheme_id_spec.rb +++ b/spec/models/form/setup/questions/scheme_id_spec.rb @@ -51,7 +51,7 @@ RSpec.describe Form::Setup::Questions::SchemeId, type: :model do end it "has the correct answer_options based on the schemes the user's organisation owns or manages" do - expected_answer = { scheme.id.to_s => scheme.service_name } + expected_answer = { scheme.id.to_s => scheme } expect(question.displayed_answer_options(case_log)).to eq(expected_answer) end end