Browse Source

Render prefix and suffix

pull/198/head
baarkerlounger 3 years ago
parent
commit
1aa14fc9c0
  1. 10
      app/models/case_log.rb
  2. 16
      app/models/form/question.rb
  3. 2
      app/views/form/_numeric_question.html.erb
  4. 18
      config/forms/2021_2022.json
  5. 4
      spec/fixtures/forms/2021_2022.json
  6. 36
      spec/views/form/page_view_spec.rb

10
app/models/case_log.rb

@ -325,11 +325,11 @@ private
dynamically_not_required << "tenancyother"
end
if net_income_known == "Tenant prefers not to say"
dynamically_not_required << "earnings"
else
dynamically_not_required << "incref"
end
dynamically_not_required << if net_income_known == "Tenant prefers not to say"
"earnings"
else
"incref"
end
start_range = (other_hhmemb || 0) + 2
(start_range..8).each do |n|

16
app/models/form/question.rb

@ -3,7 +3,7 @@ class Form::Question
:type, :min, :max, :step, :width, :fields_to_add, :result_field,
:conditional_for, :readonly, :answer_options, :page, :check_answer_label,
:inferred_answers, :hidden_in_check_answers, :inferred_check_answers_value,
:guidance_partial
:guidance_partial, :prefix, :suffix
def initialize(id, hsh, page)
@id = id
@ -24,6 +24,8 @@ class Form::Question
@inferred_answers = hsh["inferred_answers"]
@inferred_check_answers_value = hsh["inferred_check_answers_value"]
@hidden_in_check_answers = hsh["hidden_in_check_answers"]
@prefix = hsh["prefix"]
@suffix = hsh["suffix"]
@page = page
end
@ -80,6 +82,18 @@ class Form::Question
case_log[id].present?
end
def prefix_text(case_log = nil)
return prefix.to_s unless prefix && case_log.respond_to?(prefix)
case_log.public_send(prefix).to_s
end
def suffix_text(case_log = nil)
return suffix.to_s unless suffix && case_log.respond_to?(suffix)
case_log.public_send(suffix).to_s
end
private
def checkbox_answer_label(case_log)

2
app/views/form/_numeric_question.html.erb

@ -6,5 +6,7 @@
hint: { text: question.hint_text&.html_safe },
min: question.min, max: question.max, step: question.step,
width: question.width, :readonly => question.read_only?,
prefix_text: question.prefix_text(@case_log),
suffix_text: question.suffix_text(@case_log),
**stimulus_html_attributes(question)
%>

18
config/forms/2021_2022.json

@ -1804,7 +1804,7 @@
"label": "Income and benefits",
"depends_on": { "about_this_log": "completed" },
"pages": {
"net_income": {
"net_income_known": {
"header": "Household’s combined income",
"description": "",
"questions": {
@ -1820,21 +1820,23 @@
"2": "Yes – the household has a yearly income",
"divider_a": true,
"3": "Tenant prefers not to say"
},
"conditional_for": {
"earnings": ["Yes"],
"incfreq": ["Yes"]
}
},
}
}
},
"net_income": {
"header": "",
"description": "",
"questions": {
"earnings": {
"check_answer_label": "Income",
"header": "What is the tenant’s /and partner’s combined income after tax?",
"header": "How much income does the household have in total every week?",
"hint_text": "",
"type": "numeric",
"min": 0,
"step": "1",
"prefix": "£",
"suffix": ""
"suffix": "incfreq"
}
},
"soft_validations": {

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

@ -372,7 +372,9 @@
"type": "numeric",
"min": 0,
"step": 1,
"width": 5
"width": 5,
"prefix": "£",
"suffix": "incfreq"
},
"incfreq": {
"check_answer_label": "Income Frequency",

36
spec/views/form/page_view_spec.rb

@ -11,7 +11,7 @@ RSpec.describe "form/page" do
let(:subsection) { form.get_subsection("income_and_benefits") }
let(:page) { form.get_page("net_income") }
let(:question) { page.questions.find { |q| q.id == "earnings" } }
let(:initial_attribs) { { type: "numeric", answer_options: nil } }
let(:initial_attribs) { { type: "numeric", answer_options: nil, prefix: nil, suffix: nil } }
def assign_attributes(object, attrs)
attrs.each_pair do |attr, value|
@ -19,10 +19,19 @@ RSpec.describe "form/page" do
end
end
context "given a question with extra guidance" do
let(:expected_guidance) { /What counts as income?/ }
after do
# Revert any changes we've made to avoid affecting other specs as the form,
# subsection, page, question objects being acted on are in memory
assign_attributes(question, initial_attribs)
end
context "given a numeric question with prefix and suffix" do
let(:attribs) { { type: "numeric", prefix: "£", suffix: "incfreq" } }
let(:net_income_known) { "Yes – the household has a weekly income" }
let(:expected_suffix) { "Weekly" }
before do
case_log.update!(net_income_known: net_income_known)
assign(:case_log, case_log)
assign(:page, page)
assign(:subsection, subsection)
@ -30,12 +39,25 @@ RSpec.describe "form/page" do
render
end
after do
# Revert any changes we've made to avoid affecting other specs as the form,
# subsection, page, question objects being acted on are in memory
assign_attributes(question, initial_attribs)
it "renders prefix and suffix text" do
expect(rendered).to match(/govuk-input__prefix/)
expect(rendered).to match(/£/)
expect(rendered).to match(/govuk-input__suffix/)
expect(rendered).to match(expected_suffix)
end
end
context "given a question with extra guidance" do
before do
assign(:case_log, case_log)
assign(:page, page)
assign(:subsection, subsection)
assign_attributes(question, attribs)
render
end
let(:expected_guidance) { /What counts as income?/ }
context "with radio type" do
let(:attribs) { { type: "radio", answer_options: { "1": "A", "2": "B" } } }
it "renders the guidance partial for radio questions" do

Loading…
Cancel
Save