Fix Rubocop issue: Do not delete addresses having shipments

The reasoning is that we should not delete an address that has
ever received a shipment
This commit is contained in:
Anthony Musyoki
2024-03-21 15:54:29 +03:00
parent 5559816e12
commit 1ec453df4d
2 changed files with 12 additions and 1 deletions

View File

@@ -12,7 +12,7 @@ module Spree
belongs_to :state, class_name: "Spree::State", optional: true
has_one :enterprise, dependent: :restrict_with_exception
has_many :shipments
has_many :shipments, dependent: :restrict_with_exception
validates :address1, :city, :phone, presence: true
validates :company, presence: true, unless: -> { first_name.blank? || last_name.blank? }

View File

@@ -134,6 +134,17 @@ describe Spree::Address do
end
end
context "associations" do
it "destroys shipments upon destroy" do
address = create(:address)
create(:shipment, address:)
expect {
address.destroy
}.to raise_error(ActiveRecord::DeleteRestrictionError)
end
end
context ".default" do
it "sets up a new record the default country" do
expect(Spree::Address.default.country).to eq DefaultCountry.country