Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Handle deletion of Tax Category (#11655)

Damian Legawiec78ea29fccf50
diff --git a/core/app/models/spree/line_item.rb b/core/app/models/spree/line_item.rb
index 811a9ba26b..c8c91e2a2c 100644
--- a/core/app/models/spree/line_item.rb
+++ b/core/app/models/spree/line_item.rb
@@ -11,7 +11,7 @@ module Spree
belongs_to :order, class_name: 'Spree::Order', touch: true
belongs_to :variant, -> { with_deleted }, class_name: 'Spree::Variant'
end
- belongs_to :tax_category, class_name: 'Spree::TaxCategory'
+ belongs_to :tax_category, -> { with_deleted }, class_name: 'Spree::TaxCategory'
has_one :product, through: :variant
diff --git a/core/app/models/spree/tax_category.rb b/core/app/models/spree/tax_category.rb
index 2d444a19d3..35ae817570 100644
--- a/core/app/models/spree/tax_category.rb
+++ b/core/app/models/spree/tax_category.rb
@@ -8,6 +8,8 @@ module Spree
validates :name, presence: true, uniqueness: { case_sensitive: false, scope: spree_base_uniqueness_scope.push(:deleted_at) }
has_many :tax_rates, dependent: :destroy, inverse_of: :tax_category
+ has_many :products, dependent: :nullify
+ has_many :variants, dependent: :nullify
before_save :set_default_category
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index 48990bf221..02e79e4095 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -146,7 +146,7 @@ module Spree
@tax_category ||= if self[:tax_category_id].nil?
product.tax_category
else
- Spree::TaxCategory.find(self[:tax_category_id])
+ Spree::TaxCategory.find_by(id: self[:tax_category_id]) || product.tax_category
end
end
diff --git a/core/spec/models/spree/tax_category_spec.rb b/core/spec/models/spree/tax_category_spec.rb
index 127a052341..909bd2fe65 100644
--- a/core/spec/models/spree/tax_category_spec.rb
+++ b/core/spec/models/spree/tax_category_spec.rb
@@ -24,4 +24,22 @@ describe Spree::TaxCategory, type: :model do
expect(tax_category.is_default).to be true
end
end
+
+ context '#destroy' do
+ let!(:tax_category) { create(:tax_category) }
+ let!(:tax_rate) { create(:tax_rate, tax_category: tax_category) }
+ let!(:product) { create(:product, tax_category: tax_category) }
+ let!(:variant) { create(:variant, product: product, tax_category: tax_category) }
+
+ it 'removes all tax rates' do
+ expect { tax_category.destroy }.to change { Spree::TaxRate.count }.by(-1)
+ end
+
+ it 'nullifies all products and variants' do
+ expect { tax_category.destroy }.not_to change { Spree::Product.count }
+ expect { tax_category.destroy }.not_to change { Spree::Variant.count }
+ expect(product.reload.tax_category).to be_nil
+ expect(variant.reload.tax_category).to be_nil
+ end
+ end
end
diff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rb
index 4bdb645e7d..5b6a75411d 100644
--- a/core/spec/models/spree/variant_spec.rb
+++ b/core/spec/models/spree/variant_spec.rb
@@ -720,6 +720,47 @@ describe Spree::Variant, type: :model do
expect(variant.tax_category).to eq(tax_category)
end
end
+
+ context 'when tax category is deleted' do
+ let(:tax_category) { create(:tax_category) }
+ let(:variant) { build(:variant, tax_category: tax_category) }
+
+ before do
+ tax_category.destroy
+ end
+
+ it 'returns the parent products tax_category' do
+ expect(variant.tax_category).to eq(variant.product.tax_category)
+ end
+ end
+
+ context 'when tax category is deleted also in product' do
+ let(:tax_category) { create(:tax_category) }
+ let!(:default_tax_category) { create(:tax_category, is_default: true) }
+ let(:product) { create(:product, tax_category: tax_category) }
+ let(:variant) { build(:variant, product: product, tax_category: tax_category) }
+
+ before do
+ tax_category.destroy
+ product.reload
+ end
+
+ context 'with default tax category' do
+ it 'returns the default tax category' do
+ expect(variant.tax_category).to eq(default_tax_category)
+ end
+ end
+
+ context 'without default tax category' do
+ before do
+ default_tax_category.destroy
+ end
+
+ it 'returns nil' do
+ expect(variant.tax_category).to eq(nil)
+ end
+ end
+ end
end
describe 'touching' do