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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
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.rbindex 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 Spreemodule Webhooksmodule StockItemDecoratordef 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!endprivate- 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_locationendendenddiff --git a/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rb b/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rbindex 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 dodescribe 'emitting variant.backorderable' dolet(: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 == falsestock_item.update(backorderable: false)end- let(:stock_item) { variant.stock_items.first }-context 'when creating a variant stock item' docontext 'when variant has no stock items' dobefore { variant.stock_items.delete_all }@@ -20,6 +19,7 @@ describe Spree::Api::Webhooks::StockItemDecorator docontext 'when variant changes to be backorderable' doit doexpect do+ variant.reloadTimecop.freeze docreate(:stock_item, variant: variant, backorderable: true, count_on_hand: 1)end