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.

Preserve automatic collection membership on product writes

Damian Legawiecd98c8d8d246e
diff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rb
index fc344d170c..301c2b2276 100644
--- a/spree/core/app/models/spree/product.rb
+++ b/spree/core/app/models/spree/product.rb
@@ -263,20 +263,23 @@ module Spree
super(Spree::Category.for_store(assignable_store).where(id: decoded_ids).ids)
end
- # Accepts both prefixed IDs and raw integer IDs. Only manual collections
- # belonging to the product's own store are assigned; this overrides the
- # generated setter and calls super.
+ # Accepts both prefixed IDs and raw integer IDs. Only collections belonging
+ # to the product's own store are assigned; this overrides the generated
+ # setter and calls super.
#
- # Automatic collections are dropped rather than assigned: their membership
- # is materialized from their rules, so a hand-set member would silently
- # disappear on the next regeneration. This matches the bulk endpoints
- # (`Spree::Api::V3::Admin::ProductsController#apply_collections`), which
- # scope to `.manual` for the same reason.
+ # This is a replace setter, so it governs manual membership only. Automatic
+ # collections are materialized from their rules, which means they must
+ # survive a write that doesn't mention them (otherwise saving a product
+ # would wipe rule-derived membership until the next regeneration) and must
+ # not be assignable by hand (a hand-set member would vanish at that
+ # regeneration). So incoming automatic ids are ignored, and the product's
+ # existing automatic memberships are carried over untouched.
def collection_ids=(ids)
decoded_ids = Array(ids).filter_map do |id|
id.to_s.include?('_') ? Spree::Collection.decode_prefixed_id(id) : id
end
- super(Spree::Collection.for_store(assignable_store).manual.where(id: decoded_ids).ids)
+ manual_ids = Spree::Collection.for_store(assignable_store).manual.where(id: decoded_ids).ids
+ super(manual_ids | collections.automatic.ids)
end
# Sync media inline. Entries with `id` patch the existing asset
diff --git a/spree/core/spec/models/spree/product_spec.rb b/spree/core/spec/models/spree/product_spec.rb
index 0e63c0eae9..102d71eae2 100644
--- a/spree/core/spec/models/spree/product_spec.rb
+++ b/spree/core/spec/models/spree/product_spec.rb
@@ -1058,6 +1058,37 @@ describe Spree::Product, type: :model do
expect(product.reload.collections).to include(collection)
expect(product.reload.collections).not_to include(foreign)
end
+
+ # Automatic membership is materialized from rules: it must survive a write
+ # that doesn't mention it, and must not be settable by hand.
+ context 'with automatic collections' do
+ let!(:automatic) { create(:automatic_collection, store: store) }
+
+ before { Spree::ProductCollection.create!(collection: automatic, product: product) }
+
+ it 'preserves automatic membership when assigning manual collections' do
+ product.update!(collection_ids: [collection.id])
+
+ expect(product.reload.collections).to include(collection, automatic)
+ end
+
+ it 'ignores an automatic collection passed by hand' do
+ other_automatic = create(:automatic_collection, store: store)
+
+ product.update!(collection_ids: [collection.id, other_automatic.id])
+
+ expect(product.reload.collections).to include(collection, automatic)
+ expect(product.reload.collections).not_to include(other_automatic)
+ end
+
+ it 'clears manual membership on an empty array without touching automatic' do
+ product.update!(collection_ids: [collection.id])
+
+ product.update!(collection_ids: [])
+
+ expect(product.reload.collections).to eq([automatic])
+ end
+ end
end
describe '#primary_category' do