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.

Fixed: making sure we're reindexing products after using bulk operati… (#13845)

Damian Legawiec071522a5326f
diff --git a/spree/admin/app/controllers/spree/admin/products_controller.rb b/spree/admin/app/controllers/spree/admin/products_controller.rb
index ca3ee1044a..e340218aef 100644
--- a/spree/admin/app/controllers/spree/admin/products_controller.rb
+++ b/spree/admin/app/controllers/spree/admin/products_controller.rb
@@ -95,6 +95,7 @@ module Spree
def bulk_status_update
bulk_collection.update_all(status: params[:status], updated_at: Time.current)
+ bulk_collection.each(&:enqueue_search_index) # reindex products
invoke_callbacks(:bulk_status_update, :after)
handle_bulk_operation_response
@@ -238,6 +239,7 @@ module Spree
def after_bulk_tags_change
Spree::Product.bulk_auto_match_taxons(current_store, bulk_collection.ids)
+ bulk_collection.each(&:enqueue_search_index) # reindex products
end
def variant_stock_includes
diff --git a/spree/admin/spec/controllers/spree/admin/products_controller_spec.rb b/spree/admin/spec/controllers/spree/admin/products_controller_spec.rb
index 7e6425efa8..f7b3170f64 100644
--- a/spree/admin/spec/controllers/spree/admin/products_controller_spec.rb
+++ b/spree/admin/spec/controllers/spree/admin/products_controller_spec.rb
@@ -1200,9 +1200,9 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
end
describe 'POST #bulk_status_update' do
- let(:product) { create(:product, stores: [store], status: status) }
+ let(:products) { create_list(:product, 3, stores: [store], status: status) }
let(:status) { :draft }
- let(:send_request) { put :bulk_status_update, params: { ids: [product.id], status: 'active' } }
+ let(:send_request) { put :bulk_status_update, params: { ids: products.pluck(:id), status: 'active' } }
before { request.env['HTTP_REFERER'] = '/admin/products' }
@@ -1210,9 +1210,9 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
let(:status) { status }
it 'updates status to active' do
- expect(product.status).to eq status.to_s
+ expect(products.first.status).to eq status.to_s
send_request
- expect(product.reload.active?).to be(true)
+ expect(products.first.reload.active?).to be(true)
expect(response).to redirect_to('/admin/products')
end
end
@@ -1222,40 +1222,53 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
it_behaves_like 'updates status to active', status
end
end
+
+ it "reindexes products" do
+ allow_any_instance_of(Spree::Product).to receive(:search_indexing_enabled?).and_return(true)
+ expect { send_request }.to have_enqueued_job(Spree::SearchProvider::IndexJob).exactly(products.size).times
+ end
end
describe 'PUT #bulk_add_tags' do
- let(:product) { create(:product, stores: [store], status: :active) }
- let(:product2) { create(:product, stores: [store], status: :active) }
+ let(:products) { create_list(:product, 2, stores: [store], status: :active) }
let(:send_request) do
- put :bulk_add_tags, params: { ids: [product.id, product2.id], tags: ['tag1', 'tag2', 'tag3'] }
+ put :bulk_add_tags, params: { ids: products.pluck(:id), tags: ['tag1', 'tag2', 'tag3'] }
end
before { request.env['HTTP_REFERER'] = '/admin/products' }
it 'adds tags to products' do
send_request
- expect(product.reload.tag_list).to match_array(['tag1', 'tag2', 'tag3'])
- expect(product2.reload.tag_list).to match_array(['tag1', 'tag2', 'tag3'])
+ expect(products.first.reload.tag_list).to match_array(['tag1', 'tag2', 'tag3'])
+ expect(products.last.reload.tag_list).to match_array(['tag1', 'tag2', 'tag3'])
expect(response).to redirect_to('/admin/products')
end
+
+ it "reindexes products" do
+ allow_any_instance_of(Spree::Product).to receive(:search_indexing_enabled?).and_return(true)
+ expect { send_request }.to have_enqueued_job(Spree::SearchProvider::IndexJob).exactly(products.size).times
+ end
end
describe 'PUT #bulk_remove_tags' do
- let(:product) { create(:product, stores: [store], status: :active, tag_list: ['tag1', 'tag2', 'tag3']) }
- let(:product2) { create(:product, stores: [store], status: :active, tag_list: ['tag1', 'tag2', 'tag3']) }
+ let(:products) { create_list(:product, 2, stores: [store], status: :active, tag_list: ['tag1', 'tag2', 'tag3']) }
let(:send_request) do
- put :bulk_remove_tags, params: { ids: [product.id, product2.id], tags: ['tag1', 'tag2', 'tag3'] }
+ put :bulk_remove_tags, params: { ids: products.pluck(:id), tags: ['tag1', 'tag2', 'tag3'] }
end
before { request.env['HTTP_REFERER'] = '/admin/products' }
it 'removes tags from products' do
send_request
- expect(product.reload.tag_list).to eq([])
- expect(product2.reload.tag_list).to eq([])
+ expect(products.first.reload.tag_list).to eq([])
+ expect(products.last.reload.tag_list).to eq([])
expect(response).to redirect_to('/admin/products')
end
+
+ it "reindexes products" do
+ allow_any_instance_of(Spree::Product).to receive(:search_indexing_enabled?).and_return(true)
+ expect { send_request }.to have_enqueued_job(Spree::SearchProvider::IndexJob).exactly(products.size).times
+ end
end
describe 'PUR #bulk_remove_from_taxons' do
diff --git a/spree/core/app/models/concerns/spree/search_indexable.rb b/spree/core/app/models/concerns/spree/search_indexable.rb
index 3912803d62..2e8a30fc04 100644
--- a/spree/core/app/models/concerns/spree/search_indexable.rb
+++ b/spree/core/app/models/concerns/spree/search_indexable.rb
@@ -45,8 +45,6 @@ module Spree
end
end
- private
-
def enqueue_search_index
return unless search_indexing_enabled?
diff --git a/spree/core/app/services/spree/taxons/add_products.rb b/spree/core/app/services/spree/taxons/add_products.rb
index 46e50fc6b8..88aa4f5a1c 100644
--- a/spree/core/app/services/spree/taxons/add_products.rb
+++ b/spree/core/app/services/spree/taxons/add_products.rb
@@ -34,8 +34,10 @@ module Spree
taxon_ids.each { |id| Spree::Taxon.reset_counters(id, :classifications) }
product_ids.each { |id| Spree::Product.reset_counters(id, :classifications) }
- # clearing cache
+ # clear cache & index products
Spree::Product.where(id: product_ids).touch_all
+ products.each(&:enqueue_search_index)
+
Spree::Taxon.where(id: taxon_ids).touch_all
Spree::Taxons::TouchFeaturedSections.call(taxon_ids: taxon_ids) if defined?(Spree::Taxons::TouchFeaturedSections)
diff --git a/spree/core/app/services/spree/taxons/regenerate_products.rb b/spree/core/app/services/spree/taxons/regenerate_products.rb
index a5598ecae1..98bb6e43f9 100644
--- a/spree/core/app/services/spree/taxons/regenerate_products.rb
+++ b/spree/core/app/services/spree/taxons/regenerate_products.rb
@@ -32,7 +32,9 @@ module Spree
Spree::Classification.insert_all(records_to_insert)
# expire product cache
- Spree::Product.where(id: (previous_products_ids + product_ids_to_insert).uniq).touch_all
+ products = Spree::Product.where(id: (previous_products_ids + product_ids_to_insert).uniq)
+ products.touch_all
+ products.each(&:enqueue_search_index)
end
# update counter caches
diff --git a/spree/core/app/services/spree/taxons/remove_products.rb b/spree/core/app/services/spree/taxons/remove_products.rb
index 0589cb8988..4198e5342a 100644
--- a/spree/core/app/services/spree/taxons/remove_products.rb
+++ b/spree/core/app/services/spree/taxons/remove_products.rb
@@ -49,8 +49,10 @@ module Spree
taxon_ids.each { |id| Spree::Taxon.reset_counters(id, :classifications) }
product_ids.each { |id| Spree::Product.reset_counters(id, :classifications) }
- # clear cache
+ # clear cache & index products
Spree::Product.where(id: product_ids).touch_all
+ products.each(&:enqueue_search_index)
+
Spree::Taxon.where(id: taxon_ids).touch_all
Spree::Taxons::TouchFeaturedSections.call(taxon_ids: taxon_ids) if defined?(Spree::Taxons::TouchFeaturedSections)
diff --git a/spree/core/spec/services/spree/taxons/add_products_spec.rb b/spree/core/spec/services/spree/taxons/add_products_spec.rb
index e0677223f9..d1a0c55e32 100644
--- a/spree/core/spec/services/spree/taxons/add_products_spec.rb
+++ b/spree/core/spec/services/spree/taxons/add_products_spec.rb
@@ -24,6 +24,11 @@ module Spree
expect { subject }.to change { Spree::Product.where(id: products.pluck(:id)).pluck(:updated_at) }
end
+ it "reindexes products" do
+ allow_any_instance_of(Spree::Product).to receive(:search_indexing_enabled?).and_return(true)
+ expect { subject }.to have_enqueued_job(Spree::SearchProvider::IndexJob).exactly(products.size).times
+ end
+
it 'touches all taxons' do
expect { subject }.to change { Spree::Taxon.where(id: taxons.pluck(:id)).pluck(:updated_at) }
end
diff --git a/spree/core/spec/services/spree/taxons/remove_products_spec.rb b/spree/core/spec/services/spree/taxons/remove_products_spec.rb
index a95b20a7a7..ce6c780147 100644
--- a/spree/core/spec/services/spree/taxons/remove_products_spec.rb
+++ b/spree/core/spec/services/spree/taxons/remove_products_spec.rb
@@ -36,6 +36,11 @@ module Spree
expect { subject }.to change { Spree::Taxon.where(id: taxons.pluck(:id)).pluck(:updated_at) }
end
+ it "reindexes products" do
+ allow_any_instance_of(Spree::Product).to receive(:search_indexing_enabled?).and_return(true)
+ expect { subject }.to have_enqueued_job(Spree::SearchProvider::IndexJob).exactly(products.size).times
+ end
+
it 'returns a successful result' do
expect(subject.success?).to be true
expect(subject.value).to eq true