diff --git a/app/controllers/providers/correspondence_address/care_ofs_controller.rb b/app/controllers/providers/correspondence_address/care_ofs_controller.rb index 72552d8f3b0..825bf8c1c90 100644 --- a/app/controllers/providers/correspondence_address/care_ofs_controller.rb +++ b/app/controllers/providers/correspondence_address/care_ofs_controller.rb @@ -10,20 +10,11 @@ def show def update @form = Addresses::CareOfForm.new(form_params) - delete_care_of_information if form_params[:care_of].eql?("no") - render :show unless save_continue_or_draft(@form) end private - def delete_care_of_information - address.care_of_first_name = "" - address.care_of_last_name = "" - address.care_of_organisation_name = "" - address.save! - end - def address @address ||= legal_aid_application.applicant.address end diff --git a/app/controllers/providers/correspondence_address/choices_controller.rb b/app/controllers/providers/correspondence_address/choices_controller.rb index 73040f3e865..3e3974036ed 100644 --- a/app/controllers/providers/correspondence_address/choices_controller.rb +++ b/app/controllers/providers/correspondence_address/choices_controller.rb @@ -10,12 +10,6 @@ def show def update @form = Addresses::ChoiceForm.new(form_params) - delete_no_fixed_residence if form_params[:correspondence_address_choice].eql?("home") - - if form_params[:correspondence_address_choice].eql?("office") && !applicant.correspondence_address_choice.eql?("office") - delete_correspondence_address - end - render :show unless save_continue_or_draft(@form) end @@ -25,11 +19,6 @@ def applicant legal_aid_application.applicant end - def delete_no_fixed_residence - applicant.no_fixed_residence = nil - applicant.save! - end - def form_params merge_with_model(applicant) do next {} unless params[:applicant] diff --git a/app/controllers/providers/home_address/statuses_controller.rb b/app/controllers/providers/home_address/statuses_controller.rb index 7efbed8f015..60a422c3f01 100644 --- a/app/controllers/providers/home_address/statuses_controller.rb +++ b/app/controllers/providers/home_address/statuses_controller.rb @@ -12,8 +12,6 @@ def update @form = ::HomeAddress::StatusForm.new(form_params) @correspondence_address = applicant.address - delete_home_address if form_params[:no_fixed_residence].eql?("true") - render :show unless save_continue_or_draft(@form) end diff --git a/app/controllers/providers/provider_base_controller.rb b/app/controllers/providers/provider_base_controller.rb index 94461d0b9e0..ef1716a63f7 100644 --- a/app/controllers/providers/provider_base_controller.rb +++ b/app/controllers/providers/provider_base_controller.rb @@ -9,16 +9,6 @@ class ProviderBaseController < FlowBaseController private - def delete_home_address - applicant.home_address = nil - applicant.save! - end - - def delete_correspondence_address - applicant.address = nil - applicant.save! - end - def display_employment_income? @legal_aid_application.cfe_result.version >= 4 && @legal_aid_application.cfe_result.jobs? diff --git a/app/forms/addresses/care_of_form.rb b/app/forms/addresses/care_of_form.rb index d17d8718b0f..6160cc595e0 100644 --- a/app/forms/addresses/care_of_form.rb +++ b/app/forms/addresses/care_of_form.rb @@ -8,8 +8,20 @@ class CareOfForm < BaseForm validates :care_of_first_name, :care_of_last_name, presence: true, if: :person_name_required? validates :care_of_organisation_name, presence: true, if: :organisation_name_required? + def save + delete_care_of_information if care_of.eql?("no") + super + end + alias_method :save!, :save + private + def delete_care_of_information + attributes["care_of_first_name"] = nil + attributes["care_of_last_name"] = nil + attributes["care_of_organisation_name"] = nil + end + def person_name_required? !draft? && care_of == "person" end diff --git a/app/forms/addresses/choice_form.rb b/app/forms/addresses/choice_form.rb index 0785c8f9acd..eb9b992ac9b 100644 --- a/app/forms/addresses/choice_form.rb +++ b/app/forms/addresses/choice_form.rb @@ -8,6 +8,12 @@ class ChoiceForm < BaseForm validates :correspondence_address_choice, inclusion: { in: %w[home residence office] }, allow_blank: true, unless: :draft? def save + model.no_fixed_residence = nil if correspondence_address_choice.eql?("home") + + if correspondence_address_choice.eql?("office") && !model.correspondence_address_choice.eql?("office") + model.address = nil + end + model.update!(same_correspondence_and_home_address: correspondence_address_choice.eql?("home")) super end diff --git a/app/forms/home_address/status_form.rb b/app/forms/home_address/status_form.rb index f22bd6bc98b..004bb96322d 100644 --- a/app/forms/home_address/status_form.rb +++ b/app/forms/home_address/status_form.rb @@ -5,5 +5,11 @@ class StatusForm < BaseForm attr_accessor :no_fixed_residence validates :no_fixed_residence, presence: true, unless: :draft? + + def save + model.home_address = nil if no_fixed_residence.eql?("true") + super + end + alias_method :save!, :save end end diff --git a/spec/forms/addresses/care_of_form_spec.rb b/spec/forms/addresses/care_of_form_spec.rb index 200fc8a1f8b..c694ea7f431 100644 --- a/spec/forms/addresses/care_of_form_spec.rb +++ b/spec/forms/addresses/care_of_form_spec.rb @@ -19,8 +19,6 @@ describe "#save" do subject(:call_save) { instance.save } - before { call_save } - context "with care of person chosen" do let(:care_of) { "person" } let(:care_of_last_name) { "Smith" } @@ -28,6 +26,7 @@ let(:care_of_organisation_name) { nil } it "updates the address care_of fields" do + call_save expect(address.care_of).to eq "person" expect(address.care_of_last_name).to eq "Smith" expect(address.care_of_first_name).to eq "Bob" @@ -42,6 +41,7 @@ let(:care_of_organisation_name) { "An Organisation Name" } it "updates the address care_of fields" do + call_save expect(address.care_of).to eq "organisation" expect(address.care_of_last_name).to be_nil expect(address.care_of_first_name).to be_nil @@ -56,11 +56,26 @@ let(:care_of_organisation_name) { nil } it "updates the address care_of fields" do + call_save expect(address.care_of).to eq "no" expect(address.care_of_last_name).to be_nil expect(address.care_of_first_name).to be_nil expect(address.care_of_organisation_name).to be_nil end + + context "when changing from another option to no" do + let(:address) do + create(:address, care_of_first_name: "first_name", + care_of_last_name: "last_name", + care_of_organisation_name: "organisation_name") + end + + it "clears previously entered 'care of' information" do + expect { call_save }.to change(address, :care_of_last_name).from("last_name").to(nil) + .and change(address, :care_of_first_name).from("first_name").to(nil) + .and change(address, :care_of_organisation_name).from("organisation_name").to(nil) + end + end end context "with no answer chosen" do @@ -69,6 +84,8 @@ let(:care_of_first_name) { nil } let(:care_of_organisation_name) { nil } + before { call_save } + it "is invalid" do expect(instance).not_to be_valid end @@ -85,6 +102,8 @@ let(:care_of_first_name) { nil } let(:care_of_organisation_name) { nil } + before { call_save } + it "is invalid" do expect(instance).not_to be_valid end @@ -101,6 +120,8 @@ let(:care_of_first_name) { nil } let(:care_of_organisation_name) { nil } + before { call_save } + it "is invalid" do expect(instance).not_to be_valid end diff --git a/spec/forms/addresses/choice_form_spec.rb b/spec/forms/addresses/choice_form_spec.rb index d40f7caf960..35673c620f0 100644 --- a/spec/forms/addresses/choice_form_spec.rb +++ b/spec/forms/addresses/choice_form_spec.rb @@ -38,6 +38,14 @@ describe "#save" do context "when the correspondence_address_choice is home" do it { expect { form.save }.to change(applicant, :same_correspondence_and_home_address).from(nil).to(true) } + + context "when changing from another option" do + let(:applicant) { create(:applicant, no_fixed_residence: true) } + + it "clears no fixed residence information" do + expect { form.save }.to change(applicant, :no_fixed_residence).from(true).to(nil) + end + end end context "when the correspondence_address_choice is residence" do @@ -50,6 +58,22 @@ let(:correspondence_address_choice) { "office" } it { expect { form.save }.to change(applicant, :same_correspondence_and_home_address).from(nil).to(false) } + + context "when changing from another option" do + let(:applicant) { create(:applicant, :with_correspondence_address, correspondence_address_choice: "home") } + + it "clears address information" do + expect { form.save }.to change(applicant, :address).to(nil) + end + + context "and the correspondence_address_choice was previously office" do + let(:applicant) { create(:applicant, :with_correspondence_address, correspondence_address_choice: "office") } + + it "doesn't clear address information" do + expect { form.save }.not_to change(applicant, :address) + end + end + end end end end diff --git a/spec/requests/providers/correspondence_address/care_ofs_controller_spec.rb b/spec/requests/providers/correspondence_address/care_ofs_controller_spec.rb index 080faf13ca5..2826b3b1d2f 100644 --- a/spec/requests/providers/correspondence_address/care_ofs_controller_spec.rb +++ b/spec/requests/providers/correspondence_address/care_ofs_controller_spec.rb @@ -99,13 +99,6 @@ expect(applicant.reload.address.care_of).to eq "no" end - it "clears previously entered 'care of' information" do - patch_request - expect(applicant.reload.address.care_of_first_name).to eq "" - expect(applicant.reload.address.care_of_last_name).to eq "" - expect(applicant.reload.address.care_of_organisation_name).to eq "" - end - it "redirects to the next page" do patch_request expect(response).to have_http_status(:redirect) diff --git a/spec/requests/providers/correspondence_address/choices_controller_spec.rb b/spec/requests/providers/correspondence_address/choices_controller_spec.rb index b1fc549ead6..fb74bb25c76 100644 --- a/spec/requests/providers/correspondence_address/choices_controller_spec.rb +++ b/spec/requests/providers/correspondence_address/choices_controller_spec.rb @@ -72,27 +72,6 @@ patch_request expect(response).to have_http_status(:redirect) end - - context "when 'My client's UK home address' is chosen" do - let(:legal_aid_application) { create(:legal_aid_application, applicant:) } - let(:applicant) { create(:applicant, no_fixed_residence: true) } - - it "deletes the home address" do - patch_request - expect(applicant.reload.no_fixed_residence).to be_nil - end - end - - context "when 'Another UK residential address' is chosen" do - let(:legal_aid_application) { create(:legal_aid_application, applicant:) } - let(:applicant) { create(:applicant, no_fixed_residence: true) } - let(:correspondence_address_choice) { "residence" } - - it "does not delete the home address" do - patch_request - expect(applicant.reload.no_fixed_residence).to be true - end - end end context "with form submitted using Save as draft button" do