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.

Replace around_create/around_update with around_save.

Sebastian Palma24ce9f5e0dc9
diff --git a/api/app/models/spree/api/webhooks/stock_item_decorator.rb b/api/app/models/spree/api/webhooks/stock_item_decorator.rb
index fd3c99f203..a98a2a6b88 100644
--- a/api/app/models/spree/api/webhooks/stock_item_decorator.rb
+++ b/api/app/models/spree/api/webhooks/stock_item_decorator.rb
@@ -3,39 +3,37 @@ module Spree
module Webhooks
module StockItemDecorator
def self.prepended(base)
- base.around_create :queue_webhooks_requests_for_variant_backorderable_on_create!
- base.around_update :queue_webhooks_requests_for_variant_backorderable_on_update!
+ base.around_save :queue_webhooks_requests_for_variant_backorderable!
end
private
- def queue_webhooks_requests_for_variant_backorderable_on_create!
- was_out_of_stock = variant.out_of_stock?
+ def queue_webhooks_requests_for_variant_backorderable!
+ was_out_of_stock = !variant_in_stock?
+ was_not_backorderable = !variant_backorderable?
yield
- reload
- return unless emits_webhook_event_on_create?(variant.reload, was_out_of_stock)
+ touch # changes must be reflected before instantiating the serializer
+ return unless emits_webhook_event?(was_not_backorderable, was_out_of_stock)
- variant.queue_webhooks_requests!('variant.backorderable')
+ variant.queue_webhooks_requests!('variant.backorderable')
end
- def emits_webhook_event_on_create?(variant, was_out_of_stock)
- was_out_of_stock && variant.in_stock? &&
- # rewriting `variant.backorderable?` as is currently being cached
- variant.stock_items.with_active_stock_location.any?(&:backorderable)
+ def emits_webhook_event?(was_not_backorderable, was_out_of_stock)
+ was_out_of_stock && was_not_backorderable && variant_in_stock? && variant_backorderable?
end
- def queue_webhooks_requests_for_variant_backorderable_on_update!
- was_out_of_stock = variant.out_of_stock?
- was_not_orderable = !variant.backorderable?
- yield
- touch # changes must be reflected before instantiating the serializer
- return unless emits_webhook_event_on_update?(variant, was_out_of_stock, was_not_orderable)
+ # rewriting `variant.in_stock?` as is currently being cached
+ def variant_in_stock?
+ stock_items.sum(:count_on_hand) > 0
+ end
- variant.queue_webhooks_requests!('variant.backorderable')
+ # rewriting `variant.backorderable?` as is currently being cached
+ def variant_backorderable?
+ stock_items.any?(&:backorderable)
end
- def emits_webhook_event_on_update?(variant, was_out_of_stock, was_not_orderable)
- was_out_of_stock && was_not_orderable && variant.in_stock? && variant.backorderable?
+ def stock_items
+ variant.stock_items.with_active_stock_location
end
end
end
diff --git a/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rb b/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rb
index dc99286513..d797b6d0d6 100644
--- a/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rb
+++ b/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rb
@@ -4,14 +4,13 @@ describe Spree::Api::Webhooks::StockItemDecorator do
describe 'emitting variant.backorderable' do
let(:body) { Spree::Api::V2::Platform::VariantSerializer.new(variant.reload).serializable_hash.to_json }
let(:variant) { create(:variant) }
+ let(:stock_item) { variant.stock_items.first }
before do
# makes sure variant.backorderable == false
stock_item.update(backorderable: false)
end
- let(:stock_item) { variant.stock_items.first }
-
context 'when creating a variant stock item' do
context 'when variant has no stock items' do
before { variant.stock_items.delete_all }
@@ -20,6 +19,7 @@ describe Spree::Api::Webhooks::StockItemDecorator do
context 'when variant changes to be backorderable' do
it do
expect do
+ variant.reload
Timecop.freeze do
create(:stock_item, variant: variant, backorderable: true, count_on_hand: 1)
end