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.

Update Spree::Product#touch_taxons to enqueue a job for touching the taxons (#12583)

Łukasz Adamczyk7b6124fe54eb
diff --git a/core/app/jobs/spree/products/touch_taxons_job.rb b/core/app/jobs/spree/products/touch_taxons_job.rb
new file mode 100644
index 0000000000..6762daf911
--- /dev/null
+++ b/core/app/jobs/spree/products/touch_taxons_job.rb
@@ -0,0 +1,12 @@
+module Spree
+ module Products
+ class TouchTaxonsJob < ::Spree::BaseJob
+ queue_as Spree.queues.taxons
+
+ def perform(taxon_ids, taxonomy_ids)
+ Spree::Taxon.where(id: taxon_ids).update_all(updated_at: Time.current)
+ Spree::Taxonomy.where(id: taxonomy_ids).update_all(updated_at: Time.current)
+ end
+ end
+ end
+end
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index e656851cf6..213790d21d 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -811,8 +811,11 @@ module Spree
# Iterate through this products taxons and taxonomies and touch their timestamps in a batch
def touch_taxons
- Spree::Taxon.where(id: taxon_and_ancestors.map(&:id)).update_all(updated_at: Time.current)
- Spree::Taxonomy.where(id: taxonomy_ids).update_all(updated_at: Time.current)
+ if taxons.any?
+ Spree::Products::TouchTaxonsJob.
+ set(wait: 5.seconds).
+ perform_later(taxon_and_ancestors.map(&:id), taxonomy_ids.uniq)
+ end
end
def ensure_not_in_complete_orders
diff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rb
index e86998d71d..bdd2764d9d 100644
--- a/core/spec/models/spree/product_spec.rb
+++ b/core/spec/models/spree/product_spec.rb
@@ -1483,4 +1483,34 @@ describe Spree::Product, type: :model do
end
end
end
+
+ describe 'after_touch :touch_taxons' do
+ subject { product.touch }
+
+ let!(:product) { create(:product, taxons: taxons) }
+
+ context 'without taxons' do
+ let(:taxons) { [] }
+
+ it 'skips enqueuing a job for touching the taxons' do
+ expect { subject }.not_to have_enqueued_job(Spree::Products::TouchTaxonsJob)
+ end
+ end
+
+ context 'with taxons' do
+ let(:taxons) { [taxon_1, taxon_2] }
+
+ let!(:taxon_1) { create(:taxon, taxonomy: taxonomy, parent: taxonomy.root) }
+ let!(:taxon_2) { create(:taxon, taxonomy: taxonomy, parent: taxonomy.root) }
+
+ let(:taxonomy) { create(:taxonomy) }
+
+ it 'enqueues a job for touching the taxons' do
+ expect { subject }.to have_enqueued_job(Spree::Products::TouchTaxonsJob).with(
+ [taxonomy.root.id, taxon_1.id, taxon_2.id],
+ [taxonomy.id]
+ )
+ end
+ end
+ end
end