Browse Source

refactor synonym, append and hint methods to take in resource

pull/739/head
Kat 3 years ago
parent
commit
86bcbeb97d
  1. 31
      app/models/form/question.rb
  2. 4
      app/models/form/setup/questions/scheme_id.rb
  3. 12
      app/models/scheme.rb
  4. 12
      app/views/form/_select_question.html.erb
  5. 6
      spec/features/form/accessible_autocomplete_spec.rb
  6. 2
      spec/models/form/setup/questions/scheme_id_spec.rb

31
app/models/form/question.rb

@ -136,7 +136,11 @@ class Form::Question
labels = answer_options[value.to_s] labels = answer_options[value.to_s]
labels["value"] if labels labels["value"] if labels
when "select" 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 else
value.to_s value.to_s
end end
@ -191,23 +195,22 @@ class Form::Question
label label
end end
def answer_option_synonyms(answer_id) def answer_option_synonyms(resource)
if id == "scheme_id" return unless resource.respond_to?(:synonyms)
Scheme.find(answer_id).locations.map(&:postcode).join(",")
end resource.synonyms
end end
def answer_option_append(answer_id) def answer_option_append(resource)
if id == "scheme_id" return unless resource.respond_to?(:appended_text)
"(" + Scheme.find(answer_id).locations.count.to_s + " locations)"
end resource.appended_text
end end
def answer_option_hint(answer_id) def answer_option_hint(resource)
if id == "scheme_id" return unless resource.respond_to?(:hint)
scheme = Scheme.find(answer_id)
[scheme.primary_client_group, scheme.secondary_client_group].filter { |x| x.present? }.join(", ") resource.hint
end
end end
private private

4
app/models/form/setup/questions/scheme_id.rb

@ -13,8 +13,8 @@ class Form::Setup::Questions::SchemeId < ::Form::Question
answer_opts = {} answer_opts = {}
return answer_opts unless ActiveRecord::Base.connected? return answer_opts unless ActiveRecord::Base.connected?
Scheme.select(:id, :service_name).each_with_object(answer_opts) do |scheme, hsh| 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.service_name hsh[scheme.id.to_s] = scheme
hsh hsh
end end
end end

12
app/models/scheme.rb

@ -142,4 +142,16 @@ class Scheme < ApplicationRecord
{ name: "Intended length of stay", value: intended_stay }, { name: "Intended length of stay", value: intended_stay },
] ]
end 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 end

12
app/views/form/_select_question.html.erb

@ -1,7 +1,7 @@
<%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %> <%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %>
<% selected = @case_log.public_send(question.id) || "" %> <% 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, <%= f.govuk_select(question.id.to_sym,
label: legend(question, page_header, conditional), label: legend(question, page_header, conditional),
"data-controller": "accessible-autocomplete", "data-controller": "accessible-autocomplete",
@ -9,10 +9,10 @@
hint: { text: question.hint_text&.html_safe }) do %> hint: { text: question.hint_text&.html_safe }) do %>
<% answers.each do |answer| %> <% answers.each do |answer| %>
<option value="<%= answer.id %>" <option value="<%= answer.id %>"
data-synonyms="<%= question.answer_option_synonyms(answer.id) %>" data-synonyms="<%= question.answer_option_synonyms(answer.resource) %>"
data-append="<%= question.answer_option_append(answer.id) %>" data-append="<%= question.answer_option_append(answer.resource) %>"
data-hint="<%= question.answer_option_hint(answer.id) %>" data-hint="<%= question.answer_option_hint(answer.resource) %>"
<%= @case_log[question.id] == answer.name || @case_log[question.id].to_s == answer.id ? "selected" : "" %> <%= @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 %></option> <%= answer.id == "" ? "disabled" : "" %>><%= answer.name || answer.resource %></option>
<% end %> <% end %>
<% end %> <% end %>

6
spec/features/form/accessible_autocomplete_spec.rb

@ -24,11 +24,11 @@ RSpec.describe "Accessible Automcomplete" do
context "when using accessible autocomplete" do context "when using accessible autocomplete" do
before 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).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).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).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") visit("/logs/#{case_log.id}/accessible-select")
end end

2
spec/models/form/setup/questions/scheme_id_spec.rb

@ -51,7 +51,7 @@ RSpec.describe Form::Setup::Questions::SchemeId, type: :model do
end end
it "has the correct answer_options based on the schemes the user's organisation owns or manages" do 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) expect(question.displayed_answer_options(case_log)).to eq(expected_answer)
end end
end end

Loading…
Cancel
Save