From 465bf39b0c489b3eb37b8d22b3f4335981b84002 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Thu, 11 Jul 2024 10:02:50 +0100 Subject: [PATCH] CLDC-3465 Allow clearing all error questions (#2483) * Allow clearing all error questions * Update setup question link * Update clear all to ignore setup questions * Update correct_validation_action_href * Extract some vriables --- ...swers_summary_list_card_component.html.erb | 2 +- ...eck_answers_summary_list_card_component.rb | 4 +- app/controllers/check_errors_controller.rb | 16 +- app/controllers/form_controller.rb | 17 ++- app/helpers/check_errors_helper.rb | 11 ++ .../confirm_clear_all_answers.html.erb | 32 ++++ .../confirm_clear_answer.html.erb | 2 +- app/views/form/check_errors.html.erb | 8 +- spec/requests/check_errors_controller_spec.rb | 137 +++++++++++++++++- 9 files changed, 209 insertions(+), 20 deletions(-) create mode 100644 app/helpers/check_errors_helper.rb create mode 100644 app/views/check_errors/confirm_clear_all_answers.html.erb diff --git a/app/components/check_answers_summary_list_card_component.html.erb b/app/components/check_answers_summary_list_card_component.html.erb index 4f3df47fa..4c1f9aa2d 100644 --- a/app/components/check_answers_summary_list_card_component.html.erb +++ b/app/components/check_answers_summary_list_card_component.html.erb @@ -36,7 +36,7 @@ <% if @log.collection_period_open_for_editing? %> <% row.with_action( text: question.action_text(log, correcting_hard_validation: @correcting_hard_validation), - href: @correcting_hard_validation ? correct_validation_action_href(question, log, applicable_questions.map(&:id)) : action_href(question, log), + href: correct_validation_action_href(question, log, applicable_questions.map(&:id), @correcting_hard_validation), visually_hidden_text: question.check_answer_label.to_s.downcase, ) %> <% end %> diff --git a/app/components/check_answers_summary_list_card_component.rb b/app/components/check_answers_summary_list_card_component.rb index f229b76d4..5242c7f41 100644 --- a/app/components/check_answers_summary_list_card_component.rb +++ b/app/components/check_answers_summary_list_card_component.rb @@ -34,7 +34,9 @@ class CheckAnswersSummaryListCardComponent < ViewComponent::Base send("#{log.model_name.param_key}_#{question.page.id}_path", log, referrer:) end - def correct_validation_action_href(question, log, _related_question_ids) + def correct_validation_action_href(question, log, _related_question_ids, correcting_hard_validation) + return action_href(question, log) unless correcting_hard_validation + if question.displayed_as_answered?(log) send("#{log.model_name.param_key}_confirm_clear_answer_path", log, question_id: question.id) else diff --git a/app/controllers/check_errors_controller.rb b/app/controllers/check_errors_controller.rb index b828a4cd9..f246e0b01 100644 --- a/app/controllers/check_errors_controller.rb +++ b/app/controllers/check_errors_controller.rb @@ -8,9 +8,21 @@ class CheckErrorsController < ApplicationController return render_not_found unless @log @related_question_ids = params[@log.model_name.param_key].keys.reject { |id| id == "page_id" } - question_id = @related_question_ids.find { |id| !params[id].nil? } - @question = @log.form.get_question(question_id, @log) @page = @log.form.get_page(params[@log.model_name.param_key]["page_id"]) + + if params["clear_all"] + @questions_to_clear = @related_question_ids.map { |id| + question = @log.form.get_question(id, @log) + next if question.subsection.id == "setup" + + question.page.questions.map(&:id) + }.flatten.compact + + render :confirm_clear_all_answers + else + question_id = @related_question_ids.find { |id| !params[id].nil? } + @question = @log.form.get_question(question_id, @log) + end end private diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index 8c4fd9f49..ed7e8ba58 100644 --- a/app/controllers/form_controller.rb +++ b/app/controllers/form_controller.rb @@ -230,7 +230,9 @@ private if params[@log.model_name.param_key]["check_errors"] @page = form.get_page(params[@log.model_name.param_key]["page"]) flash[:notice] = "You have successfully updated #{@page.questions.map(&:check_answer_label).to_sentence}" - return send("#{@log.class.name.underscore}_#{params[@log.model_name.param_key]['original_page_id']}_path", @log, { check_errors: true, related_question_ids: params[@log.model_name.param_key]["related_question_ids"].split(" ") }.compact) + original_page_id = params[@log.model_name.param_key]["original_page_id"] + related_question_ids = params[@log.model_name.param_key]["related_question_ids"].split(" ") + return send("#{@log.class.name.underscore}_#{original_page_id}_path", @log, { check_errors: true, related_question_ids: }.compact) end if params["referrer"] == "check_errors" @@ -402,11 +404,16 @@ private end def render_check_errors_page - if params[@log.model_name.param_key]["clear_question_id"] - question_id = params[@log.model_name.param_key]["clear_question_id"] - @log.form.get_question(question_id, @log).page.questions.map(&:id).each { |id| @log[id] = nil } + if params[@log.model_name.param_key]["clear_question_ids"].present? + question_ids = params[@log.model_name.param_key]["clear_question_ids"].split(" ") + question_ids.each do |question_id| + question = @log.form.get_question(question_id, @log) + next if question.subsection.id == "setup" + + question.page.questions.map(&:id).each { |id| @log[id] = nil } + end @log.save! - @questions = params[@log.model_name.param_key].keys.reject { |id| %w[clear_question_id page].include?(id) }.map { |id| @log.form.get_question(id, @log) } + @questions = params[@log.model_name.param_key].keys.reject { |id| %w[clear_question_ids page].include?(id) }.map { |id| @log.form.get_question(id, @log) } else responses_for_page = responses_for_page(@page) @log.assign_attributes(responses_for_page) diff --git a/app/helpers/check_errors_helper.rb b/app/helpers/check_errors_helper.rb new file mode 100644 index 000000000..6d1ff0166 --- /dev/null +++ b/app/helpers/check_errors_helper.rb @@ -0,0 +1,11 @@ +module CheckErrorsHelper + include GovukLinkHelper + + def check_errors_answer_text(question, log) + question.displayed_as_answered?(log) ? "Change" : "Answer" + end + + def check_errors_answer_link(log, question, page, applicable_questions) + send("#{log.model_name.param_key}_#{question.page.id}_path", log, referrer: "check_errors", original_page_id: page.id, related_question_ids: applicable_questions.map(&:id)) + end +end diff --git a/app/views/check_errors/confirm_clear_all_answers.html.erb b/app/views/check_errors/confirm_clear_all_answers.html.erb new file mode 100644 index 000000000..f49ea98cd --- /dev/null +++ b/app/views/check_errors/confirm_clear_all_answers.html.erb @@ -0,0 +1,32 @@ +<% content_for :before_content do %> + <% content_for :title, "Are you sure you want to clear all?" %> +<% end %> + +
You've selected <%= @questions_to_clear.count %> answers to clear
+ + <%= govuk_warning_text(text: "You will not be able to undo this action") %> + <%= form_with model: @log, url: send("#{@log.model_name.param_key}_#{@page.id}_path", @log), method: "post", local: true do |f| %> + + <% @related_question_ids.each do |id| %> + <%= f.hidden_field id, value: @log[id] %> + <% end %> + + <%= f.hidden_field :clear_question_ids, value: @questions_to_clear %> + <%= f.hidden_field :page, value: @page.id %> + + + <% end %> +