From 61797fff56dac6c5a0e7357c9331d2af3b0ff82c Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Tue, 16 Oct 2018 15:18:44 +1100 Subject: [PATCH] Restrict deletion of address explicitely Enterprises have an `address_id` which must point to a valid `Spree::Address`. As Rubocop suggested, I restricted the deletion of addresses when they are still associated to an enterprise. Without declaring `dependent: :restrict`, trying to delete the address would raise `ActiveRecord::InvalidForeignKey`. Now it is more specific and raises `ActiveRecord::DeleteRestrictionError`. I didn't find code rescuing the InvalidForeignKey when deleting addresses. I actually think that we never delete addresses. So this change should not have any impact on the execution. --- .rubocop_todo.yml | 1 - app/models/spree/address_decorator.rb | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index cfc4e57e90..8d06d3cfad 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1440,7 +1440,6 @@ Rails/HasManyOrHasOneDependent: - 'app/models/customer.rb' - 'app/models/enterprise.rb' - 'app/models/order_cycle.rb' - - 'app/models/spree/address_decorator.rb' - 'app/models/spree/adjustment_decorator.rb' - 'app/models/spree/order_decorator.rb' - 'app/models/spree/payment_method_decorator.rb' diff --git a/app/models/spree/address_decorator.rb b/app/models/spree/address_decorator.rb index 04c8014d46..ff7c1f0665 100644 --- a/app/models/spree/address_decorator.rb +++ b/app/models/spree/address_decorator.rb @@ -1,5 +1,5 @@ Spree::Address.class_eval do - has_one :enterprise + has_one :enterprise, dependent: :restrict belongs_to :country, class_name: "Spree::Country" after_save :touch_enterprise