Skip to content

Commit

Permalink
AP-5220: Refactor to move delete methods from controllers into forms
Browse files Browse the repository at this point in the history
  • Loading branch information
agoldstone93 committed Aug 29, 2024
1 parent 5c5c62e commit 1da63c4
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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]
Expand Down
2 changes: 0 additions & 2 deletions app/controllers/providers/home_address/statuses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 0 additions & 10 deletions app/controllers/providers/provider_base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
12 changes: 12 additions & 0 deletions app/forms/addresses/care_of_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions app/forms/addresses/choice_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions app/forms/home_address/status_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 23 additions & 2 deletions spec/forms/addresses/care_of_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
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" }
let(:care_of_first_name) { "Bob" }
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"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
24 changes: 24 additions & 0 deletions spec/forms/addresses/choice_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1da63c4

Please sign in to comment.