Browse Source

Refactor check answers row to use components

pull/499/head
Paul Robert Lloyd 3 years ago
parent
commit
2d2da174fe
  1. 9
      app/models/form/question.rb
  2. 22
      app/views/form/_check_answers_table.html.erb
  3. 10
      app/views/form/check_answers.html.erb
  4. 10
      app/views/form/review.html.erb
  5. 11
      spec/models/form/question_spec.rb

9
app/models/form/question.rb

@ -86,15 +86,18 @@ class Form::Question
end
end
def update_answer_link_name(case_log)
link_type = if has_inferred_check_answers_value?(case_log)
def action_text(case_log)
if has_inferred_check_answers_value?(case_log)
"Change"
elsif type == "checkbox"
answer_options.keys.any? { |key| value_is_yes?(case_log[key]) } ? "Change" : "Answer"
else
case_log[id].blank? ? "Answer" : "Change"
end
"#{link_type}<span class=\"govuk-visually-hidden\"> #{check_answer_label.to_s.downcase}</span>".html_safe
end
def action_href(case_log, page_id)
"/logs/#{case_log.id}/#{page_id.to_s.dasherize}?referrer=check_answers"
end
def completed?(case_log)

22
app/views/form/_check_answers_table.html.erb

@ -1,14 +1,14 @@
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">
<%= question.check_answer_label.to_s.presence || question.header.to_s %>
</dt>
<dd class="govuk-summary-list__value">
<% summary_list.row do |row| %>
<% row.key { question.check_answer_label.to_s.presence || question.header.to_s } %>
<% row.value do %>
<%= get_answer_label(question, @case_log) %><br>
<% question.get_inferred_answers(@case_log).each do |inferred_answer| %>
<span class="govuk-!-font-weight-regular app-!-colour-muted"><%= inferred_answer %></span><br>
<span class="govuk-!-font-weight-regular app-!-colour-muted"><%= inferred_answer %></span>
<% end %>
<% end %>
<% row.action(
text: question.action_text(@case_log),
href: question.action_href(@case_log, question.page.id),
visually_hidden_text: question.check_answer_label.to_s.downcase,
) %>
<% end %>
</dd>
<dd class="govuk-summary-list__actions">
<%= govuk_link_to(question.update_answer_link_name(@case_log), "/logs/#{@case_log.id}/#{question.page.id.to_s.dasherize}?referrer=check_answers").html_safe %>
</dd>
</div>

10
app/views/form/check_answers.html.erb

@ -19,11 +19,15 @@
<% end %>
<%= display_answered_questions_summary(subsection, @case_log) %>
<dl class="govuk-summary-list">
<%= govuk_summary_list do |summary_list| %>
<% subsection.applicable_questions(@case_log).each do |question| %>
<%= render partial: "form/check_answers_table", locals: { question:, case_log: @case_log } %>
<%= render partial: "form/check_answers_table", locals: {
summary_list:,
question:,
case_log: @case_log,
} %>
<% end %>
<% end %>
</dl>
<%= form_with model: @case_log, method: "get" do |f| %>
<%= f.govuk_submit "Save and return to log" do %>

10
app/views/form/review.html.erb

@ -21,11 +21,15 @@
<h3 class="x-govuk-summary-card__title"><%= subsection.label %></h3>
</div>
<div class="x-govuk-summary-card__body">
<dl class="govuk-summary-list">
<%= govuk_summary_list do |summary_list| %>
<% subsection.applicable_questions(@case_log).each do |question| %>
<%= render partial: 'form/check_answers_table', locals: { question: question, case_log: @case_log } %>
<%= render partial: 'form/check_answers_table', locals: {
summary_list:,
question:,
case_log: @case_log,
} %>
<% end %>
<% end %>
</dl>
</div>
</div>
<% end %>

11
spec/models/form/question_spec.rb

@ -226,9 +226,14 @@ RSpec.describe Form::Question, type: :model do
end
it "has an update answer link text helper" do
expect(question.update_answer_link_name(case_log)).to match(/Answer/)
expect(question.action_text(case_log)).to match(/Answer/)
case_log["incfreq"] = 0
expect(question.update_answer_link_name(case_log)).to match(/Change/)
expect(question.action_text(case_log)).to match(/Change/)
end
it "has an update answer link href helper" do
case_log.id = 1
expect(question.action_href(case_log, page.id)).to eq("/logs/1/net-income?referrer=check_answers")
end
context "when the question has an inferred answer" do
@ -239,7 +244,7 @@ RSpec.describe Form::Question, type: :model do
let(:question_id) { "postcode_full" }
it "displays 'change' in the check answers link text" do
expect(question.update_answer_link_name(case_log)).to match(/Change/)
expect(question.action_text(case_log)).to match(/Change/)
end
end

Loading…
Cancel
Save