From 4bec6cb8aaf10d7a961a9febea53f4705a0dbd5d Mon Sep 17 00:00:00 2001 From: Kat <54268893+kosiakkatrina@users.noreply.github.com> Date: Wed, 18 Dec 2024 11:41:25 +0000 Subject: [PATCH] Restore invalid postcode input value in postcode field --- app/controllers/form_controller.rb | 9 +++++++++ .../questions/postcode_for_full_address.rb | 1 + app/models/form/question.rb | 2 +- app/models/log.rb | 4 ++++ spec/requests/form_controller_spec.rb | 19 ++++++++++++++++++- 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index f31662c4f..ad12352a0 100644 --- a/app/controllers/form_controller.rb +++ b/app/controllers/form_controller.rb @@ -93,6 +93,7 @@ class FormController < ApplicationController @questions = request.params["related_question_ids"].map { |id| @log.form.get_question(id, @log) } render "form/check_errors" else + restore_placeholder_values(@log, @page) if flash[:errors].present? restore_previous_errors(flash[:errors]) restore_error_field_values(flash[:log_data]) @@ -125,6 +126,14 @@ private @log.assign_attributes(previous_responses_to_reset) end + def restore_placeholder_values(log, page) + page.questions.each do |question| + next if question.placeholder_method.blank? + + log[question.id] ||= log.send(question.placeholder_method) + end + end + def restore_previous_errors(previous_errors) return unless previous_errors diff --git a/app/models/form/lettings/questions/postcode_for_full_address.rb b/app/models/form/lettings/questions/postcode_for_full_address.rb index d6ee9859f..edd08b7c3 100644 --- a/app/models/form/lettings/questions/postcode_for_full_address.rb +++ b/app/models/form/lettings/questions/postcode_for_full_address.rb @@ -20,6 +20,7 @@ class Form::Lettings::Questions::PostcodeForFullAddress < ::Form::Question @disable_clearing_if_not_routed_or_dynamic_answer_options = true @question_number = QUESTION_NUMBER_FROM_YEAR[form.start_date.year] || QUESTION_NUMBER_FROM_YEAR[QUESTION_NUMBER_FROM_YEAR.keys.max] @hide_question_number_on_page = true + @placeholder_method = "postcode_full_input_placeholder" end QUESTION_NUMBER_FROM_YEAR = { 2023 => 12, 2024 => 13 }.freeze diff --git a/app/models/form/question.rb b/app/models/form/question.rb index 05eec89dd..ce272e610 100644 --- a/app/models/form/question.rb +++ b/app/models/form/question.rb @@ -8,7 +8,7 @@ class Form::Question :top_guidance_partial, :bottom_guidance_partial, :prefix, :suffix, :requires_js, :fields_added, :derived, :check_answers_card_number, :unresolved_hint_text, :question_number, :hide_question_number_on_page, - :plain_label, :error_label + :plain_label, :error_label, :placeholder_method def initialize(id, hsh, page) @id = id diff --git a/app/models/log.rb b/app/models/log.rb index 94f0ff58d..dcd326059 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -294,6 +294,10 @@ class Log < ApplicationRecord !!public_send("age#{person_num}_known")&.zero? end + def postcode_full_input_placeholder + postcode_full_input + end + private # Handle logs that are older than previous collection start date diff --git a/spec/requests/form_controller_spec.rb b/spec/requests/form_controller_spec.rb index 6ab49e302..f2795d87b 100644 --- a/spec/requests/form_controller_spec.rb +++ b/spec/requests/form_controller_spec.rb @@ -371,8 +371,10 @@ RSpec.describe FormController, type: :request do end describe "GET" do + let(:current_time) { Time.zone.local(2022, 5, 1) } + around do |example| - Timecop.freeze(Time.zone.local(2022, 5, 1)) do + Timecop.freeze(current_time) do Singleton.__init__(FormHandler) example.run end @@ -443,6 +445,21 @@ RSpec.describe FormController, type: :request do end end end + + context "when viewing the manual adress entry page" do + let(:current_time) { Time.zone.local(2024, 5, 1) } + + context "and postcode input exists but postcode is not set" do + let(:user) { create(:user, :support) } + let(:lettings_log) { create(:lettings_log, :setup_completed, postcode_full_input: "A1", address_line1_input: "xx", postcode_full: nil, uprn_known: 0, uprn_selection: "uprn_not_listed") } + + it "displays the postcode_full_input in postcode_full field" do + get "/lettings-logs/#{lettings_log.id}/address", headers: headers, params: {} + expect(page).to have_field("lettings-log-postcode-full-field", with: "A1") + expect(lettings_log.reload.postcode_full).to eq(nil) + end + end + end end context "when displaying check answers pages" do