Skip to content

Commit

Permalink
Delete blank contacts
Browse files Browse the repository at this point in the history
  • Loading branch information
brucebolt authored and callumknights committed May 29, 2024
1 parent 49ef76e commit 4e7d000
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
13 changes: 11 additions & 2 deletions app/controllers/admin/worldwide_offices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,17 @@ def edit
end

def update
worldwide_office_params[:service_ids] ||= []
if @worldwide_office.update(worldwide_office_params)
update_params = worldwide_office_params

update_params[:service_ids] ||= []

if update_params.dig(:contact_attributes, :contact_numbers_attributes)
update_params.dig(:contact_attributes, :contact_numbers_attributes).each do |_id, contact_number_attribute|
contact_number_attribute[:_destroy] = true if contact_number_attribute[:label].empty? && contact_number_attribute[:number].empty?
end
end

if @worldwide_office.update(update_params)
handle_show_on_home_page_param
republish_draft_worldwide_organisation
redirect_to admin_worldwide_organisation_worldwide_offices_path(@worldwide_organisation), notice: "#{@worldwide_office.title} has been edited"
Expand Down
28 changes: 27 additions & 1 deletion test/functional/admin/worldwide_offices_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ class Admin::WorldwideOfficesControllerTest < ActionController::TestCase
assert_equal ["Main phone: 5678"], actual_numbers
end

test "PUT :update deletes contact numbers that have only blank fields" do
test "PUT :update deletes contact numbers that are marked as to be destroyed" do
worldwide_organisation, office = create_worldwide_organisation_and_office
contact_number = office.contact.contact_numbers.create!(label: "Phone", number: "1234")

Expand All @@ -306,6 +306,32 @@ class Admin::WorldwideOfficesControllerTest < ActionController::TestCase
assert_not ContactNumber.exists?(contact_number.id)
end

test "PUT :update deletes contact numbers that have only blank fields" do
worldwide_organisation, office = create_worldwide_organisation_and_office
contact_number = office.contact.contact_numbers.create!(label: "Phone", number: "1234")

put :update,
params: {
worldwide_office: {
contact_attributes: {
id: office.contact.id,
title: "Head office",
contact_numbers_attributes: {
"0" => {
id: contact_number.id,
label: "",
number: "",
},
},
},
},
id: office,
worldwide_organisation_id: worldwide_organisation,
}

assert_not ContactNumber.exists?(contact_number.id)
end

test "POST on :reorder_for_home_page takes id => ordering mappings and reorders the list based on this" do
worldwide_organisation, office1 = create_worldwide_organisation_and_office
office2 = worldwide_organisation.offices.create!(
Expand Down

0 comments on commit 4e7d000

Please sign in to comment.