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
feat(core): wire Collection membership on Product and Store
Łukasz Adamczyk3ee6e2e817f6
diff --git a/spree/core/app/models/concerns/spree/product_scopes.rb b/spree/core/app/models/concerns/spree/product_scopes.rbindex 1304458f21..dee9d91a6e 100644--- a/spree/core/app/models/concerns/spree/product_scopes.rb+++ b/spree/core/app/models/concerns/spree/product_scopes.rb@@ -174,6 +174,15 @@ module Spreejoins(:classifications).where(Classification.table_name => { taxon_id: taxon_ids }).distinctend+ # Products in a collection (flat — collections have no hierarchy).+ # Accepts a Collection record or a prefixed ID string (e.g. 'coll_xxx').+ def self.in_collection(collection_or_id)+ collection = collection_or_id.is_a?(String) ? Spree::Collection.find_by_prefix_id(collection_or_id) : collection_or_id+ return none unless collection++ joins(:product_collections).where(Spree::ProductCollection.table_name => { collection_id: collection.id }).distinct+ end+# Deprecated — remove in 6.0. Use in_taxon instead.def self.in_taxons(*taxons)Spree::Deprecation.warn('in_taxons is deprecated and will be removed in Spree 6.0. Use in_taxon instead.')diff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rbindex 67705eaf8f..6852e795f8 100644--- a/spree/core/app/models/spree/product.rb+++ b/spree/core/app/models/spree/product.rb@@ -145,6 +145,7 @@ module Spreeafter_touch :touch_taxonsafter_commit :auto_match_taxons, if: :eligible_for_taxon_matching?+ after_commit :auto_match_collections, if: :eligible_for_taxon_matching?with_options length: { maximum: 255 }, allow_blank: true dovalidates :meta_keywords@@ -225,6 +226,17 @@ module Spreeself.taxon_ids = Spree::Taxon.for_store(assignable_store).where(id: decoded_ids).idsend+ # Accepts both prefixed IDs and raw integer IDs. Only collections belonging+ # to the product's own store are assigned. `collections` is a direct+ # has_many :through, so this overrides the generated setter and calls super+ # (unlike #category_ids=, which writes through to the sibling taxon_ids).+ 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).where(id: decoded_ids).ids)+ end+# Sync media inline. Entries with `id` patch the existing asset# (alt/position/variant_ids); entries with `signed_id` create + attach a# fresh upload; missing items are left alone (delete still goes through@@ -269,9 +281,9 @@ module Spreeendself.whitelisted_ransackable_attributes = %w[description name slug discontinue_on status available_on created_at updated_at]- self.whitelisted_ransackable_associations = %w[taxons categories store channels variants_including_master master variants tags labels+ self.whitelisted_ransackable_associations = %w[taxons categories collections store channels variants_including_master master variants tags labelsshipping_category classifications option_types]- self.whitelisted_ransackable_scopes = %w[not_discontinued search_by_name in_taxon in_category in_categories price_between+ self.whitelisted_ransackable_scopes = %w[not_discontinued search_by_name in_taxon in_category in_categories in_collection price_betweenprice_lte price_gtesearch multi_search in_stock out_of_stock with_option_value_ids@@ -629,6 +641,13 @@ module Spree@main_taxon ||= taxons.firstend+ # First category (the main_taxon -> primary_category rename). main_taxon is+ # retained until the Phase 4 taxon->category sweep — reports and CSV+ # presenters still call it.+ def primary_category+ @primary_category ||= categories.first+ end+def taxons_for_store(store)return if classification_count.zero?@@ -660,6 +679,14 @@ module SpreeSpree::Products::AutoMatchTaxonsJob.set(wait: 30.seconds).perform_later(id)end+ def auto_match_collections+ return if deleted?+ return if archived?+ return if store.nil? || store.collections.automatic.none?++ Spree::Products::AutoMatchCollectionsJob.set(wait: 30.seconds).perform_later(id)+ end+def to_csv(store = nil)store ||= self.storeproperties_for_csv = if respond_to?(:product_properties) && Spree::Config.respond_to?(:product_properties_enabled) && Spree::Config[:product_properties_enabled]diff --git a/spree/core/app/models/spree/store.rb b/spree/core/app/models/spree/store.rbindex 6d858992e6..24eaf61a87 100644--- a/spree/core/app/models/spree/store.rb+++ b/spree/core/app/models/spree/store.rb@@ -98,6 +98,7 @@ module Spreehas_many :taxonomies, class_name: 'Spree::Taxonomy'has_many :taxons, class_name: 'Spree::Taxon'has_many :categories, class_name: 'Spree::Category'+ has_many :collections, class_name: 'Spree::Collection', dependent: :destroy_asynchas_many :promotions, class_name: 'Spree::Promotion', dependent: :nullifydiff --git a/spree/core/spec/models/spree/product/scopes_spec.rb b/spree/core/spec/models/spree/product/scopes_spec.rbindex 8369d84580..41538b1679 100644--- a/spree/core/spec/models/spree/product/scopes_spec.rb+++ b/spree/core/spec/models/spree/product/scopes_spec.rb@@ -95,6 +95,30 @@ describe 'Product scopes', type: :model doendend+ describe '#in_collection (flat — no hierarchy)' do+ let!(:collection_a) { create(:collection, store: store) }+ let!(:collection_b) { create(:collection, store: store) }+ let!(:product_a) { create(:product) }+ let!(:product_b) { create(:product) }++ before do+ Spree::ProductCollection.create!(collection: collection_a, product: product_a)+ Spree::ProductCollection.create!(collection: collection_b, product: product_b)+ end++ it 'returns only products directly in the collection' do+ expect(Spree::Product.in_collection(collection_a)).to contain_exactly(product_a)+ end++ it 'accepts a prefixed id string' do+ expect(Spree::Product.in_collection(collection_a.prefixed_id)).to contain_exactly(product_a)+ end++ it 'returns none for an unknown collection' do+ expect(Spree::Product.in_collection('coll_nope')).to be_empty+ end+ end+context 'sorting scopes' docontext 'ascend_by_updated_at' doit { expect(Spree::Product.ascend_by_updated_at.to_sql).to include('updated_at ASC') }diff --git a/spree/core/spec/models/spree/product_spec.rb b/spree/core/spec/models/spree/product_spec.rbindex 1fa01f5624..ad4add8826 100644--- a/spree/core/spec/models/spree/product_spec.rb+++ b/spree/core/spec/models/spree/product_spec.rb@@ -1128,6 +1128,64 @@ describe Spree::Product, type: :model doendend+ describe '#collection_ids=' do+ let(:product) { create(:product, store: store) }+ let(:collection) { create(:collection, store: store) }++ it 'assigns collections belonging to the product store' do+ product.update!(collection_ids: [collection.prefixed_id])+ expect(product.reload.collections).to include(collection)+ end++ it 'ignores collections from another store' do+ foreign = create(:collection, store: create(:store))++ product.update!(collection_ids: [collection.prefixed_id, foreign.prefixed_id])++ expect(product.reload.collections).to include(collection)+ expect(product.reload.collections).not_to include(foreign)+ end+ end++ describe '#primary_category' do+ let(:product) { create(:product, store: store) }+ let(:taxonomy) { create(:taxonomy, store: store) }+ let(:category) { create(:taxon, taxonomy: taxonomy) }++ it 'returns the first assigned category' do+ product.update!(category_ids: [category.prefixed_id])+ # categories are read back as Spree::Category (scoped subclass), so match by id+ expect(product.reload.primary_category.id).to eq(category.id)+ end++ it 'is nil when the product has no categories' do+ expect(product.primary_category).to be_nil+ end+ end++ describe 'after_commit :auto_match_collections' do+ it 'is invoked on an eligible save' do+ expect_any_instance_of(described_class).to receive(:auto_match_collections).at_least(:once)+ create(:product, store: store)+ end+ end++ describe '#auto_match_collections' do+ let!(:product) { create(:product, store: store) }++ it 'enqueues AutoMatchCollectionsJob when the store has automatic collections' do+ create(:automatic_collection, store: store)++ expect { product.auto_match_collections }.+ to have_enqueued_job(Spree::Products::AutoMatchCollectionsJob).with(product.id)+ end++ it 'does not enqueue when the store has no automatic collections' do+ expect { product.auto_match_collections }.+ not_to have_enqueued_job(Spree::Products::AutoMatchCollectionsJob)+ end+ end+describe '#any_variant_in_stock_or_backorderable?' dosubject { product.any_variant_in_stock_or_backorderable? }