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.
This commit is contained in:
Maikel Linke
2018-10-16 15:18:44 +11:00
parent a8705ca179
commit 61797fff56
2 changed files with 1 additions and 2 deletions

View File

@@ -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'

View File

@@ -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