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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
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.rbindex 811a9ba26b..c8c91e2a2c 100644--- a/core/app/models/spree/line_item.rb+++ b/core/app/models/spree/line_item.rb@@ -11,7 +11,7 @@ module Spreebelongs_to :order, class_name: 'Spree::Order', touch: truebelongs_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: :variantdiff --git a/core/app/models/spree/tax_category.rb b/core/app/models/spree/tax_category.rbindex 2d444a19d3..35ae817570 100644--- a/core/app/models/spree/tax_category.rb+++ b/core/app/models/spree/tax_category.rb@@ -8,6 +8,8 @@ module Spreevalidates :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: :nullifybefore_save :set_default_categorydiff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rbindex 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_categoryelse- Spree::TaxCategory.find(self[:tax_category_id])+ Spree::TaxCategory.find_by(id: self[:tax_category_id]) || product.tax_categoryendenddiff --git a/core/spec/models/spree/tax_category_spec.rb b/core/spec/models/spree/tax_category_spec.rbindex 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 doexpect(tax_category.is_default).to be trueendend++ 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+ endenddiff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rbindex 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 doexpect(variant.tax_category).to eq(tax_category)endend++ 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+ endenddescribe 'touching' do