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
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.rbindex 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 dolet!(: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.rbindex 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 dosubject { 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.rbindex 26a1fb9a66..4b07cf5773 100644--- a/core/app/models/spree/taxon.rb+++ b/core/app/models/spree/taxon.rb@@ -35,6 +35,7 @@ module Spreevalidates :meta_titleend+ before_validation :copy_taxonomy_from_parentafter_save :touch_ancestors_and_taxonomyafter_touch :touch_ancestors_and_taxonomy@@ -119,5 +120,9 @@ module Spreeerrors.add(:parent, 'must belong to the same taxonomy')endend++ def copy_taxonomy_from_parent+ self.taxonomy = parent.taxonomy if parent.present? && taxonomy.blank?+ endendenddiff --git a/core/spec/models/spree/product/scopes_spec.rb b/core/spec/models/spree/product/scopes_spec.rbindex 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 dolet(: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.rbindex 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 dolet(: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' dosubject { super().to_param }@@ -114,4 +114,12 @@ describe Spree::Taxon, type: :model dodescribe '#cached_self_and_descendants_ids' doit { 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) }+ endenddiff --git a/frontend/spec/helpers/taxons_helper_spec.rb b/frontend/spec/helpers/taxons_helper_spec.rbindex 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 #4382it '#taxon_preview' dotaxon = 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.rbindex 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' dobefore dotaxonomy = 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.defaultFactoryBot.create(:custom_product, name: 'Ruby on Rails Ringer T-Shirt', price: '159.99', taxons: [rails_taxon, clothing_taxon], stores: [store])