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
Seed product type associations on assignment, not only on create
Damian Legawiec22631a7c4900
diff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rbindex 0c0ff28012..84c50402a0 100644--- a/spree/core/app/models/spree/product.rb+++ b/spree/core/app/models/spree/product.rb@@ -167,6 +167,7 @@ module Spreeafter_initialize :assign_default_tax_categoryafter_create :sync_associations_from_product_type+ after_update :sync_associations_from_product_type, if: :saved_change_to_product_type_id?after_create :apply_pending_variants, if: :pending_variants?after_create :ensure_default_variantafter_create :set_default_variant@@ -847,15 +848,19 @@ module Spreeend# Additive, unlike the legacy prototype callback which replaced both sets —- # the type is a floor the product builds on, never a ceiling.+ # the type seeds the product when attached (creation, later assignment,+ # reassignment) and never removes anything; detaching seeds nothing.+ # Presence guards query the join tables directly: the cached associations+ # can be stale here (pending variants insert option-type joins through+ # product_option_types within the same save).def sync_associations_from_product_typereturn unless product_typeproduct_type.option_types.each do |option_type|- option_types << option_type unless option_types.include?(option_type)+ option_types << option_type unless product_option_types.exists?(option_type_id: option_type.id)endproduct_type.categories.each do |category|- categories << category unless categories.include?(category)+ categories << category unless product_categories.exists?(category_id: category.id)endenddiff --git a/spree/core/spec/models/spree/product_spec.rb b/spree/core/spec/models/spree/product_spec.rbindex ed6e280971..da693a92ba 100644--- a/spree/core/spec/models/spree/product_spec.rb+++ b/spree/core/spec/models/spree/product_spec.rb@@ -504,6 +504,59 @@ describe Spree::Product, type: :model doendend+ describe 'assigning a product type to a persisted product' do+ let(:size) { create(:option_type, name: 'size') }+ let(:color) { create(:option_type, name: 'color') }+ let(:category) { create(:category) }+ let(:product_type) { create(:product_type, option_types: [size], categories: [category]) }+ let(:product) { create(:product) }++ it 'seeds the type option types and categories' do+ product.update!(product_type: product_type)++ expect(product.reload.option_types).to include(size)+ expect(product.categories).to include(category)+ end++ it 'keeps associations the product already had' do+ product.option_types << color+ product.update!(product_type: product_type)++ expect(product.reload.option_types).to match_array([color, size])+ end++ it 'seeds nothing when an unrelated attribute changes' do+ product.update!(product_type: product_type)+ product_type.option_types << create(:option_type, name: 'material')++ product.update!(name: 'Renamed')++ expect(product.reload.option_types).to match_array([size])+ end++ context 'reassigning to another type' do+ let(:other_type) { create(:product_type, name: 'Other Type', option_types: [color]) }++ it 'seeds the new type additively and removes nothing' do+ product.update!(product_type: product_type)+ product.update!(product_type: other_type)++ expect(product.reload.option_types).to match_array([size, color])+ expect(product.categories).to include(category)+ end+ end++ context 'detaching the type' do+ it 'removes nothing' do+ product.update!(product_type: product_type)+ product.update!(product_type: nil)++ expect(product.reload.option_types).to match_array([size])+ expect(product.categories).to include(category)+ end+ end+ end+describe '#variants= on a persisted product' dolet!(:product) { create(:product) }let!(:default_variant) { product.default_variant }