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.

Auto-assign taxonomy from the parent Taxon if not set

Damian Legawiecd15be4a4b13a
diff --git a/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb b/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb
index 43e01edbe4..d13cc5bd28 100644
--- a/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb
@@ -3,7 +3,7 @@ require 'spec_helper'
describe 'Taxons Spec', type: :request do
let!(:default_store) { taxonomy.store }
let!(:taxonomy) { create(:taxonomy) }
- let!(:taxons) { create_list(:taxon, 2, taxonomy: taxonomy, parent_id: taxonomy.root.id) }
+ let!(:taxons) { create_list(:taxon, 2, taxonomy: taxonomy, parent: taxonomy.root) }
let(:store2) { create(:store)}
let!(:taxonomy2) { create(:taxonomy, store: store2) }
diff --git a/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb
index b74b00e1c5..5258e04c62 100644
--- a/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb
+++ b/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb
@@ -5,8 +5,9 @@ describe Spree::Api::V2::Platform::TaxonSerializer do
subject { described_class.new(taxon, params: serializer_params) }
- let(:taxon) { create(:taxon, products: create_list(:product, 2)) }
- let!(:children) { [create(:taxon, parent_id: taxon.id), create(:taxon, parent_id: taxon.id)] }
+ let(:taxonomy) { create(:taxonomy, store: store) }
+ let(:taxon) { create(:taxon, products: create_list(:product, 2, stores: [store]), taxonomy: taxonomy) }
+ let!(:children) { [create(:taxon, parent: taxon, taxonomy: taxonomy), create(:taxon, parent: taxon, taxonomy: taxonomy)] }
it { expect(subject.serializable_hash).to be_kind_of(Hash) }
diff --git a/core/app/models/spree/taxon.rb b/core/app/models/spree/taxon.rb
index 26a1fb9a66..4b07cf5773 100644
--- a/core/app/models/spree/taxon.rb
+++ b/core/app/models/spree/taxon.rb
@@ -35,6 +35,7 @@ module Spree
validates :meta_title
end
+ before_validation :copy_taxonomy_from_parent
after_save :touch_ancestors_and_taxonomy
after_touch :touch_ancestors_and_taxonomy
@@ -119,5 +120,9 @@ module Spree
errors.add(:parent, 'must belong to the same taxonomy')
end
end
+
+ def copy_taxonomy_from_parent
+ self.taxonomy = parent.taxonomy if parent.present? && taxonomy.blank?
+ end
end
end
diff --git a/core/spec/models/spree/product/scopes_spec.rb b/core/spec/models/spree/product/scopes_spec.rb
index b15b72e689..4ff6de1630 100644
--- a/core/spec/models/spree/product/scopes_spec.rb
+++ b/core/spec/models/spree/product/scopes_spec.rb
@@ -263,11 +263,11 @@ describe 'Product scopes', type: :model do
let(:parent_taxon) { create(:taxon) }
- let(:child_taxon_1) { create(:taxon, parent: parent_taxon) }
- let(:child_taxon_1_1) { create(:taxon, parent: child_taxon_1) }
+ let(:child_taxon_1) { create(:taxon, parent: parent_taxon, taxonomy: parent_taxon.taxonomy) }
+ let(:child_taxon_1_1) { create(:taxon, parent: child_taxon_1, taxonomy: child_taxon_1.taxonomy) }
- let(:child_taxon_2) { create(:taxon, parent: parent_taxon) }
- let(:child_taxon_2_1) { create(:taxon, parent: child_taxon_2) }
+ let(:child_taxon_2) { create(:taxon, parent: parent_taxon, taxonomy: parent_taxon.taxonomy) }
+ let(:child_taxon_2_1) { create(:taxon, parent: child_taxon_2,taxonomy: child_taxon_2.taxonomy) }
let!(:product_1) { create(:product, stores: [store]) }
let!(:classification_1_1) { create(:classification, position: 5, product: product_1, taxon: parent_taxon) }
diff --git a/core/spec/models/spree/taxon_spec.rb b/core/spec/models/spree/taxon_spec.rb
index f4aec4fcfa..f69ae8c000 100644
--- a/core/spec/models/spree/taxon_spec.rb
+++ b/core/spec/models/spree/taxon_spec.rb
@@ -2,7 +2,7 @@ require 'spec_helper'
describe Spree::Taxon, type: :model do
let(:taxonomy) { create(:taxonomy) }
- let(:taxon) { build(:taxon, name: 'Ruby on Rails', parent_id: nil) }
+ let(:taxon) { build(:taxon, name: 'Ruby on Rails', parent: nil) }
describe '#to_param' do
subject { super().to_param }
@@ -114,4 +114,12 @@ describe Spree::Taxon, type: :model do
describe '#cached_self_and_descendants_ids' do
it { expect(taxon.cached_self_and_descendants_ids).to eq(taxon.self_and_descendants.ids) }
end
+
+ describe '#copy_taxonomy_from_parent' do
+ let!(:parent) { create(:taxon, taxonomy: taxonomy) }
+ let(:taxon) { build(:taxon, parent: parent, taxonomy: nil) }
+
+ it { expect(taxon.valid?).to eq(true) }
+ it { expect { taxon.save }.to change(taxon, :taxonomy).to(taxonomy) }
+ end
end
diff --git a/frontend/spec/helpers/taxons_helper_spec.rb b/frontend/spec/helpers/taxons_helper_spec.rb
index c84fad80c6..bd6e9b14da 100644
--- a/frontend/spec/helpers/taxons_helper_spec.rb
+++ b/frontend/spec/helpers/taxons_helper_spec.rb
@@ -4,7 +4,7 @@ describe Spree::TaxonsHelper, type: :helper do
# Regression test for #4382
it '#taxon_preview' do
taxon = create(:taxon)
- child_taxon = create(:taxon, parent: taxon)
+ child_taxon = create(:taxon, parent: taxon, taxonomy: taxon.taxonomy)
product_1 = create(:product, stores: Spree::Store.all)
product_2 = create(:product, stores: Spree::Store.all)
product_3 = create(:product, stores: Spree::Store.all)
diff --git a/frontend/spec/support/shared_contexts/custom_products.rb b/frontend/spec/support/shared_contexts/custom_products.rb
index ed5ae72185..1f421a90e0 100644
--- a/frontend/spec/support/shared_contexts/custom_products.rb
+++ b/frontend/spec/support/shared_contexts/custom_products.rb
@@ -2,16 +2,16 @@ shared_context 'custom products' do
before do
taxonomy = FactoryBot.create(:taxonomy, name: 'Categories')
root = taxonomy.root
- clothing_taxon = FactoryBot.create(:taxon, name: 'Clothing', parent_id: root.id)
+ clothing_taxon = FactoryBot.create(:taxon, name: 'Clothing', parent: root, taxonomy: taxonomy)
trending_taxon = FactoryBot.create(:taxon, name: 'Trending')
- bags_taxon = FactoryBot.create(:taxon, name: 'Bags', parent_id: root.id)
- mugs_taxon = FactoryBot.create(:taxon, name: 'Mugs', parent_id: root.id)
+ bags_taxon = FactoryBot.create(:taxon, name: 'Bags', parent: root, taxonomy: taxonomy)
+ mugs_taxon = FactoryBot.create(:taxon, name: 'Mugs', parent: root, taxonomy: taxonomy)
taxonomy = FactoryBot.create(:taxonomy, name: 'Brands')
root = taxonomy.root
- apache_taxon = FactoryBot.create(:taxon, name: 'Apache', parent_id: root.id)
- rails_taxon = FactoryBot.create(:taxon, name: 'Ruby on Rails', parent_id: root.id)
- ruby_taxon = FactoryBot.create(:taxon, name: 'Ruby', parent_id: root.id)
+ apache_taxon = FactoryBot.create(:taxon, name: 'Apache', parent: root, taxonomy: taxonomy)
+ rails_taxon = FactoryBot.create(:taxon, name: 'Ruby on Rails', parent: root, taxonomy: taxonomy)
+ ruby_taxon = FactoryBot.create(:taxon, name: 'Ruby', parent: root, taxonomy: taxonomy)
store = Spree::Store.default
FactoryBot.create(:custom_product, name: 'Ruby on Rails Ringer T-Shirt', price: '159.99', taxons: [rails_taxon, clothing_taxon], stores: [store])