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
Use `RequestStore` to store whether webhooks are disabled (#12083)
Damian Kaczmarczyk32b3737848b1
diff --git a/api/app/models/concerns/spree/webhooks/has_webhooks.rb b/api/app/models/concerns/spree/webhooks/has_webhooks.rbindex f76ed37a7b..4502d86193 100644--- a/api/app/models/concerns/spree/webhooks/has_webhooks.rb+++ b/api/app/models/concerns/spree/webhooks/has_webhooks.rb@@ -74,7 +74,7 @@ module Spreeenddef disable_spree_webhooks?- ENV['DISABLE_SPREE_WEBHOOKS'] == 'true'+ Spree::Webhooks.disabled?enddef webhooks_request_optionsdiff --git a/api/lib/spree/api/testing_support/matchers/webhooks.rb b/api/lib/spree/api/testing_support/matchers/webhooks.rbindex b1ddb266ac..e69efcb44b 100644--- a/api/lib/spree/api/testing_support/matchers/webhooks.rb+++ b/api/lib/spree/api/testing_support/matchers/webhooks.rb@@ -61,7 +61,7 @@ RSpec::Matchers.define :emit_webhook_event do |event_to_emit|enddef with_webhooks_enabled- ENV['DISABLE_SPREE_WEBHOOKS'] = nil+ Spree::Webhooks.disabled = falseyield- ENV['DISABLE_SPREE_WEBHOOKS'] = 'true'+ Spree::Webhooks.disabled = trueenddiff --git a/api/lib/spree/api/testing_support/spree_webhooks.rb b/api/lib/spree/api/testing_support/spree_webhooks.rbindex 27ce53020f..5cda49ca8c 100644--- a/api/lib/spree/api/testing_support/spree_webhooks.rb+++ b/api/lib/spree/api/testing_support/spree_webhooks.rb@@ -1,9 +1,9 @@RSpec.configure do |config|config.before(:each, :spree_webhooks) do- ENV['DISABLE_SPREE_WEBHOOKS'] = nil+ Spree::Webhooks.disabled = falseendconfig.after(:each, :spree_webhooks) do- ENV['DISABLE_SPREE_WEBHOOKS'] = 'true'+ Spree::Webhooks.disabled = trueendenddiff --git a/api/spec/services/spree/webhooks_spec.rb b/api/spec/services/spree/webhooks_spec.rbindex 3a9f4be8d6..2d675d7542 100644--- a/api/spec/services/spree/webhooks_spec.rb+++ b/api/spec/services/spree/webhooks_spec.rb@@ -18,15 +18,15 @@ describe Spree::Webhooks doallow(queue_requests).to receive(:call).with(any_args)end- after { ENV['DISABLE_SPREE_WEBHOOKS'] = 'true' }+ after { Spree::Webhooks.disabled = true }- describe 'the value of DISABLE_SPREE_WEBHOOKS environment variable' do- before { ENV['DISABLE_SPREE_WEBHOOKS'] = 'some_value' }+ describe 'the value of spree_webhooks_disabled request variable' do+ before { RequestStore.store[:disable_spree_webhooks] = false }describe 'when an error is not raised' doit 'sets it to the original value' dodescribed_class.disable_webhooks { variant.discontinue! }- expect(ENV['DISABLE_SPREE_WEBHOOKS']).to eq('some_value')+ expect(RequestStore.store[:disable_spree_webhooks]).to eq(false)endend@@ -36,13 +36,13 @@ describe Spree::Webhooks dodescribed_class.disable_webhooks { raise StandardError }rescue StandardErrorend- expect(ENV['DISABLE_SPREE_WEBHOOKS']).to eq('some_value')+ expect(RequestStore.store[:disable_spree_webhooks]).to eq(false)endendenddescribe 'when webhooks are already disabled' do- before { ENV['DISABLE_SPREE_WEBHOOKS'] = 'true' }+ before { Spree::Webhooks.disabled = true }it 'does not emit the event' dodescribed_class.disable_webhooks { variant.discontinue! }@@ -51,6 +51,8 @@ describe Spree::Webhooks doenddescribe 'when webhooks are enabled' do+ before { Spree::Webhooks.disabled = false }+describe 'when not using #disable_webhooks' doit 'emits the event' doexpect dodiff --git a/api/spec/spec_helper.rb b/api/spec/spec_helper.rbindex cfe241bc39..12af8095fa 100644--- a/api/spec/spec_helper.rb+++ b/api/spec/spec_helper.rb@@ -77,7 +77,7 @@ RSpec.configure do |config|config.include Spree::TestingSupport::ImageHelpersconfig.before do- ENV['DISABLE_SPREE_WEBHOOKS'] = 'true'+ Spree::Webhooks.disabled = trueRails.cache.clearreset_spree_preferencesdiff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rbindex 9a8f7a5acd..af41674e48 100644--- a/core/lib/spree/core.rb+++ b/core/lib/spree/core.rb@@ -19,6 +19,7 @@ require 'ransack'require 'state_machines-activerecord'require 'active_storage_validations'require 'activerecord-typedstore'+require 'request_store'# This is required because ActiveModel::Validations#invalid? conflicts with the# invalid state of a Payment. In the future this should be removed.diff --git a/core/lib/spree/core/webhooks.rb b/core/lib/spree/core/webhooks.rbindex ad19c63b78..972ae02206 100644--- a/core/lib/spree/core/webhooks.rb+++ b/core/lib/spree/core/webhooks.rb@@ -1,13 +1,21 @@module Spreemodule Webhooksdef self.disable_webhooks- webhooks_disabled_previously = ENV['DISABLE_SPREE_WEBHOOKS']- begin- ENV['DISABLE_SPREE_WEBHOOKS'] = 'true'- yield- ensure- ENV['DISABLE_SPREE_WEBHOOKS'] = webhooks_disabled_previously- end+ prev_value = disabled?+ RequestStore.store[:disable_spree_webhooks] = true+ yield+ ensure+ RequestStore.store[:disable_spree_webhooks] = prev_value+ end++ def self.disabled?+ # rubocop:disable Style/RedundantFetchBlock+ RequestStore.fetch(:disable_spree_webhooks) { false }+ # rubocop:enable Style/RedundantFetchBlock+ end++ def self.disabled=(value)+ RequestStore.store[:disable_spree_webhooks] = valueendendenddiff --git a/core/spree_core.gemspec b/core/spree_core.gemspecindex d5160171b9..6615c20926 100644--- a/core/spree_core.gemspec+++ b/core/spree_core.gemspec@@ -57,4 +57,5 @@ Gem::Specification.new do |s|s.add_dependency 'mobility', '~> 1.2's.add_dependency 'mobility-ransack', '~> 1.2's.add_dependency 'friendly_id-mobility', '~> 1.0'+ s.add_dependency 'request_store', '~> 1.7'end