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.

Scope the product type backfill to the type's store

Damian Legawiec80c5c5e9a46f
diff --git a/spree/core/app/jobs/spree/product_types/apply_to_products_job.rb b/spree/core/app/jobs/spree/product_types/apply_to_products_job.rb
index 38f8bd984b..89045eca21 100644
--- a/spree/core/app/jobs/spree/product_types/apply_to_products_job.rb
+++ b/spree/core/app/jobs/spree/product_types/apply_to_products_job.rb
@@ -57,15 +57,17 @@ module Spree
return if category_ids.empty?
service = Spree::ProductTypes::ApplyToProducts.new
+ store = @product_type.store
+
# Per-product counters batch — the whole point of the cursor is not
# holding a catalog-sized id list in memory.
@product_type.products.select(:id).
find_in_batches(batch_size: Spree::ProductTypes::ApplyToProducts::BATCH_SIZE) do |products|
- service.settle_product_counters(products.map(&:id))
+ service.settle_product_counters(store, products.map(&:id))
end
# The subtree-wide category recount runs once.
- service.settle_category_counters(category_ids)
+ service.settle_category_counters(store, category_ids)
end
end
end
diff --git a/spree/core/app/models/spree/category.rb b/spree/core/app/models/spree/category.rb
index ba9019679e..058c473311 100644
--- a/spree/core/app/models/spree/category.rb
+++ b/spree/core/app/models/spree/category.rb
@@ -150,8 +150,9 @@ module Spree
# Categories are manual only in 6.0 — rule-based (automatic) membership lives on
# Spree::Collection. The automatic/rules_match_policy/sort_order columns and the
# spree_taxon_rules table are retained for the Phase 4 data migration and dropped
- # in 6.1. The manual scope stays as a guard against any stray automatic row on the
- # category surfaces (a no-op once the migration clears them).
+ # in 6.1. Nothing creates an automatic category any more, so the model
+ # associations carry no scope; the API controllers still apply it as a
+ # boundary guard against rows an install has yet to migrate.
scope :manual, -> { where.not(automatic: true) }
def manual?
diff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rb
index 97d3abc048..2d9ab8943d 100644
--- a/spree/core/app/models/spree/product.rb
+++ b/spree/core/app/models/spree/product.rb
@@ -75,7 +75,7 @@ module Spree
has_many :product_option_types, -> { order(:position) }, dependent: :destroy, inverse_of: :product
has_many :option_types, through: :product_option_types
has_many :product_categories, -> { order(created_at: :asc) }, class_name: 'Spree::ProductCategory', dependent: :delete_all, inverse_of: :product
- has_many :categories, -> { manual }, through: :product_categories, class_name: 'Spree::Category', source: :category, before_remove: :remove_category
+ has_many :categories, through: :product_categories, class_name: 'Spree::Category', source: :category, before_remove: :remove_category
has_many :taxonomies, through: :categories
# @deprecated The classification_count column was renamed to categories_count in 6.0.
diff --git a/spree/core/app/models/spree/store.rb b/spree/core/app/models/spree/store.rb
index d4baff23a5..75da4af8f7 100644
--- a/spree/core/app/models/spree/store.rb
+++ b/spree/core/app/models/spree/store.rb
@@ -104,9 +104,7 @@ module Spree
has_many :store_credit_events, through: :store_credits, class_name: 'Spree::StoreCreditEvent'
has_many :taxonomies, class_name: 'Spree::Taxonomy'
- # Category surfaces exclude automatic (rule-based) rows. The manual scope is a
- # no-op once the Phase 4 migration moves them to Collections; removed in 6.1.
- has_many :categories, -> { manual }, class_name: 'Spree::Category'
+ has_many :categories, class_name: 'Spree::Category'
has_many :collections, class_name: 'Spree::Collection', dependent: :destroy_async
# restrict, not destroy — a type in use is refused deletion, so wiping a
diff --git a/spree/core/app/services/spree/product_types/apply_to_products.rb b/spree/core/app/services/spree/product_types/apply_to_products.rb
index b059183de2..c1ecbd29a3 100644
--- a/spree/core/app/services/spree/product_types/apply_to_products.rb
+++ b/spree/core/app/services/spree/product_types/apply_to_products.rb
@@ -22,6 +22,7 @@ module Spree
def call(product_type:)
return success(0) if nothing_to_apply?(product_type)
+ store = product_type.store
changed_product_ids = Set.new
categorized_product_ids = Set.new
@@ -33,7 +34,7 @@ module Spree
categorized_product_ids.merge(result[:categorized_ids])
end
- settle_category_bookkeeping(categorized_product_ids.to_a, product_type.category_ids)
+ settle_category_bookkeeping(store, categorized_product_ids.to_a, product_type.category_ids)
success(changed_product_ids.size)
end
@@ -48,57 +49,69 @@ module Spree
#
# @return [Hash] :changed_ids and :categorized_ids for this batch
def apply_batch(product_type, product_ids)
- changed = add_option_types(product_ids, product_type.option_type_ids)
- categorized = add_categories(product_ids, product_type.category_ids)
+ store = product_type.store
+ # Only ids that belong to this store — a type pointing at another
+ # store's category must not drag it onto a product.
+ category_ids = Spree::Category.for_store(store).where(id: product_type.category_ids).ids
+
+ changed = add_option_types(store, product_ids, product_type.option_type_ids)
+ categorized = add_categories(store, product_ids, category_ids)
{ changed_ids: changed | categorized, categorized_ids: categorized }
end
# insert_all skips the ProductCategory callbacks, so the per-product
# counts they maintain are recomputed here. Safe to call per batch.
- def settle_product_counters(product_ids)
+ def settle_product_counters(store, product_ids)
return if product_ids.empty?
- counts = Spree::ProductCategory.where(product_id: product_ids).group(:product_id).count
+ products = store.products.where(id: product_ids)
+ counts = Spree::ProductCategory.where(product_id: products.select(:id)).group(:product_id).count
counts.each do |product_id, count|
- Spree::Product.where(id: product_id).update_all(categories_count: count)
+ store.products.where(id: product_id).update_all(categories_count: count)
end
- Spree::Product.where(id: product_ids).find_each(&:enqueue_search_index)
+ products.find_each(&:enqueue_search_index)
end
# The category side settles once for the whole run: `products_count` is a
# subtree-wide recount over each category and its ancestors, so repeating
# it per batch would cost more than the backfill.
- def settle_category_counters(category_ids)
+ def settle_category_counters(store, category_ids)
return if category_ids.empty?
- Spree::Category.recalculate_products_count(category_ids)
- Spree::Category.where(id: category_ids).touch_all
+ categories = Spree::Category.for_store(store).where(id: category_ids)
+ Spree::Category.recalculate_products_count(categories.ids)
+ categories.touch_all
end
# Both halves, for callers that run the backfill in one pass.
- def settle_category_bookkeeping(product_ids, category_ids)
- settle_product_counters(product_ids)
- settle_category_counters(category_ids) if product_ids.any?
+ def settle_category_bookkeeping(store, product_ids, category_ids)
+ settle_product_counters(store, product_ids)
+ settle_category_counters(store, category_ids) if product_ids.any?
end
private
# @return [Array] ids of products that gained at least one option type
- def add_option_types(product_ids, option_type_ids)
+ def add_option_types(store, product_ids, option_type_ids)
return [] if option_type_ids.empty?
- existing = Spree::ProductOptionType.where(product_id: product_ids, option_type_id: option_type_ids).
+ # Ids come from the type's own products, but re-scoping through the
+ # store keeps a stale or cross-store id from reaching an insert.
+ scoped_ids = store.products.where(id: product_ids).ids
+ return [] if scoped_ids.empty?
+
+ existing = Spree::ProductOptionType.where(product_id: scoped_ids, option_type_id: option_type_ids).
pluck(:product_id, :option_type_id).to_set
# acts_as_list scopes position per product, so continue each product's
# own sequence rather than restarting at 1.
- highest_positions = Spree::ProductOptionType.where(product_id: product_ids).
+ highest_positions = Spree::ProductOptionType.where(product_id: scoped_ids).
group(:product_id).maximum(:position)
now = Time.current
- rows = product_ids.flat_map do |product_id|
+ rows = scoped_ids.flat_map do |product_id|
position = highest_positions[product_id].to_i
option_type_ids.filter_map do |option_type_id|
@@ -117,10 +130,13 @@ module Spree
end
# @return [Array] ids of products that gained at least one category
- def add_categories(product_ids, category_ids)
+ def add_categories(store, product_ids, category_ids)
return [] if category_ids.empty?
- existing = Spree::ProductCategory.where(product_id: product_ids, category_id: category_ids).
+ scoped_ids = store.products.where(id: product_ids).ids
+ return [] if scoped_ids.empty?
+
+ existing = Spree::ProductCategory.where(product_id: scoped_ids, category_id: category_ids).
pluck(:product_id, :category_id).to_set
# acts_as_list scopes position per category here (unlike option types,
@@ -132,7 +148,7 @@ module Spree
rows = category_ids.flat_map do |category_id|
position = highest_positions[category_id].to_i
- product_ids.filter_map do |product_id|
+ scoped_ids.filter_map do |product_id|
next if existing.include?([product_id, category_id])
position += 1
diff --git a/spree/core/spec/services/spree/product_types/apply_to_products_spec.rb b/spree/core/spec/services/spree/product_types/apply_to_products_spec.rb
index 685d01490a..b368a6b634 100644
--- a/spree/core/spec/services/spree/product_types/apply_to_products_spec.rb
+++ b/spree/core/spec/services/spree/product_types/apply_to_products_spec.rb
@@ -106,4 +106,29 @@ RSpec.describe Spree::ProductTypes::ApplyToProducts do
expect(apply.value).to eq(1)
end
+ describe 'store boundaries' do
+ let(:other_store) { create(:store) }
+
+ it 'does not attach a category belonging to another store' do
+ foreign_category = create(:category, store: other_store)
+ product_type.categories << foreign_category
+ product_type.categories << category
+ product = create(:product)
+ product.update_column(:product_type_id, product_type.id)
+
+ apply
+
+ expect(product.reload.categories).to eq([category])
+ end
+
+ it 'leaves another store\'s products alone even if they carry the type' do
+ product_type.option_types << size
+ foreign_product = create(:product, store: other_store)
+ foreign_product.update_column(:product_type_id, product_type.id)
+
+ apply
+
+ expect(foreign_product.reload.option_types).to be_empty
+ end
+ end
end