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.

Added Taxon counter cache (#13472)

Damian Legawiec05ab3f17af12
diff --git a/admin/app/views/spree/admin/classifications/create.turbo_stream.erb b/admin/app/views/spree/admin/classifications/create.turbo_stream.erb
index bccd289e8e..829bc008d3 100644
--- a/admin/app/views/spree/admin/classifications/create.turbo_stream.erb
+++ b/admin/app/views/spree/admin/classifications/create.turbo_stream.erb
@@ -1,6 +1,6 @@
<%= turbo_close_dialog %>
-<%= turbo_stream.update :product_count, @taxon.products.count %>
+<%= turbo_stream.update :product_count, @taxon.classification_count %>
<%= turbo_stream.replace :classifications do %>
<%= render template: 'spree/admin/classifications/index' %>
diff --git a/admin/app/views/spree/admin/classifications/destroy.turbo_stream.erb b/admin/app/views/spree/admin/classifications/destroy.turbo_stream.erb
index 9cb2d21a5a..71bab14384 100644
--- a/admin/app/views/spree/admin/classifications/destroy.turbo_stream.erb
+++ b/admin/app/views/spree/admin/classifications/destroy.turbo_stream.erb
@@ -1,2 +1,2 @@
-<%= turbo_stream.update :product_count, @taxon.products.count %>
+<%= turbo_stream.update :product_count, @taxon.classification_count %>
<%= turbo_stream.remove spree_dom_id(@classification) %>
diff --git a/admin/app/views/spree/admin/shared/sortable_tree/_taxonomy.html.erb b/admin/app/views/spree/admin/shared/sortable_tree/_taxonomy.html.erb
index 971a67faf0..edea8b8a5b 100644
--- a/admin/app/views/spree/admin/shared/sortable_tree/_taxonomy.html.erb
+++ b/admin/app/views/spree/admin/shared/sortable_tree/_taxonomy.html.erb
@@ -21,8 +21,8 @@
</div>
<div class="overflow-hidden text-gray-600 text-sm ml-auto">
- <% if taxon.classifications.count > 0 %>
- <%= pluralize(taxon.classifications.count, Spree.t(:product)) %>
+ <% if taxon.classification_count.positive? %>
+ <%= pluralize(taxon.classification_count, Spree.t(:product)) %>
<% end %>
</div>
diff --git a/admin/app/views/spree/admin/taxons/_form.html.erb b/admin/app/views/spree/admin/taxons/_form.html.erb
index d1ddc70fb2..f0c45c5345 100644
--- a/admin/app/views/spree/admin/taxons/_form.html.erb
+++ b/admin/app/views/spree/admin/taxons/_form.html.erb
@@ -49,7 +49,7 @@
<h5 class="card-title">
<%= Spree.t(:products) %>
<span class="badge badge-info ml-2" id="product_count">
- <%= @taxon.products.not_archived.count %>
+ <%= @taxon.classification_count %>
</span>
</h5>
diff --git a/admin/config/initializers/spree_admin_tables.rb b/admin/config/initializers/spree_admin_tables.rb
index cbe7a2daf9..c44a9e54af 100644
--- a/admin/config/initializers/spree_admin_tables.rb
+++ b/admin/config/initializers/spree_admin_tables.rb
@@ -112,7 +112,7 @@ Rails.application.config.after_initialize do
ransack_attribute: 'taxons_id',
operators: %i[in],
search_url: '/admin/taxons/select_options.json',
- method: ->(product) { product.taxons.pluck(:pretty_name).to_sentence }
+ method: ->(product) { product.taxons.pluck(:pretty_name).to_sentence if product.classification_count.positive? }
# Tags - displayed as comma-separated list, filtered via autocomplete
Spree.admin.tables.products.add :tags,
diff --git a/admin/spec/controllers/spree/admin/taxons_controller_spec.rb b/admin/spec/controllers/spree/admin/taxons_controller_spec.rb
index 23da06a67f..5dfde46bcc 100644
--- a/admin/spec/controllers/spree/admin/taxons_controller_spec.rb
+++ b/admin/spec/controllers/spree/admin/taxons_controller_spec.rb
@@ -153,7 +153,7 @@ RSpec.describe Spree::Admin::TaxonsController, type: :controller do
describe 'GET #select_options' do
before do
- Spree::Taxon.destroy_all
+ Spree::Taxon.delete_all
end
context 'with automatic taxons param' do
diff --git a/api/app/serializers/spree/v2/storefront/product_serializer.rb b/api/app/serializers/spree/v2/storefront/product_serializer.rb
index 3c0ab3f625..50bf9362a7 100644
--- a/api/app/serializers/spree/v2/storefront/product_serializer.rb
+++ b/api/app/serializers/spree/v2/storefront/product_serializer.rb
@@ -65,7 +65,7 @@ module Spree
has_many :product_properties, serializer: Spree.api.storefront_product_property_serializer
has_many :taxons, serializer: Spree.api.storefront_taxon_serializer, record_type: :taxon do |object, params|
- object.taxons_for_store(params[:store]).order(:id)
+ object.taxons_for_store(params[:store])&.order(:id)
end
# all images from all variants
diff --git a/api/app/serializers/spree/v2/storefront/taxon_serializer.rb b/api/app/serializers/spree/v2/storefront/taxon_serializer.rb
index a8091a6dd0..2356d9fec0 100644
--- a/api/app/serializers/spree/v2/storefront/taxon_serializer.rb
+++ b/api/app/serializers/spree/v2/storefront/taxon_serializer.rb
@@ -14,7 +14,7 @@ module Spree
end
attribute :has_products do |taxon|
- taxon.active_products_with_descendants.exists?
+ taxon.classification_count.positive? || taxon.active_products_with_descendants.exists?
end
attribute :header_url do |taxon|
diff --git a/api/spec/serializers/spree/api/v2/platform/product_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/product_serializer_spec.rb
index bfa0659665..d0dd6fa8b6 100644
--- a/api/spec/serializers/spree/api/v2/platform/product_serializer_spec.rb
+++ b/api/spec/serializers/spree/api/v2/platform/product_serializer_spec.rb
@@ -50,6 +50,7 @@ describe Spree::Api::V2::Platform::ProductSerializer do
compare_at_price: BigDecimal(15),
display_compare_at_price: '$15.00',
variant_count: product.variant_count,
+ classification_count: product.classification_count,
public_metadata: {},
private_metadata: {}
},
diff --git a/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb
index a8753ea397..545c050282 100644
--- a/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb
+++ b/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb
@@ -40,7 +40,9 @@ describe Spree::Api::V2::Platform::TaxonSerializer do
rules_match_policy: taxon.rules_match_policy,
header_url: nil,
public_metadata: {},
- private_metadata: {}
+ private_metadata: {},
+ children_count: taxon.children_count,
+ classification_count: taxon.classification_count
},
relationships: {
parent: {
@@ -114,7 +116,9 @@ describe Spree::Api::V2::Platform::TaxonSerializer do
rules_match_policy: taxon.rules_match_policy,
header_url: nil,
public_metadata: {},
- private_metadata: {}
+ private_metadata: {},
+ children_count: taxon.children_count,
+ classification_count: taxon.classification_count
},
relationships: {
parent: {
diff --git a/core/app/models/spree/classification.rb b/core/app/models/spree/classification.rb
index c3802ac224..d55c4a64e0 100644
--- a/core/app/models/spree/classification.rb
+++ b/core/app/models/spree/classification.rb
@@ -4,8 +4,8 @@ module Spree
acts_as_list scope: :taxon
with_options inverse_of: :classifications, touch: true do
- belongs_to :product, class_name: 'Spree::Product'
- belongs_to :taxon, class_name: 'Spree::Taxon'
+ belongs_to :product, class_name: 'Spree::Product', counter_cache: :classification_count
+ belongs_to :taxon, class_name: 'Spree::Taxon', counter_cache: :classification_count
end
validates :taxon, :product, presence: true
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index ca104323b4..84a769e2e5 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -40,7 +40,7 @@ module Spree
MEMOIZED_METHODS = %w[total_on_hand taxonomy_ids taxon_and_ancestors category
default_variant_id tax_category default_variant
- default_image secondary_image
+ default_image secondary_image category_taxon brand_taxon main_taxon
purchasable? in_stock? backorderable? digital?]
STATUSES = %w[draft active archived].freeze
@@ -565,17 +565,19 @@ module Spree
# Returns the brand taxon for the product
# @return [Spree::Taxon]
def brand_taxon
- @brand ||= if Spree.use_translations?
- taxons.joins(:taxonomy).
- join_translation_table(Taxonomy).
- find_by(Taxonomy.translation_table_alias => { name: Spree.t(:taxonomy_brands_name) })
- else
- if taxons.loaded?
- taxons.find { |taxon| taxon.taxonomy.name == Spree.t(:taxonomy_brands_name) }
- else
- taxons.joins(:taxonomy).find_by(Taxonomy.table_name => { name: Spree.t(:taxonomy_brands_name) })
- end
- end
+ @brand_taxon ||= if classification_count.zero?
+ nil
+ elsif Spree.use_translations?
+ taxons.joins(:taxonomy).
+ join_translation_table(Taxonomy).
+ find_by(Taxonomy.translation_table_alias => { name: Spree.t(:taxonomy_brands_name) })
+ else
+ if taxons.loaded?
+ taxons.find { |taxon| taxon.taxonomy.name == Spree.t(:taxonomy_brands_name) }
+ else
+ taxons.joins(:taxonomy).find_by(Taxonomy.table_name => { name: Spree.t(:taxonomy_brands_name) })
+ end
+ end
end
# Returns the brand name for the product
@@ -600,25 +602,31 @@ module Spree
# Returns the category taxon for the product
# @return [Spree::Taxon]
def category_taxon
- @category ||= if Spree.use_translations?
- taxons.joins(:taxonomy).
- join_translation_table(Taxonomy).
- order(depth: :desc).
- find_by(Taxonomy.translation_table_alias => { name: Spree.t(:taxonomy_categories_name) })
- else
- if taxons.loaded?
- taxons.find { |taxon| taxon.taxonomy.name == Spree.t(:taxonomy_categories_name) }
- else
- taxons.joins(:taxonomy).order(depth: :desc).find_by(Taxonomy.table_name => { name: Spree.t(:taxonomy_categories_name) })
- end
- end
+ @category_taxon ||= if classification_count.zero?
+ nil
+ elsif Spree.use_translations?
+ taxons.joins(:taxonomy).
+ join_translation_table(Taxonomy).
+ order(depth: :desc).
+ find_by(Taxonomy.translation_table_alias => { name: Spree.t(:taxonomy_categories_name) })
+ else
+ if taxons.loaded?
+ taxons.find { |taxon| taxon.taxonomy.name == Spree.t(:taxonomy_categories_name) }
+ else
+ taxons.joins(:taxonomy).order(depth: :desc).find_by(Taxonomy.table_name => { name: Spree.t(:taxonomy_categories_name) })
+ end
+ end
end
def main_taxon
- category_taxon || taxons.first
+ return if classification_count.zero?
+
+ @main_taxon ||= category_taxon || taxons.first
end
def taxons_for_store(store)
+ return if classification_count.zero?
+
Rails.cache.fetch("#{cache_key_with_version}/taxons-per-store/#{store.id}") do
taxons.for_store(store)
end
diff --git a/core/app/models/spree/taxon.rb b/core/app/models/spree/taxon.rb
index 1c652c963a..ae0fa3df92 100644
--- a/core/app/models/spree/taxon.rb
+++ b/core/app/models/spree/taxon.rb
@@ -27,7 +27,7 @@ module Spree
#
extend FriendlyId
friendly_id :permalink, slug_column: :permalink, use: :history
- acts_as_nested_set dependent: :destroy
+ acts_as_nested_set dependent: :destroy, counter_cache: :children_count
#
# Associations
@@ -354,6 +354,7 @@ module Spree
end
def active_products
+ Spree::Deprecation.warn('active_products is deprecated and will be removed in Spree 5.5. Please use taxon.products.active instead.')
products.active
end
diff --git a/core/app/services/spree/taxons/add_products.rb b/core/app/services/spree/taxons/add_products.rb
index 5ca67b6aa2..46e50fc6b8 100644
--- a/core/app/services/spree/taxons/add_products.rb
+++ b/core/app/services/spree/taxons/add_products.rb
@@ -28,10 +28,14 @@ module Spree
# doing a quick insert_all here to avoid the overhead of instantiating
Spree::Classification.insert_all(classifications_params)
- # clearing cache
- Spree::Product.where(id: products.pluck(:id)).touch_all
-
+ # update counter caches
taxon_ids = taxons.pluck(:id)
+ product_ids = products.pluck(:id)
+ taxon_ids.each { |id| Spree::Taxon.reset_counters(id, :classifications) }
+ product_ids.each { |id| Spree::Product.reset_counters(id, :classifications) }
+
+ # clearing cache
+ Spree::Product.where(id: product_ids).touch_all
Spree::Taxon.where(id: taxon_ids).touch_all
Spree::Taxons::TouchFeaturedSections.call(taxon_ids: taxon_ids) if defined?(Spree::Taxons::TouchFeaturedSections)
diff --git a/core/app/services/spree/taxons/regenerate_products.rb b/core/app/services/spree/taxons/regenerate_products.rb
index ebc995de42..a5598ecae1 100644
--- a/core/app/services/spree/taxons/regenerate_products.rb
+++ b/core/app/services/spree/taxons/regenerate_products.rb
@@ -5,6 +5,7 @@ module Spree
def call(taxon:)
return if taxon.nil?
+ return if taxon.destroyed?
return if taxon.manual?
previous_products_ids = taxon.classifications.order(position: :asc).pluck(:product_id)
@@ -34,6 +35,13 @@ module Spree
Spree::Product.where(id: (previous_products_ids + product_ids_to_insert).uniq).touch_all
end
+ # update counter caches
+ # Check if taxon still exists (may have been destroyed)
+ Spree::Taxon.reset_counters(taxon.id, :classifications)
+ 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, :classifications) }
+
success(taxon)
end
diff --git a/core/app/services/spree/taxons/remove_products.rb b/core/app/services/spree/taxons/remove_products.rb
index 8a05c0f477..0589cb8988 100644
--- a/core/app/services/spree/taxons/remove_products.rb
+++ b/core/app/services/spree/taxons/remove_products.rb
@@ -11,12 +11,15 @@ module Spree
def call(taxons:, products:)
return if taxons.blank? || products.blank?
+ taxon_ids = taxons.pluck(:id)
+ product_ids = products.pluck(:id)
+
ApplicationRecord.transaction do
- taxons.pluck(:id).each do |taxon_id|
- Spree::Classification.where(taxon_id: taxon_id, product_id: products.pluck(:id)).delete_all
+ taxon_ids.each do |taxon_id|
+ Spree::Classification.where(taxon_id: taxon_id, product_id: product_ids).delete_all
end
- classifications_params = taxons.pluck(:id).flat_map do |taxon_id|
+ classifications_params = taxon_ids.flat_map do |taxon_id|
position = 0
existing_product_ids = Spree::Classification.where(taxon_id: taxon_id).pluck(:product_id)
@@ -42,10 +45,12 @@ module Spree
end
end
- # clear cache
- Spree::Product.where(id: products.pluck(:id)).touch_all
+ # update counter caches
+ taxon_ids.each { |id| Spree::Taxon.reset_counters(id, :classifications) }
+ product_ids.each { |id| Spree::Product.reset_counters(id, :classifications) }
- taxon_ids = taxons.pluck(:id)
+ # clear cache
+ Spree::Product.where(id: product_ids).touch_all
Spree::Taxon.where(id: taxon_ids).touch_all
Spree::Taxons::TouchFeaturedSections.call(taxon_ids: taxon_ids) if defined?(Spree::Taxons::TouchFeaturedSections)
diff --git a/core/db/migrate/20260119120000_add_counter_caches_to_spree_products.rb b/core/db/migrate/20260119120000_add_counter_caches_to_spree_products.rb
new file mode 100644
index 0000000000..12d5372baa
--- /dev/null
+++ b/core/db/migrate/20260119120000_add_counter_caches_to_spree_products.rb
@@ -0,0 +1,9 @@
+class AddCounterCachesToSpreeProducts < ActiveRecord::Migration[7.2]
+ def change
+ add_column :spree_products, :variant_count, :integer, default: 0, null: false, if_not_exists: true
+ add_column :spree_products, :classification_count, :integer, default: 0, null: false, if_not_exists: true
+
+ add_index :spree_products, :variant_count, if_not_exists: true
+ add_index :spree_products, :classification_count, if_not_exists: true
+ end
+end
diff --git a/core/db/migrate/20260119120000_add_variant_count_to_spree_products.rb b/core/db/migrate/20260119120000_add_variant_count_to_spree_products.rb
deleted file mode 100644
index 1454df9b66..0000000000
--- a/core/db/migrate/20260119120000_add_variant_count_to_spree_products.rb
+++ /dev/null
@@ -1,6 +0,0 @@
-class AddVariantCountToSpreeProducts < ActiveRecord::Migration[7.2]
- def change
- add_column :spree_products, :variant_count, :integer, default: 0, null: false, if_not_exists: true
- add_index :spree_products, :variant_count, if_not_exists: true
- end
-end
diff --git a/core/db/migrate/20260119170000_add_counter_caches_to_spree_taxons.rb b/core/db/migrate/20260119170000_add_counter_caches_to_spree_taxons.rb
new file mode 100644
index 0000000000..56072348d2
--- /dev/null
+++ b/core/db/migrate/20260119170000_add_counter_caches_to_spree_taxons.rb
@@ -0,0 +1,9 @@
+class AddCounterCachesToSpreeTaxons < ActiveRecord::Migration[7.2]
+ def change
+ add_column :spree_taxons, :children_count, :integer, default: 0, null: false, if_not_exists: true
+ add_column :spree_taxons, :classification_count, :integer, default: 0, null: false, if_not_exists: true
+
+ add_index :spree_taxons, :children_count, if_not_exists: true
+ add_index :spree_taxons, :classification_count, if_not_exists: true
+ end
+end
diff --git a/core/lib/tasks/products.rake b/core/lib/tasks/products.rake
index 9fa109272c..bfea069cee 100644
--- a/core/lib/tasks/products.rake
+++ b/core/lib/tasks/products.rake
@@ -9,6 +9,15 @@ namespace :spree do
puts 'Done!'
end
+ desc 'Reset classification_count counter cache on products'
+ task reset_classification_count: :environment do |_t, _args|
+ puts 'Resetting classification_count counter cache...'
+ Spree::Product.in_batches.update_all(
+ "classification_count = (SELECT COUNT(*) FROM spree_products_taxons WHERE spree_products_taxons.product_id = spree_products.id)"
+ )
+ puts 'Done!'
+ end
+
desc 'Enqueue background jobs to populate product metrics for all store products'
task populate: :environment do
total_count = 0
diff --git a/core/lib/tasks/taxons.rake b/core/lib/tasks/taxons.rake
new file mode 100644
index 0000000000..7cd53bd6a8
--- /dev/null
+++ b/core/lib/tasks/taxons.rake
@@ -0,0 +1,21 @@
+namespace :spree do
+ namespace :taxons do
+ desc 'Reset children_count counter cache on taxons'
+ task reset_children_count: :environment do |_t, _args|
+ puts 'Resetting children_count counter cache...'
+ Spree::Taxon.in_batches.update_all(
+ "children_count = (SELECT COUNT(*) FROM spree_taxons AS children WHERE children.parent_id = spree_taxons.id)"
+ )
+ puts 'Done!'
+ end
+
+ desc 'Reset classification_count counter cache on taxons'
+ task reset_classification_count: :environment do |_t, _args|
+ puts 'Resetting classification_count counter cache...'
+ Spree::Taxon.in_batches.update_all(
+ "classification_count = (SELECT COUNT(*) FROM spree_products_taxons WHERE spree_products_taxons.taxon_id = spree_taxons.id)"
+ )
+ puts 'Done!'
+ end
+ end
+end
diff --git a/core/spec/models/spree/classification_spec.rb b/core/spec/models/spree/classification_spec.rb
index 607283917b..f196891744 100644
--- a/core/spec/models/spree/classification_spec.rb
+++ b/core/spec/models/spree/classification_spec.rb
@@ -91,6 +91,53 @@ module Spree
end
end
+ describe 'counter cache' do
+ let(:taxon) { create(:taxon) }
+ let(:product) { create(:product) }
+
+ describe 'classification_count on taxon' do
+ it 'increments when a classification is created' do
+ expect {
+ create(:classification, taxon: taxon, product: product)
+ }.to change { taxon.reload.classification_count }.from(0).to(1)
+ end
+
+ it 'decrements when a classification is destroyed' do
+ classification = create(:classification, taxon: taxon, product: product)
+ expect {
+ classification.destroy
+ }.to change { taxon.reload.classification_count }.from(1).to(0)
+ end
+
+ it 'correctly counts multiple classifications' do
+ products = create_list(:product, 3)
+ products.each { |p| create(:classification, taxon: taxon, product: p) }
+ expect(taxon.reload.classification_count).to eq(3)
+ end
+ end
+
+ describe 'classification_count on product' do
+ it 'increments when a classification is created' do
+ expect {
+ create(:classification, taxon: taxon, product: product)
+ }.to change { product.reload.classification_count }.from(0).to(1)
+ end
+
+ it 'decrements when a classification is destroyed' do
+ classification = create(:classification, taxon: taxon, product: product)
+ expect {
+ classification.destroy
+ }.to change { product.reload.classification_count }.from(1).to(0)
+ end
+
+ it 'correctly counts multiple classifications' do
+ taxons = create_list(:taxon, 3)
+ taxons.each { |t| create(:classification, taxon: t, product: product) }
+ expect(product.reload.classification_count).to eq(3)
+ end
+ end
+ end
+
describe '.grouped_taxon_ids_for_products' do
let(:taxon1) { create(:taxon) }
let(:taxon2) { create(:taxon) }
diff --git a/core/spec/services/spree/taxons/add_products_spec.rb b/core/spec/services/spree/taxons/add_products_spec.rb
index bf9b267953..e0677223f9 100644
--- a/core/spec/services/spree/taxons/add_products_spec.rb
+++ b/core/spec/services/spree/taxons/add_products_spec.rb
@@ -32,6 +32,16 @@ module Spree
expect(subject.success?).to be true
expect(subject.value).to eq true
end
+
+ it 'updates classification_count on taxons' do
+ expect { subject }.to change { taxons.first.reload.classification_count }.from(0).to(products.size)
+ .and change { taxons.second.reload.classification_count }.from(0).to(products.size)
+ end
+
+ it 'updates classification_count on products' do
+ expect { subject }.to change { products.first.reload.classification_count }.from(0).to(taxons.size)
+ .and change { products.second.reload.classification_count }.from(0).to(taxons.size)
+ end
end
end
end
diff --git a/core/spec/services/spree/taxons/regenerate_products_spec.rb b/core/spec/services/spree/taxons/regenerate_products_spec.rb
index 1de5a0e89b..ca7ba94c05 100644
--- a/core/spec/services/spree/taxons/regenerate_products_spec.rb
+++ b/core/spec/services/spree/taxons/regenerate_products_spec.rb
@@ -25,6 +25,15 @@ RSpec.describe Spree::Taxons::RegenerateProducts do
expect { regenerate_products }.to change(taxon.products, :count).from(2).to(1)
expect(taxon.reload.products).to contain_exactly(other_tagged_product)
end
+
+ it 'updates classification_count on taxon' do
+ expect { regenerate_products }.to change { taxon.reload.classification_count }.from(2).to(1)
+ end
+
+ it 'updates classification_count on products' do
+ removed_product = tagged_products.first
+ expect { regenerate_products }.to change { removed_product.reload.classification_count }.from(1).to(0)
+ end
end
context 'when nothing changed' do
diff --git a/core/spec/services/spree/taxons/remove_products_spec.rb b/core/spec/services/spree/taxons/remove_products_spec.rb
index a51f393a74..a95b20a7a7 100644
--- a/core/spec/services/spree/taxons/remove_products_spec.rb
+++ b/core/spec/services/spree/taxons/remove_products_spec.rb
@@ -40,6 +40,16 @@ module Spree
expect(subject.success?).to be true
expect(subject.value).to eq true
end
+
+ it 'updates classification_count on taxons' do
+ expect { subject }.to change { taxons.first.reload.classification_count }.from(products.size).to(0)
+ .and change { taxons.second.reload.classification_count }.from(products.size).to(0)
+ end
+
+ it 'updates classification_count on products' do
+ expect { subject }.to change { products.first.reload.classification_count }.from(taxons.size).to(0)
+ .and change { products.second.reload.classification_count }.from(taxons.size).to(0)
+ end
end
end
end
diff --git a/storefront/spec/features/featured_taxon_spec.rb b/storefront/spec/features/featured_taxon_spec.rb
index 3028056da8..32defd05af 100644
--- a/storefront/spec/features/featured_taxon_spec.rb
+++ b/storefront/spec/features/featured_taxon_spec.rb
@@ -8,7 +8,7 @@ RSpec.describe 'Featured taxon', type: :feature do
let(:section) { Spree::PageSections::FeaturedTaxon.create(preferred_taxon_id: taxon&.id, pageable: Spree::Page.find_by(name: 'Homepage')) }
before do
- store.taxons.automatic.destroy_all
+ store.taxons.automatic.delete_all
taxon.products << products if taxon.present?
visit spree.page_section_path(section.id)
end