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
Add after_commit callbacks only to non Spree::Webhooks classes.
Sebastian Palmabe8500e0b0d0
diff --git a/api/app/models/concerns/spree/webhooks/has_webhooks.rb b/api/app/models/concerns/spree/webhooks/has_webhooks.rbnew file mode 100644index 0000000000..5df945f241--- /dev/null+++ b/api/app/models/concerns/spree/webhooks/has_webhooks.rb@@ -0,0 +1,24 @@+module Spree+ module Webhooks+ module HasWebhooks+ extend ActiveSupport::Concern++ included do+ after_commit :execute_webhook_logic!,+ on: %i[create destroy update],+ unless: proc { self.class.module_parents.include?(Spree::Webhooks) }+ end++ private++ def execute_webhook_logic!+ uri = URI('https://google.com/')+ request = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json')+ request.body = {foo: :bar}.to_json+ http = Net::HTTP.new(uri.host, uri.port)+ http.use_ssl = true+ http.request(request)+ end+ end+ end+enddiff --git a/api/lib/spree/api/engine.rb b/api/lib/spree/api/engine.rbindex 109073ed3a..89298616bf 100644--- a/api/lib/spree/api/engine.rb+++ b/api/lib/spree/api/engine.rb@@ -1,5 +1,4 @@require 'rails/engine'-require 'spree/webhooks/has_webhooks'module Spreemodule Api@@ -23,12 +22,6 @@ module Spreeendend- initializer 'extend ActiveRecord with Spree::Webhooks' do |_app|- ActiveSupport.on_load(:active_record) do- Spree::Base.send :include, Spree::Webhooks::HasSpreeWebhooks- end- end-def self.root@root ||= Pathname.new(File.expand_path('../../..', __dir__))enddiff --git a/api/lib/spree/webhooks/has_webhooks.rb b/api/lib/spree/webhooks/has_webhooks.rbdeleted file mode 100644index 25173b0c1d..0000000000--- a/api/lib/spree/webhooks/has_webhooks.rb+++ /dev/null@@ -1,24 +0,0 @@-module Spree- module Webhooks- module HasSpreeWebhooks- extend ActiveSupport::Concern-- module ClassMethods- def has_spree_webhooks(on: [])- Array(on).each do |action|- public_send(:after_commit, on: action) do- execute_webhook_logic!- end- end- end- end-- private-- def execute_webhook_logic!- self.name = 'execute_webhook_logic!'- self.save- end- end- end-enddiff --git a/api/spec/models/spree/webhooks/has_webhooks_spec.rb b/api/spec/models/spree/webhooks/has_webhooks_spec.rbindex 43b60c1fc5..5ef262f063 100644--- a/api/spec/models/spree/webhooks/has_webhooks_spec.rb+++ b/api/spec/models/spree/webhooks/has_webhooks_spec.rb@@ -1,32 +1,107 @@require 'spec_helper'+module Test+ class Product < ActiveRecord::Base+ self.table_name = 'test_products'++ include Spree::Webhooks::HasWebhooks+ end+end++module Spree+ module Webhooks+ class Product < ActiveRecord::Base+ self.table_name = 'test_products'++ include Spree::Webhooks::HasWebhooks+ end+ end+end+module Spree- describe Webhooks::HasSpreeWebhooks do- before(:all) do- ActiveRecord::Base.connection.create_table(:test_spree_products, force: true) do |table|+ describe Webhooks::HasWebhooks do+ let(:connection) { ActiveRecord::Base.connection }+ let(:url) { 'https://google.com' }++ before do+ WebMock.disable_net_connect!+ stub_request(:any, url)+ connection.create_table :test_products, force: true do |table|table.string :nameend- Spree.const_set(- "TestSpreeProduct",- Class.new(Spree::Base) do- self.table_name = 'test_spree_products'+ end- has_spree_webhooks(on: :create)- end- )+ after do+ WebMock.allow_net_connect!+ connection.drop_table :test_products, if_exists: trueend- it 'implements has_spree_webhooks' do- expect(TestSpreeProduct).to respond_to(:has_spree_webhooks)+ context 'with a Spree::Webhooks class including the module' do+ context 'after commit on create' do+ it 'does not execute the webhook callback' do+ Spree::Webhooks::Product.create(name: 'new product #1')+ expect(WebMock).to_not have_requested(:post, url)+ end+ end++ context 'after commit on destroy' do+ let(:test_product) { Spree::Webhooks::Product.create(name: 'test') }++ it 'does not execute the webhook callback' do+ test_product.destroy+ expect(WebMock).to_not have_requested(:post, url)+ end+ end++ context 'after commit on update' do+ let(:test_product) { Spree::Webhooks::Product.create(name: 'test') }++ it 'does not execute the webhook callback' do+ test_product.update(name: 'updated')+ expect(WebMock).to_not have_requested(:post, url)+ end+ endend- context 'after commit on :create' do- let(:test_spree_product) { TestSpreeProduct.new(name: 'test') }+ context 'with a non Spree::Webhooks class including the module' do+ context 'after commit on create' do+ it 'executes the webhook callback' do+ Test::Product.create(name: 'new product #1')+ expect(WebMock).to(+ have_requested(:post, url).with(+ body: {foo: :bar}.to_json,+ headers: {'Content-Type' => 'application/json'}+ ).once+ )+ end+ end++ context 'after commit on destroy' do+ let(:test_product) { Test::Product.create(name: 'test') }- it 'executes the webhook logic' do- expect do- test_spree_product.save- end.to change { test_spree_product.name }.from('test').to('execute_webhook_logic!')+ it 'executes the webhook callback' do+ test_product.destroy+ expect(WebMock).to(+ have_requested(:post, url).with(+ body: {foo: :bar}.to_json,+ headers: {'Content-Type' => 'application/json'}+ ).twice+ )+ end+ end++ context 'after commit on update' do+ let(:test_product) { Test::Product.create(name: 'test') }++ it 'executes the webhook callback' do+ test_product.update(name: 'updated')+ expect(WebMock).to(+ have_requested(:post, url).with(+ body: {foo: :bar}.to_json,+ headers: {'Content-Type' => 'application/json'}+ ).twice+ )+ endendendenddiff --git a/api/spec/spec_helper.rb b/api/spec/spec_helper.rbindex ec98f89e76..998de3c110 100644--- a/api/spec/spec_helper.rb+++ b/api/spec/spec_helper.rb@@ -27,6 +27,7 @@ endrequire 'rspec/rails'require 'ffaker'+require 'webmock/rspec'# Requires supporting ruby files with custom matchers and macros, etc,# in spec/support/ and its subdirectories.