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.

Fix: Stopped placeholder prices and products from cascading needless touches (#14844)

Damian Legawiec0681817eb1dc
diff --git a/docs/developer/upgrades/5.6-to-6.0.mdx b/docs/developer/upgrades/5.6-to-6.0.mdx
index d717c63393..de9dc244c5 100644
--- a/docs/developer/upgrades/5.6-to-6.0.mdx
+++ b/docs/developer/upgrades/5.6-to-6.0.mdx
@@ -594,6 +594,10 @@ These had no caller left in Spree, so they are removed with no replacement shim.
| `Spree::Carts::Empty` and `cart_empty_service` | remove items through the Store API cart items endpoints, or delete the cart with `Spree.cart_destroy_service` |
| `Spree::Payments::Create` and `payment_create_service` | the Store API payments endpoint, `Spree::StoreCredits::Apply` or `Spree.gift_card_apply_workflow` |
+### Products no longer touch their categories
+
+Saving or touching a product used to bump the `updated_at` of each of its categories and their parents, through `Spree::Products::TouchCategoriesJob` (formerly `TouchTaxonsJob`). That kept the old Rails storefront's page caches fresh. The Store API doesn't need it: a category's response has no product data, and product responses are cached on the products' own timestamps. Both jobs are removed. If your own code caches product data under a category's `cache_key_with_version`, include the products' timestamps in that key instead. Jobs still queued from before the upgrade fail with an unknown class error and can be discarded.
+
### The Legacy order-routing strategy is gone
`Spree::OrderRouting::Strategy::Legacy` — the pre-5.5 escape hatch that delegated straight to `Spree::Stock::Coordinator` and consulted no routing rules — is removed and no longer registered in `Spree.order_routing.strategies`.
diff --git a/docs/plans/6.0-replace-taxons-with-categories.md b/docs/plans/6.0-replace-taxons-with-categories.md
index 950538b399..c51847f0d0 100644
--- a/docs/plans/6.0-replace-taxons-with-categories.md
+++ b/docs/plans/6.0-replace-taxons-with-categories.md
@@ -604,12 +604,12 @@ Now that Collections exist (Phase 1) and the tables are renamed (Phase 2), renam
- **Stored class-name strings become `'Spree::Category'`** — the metafield/translation resource_type registries move to `'Spree::Category'`, and renamed classes write the new name into polymorphic/STI/slug columns. Existing rows still hold `'Spree::Taxon*'`; they're backfilled by the **Phase 4** data migration (see its step 4). This is a deliberate reversal of the Phase 2 pin-the-old-name approach — 6.0 does NOT keep using `'Spree::Taxon'` in data.
- **De-rule `Spree::Category` at the model level** (the `automatic`/`rules_match_policy`/`sort_order` columns + `spree_taxon_rules` table stay for the Phase 4 migration, dropped in 6.1): remove `products_matching_rules`, the `taxon_rules` association + `accepts_nested_attributes_for` + `rules` alias, the `rules_match_policy`/`sort_order` validations, `scope :automatic`, and `manual_sort_order?`. **`SORT_ORDERS` + `RULES_MATCH_POLICIES` move to `Spree::Collection`** (its rightful owner); `FiltersAggregator` reads `Spree::Collection::SORT_ORDERS`. `scope :manual` + the `manual?`/`automatic?` column readers stay (the manual scope guards the category surfaces). `TaxonRule#belongs_to :taxon` drops its now-dangling `inverse_of: :taxon_rules`.
- **Brand removal (moved here from 6.1):** delete `Product#brand_taxon` / `#brand_name` (+ the `MEMOIZED_METHODS` entry), the `brand` field on the Products Performance report, and the `brand_name` column from the product CSV export — brands are out of scope with no replacement (see Key Decisions → Brands). The `spree_taxonomies` table + `taxonomy_brands_name` locale key still drop in 6.1 with `Taxonomy`; only the brand *methods/fields* go now.
-- **Wording sweep** (Ruby names → category): `Product#main_taxon` → `#primary_category` (deprecated delegate), `touch_taxons` → `touch_categories`, `Spree::Products::TouchTaxonsJob` → `TouchCategoriesJob` + `Spree.queues.taxons` → `categories` (both with deprecation aliases), `taxon_and_ancestors` → `category_and_ancestors`, `remove_taxon` → `remove_category`, the `grouped_taxon_ids_for_products` scope → `grouped_category_ids_for_products`, and `recalculate_products_count`'s param; delete the dead, buggy `Product#taxons_for_store`. Comments/param names across `Category`/`Product` de-taxoned.
+- **Wording sweep** (Ruby names → category): `Product#main_taxon` → `#primary_category` (deprecated delegate), `touch_taxons` → `touch_categories`, `Spree::Products::TouchTaxonsJob` → `TouchCategoriesJob` (both jobs later removed in 6.0 — see the deprecation shims below) + `Spree.queues.taxons` → `categories` (with a deprecation alias), `taxon_and_ancestors` → `category_and_ancestors`, `remove_taxon` → `remove_category`, the `grouped_taxon_ids_for_products` scope → `grouped_category_ids_for_products`, and `recalculate_products_count`'s param; delete the dead, buggy `Product#taxons_for_store`. Comments/param names across `Category`/`Product` de-taxoned.
- **Remove the deprecated taxon-tied `Product` scopes** marked remove-in-6.0: `in_taxons`, `for_filters`, `taxons_name_eq`, plus their now-orphaned private helpers `prepare_taxon_conditions` / `get_taxons`. The active scopes stay (`in_taxon`, `in_category`, `in_categories`, `in_collection`, `ascend_by_taxons_min_position`). Non-taxon deprecated scopes (master-price, option, name, etc.) are left for their own plans.
- **Rename the manual-membership services** `Spree::Taxons::AddProducts` / `RemoveProducts` → `Spree::Categories::AddProducts` / `RemoveProducts` (param `taxons:` → `categories:`, matching `Spree::Collections::*`). All monorepo callers move to the new names, but the old classes are **kept as deprecating aliases** (see "Deprecation aliases" below). The optional `Spree::Taxons::TouchFeaturedSections` hook (guarded by `defined?`, its namespace owned by another gem) is left untouched.
- **Deprecation aliases (kept for one release, removed in 6.1).** Every renamed/moved taxon name keeps working with a runtime deprecation warning:
- **Model constants** (`Spree::Taxon`, `Spree::Classification`, `Spree::PromotionRuleTaxon`, `Spree::Promotion::Rules::Taxon`) stay **true constant aliases** — not `DeprecatedConstantProxy`, which silently breaks `is_a?`/`case`/STI/polymorphic-`constantize`. Each calls `Spree::Deprecation.warn` in the alias's module body, so the notice fires through Spree's deprecator when the file loads (at boot under eager load, on first reference otherwise). Since a plain alias is the same object as the canonical class, a reliable *per-reference* warning isn't achievable without a proxy (breaks `is_a?`) or `const_missing` (breaks `defined?`), so the load-time notice is the deliberate trade-off.
- - **Service/job classes** are resurrected as thin deprecating wrappers that `Spree::Deprecation.warn` on invocation and delegate: `Taxons::AddProducts`/`RemoveProducts` translate `taxons:` → `categories:`; `Products::AutoMatchTaxons` (+ `AutoMatchTaxonsJob`) and `Products::TouchTaxonsJob` delegate to their `*Collections*`/`*Categories*` counterparts; `Taxons::RegenerateProducts` warns and no-ops (categories are manual in 6.0 — its behavior only survives for `Collection`, keyed on a collection rather than a taxon, so there is no faithful delegate).
+ - **Service/job classes** are resurrected as thin deprecating wrappers that `Spree::Deprecation.warn` on invocation and delegate: `Taxons::AddProducts`/`RemoveProducts` translate `taxons:` → `categories:`; `Products::AutoMatchTaxons` (+ `AutoMatchTaxonsJob`) delegate to their `*Collections*` counterparts (`Products::TouchTaxonsJob` was removed outright with `TouchCategoriesJob` in 6.0 — the Store API caches on product timestamps, so products no longer touch their categories); `Taxons::RegenerateProducts` warns and no-ops (categories are manual in 6.0 — its behavior only survives for `Collection`, keyed on a collection rather than a taxon, so there is no faithful delegate).
- **`Spree.queues.taxons`** is restored as a singleton method that warns and returns `Spree.queues.categories`.
- **Method bridges** (`Product#taxons`/`taxon_ids`/`main_taxon`/`classifications`, `Store#taxons`, the join models' `#taxon`, the rule's `#taxons`/`#taxon_ids`/`#eligible_taxon_ids`/`#taxon_ids_to_add`/`#taxon_ids_string`) each `Spree::Deprecation.warn` and delegate. Internal core callers were repointed to the canonical names so the warnings only fire for external code. (`Spree::Taxonomy` and `Spree::Taxons::Find` are themselves legacy/dropped in 6.1 and keep referencing the `Taxon` constant internally.)
@@ -678,7 +678,7 @@ Gated on the channels plan finishing the `available_on` → `ProductPublication#
Taxonomy is **data-only** from 6.0. Nothing creates, touches, or manages a taxonomy at runtime; the model and table survive purely so the 5.6 → 6.0 upgrade task can read existing rows (and so an interrupted upgrade can roll back). What landed:
- **`Imports::CreateCategoriesJob` builds store-owned categories** — a CSV import no longer creates a taxonomy per path root. Every `A -> B -> C` segment is a category, the first one top-level, which matches what the data migration produces.
-- **Taxonomy timestamp touching removed** — `Product#taxonomies` (the `has_many through:`) and the `taxonomy_ids` memoized method are gone; `TouchCategoriesJob` takes category ids only (second argument retained as ignored, so jobs enqueued before the deploy still deserialize).
+- **Taxonomy timestamp touching removed** — `Product#taxonomies` (the `has_many through:`) and the `taxonomy_ids` memoized method are gone; product-to-category touching was later removed altogether, along with `TouchCategoriesJob`.
- **Taxonomy write-back callbacks removed** — `Taxonomy#set_root` / `#set_root_taxon_name` and `Category#sync_taxonomy_name`. Renaming a category no longer rewrites a taxonomy's name.
- **`Category#requires_taxonomy?` and the taxonomy presence validation removed.** The method always returned `false`, so the taxonomy-scoped uniqueness branches were dead code — uniqueness is now unconditionally store-scoped, and `check_for_root` and `parent_belongs_to_same_taxonomy` are gone. `copy_taxonomy_from_parent` **stays**: a category created under an un-migrated parent still inherits its taxonomy so `#ensure_store` and `.for_store` can resolve a store pre-upgrade.
@@ -712,7 +712,7 @@ Taxonomy is **data-only** from 6.0. Nothing creates, touches, or manages a taxon
- Remove `automatic`, `rules_match_policy`, `sort_order` columns from categories (moved to collections), plus the dead `hide_from_nav` column (its Ruby/SDK surface went in 6.0 — see Key Decisions → Category)
- **Drop `spree_taxon_rules` + `Spree::TaxonRule` / `TaxonRules::*` + the `config.spree.taxon_rules` registry** (retained data-only through 6.0 so the Phase 4 data migration can read them; rows were copied to `spree_collection_rules`, and `CollectionRule` is net-new so there's no alias to keep)
- Remove the deprecated Ruby-level aliases kept through 6.0: the `taxons` and `classifications` method aliases on `Product`/`Category`/`Store`, the `taxon`/`taxon=` readers on the join models, and the rule's `taxons`/`taxon_ids`/`eligible_taxon_ids`/`taxon_ids_to_add` aliases
-- Remove the deprecated **constant + service/job/queue aliases** kept through 6.0: `Spree::Taxon`, `Spree::Classification`, `Spree::PromotionRuleTaxon`, `Spree::Promotion::Rules::Taxon` (constant aliases + their load-time `Spree::Deprecation.warn`); the `Spree::Taxons::AddProducts`/`RemoveProducts`/`RegenerateProducts`, `Spree::Products::AutoMatchTaxons`/`AutoMatchTaxonsJob`/`TouchTaxonsJob` wrappers; and the `Spree.queues.taxons` singleton method
+- Remove the deprecated **constant + service/job/queue aliases** kept through 6.0: `Spree::Taxon`, `Spree::Classification`, `Spree::PromotionRuleTaxon`, `Spree::Promotion::Rules::Taxon` (constant aliases + their load-time `Spree::Deprecation.warn`); the `Spree::Taxons::AddProducts`/`RemoveProducts`/`RegenerateProducts`, `Spree::Products::AutoMatchTaxons`/`AutoMatchTaxonsJob` wrappers; and the `Spree.queues.taxons` singleton method
- Rename `spree_prototype_taxons` (with `6.0-product-types.md`), dropping the `foreign_key: :taxon_id` pin on `Category#prototype_taxons` (the `#taxon_rules` pin goes away with the `spree_taxon_rules` drop above)
## Constraints on Current Work
diff --git a/spree/core/app/jobs/spree/products/touch_categories_job.rb b/spree/core/app/jobs/spree/products/touch_categories_job.rb
deleted file mode 100644
index 142755b674..0000000000
--- a/spree/core/app/jobs/spree/products/touch_categories_job.rb
+++ /dev/null
@@ -1,15 +0,0 @@
-module Spree
- module Products
- class TouchCategoriesJob < ::Spree::BaseJob
- queue_as Spree.queues.categories
-
- # @param category_ids [Array<Integer>]
- # @param _legacy_taxonomy_ids [Array<Integer>] ignored; accepted so jobs
- # enqueued before 6.0 stopped touching taxonomies still deserialize.
- # Removed in 6.1.
- def perform(category_ids, _legacy_taxonomy_ids = nil)
- Spree::Category.where(id: category_ids).update_all(updated_at: Time.current)
- end
- end
- end
-end
diff --git a/spree/core/app/jobs/spree/products/touch_taxons_job.rb b/spree/core/app/jobs/spree/products/touch_taxons_job.rb
deleted file mode 100644
index 30c4b22ef9..0000000000
--- a/spree/core/app/jobs/spree/products/touch_taxons_job.rb
+++ /dev/null
@@ -1,13 +0,0 @@
-module Spree
- module Products
- # @deprecated Renamed to Spree::Products::TouchCategoriesJob in 6.0. Subclasses it
- # so jobs enqueued under the old class name before the deploy still deserialize
- # and run; removed in 6.1.
- class TouchTaxonsJob < TouchCategoriesJob
- def perform(*args)
- Spree::Deprecation.warn('Spree::Products::TouchTaxonsJob is deprecated and will be removed in Spree 6.1. Use Spree::Products::TouchCategoriesJob instead.')
- super
- end
- end
- end
-end
diff --git a/spree/core/app/models/spree/price.rb b/spree/core/app/models/spree/price.rb
index 0b04778be7..82018c5438 100644
--- a/spree/core/app/models/spree/price.rb
+++ b/spree/core/app/models/spree/price.rb
@@ -24,7 +24,7 @@ module Spree
# every priced line (docs/plans/6.0-volume-pricing.md).
MAXIMUM_BREAKS_PER_VARIANT = 10
- belongs_to :variant, -> { with_deleted }, class_name: 'Spree::Variant', inverse_of: :prices, touch: true
+ belongs_to :variant, -> { with_deleted }, class_name: 'Spree::Variant', inverse_of: :prices
belongs_to :price_list, class_name: 'Spree::PriceList', optional: true
has_many :price_histories, class_name: 'Spree::PriceHistory', dependent: :delete_all
@@ -32,6 +32,8 @@ module Spree
before_validation :ensure_currency
before_save :remove_compare_at_amount_if_equals_amount
after_save :record_price_history, if: :should_record_price_history?
+ after_save :touch_variant, if: -> { saved_changes? && (amount.present? || amount_before_last_save.present?) }
+ after_destroy :touch_variant, if: -> { amount.present? }
# legacy behavior
validates :amount, allow_nil: true, numericality: {
@@ -342,6 +344,15 @@ module Spree
rungs.sort_by(&:first)
end
+ # Stands in for `touch: true` on the variant, skipping placeholder rows: a
+ # price list holds one without an amount for every variant and currency
+ # nobody has priced yet, and it charges nothing, so writing one changes no
+ # price a shopper sees — yet the touch would expire the product's cached
+ # responses and reindex it for search.
+ def touch_variant
+ variant&.touch_later
+ end
+
def should_record_price_history?
price_list_id.nil? &&
amount.present? &&
diff --git a/spree/core/app/models/spree/price_list.rb b/spree/core/app/models/spree/price_list.rb
index dca9652f8d..645e2a9ddc 100644
--- a/spree/core/app/models/spree/price_list.rb
+++ b/spree/core/app/models/spree/price_list.rb
@@ -333,9 +333,9 @@ module Spree
return if prices_to_insert.empty?
- # Use upsert_all with on_duplicate: :skip to handle race conditions
+ # Use upsert_all with on_duplicate: :skip to handle race conditions.
+ # Placeholders charge nothing, so the variants are not touched.
Spree::Price.upsert_all(prices_to_insert, on_duplicate: :skip)
- touch_variants(variant_ids)
touch
end
@@ -354,10 +354,13 @@ module Spree
variant_ids = Spree::Variant.where(product_id: product_ids).distinct.pluck(:id)
return if variant_ids.empty?
+ removed_prices = prices.where(variant_id: variant_ids)
+ priced_variant_ids = removed_prices.where.not(amount: nil).distinct.pluck(:variant_id)
+
# Use delete_all for hard delete - this bypasses acts_as_paranoid
# which is required for the unique index to work when re-adding products
- prices.where(variant_id: variant_ids).delete_all
- touch_variants(variant_ids)
+ removed_prices.delete_all
+ touch_variants(priced_variant_ids)
touch
end
diff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rb
index 25dda88740..37cb87aa9c 100644
--- a/spree/core/app/models/spree/product.rb
+++ b/spree/core/app/models/spree/product.rb
@@ -40,8 +40,7 @@ module Spree
publishes_lifecycle_events
publishes_events :activated, :approved, :archived, :back_in_stock, :drafted, :out_of_stock, :proposed, :rejected
- MEMOIZED_METHODS = %w[total_on_hand category_and_ancestors
- default_variant_id tax_category default_variant variant_for_images
+ MEMOIZED_METHODS = %w[total_on_hand default_variant_id tax_category default_variant variant_for_images
primary_category buy_box_variants resolved_delivery_profile
purchasable? in_stock? backorderable? digital?]
@@ -243,8 +242,6 @@ module Spree
after_create :set_default_variant
after_save :auto_promote_default_variant
- after_save :run_touch_callbacks, if: :saved_changes?
- after_touch :touch_categories
after_commit :auto_match_collections, if: :eligible_for_collection_matching?
@@ -996,23 +993,6 @@ module Spree
errors.add(:seller, :invalid)
end
- def run_touch_callbacks
- run_callbacks(:touch)
- end
-
- def category_and_ancestors
- @category_and_ancestors ||= categories.map(&:self_and_ancestors).flatten.uniq
- end
-
- # Iterate through this product's categories and touch their timestamps in a batch
- def touch_categories
- if categories.any?
- Spree::Products::TouchCategoriesJob.
- set(wait: 5.seconds).
- perform_later(category_and_ancestors.map(&:id))
- end
- end
-
def ensure_not_in_complete_orders
if orders.complete.any?
errors.add(:base, :cannot_destroy_if_attached_to_line_items)
diff --git a/spree/core/app/services/spree/prices/bulk_upsert.rb b/spree/core/app/services/spree/prices/bulk_upsert.rb
index cd3dde29ba..31ed309458 100644
--- a/spree/core/app/services/spree/prices/bulk_upsert.rb
+++ b/spree/core/app/services/spree/prices/bulk_upsert.rb
@@ -98,12 +98,13 @@ module Spree
upsert_batch(base_rows, BASE_UNIQUE_BY)
end
upsert_batch(override_rows, OVERRIDE_UNIQUE_BY)
- sweep(affected_keys, clear_rows)
+ cleared_variant_ids = sweep(affected_keys, clear_rows)
# `upsert_all` and `delete_all` both skip AR callbacks, so the
# `Price -> Variant -> Product` `touch:` chain never fires —
# downstream caches (`cache_key_with_version`) would stay stale.
- # Re-trigger the chain with one `.touch` per affected variant.
- touch_variants(affected_keys.map { |k| k[0] }.uniq)
+ # A blank row clearing a rung that had no amount changes nothing,
+ # so only priced rows and cleared amounts touch their variant.
+ touch_variants((upsert_rows.map { |r| r[:variant_id].to_s } + cleared_variant_ids).uniq)
end
success(price_count: payload.length)
@@ -392,16 +393,14 @@ module Spree
)
.pluck(:id, :variant_id, :currency, :price_list_id, :min_quantity, :amount)
- doomed_ids = candidates.filter_map do |id, variant_id, currency, price_list_id, min_quantity, amount|
+ doomed = candidates.select do |_, variant_id, currency, price_list_id, min_quantity, amount|
key = [variant_id.to_s, currency, price_list_id&.to_s, min_quantity]
- next unless affected_set.include?(key)
- next id if amount.nil?
- next id if cleared_keys.include?(key)
-
- nil
+ affected_set.include?(key) && (amount.nil? || cleared_keys.include?(key))
end
- Spree::Price.where(id: doomed_ids).delete_all if doomed_ids.any?
+ Spree::Price.where(id: doomed.map(&:first)).delete_all if doomed.any?
+
+ doomed.filter_map { |_, variant_id, *, amount| variant_id.to_s if amount }
end
# Bumps `updated_at` on the affected variants and their parent
diff --git a/spree/core/app/workflows/spree/price_lists/update.rb b/spree/core/app/workflows/spree/price_lists/update.rb
index 2bb096a344..f6386cf25f 100644
--- a/spree/core/app/workflows/spree/price_lists/update.rb
+++ b/spree/core/app/workflows/spree/price_lists/update.rb
@@ -54,6 +54,17 @@ module Spree
rows = price_rows
return if rows.empty?
+ # A blank row only matters when it clears an amount the list held at
+ # that exact rung.
+ cleared_keys = rows.select { |row| row[:amount].blank? }.to_set do |row|
+ [row[:variant_id].to_s, row[:currency], (row[:min_quantity].presence || 1).to_i]
+ end
+ stored_rungs = price_list.prices.where(variant_id: cleared_keys.map(&:first)).where.not(amount: nil)
+ .pluck(:variant_id, :currency, :min_quantity)
+ previously_priced_ids = stored_rungs.filter_map do |variant_id, currency, quantity|
+ variant_id if cleared_keys.include?([variant_id.to_s, currency, quantity])
+ end
+
# The service refuses a batch that would take a ladder past the break
# cap. Ignoring that would answer 200 while writing nothing
# (docs/plans/6.0-volume-pricing.md).
@@ -77,11 +88,12 @@ module Spree
return failure(price_list)
end
- touch_variants(rows.map { |row| row[:variant_id] }.uniq)
+ priced_ids = rows.select { |row| row[:amount].present? }.map { |row| row[:variant_id] }
+ touch_variants((priced_ids + previously_priced_ids).map(&:to_s).uniq)
end
def clear_prices
- variant_ids = price_list.prices.distinct.pluck(:variant_id)
+ variant_ids = price_list.prices.where.not(amount: nil).distinct.pluck(:variant_id)
price_list.prices.update_all(amount: nil, compare_at_amount: nil, updated_at: Time.current)
touch_variants(variant_ids)
end
diff --git a/spree/core/spec/jobs/spree/products/touch_categories_job_spec.rb b/spree/core/spec/jobs/spree/products/touch_categories_job_spec.rb
deleted file mode 100644
index 0f1c4cb8dc..0000000000
--- a/spree/core/spec/jobs/spree/products/touch_categories_job_spec.rb
+++ /dev/null
@@ -1,27 +0,0 @@
-require 'spec_helper'
-
-describe Spree::Products::TouchCategoriesJob, type: :job do
- describe '#perform' do
- subject { described_class.perform_now(category_ids) }
-
- let!(:category_1) { create(:category) }
- let!(:category_2) { create(:category) }
- let!(:other_category) { create(:category) }
-
- let(:category_ids) { [category_1.id, category_2.id] }
-
- it 'touches all specified categories' do
- expect { subject }.to change { Spree::Category.where(id: category_ids).pluck(:updated_at) }
- end
-
- it 'does not touch other categories' do
- expect { subject }.not_to change { other_category.reload.updated_at }
- end
-
- it 'accepts the legacy taxonomy_ids argument from jobs enqueued before 6.0' do
- expect {
- described_class.perform_now(category_ids, [1, 2])
- }.to change { Spree::Category.where(id: category_ids).pluck(:updated_at) }
- end
- end
-end
diff --git a/spree/core/spec/models/spree/price_list_spec.rb b/spree/core/spec/models/spree/price_list_spec.rb
index f7a7f1ba9b..fd843f178b 100644
--- a/spree/core/spec/models/spree/price_list_spec.rb
+++ b/spree/core/spec/models/spree/price_list_spec.rb
@@ -461,12 +461,6 @@ describe Spree::PriceList, type: :model do
}.to change { price_list.prices.count }.by(2) # Only EUR and GBP, not USD
end
- it 'enqueues a job to touch affected variants' do
- expect {
- price_list.add_products([product1.id])
- }.to have_enqueued_job(Spree::Variants::TouchJob).with([product1.default_variant.id])
- end
-
it 'touches the price list to bust cache' do
expect {
price_list.add_products([product1.id])
@@ -597,9 +591,11 @@ describe Spree::PriceList, type: :model do
}.to change { price_list.prices.count }.by(-6) # 2 products * 3 currencies
end
- it 'enqueues a job to touch affected variants' do
+ it 'enqueues a job to touch variants that lose an amount' do
+ price_list.prices.where(variant_id: product1.default_variant.id, currency: 'USD').update_all(amount: 10)
+
expect {
- price_list.remove_products([product1.id])
+ price_list.remove_products([product1.id, product2.id])
}.to have_enqueued_job(Spree::Variants::TouchJob).with([product1.default_variant.id])
end
diff --git a/spree/core/spec/models/spree/price_spec.rb b/spree/core/spec/models/spree/price_spec.rb
index 4e171edbe9..d2907158a6 100644
--- a/spree/core/spec/models/spree/price_spec.rb
+++ b/spree/core/spec/models/spree/price_spec.rb
@@ -23,6 +23,33 @@ describe Spree::Price, type: :model do
end
end
+ describe 'touching the variant' do
+ let(:variant) { create(:variant) }
+ let(:price_list) { create(:price_list) }
+
+ before { variant.update_columns(updated_at: 1.day.ago) }
+
+ it 'touches the variant when a priced row is saved' do
+ expect {
+ create(:price, variant: variant, currency: 'GBP', amount: 10)
+ }.to change { variant.reload.updated_at }
+ end
+
+ it 'touches the variant when an amount is cleared' do
+ price = create(:price, variant: variant, price_list: price_list, currency: 'GBP', amount: 10)
+ variant.update_columns(updated_at: 1.day.ago)
+
+ expect { price.update!(amount: nil) }.to change { variant.reload.updated_at }
+ end
+
+ it 'touches the variant when a priced row is destroyed' do
+ price = create(:price, variant: variant, currency: 'GBP', amount: 10)
+ variant.update_columns(updated_at: 1.day.ago)
+
+ expect { price.destroy }.to change { variant.reload.updated_at }
+ end
+ end
+
describe 'after_commit :auto_match_collections' do
context 'when price is discounted' do
context 'on create' do
diff --git a/spree/core/spec/models/spree/product_spec.rb b/spree/core/spec/models/spree/product_spec.rb
index e1ce94d6dc..00371e338b 100644
--- a/spree/core/spec/models/spree/product_spec.rb
+++ b/spree/core/spec/models/spree/product_spec.rb
@@ -1949,33 +1949,6 @@ describe Spree::Product, type: :model do
end
end
- describe 'after_touch :touch_categories' do
- subject { product.touch }
-
- let!(:product) { create(:product, categories: categories) }
-
- context 'without categories' do
- let(:categories) { [] }
-
- it 'skips enqueuing a job for touching the categories' do
- expect { subject }.not_to have_enqueued_job(Spree::Products::TouchCategoriesJob)
- end
- end
-
- context 'with categories' do
- let(:categories) { [child_category] }
-
- let!(:parent_category) { create(:category) }
- let!(:child_category) { create(:category, parent: parent_category) }
-
- it 'enqueues a job for touching the categories and their ancestors' do
- expect { subject }.to have_enqueued_job(Spree::Products::TouchCategoriesJob).with(
- [parent_category.id, child_category.id]
- )
- end
- end
- end
-
describe 'status' do
it 'has no state machine' do
expect(described_class).not_to respond_to(:state_machines)
diff --git a/spree/core/spec/models/spree/taxon_category_aliases_spec.rb b/spree/core/spec/models/spree/taxon_category_aliases_spec.rb
index e861d0d1c9..4aa3976456 100644
--- a/spree/core/spec/models/spree/taxon_category_aliases_spec.rb
+++ b/spree/core/spec/models/spree/taxon_category_aliases_spec.rb
@@ -69,17 +69,6 @@ RSpec.describe 'Taxon -> Category deprecation aliases' do
end
end
- describe Spree::Products::TouchTaxonsJob do
- it 'is a subclass of the renamed job and warns on perform' do
- expect(described_class.superclass).to eq(Spree::Products::TouchCategoriesJob)
-
- category = create(:category)
- expect(Spree::Deprecation).to receive(:warn).with(/TouchCategoriesJob/)
-
- expect { described_class.new.perform([category.id], []) }.not_to raise_error
- end
- end
-
describe 'Spree.queues.taxons' do
it 'warns and returns the categories queue' do
expect(Spree::Deprecation).to receive(:warn).with(/Spree.queues.categories/)
diff --git a/spree/core/spec/workflows/spree/price_lists/write_spec.rb b/spree/core/spec/workflows/spree/price_lists/write_spec.rb
index 1b95c3f2c6..d31631b04f 100644
--- a/spree/core/spec/workflows/spree/price_lists/write_spec.rb
+++ b/spree/core/spec/workflows/spree/price_lists/write_spec.rb
@@ -50,6 +50,21 @@ RSpec.describe 'Spree::PriceLists write workflows' do
expect(price.amount).to eq(5)
end
+ it 'touches only the variants whose prices change' do
+ priced_variant, blank_variant = create_list(:variant, 2, product: product)
+ price_list.add_products([product.id])
+
+ expect {
+ described_class.call(
+ price_list: price_list,
+ attributes: { prices: [
+ { variant_id: priced_variant.id, currency: 'USD', amount: 5 },
+ { variant_id: blank_variant.id, currency: 'USD', amount: '' }
+ ] }
+ )
+ }.to have_enqueued_job(Spree::Variants::TouchJob).with([priced_variant.id.to_s])
+ end
+
# An empty array means "clear every override", which is a different
# instruction from sending no prices at all.
it 'clears every override when given an empty prices array' do