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.

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.rb
index 88150f4bfc..0a8c4e61fa 100644
--- a/core/app/models/spree/taxon.rb
+++ b/core/app/models/spree/taxon.rb
@@ -39,6 +39,7 @@ module Spree
before_validation :copy_taxonomy_from_parent
after_save :touch_ancestors_and_taxonomy
+ after_save :sync_taxonomy_name
after_touch :touch_ancestors_and_taxonomy
has_one :icon, as: :viewable, dependent: :destroy, class_name: 'Spree::TaxonImage'
@@ -105,6 +106,14 @@ module Spree
private
+ 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.rb
index 5ee382750f..fc4fb2c726 100644
--- a/core/app/models/spree/taxonomy.rb
+++ b/core/app/models/spree/taxonomy.rb
@@ -26,6 +26,7 @@ module Spree
def set_root_taxon_name
return unless saved_change_to_name?
+ return if name.to_s == root.name.to_s
root.update(name: name)
end
diff --git a/core/spec/models/spree/taxon_spec.rb b/core/spec/models/spree/taxon_spec.rb
index 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 do
it { 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
+ end
end
diff --git a/core/spec/models/spree/taxonomy_spec.rb b/core/spec/models/spree/taxonomy_spec.rb
index 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 do
describe '#set_root_taxon_name' do
before do
@taxonomy = create(:taxonomy, name: 'Clothing')
+ @taxonomy.reload
end
context 'when Taxonomy is created' do