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
end end
def update_answer_link_name(case_log) def action_text(case_log)
link_type = if has_inferred_check_answers_value?(case_log) if has_inferred_check_answers_value?(case_log)
"Change" "Change"
elsif type == "checkbox" elsif type == "checkbox"
answer_options.keys.any? { |key| value_is_yes?(case_log[key]) } ? "Change" : "Answer" answer_options.keys.any? { |key| value_is_yes?(case_log[key]) } ? "Change" : "Answer"
else else
case_log[id].blank? ? "Answer" : "Change" case_log[id].blank? ? "Answer" : "Change"
end 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 end
def completed?(case_log) def completed?(case_log)

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

@ -1,14 +1,14 @@
<div class="govuk-summary-list__row"> <% summary_list.row do |row| %>
<dt class="govuk-summary-list__key"> <% row.key { question.check_answer_label.to_s.presence || question.header.to_s } %>
<%= question.check_answer_label.to_s.presence || question.header.to_s %> <% row.value do %>
</dt>
<dd class="govuk-summary-list__value">
<%= get_answer_label(question, @case_log) %><br> <%= get_answer_label(question, @case_log) %><br>
<% question.get_inferred_answers(@case_log).each do |inferred_answer| %> <% 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 %>
</dd> <% end %>
<dd class="govuk-summary-list__actions"> <% row.action(
<%= 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 %> text: question.action_text(@case_log),
</dd> href: question.action_href(@case_log, question.page.id),
</div> visually_hidden_text: question.check_answer_label.to_s.downcase,
) %>
<% end %>

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

@ -19,11 +19,15 @@
<% end %> <% end %>
<%= display_answered_questions_summary(subsection, @case_log) %> <%= 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| %> <% 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 %> <% end %>
</dl>
<%= form_with model: @case_log, method: "get" do |f| %> <%= form_with model: @case_log, method: "get" do |f| %>
<%= f.govuk_submit "Save and return to log" do %> <%= 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> <h3 class="x-govuk-summary-card__title"><%= subsection.label %></h3>
</div> </div>
<div class="x-govuk-summary-card__body"> <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| %> <% 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 %> <% end %>
</dl>
</div> </div>
</div> </div>
<% end %> <% end %>

11
spec/models/form/question_spec.rb

@ -226,9 +226,14 @@ RSpec.describe Form::Question, type: :model do
end end
it "has an update answer link text helper" do 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 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 end
context "when the question has an inferred answer" do 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" } let(:question_id) { "postcode_full" }
it "displays 'change' in the check answers link text" do 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
end end

Loading…
Cancel
Save