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.

feat(core): add Collection stack services + jobs

Łukasz Adamczyk7cd882817ba7
diff --git a/spree/core/app/jobs/spree/collections/regenerate_time_based_job.rb b/spree/core/app/jobs/spree/collections/regenerate_time_based_job.rb
new file mode 100644
index 0000000000..ef999789b1
--- /dev/null
+++ b/spree/core/app/jobs/spree/collections/regenerate_time_based_job.rb
@@ -0,0 +1,18 @@
+module Spree
+ module Collections
+ # Re-evaluates automatic collections whose rules are time-relative (e.g. AvailableOn),
+ # which event-driven regeneration can't keep fresh. Scheduled by the host app
+ # (sidekiq-cron / solid_queue recurring), the same pattern as Spree::StockReservations::ExpireJob.
+ class RegenerateTimeBasedJob < ::Spree::BaseJob
+ queue_as Spree.queues.collections
+
+ def perform
+ types = Rails.application.config.spree.time_based_collection_rules.map(&:to_s)
+
+ Spree::Collection.automatic.
+ where(id: Spree::CollectionRule.where(type: types).select(:collection_id)).
+ find_each { |collection| Spree::Collections::RegenerateProducts.call(collection: collection) }
+ end
+ end
+ end
+end
diff --git a/spree/core/app/jobs/spree/products/auto_match_collections_job.rb b/spree/core/app/jobs/spree/products/auto_match_collections_job.rb
new file mode 100644
index 0000000000..296aca4450
--- /dev/null
+++ b/spree/core/app/jobs/spree/products/auto_match_collections_job.rb
@@ -0,0 +1,14 @@
+module Spree
+ module Products
+ class AutoMatchCollectionsJob < ::Spree::BaseJob
+ queue_as Spree.queues.collections
+
+ def perform(product_id)
+ product = Spree::Product.find_by(id: product_id)
+ return unless product.present?
+
+ Spree::Products::AutoMatchCollections.call(product: product)
+ end
+ end
+ end
+end
diff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rb
index 5ff07545bf..67705eaf8f 100644
--- a/spree/core/app/models/spree/product.rb
+++ b/spree/core/app/models/spree/product.rb
@@ -74,6 +74,9 @@ module Spree
has_many :categories, through: :classifications, class_name: 'Spree::Category', source: :taxon
has_many :taxonomies, through: :taxons
+ has_many :product_collections, class_name: 'Spree::ProductCollection', dependent: :destroy_async, inverse_of: :product
+ has_many :collections, through: :product_collections
+
has_many :product_promotion_rules, class_name: 'Spree::ProductPromotionRule'
has_many :promotion_rules, through: :product_promotion_rules, class_name: 'Spree::PromotionRule'
diff --git a/spree/core/app/services/spree/collections/add_products.rb b/spree/core/app/services/spree/collections/add_products.rb
new file mode 100644
index 0000000000..ff68d0be30
--- /dev/null
+++ b/spree/core/app/services/spree/collections/add_products.rb
@@ -0,0 +1,42 @@
+module Spree
+ module Collections
+ class AddProducts
+ prepend Spree::ServiceModule::Base
+
+ # Adds the given products to the given collections (manual curation), in bulk.
+ #
+ # @param collections [Array<Spree::Collection>]
+ # @param products [Array<Spree::Product>]
+ # @return [Spree::ServiceModule::Base::Result]
+ def call(collections:, products:)
+ return if collections.blank? || products.blank?
+
+ product_collections_params = collections.pluck(:id).flat_map do |collection_id|
+ position = Spree::ProductCollection.where(collection_id: collection_id).count
+
+ products.pluck(:id).map do |product_id|
+ {
+ collection_id: collection_id,
+ product_id: product_id,
+ position: (position += 1),
+ created_at: Time.current,
+ updated_at: Time.current
+ }
+ end
+ end
+ Spree::ProductCollection.insert_all(product_collections_params)
+
+ collection_ids = collections.pluck(:id)
+ product_ids = products.pluck(:id)
+ collection_ids.each { |id| Spree::Collection.reset_counters(id, :product_collections) }
+ product_ids.each { |id| Spree::Product.reset_counters(id, :product_collections) }
+
+ Spree::Product.where(id: product_ids).touch_all
+ products.each(&:enqueue_search_index)
+ Spree::Collection.where(id: collection_ids).touch_all
+
+ success(true)
+ end
+ end
+ end
+end
diff --git a/spree/core/app/services/spree/collections/regenerate_products.rb b/spree/core/app/services/spree/collections/regenerate_products.rb
new file mode 100644
index 0000000000..3255544ac9
--- /dev/null
+++ b/spree/core/app/services/spree/collections/regenerate_products.rb
@@ -0,0 +1,66 @@
+module Spree
+ module Collections
+ class RegenerateProducts
+ prepend ::Spree::ServiceModule::Base
+
+ # Rebuilds a collection's materialized ProductCollection membership from its rules.
+ #
+ # @param collection [Spree::Collection]
+ # @return [Spree::ServiceModule::Base::Result]
+ def call(collection:)
+ return if collection.nil?
+ return if collection.destroyed?
+ return if collection.manual?
+
+ previous_products_ids = collection.product_collections.order(position: :asc).pluck(:product_id)
+
+ # Hard-delete the join rows (no callbacks). Class-level delete on the collection
+ # scope rather than the association proxy, which under `dependent: :destroy_async`
+ # doesn't reliably remove rows here.
+ Spree::ProductCollection.where(collection_id: collection.id).delete_all
+
+ products_matching_rules = collection.products_matching_rules
+ product_ids_to_insert = products_matching_rules.ids
+
+ previous_filtered_products_ids = previous_products_ids & product_ids_to_insert
+ max_products_position = previous_filtered_products_ids.size || 0
+
+ if product_ids_to_insert.any?
+ records_to_insert = product_ids_to_insert.map do |product_id|
+ position = previous_filtered_products_ids.index(product_id)
+ position = position.present? ? position + 1 : max_products_position += 1
+
+ product_collection_attributes(product_id, collection, position)
+ end
+
+ Spree::ProductCollection.insert_all(records_to_insert)
+
+ # expire product cache + reindex
+ products = Spree::Product.where(id: (previous_products_ids + product_ids_to_insert).uniq)
+ products.touch_all
+ products.each(&:enqueue_search_index)
+ end
+
+ # counter caches — bulk insert/delete bypass the ProductCollection counter_cache callbacks
+ Spree::Collection.reset_counters(collection.id, :product_collections)
+ all_product_ids = (previous_products_ids + product_ids_to_insert).uniq
+ existing_product_ids = Spree::Product.where(id: all_product_ids).pluck(:id)
+ existing_product_ids.each { |id| Spree::Product.reset_counters(id, :product_collections) }
+
+ success(collection)
+ end
+
+ private
+
+ def product_collection_attributes(product_id, collection, position)
+ {
+ product_id: product_id,
+ collection_id: collection.id,
+ position: position,
+ created_at: Time.current,
+ updated_at: Time.current
+ }
+ end
+ end
+ end
+end
diff --git a/spree/core/app/services/spree/collections/remove_products.rb b/spree/core/app/services/spree/collections/remove_products.rb
new file mode 100644
index 0000000000..c8b4e6381a
--- /dev/null
+++ b/spree/core/app/services/spree/collections/remove_products.rb
@@ -0,0 +1,65 @@
+module Spree
+ module Collections
+ class RemoveProducts
+ prepend Spree::ServiceModule::Base
+
+ # Removes the given products from the given collections and re-packs positions.
+ #
+ # @param collections [Array<Spree::Collection>]
+ # @param products [Array<Spree::Product>]
+ # @return [Spree::ServiceModule::Base::Result]
+ def call(collections:, products:)
+ return if collections.blank? || products.blank?
+
+ collection_ids = collections.pluck(:id)
+ product_ids = products.pluck(:id)
+
+ ApplicationRecord.transaction do
+ collection_ids.each do |collection_id|
+ Spree::ProductCollection.where(collection_id: collection_id, product_id: product_ids).delete_all
+ end
+
+ product_collections_params = collection_ids.flat_map do |collection_id|
+ position = 0
+ existing_product_ids = Spree::ProductCollection.where(collection_id: collection_id).pluck(:product_id)
+
+ existing_product_ids.map do |product_id|
+ {
+ collection_id: collection_id,
+ product_id: product_id,
+ position: (position += 1),
+ created_at: Time.current,
+ updated_at: Time.current
+ }
+ end
+ end
+
+ if product_collections_params.any?
+ opts = {}
+ # Column list rather than the index name — the composite unique index is named
+ # index_product_collections_on_collection_and_product (shortened to fit the
+ # identifier limit), so match by columns to stay name-independent.
+ opts[:unique_by] = %i[collection_id product_id] unless mysql_adapter?
+
+ Spree::ProductCollection.upsert_all(product_collections_params, **opts)
+ end
+ end
+
+ collection_ids.each { |id| Spree::Collection.reset_counters(id, :product_collections) }
+ product_ids.each { |id| Spree::Product.reset_counters(id, :product_collections) }
+
+ Spree::Product.where(id: product_ids).touch_all
+ products.each(&:enqueue_search_index)
+ Spree::Collection.where(id: collection_ids).touch_all
+
+ success(true)
+ end
+
+ private
+
+ def mysql_adapter?
+ ActiveRecord::Base.connection.adapter_name.downcase.include?('mysql')
+ end
+ end
+ end
+end
diff --git a/spree/core/app/services/spree/products/auto_match_collections.rb b/spree/core/app/services/spree/products/auto_match_collections.rb
new file mode 100644
index 0000000000..9683a04090
--- /dev/null
+++ b/spree/core/app/services/spree/products/auto_match_collections.rb
@@ -0,0 +1,74 @@
+module Spree
+ module Products
+ class AutoMatchCollections
+ prepend ::Spree::ServiceModule::Base
+
+ # Re-evaluates a product's membership in the automatic collections of its store —
+ # removing it from collections it no longer matches and adding it to ones it now does.
+ #
+ # @param product [Spree::Product]
+ # @return [Spree::ServiceModule::Base::Result]
+ def call(product:)
+ return unless product.present?
+
+ collections_to_remove = []
+ collections_to_add = []
+
+ # existing memberships that no longer match (tags/prices changed)
+ product.collections.automatic.includes(:collection_rules).each do |collection|
+ collections_to_remove << collection unless collection.products_matching_rules.ids.include?(product.id)
+ end
+
+ if collections_to_remove.any?
+ Spree::ProductCollection.where(collection: collections_to_remove, product: product).delete_all
+ end
+
+ # automatic collections in the product's store that it now matches
+ Spree::Collection.automatic.where(store_id: product.store_id).includes(:collection_rules, :products).each do |collection|
+ collections_to_add << collection if collection.products.exclude?(product) && collection.products_matching_rules.ids.include?(product.id)
+ end
+
+ if collections_to_add.any?
+ products_counts = Spree::Collection.where(id: collections_to_add.pluck(:id)).
+ joins(:product_collections).
+ reorder('').
+ group(:collection_id).
+ count(:product_id)
+
+ Spree::ProductCollection.insert_all(
+ collections_to_add.map do |collection|
+ position = products_counts[collection.id].to_i + 1
+ product_collection_attributes(collection, product, position)
+ end
+ )
+ end
+
+ all_affected = (collections_to_remove + collections_to_add).uniq
+
+ if all_affected.any?
+ affected_ids = all_affected.pluck(:id)
+ Spree::Collection.where(id: affected_ids).touch_all
+ product.touch
+
+ # counter caches — bulk insert/delete bypass the counter_cache callbacks
+ affected_ids.each { |id| Spree::Collection.reset_counters(id, :product_collections) }
+ Spree::Product.reset_counters(product.id, :product_collections)
+ end
+
+ success(product)
+ end
+
+ private
+
+ def product_collection_attributes(collection, product, position)
+ {
+ collection_id: collection.id,
+ product_id: product.id,
+ position: position,
+ created_at: Time.current,
+ updated_at: Time.current
+ }
+ end
+ end
+ end
+end
diff --git a/spree/core/lib/spree/core.rb b/spree/core/lib/spree/core.rb
index ffba03a11f..f632d4bc24 100644
--- a/spree/core/lib/spree/core.rb
+++ b/spree/core/lib/spree/core.rb
@@ -104,6 +104,7 @@ module Spree
reports: :default,
variants: :default,
taxons: :default,
+ collections: :default,
stock_location_stock_items: :default,
coupon_codes: :default,
themes: :default,
diff --git a/spree/core/lib/spree/core/engine.rb b/spree/core/lib/spree/core/engine.rb
index 5d09a3e696..3dabbe0108 100644
--- a/spree/core/lib/spree/core/engine.rb
+++ b/spree/core/lib/spree/core/engine.rb
@@ -19,6 +19,8 @@ module Spree
:export_types,
:import_types,
:taxon_rules,
+ :collection_rules,
+ :time_based_collection_rules,
:themes,
:theme_layout_sections,
:pages,
@@ -240,6 +242,21 @@ module Spree
Spree::TaxonRules::Sale,
]
+ # Mirrors config.spree.taxon_rules above. AvailableOn ships with an interim
+ # legacy-column implementation; its channel-aware rewrite is a later phase
+ # (see docs/plans/6.0-replace-taxons-with-categories.md → Migration Phase 5).
+ Rails.application.config.spree.collection_rules = [
+ Spree::CollectionRules::Tag,
+ Spree::CollectionRules::AvailableOn,
+ Spree::CollectionRules::Sale,
+ ]
+
+ # Net-new (no taxon_rules equivalent — taxons ship no scheduled refresh).
+ # Drives Spree::Collections::RegenerateTimeBasedJob.
+ Rails.application.config.spree.time_based_collection_rules = [
+ Spree::CollectionRules::AvailableOn,
+ ]
+
Rails.application.config.spree.reports = [
Spree::Reports::ProductsPerformance,
Spree::Reports::SalesTotal
diff --git a/spree/core/spec/jobs/spree/collections/regenerate_time_based_job_spec.rb b/spree/core/spec/jobs/spree/collections/regenerate_time_based_job_spec.rb
new file mode 100644
index 0000000000..eb76c320f9
--- /dev/null
+++ b/spree/core/spec/jobs/spree/collections/regenerate_time_based_job_spec.rb
@@ -0,0 +1,23 @@
+require 'spec_helper'
+
+module Spree
+ RSpec.describe Collections::RegenerateTimeBasedJob do
+ let(:store) { @default_store }
+ let!(:collection) { create(:automatic_collection, store: store) }
+ let!(:rule) { create(:available_on_collection_rule, :is_equal_to, collection: collection, value: 30) }
+ let!(:product) { create(:product) }
+
+ # Materialize membership while the product is fresh (within the 30-day window).
+ before { Spree::Collections::RegenerateProducts.call(collection: collection) }
+
+ it 'drops a product that has aged out of the window' do
+ expect(collection.reload.products).to include(product)
+
+ product.update_columns(created_at: 90.days.ago, available_on: 90.days.ago, updated_at: 90.days.ago)
+
+ described_class.perform_now
+
+ expect(collection.reload.products).not_to include(product)
+ end
+ end
+end
diff --git a/spree/core/spec/services/spree/collections/add_products_spec.rb b/spree/core/spec/services/spree/collections/add_products_spec.rb
new file mode 100644
index 0000000000..b086c9591f
--- /dev/null
+++ b/spree/core/spec/services/spree/collections/add_products_spec.rb
@@ -0,0 +1,27 @@
+require 'spec_helper'
+
+module Spree
+ RSpec.describe Collections::AddProducts do
+ let(:store) { @default_store }
+ let!(:collection) { create(:collection, store: store) }
+ let!(:products) { create_list(:product, 2) }
+
+ it 'adds products to the collection with sequential positions' do
+ described_class.call(collections: [collection], products: products)
+
+ expect(collection.reload.products).to match_array(products)
+ expect(collection.product_collections.order(:position).pluck(:position)).to eq([1, 2])
+ end
+
+ it 'maintains counter caches' do
+ described_class.call(collections: [collection], products: products)
+
+ expect(collection.reload.products_count).to eq(2)
+ expect(products.first.reload.collections_count).to eq(1)
+ end
+
+ it 'returns early when either side is blank' do
+ expect { described_class.call(collections: [], products: products) }.not_to change { Spree::ProductCollection.count }
+ end
+ end
+end
diff --git a/spree/core/spec/services/spree/collections/regenerate_products_spec.rb b/spree/core/spec/services/spree/collections/regenerate_products_spec.rb
new file mode 100644
index 0000000000..863294767e
--- /dev/null
+++ b/spree/core/spec/services/spree/collections/regenerate_products_spec.rb
@@ -0,0 +1,46 @@
+require 'spec_helper'
+
+module Spree
+ RSpec.describe Collections::RegenerateProducts do
+ let(:store) { @default_store }
+ let(:tag) { ActsAsTaggableOn::Tag.create(name: 'sale') }
+ let!(:matching_product) { create(:product, tags: [tag]) }
+ let!(:other_product) { create(:product) }
+ let!(:collection) { create(:automatic_collection, store: store) }
+ let!(:rule) { create(:tag_collection_rule, :is_equal_to, collection: collection, value: 'sale') }
+
+ subject { described_class.call(collection: collection) }
+
+ it 'materializes membership from the rules' do
+ subject
+ expect(collection.reload.products).to contain_exactly(matching_product)
+ end
+
+ it 'maintains the products_count counter cache' do
+ subject
+ expect(collection.reload.products_count).to eq(1)
+ end
+
+ it 'reconciles membership — wipes stale rows and rebuilds from the rules' do
+ # explicit calls (not the memoized `subject`) so the service actually re-runs
+ described_class.call(collection: collection)
+ expect(collection.reload.products).to contain_exactly(matching_product)
+
+ # a stale membership for a product that does not match the rules
+ Spree::ProductCollection.create!(collection: collection, product: other_product)
+ expect(collection.reload.products).to include(other_product)
+
+ described_class.call(collection: collection)
+ expect(collection.reload.products).to contain_exactly(matching_product)
+ end
+
+ context 'with a manual collection' do
+ let!(:collection) { create(:collection, store: store) }
+ let!(:rule) { nil }
+
+ it 'is a no-op' do
+ expect { subject }.not_to change { Spree::ProductCollection.count }
+ end
+ end
+ end
+end
diff --git a/spree/core/spec/services/spree/collections/remove_products_spec.rb b/spree/core/spec/services/spree/collections/remove_products_spec.rb
new file mode 100644
index 0000000000..10675559f3
--- /dev/null
+++ b/spree/core/spec/services/spree/collections/remove_products_spec.rb
@@ -0,0 +1,24 @@
+require 'spec_helper'
+
+module Spree
+ RSpec.describe Collections::RemoveProducts do
+ let(:store) { @default_store }
+ let!(:collection) { create(:collection, store: store) }
+ let!(:products) { create_list(:product, 3) }
+
+ before { Spree::Collections::AddProducts.call(collections: [collection], products: products) }
+
+ it 'removes the given products and re-packs positions' do
+ described_class.call(collections: [collection], products: [products.second])
+
+ expect(collection.reload.products).to match_array([products.first, products.third])
+ expect(collection.product_collections.order(:position).pluck(:position)).to eq([1, 2])
+ end
+
+ it 'updates counter caches' do
+ expect {
+ described_class.call(collections: [collection], products: [products.second])
+ }.to change { collection.reload.products_count }.from(3).to(2)
+ end
+ end
+end
diff --git a/spree/core/spec/services/spree/products/auto_match_collections_spec.rb b/spree/core/spec/services/spree/products/auto_match_collections_spec.rb
new file mode 100644
index 0000000000..3c8ce8b95e
--- /dev/null
+++ b/spree/core/spec/services/spree/products/auto_match_collections_spec.rb
@@ -0,0 +1,32 @@
+require 'spec_helper'
+
+module Spree
+ RSpec.describe Products::AutoMatchCollections do
+ let(:store) { @default_store }
+ let!(:collection) { create(:automatic_collection, store: store) }
+ let!(:rule) { create(:tag_collection_rule, :is_equal_to, collection: collection, value: 'sale') }
+
+ it 'adds a product that newly matches an automatic collection' do
+ product = create(:product)
+ expect(collection.reload.products).not_to include(product)
+
+ product.tag_list.add('sale')
+ product.save!
+ described_class.call(product: product)
+
+ expect(collection.reload.products).to include(product)
+ end
+
+ it 'removes a product that no longer matches' do
+ product = create(:product, tags: [ActsAsTaggableOn::Tag.create(name: 'sale')])
+ described_class.call(product: product)
+ expect(collection.reload.products).to include(product)
+
+ product.tag_list.remove('sale')
+ product.save!
+ described_class.call(product: product)
+
+ expect(collection.reload.products).not_to include(product)
+ end
+ end
+end