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
Fix Sync Taxonomy Name with Root Taxon Name. (#11519)
Matthew Kennedybe1cca51c72c
diff --git a/core/app/models/spree/taxon.rb b/core/app/models/spree/taxon.rbindex 88150f4bfc..0a8c4e61fa 100644--- a/core/app/models/spree/taxon.rb+++ b/core/app/models/spree/taxon.rb@@ -39,6 +39,7 @@ module Spreebefore_validation :copy_taxonomy_from_parentafter_save :touch_ancestors_and_taxonomy+ after_save :sync_taxonomy_nameafter_touch :touch_ancestors_and_taxonomyhas_one :icon, as: :viewable, dependent: :destroy, class_name: 'Spree::TaxonImage'@@ -105,6 +106,14 @@ module Spreeprivate+ def sync_taxonomy_name+ if saved_change_to_name? && root?+ return if taxonomy.name.to_s == name.to_s++ taxonomy.update(name: name)+ end+ end+def touch_ancestors_and_taxonomy# Touches all ancestors at once to avoid recursive taxonomy touch, and reduce queries.ancestors.update_all(updated_at: Time.current)diff --git a/core/app/models/spree/taxonomy.rb b/core/app/models/spree/taxonomy.rbindex 5ee382750f..fc4fb2c726 100644--- a/core/app/models/spree/taxonomy.rb+++ b/core/app/models/spree/taxonomy.rb@@ -26,6 +26,7 @@ module Spreedef set_root_taxon_namereturn unless saved_change_to_name?+ return if name.to_s == root.name.to_sroot.update(name: name)enddiff --git a/core/spec/models/spree/taxon_spec.rb b/core/spec/models/spree/taxon_spec.rbindex 3bb383983e..43e152be7a 100644--- a/core/spec/models/spree/taxon_spec.rb+++ b/core/spec/models/spree/taxon_spec.rb@@ -124,4 +124,62 @@ describe Spree::Taxon, type: :model doit { expect(taxon.valid?).to eq(true) }it { expect { taxon.save }.to change(taxon, :taxonomy).to(taxonomy) }end++ describe '#sync_taxonomy_name' do+ let!(:taxonomy) { create(:taxonomy, name: 'Soft Goods') }+ let!(:taxon) { create(:taxon, taxonomy: taxonomy, name: 'Socks' ) }++ context 'when none root taxon name is updated' do+ it 'does not update the taxonomy name' do+ taxon.update(name: 'Shoes')+ taxon.save!+ taxonomy.reload++ expect(taxonomy.name).not_to eql taxon.name+ expect(taxonomy.name).to eql 'Soft Goods'+ end+ end++ context 'when root taxon name is updated' do+ it 'updates the taxonomy name' do+ root_taxon = described_class.find_by(name: 'Soft Goods')++ root_taxon.update(name: 'Hard Goods')+ root_taxon.save!+ taxonomy.reload++ expect(taxonomy.name).not_to eql 'Soft Goods'+ expect(taxonomy.name).to eql root_taxon.name+ end+ end++ context 'when root taxon name is updated with special characters' do+ it 'updates the taxonomy name' do+ root_taxon = described_class.find_by(name: 'Soft Goods')++ root_taxon.update(name: 'spÉcial Numérique ƒ ˙ ¨ πø∆©')+ root_taxon.save!+ taxonomy.reload++ expect(taxonomy.name).not_to eql 'Soft Goods'+ expect(taxonomy.name).to eql root_taxon.name+ end+ end++ context 'when root taxon attribute other than name is updated' do+ it 'does not update the taxonomy' do+ root_taxon = described_class.find_by(name: 'Soft Goods')+ taxonomy_updated_at = taxonomy.updated_at.to_s++ expect {+ root_taxon.update(permalink: 'something-else')+ root_taxon.save!+ root_taxon.reload+ taxonomy.reload+ }.not_to change { taxonomy.updated_at.to_s }.from(taxonomy_updated_at)++ expect(root_taxon.permalink).to eql 'something-else'+ end+ end+ endenddiff --git a/core/spec/models/spree/taxonomy_spec.rb b/core/spec/models/spree/taxonomy_spec.rbindex 5d481dee59..b9f720889a 100644--- a/core/spec/models/spree/taxonomy_spec.rb+++ b/core/spec/models/spree/taxonomy_spec.rb@@ -20,6 +20,7 @@ describe Spree::Taxonomy, type: :model dodescribe '#set_root_taxon_name' dobefore do@taxonomy = create(:taxonomy, name: 'Clothing')+ @taxonomy.reloadendcontext 'when Taxonomy is created' do