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 HasWebhooks and examples.

Sebastian Palmab76ae8a110ac
diff --git a/core/app/models/concerns/spree/webhooks/has_webhooks.rb b/core/app/models/concerns/spree/webhooks/has_webhooks.rb
index 4c1eae4576..9040b6a2d2 100644
--- a/core/app/models/concerns/spree/webhooks/has_webhooks.rb
+++ b/core/app/models/concerns/spree/webhooks/has_webhooks.rb
@@ -4,17 +4,25 @@ module Spree
extend ActiveSupport::Concern
included do
- after_commit :execute_webhook_logic!,
- on: %i[create destroy update],
- unless: proc { self.class.module_parents.include?(Spree::Webhooks) }
+ after_create_commit proc { execute_webhook_logic!(event_name(:create)) },
+ unless: proc { webhooks_descendant? }
+ after_destroy_commit proc { execute_webhook_logic!(event_name(:destroy)) },
+ unless: proc { webhooks_descendant? }
+ after_update_commit proc { execute_webhook_logic!(event_name(:update)) },
+ unless: proc { webhooks_descendant? }
- 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)
+ def execute_webhook_logic!(event)
+ Spree::Webhooks::Endpoints::QueueRequests.call(event: event)
+ end
+
+ private
+
+ def webhooks_descendant?
+ self.class.module_parents.include?(Spree::Webhooks)
+ end
+
+ def event_name(operation)
+ "#{self.class.name.demodulize.downcase}.#{operation}"
end
end
end
diff --git a/core/spec/models/spree/webhooks/has_webhooks_spec.rb b/core/spec/models/spree/webhooks/has_webhooks_spec.rb
index 5309ce403d..6ce6a0e692 100644
--- a/core/spec/models/spree/webhooks/has_webhooks_spec.rb
+++ b/core/spec/models/spree/webhooks/has_webhooks_spec.rb
@@ -9,97 +9,95 @@ module Test
end
module Spree
- module Webhooks
- class Product < ActiveRecord::Base
- self.table_name = 'test_products'
+ class Webhooks::Product < ActiveRecord::Base
+ self.table_name = 'test_products'
- include Spree::Webhooks::HasWebhooks
- end
+ include Spree::Webhooks::HasWebhooks
end
end
-module Spree
- describe Webhooks::HasWebhooks do
- let(:connection) { ActiveRecord::Base.connection }
- let(:url) { 'https://google.com' }
-
- before do
- stub_request(:any, url)
- connection.create_table :test_products, force: true do |table|
- table.string :name
- end
- end
+describe Spree::Webhooks::HasWebhooks do
+ let(:connection) { ActiveRecord::Base.connection }
+ let(:url) { 'https://google.com' }
- after do
- connection.drop_table :test_products, if_exists: true
+ before do
+ stub_request(:any, url)
+ connection.create_table :test_products, force: true do |table|
+ table.string :name
end
+ end
- 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
+ after do
+ connection.drop_table :test_products, if_exists: true
+ end
- context 'after commit on destroy' do
- let(:test_product) { Spree::Webhooks::Product.create(name: 'test') }
+ context 'with a Spree::Webhooks class including the module' do
+ let!(: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
+ context 'after commit on create' do
+ it 'does not execute the webhook callback' do
+ expect(Spree::Webhooks::Endpoints::QueueRequests).not_to(receive(:new))
end
+ end
- context 'after commit on update' do
- let(:test_product) { Spree::Webhooks::Product.create(name: 'test') }
+ context 'after commit on destroy' do
+ it 'does not execute the webhook callback' do
+ product.destroy
+ expect(Spree::Webhooks::Endpoints::QueueRequests).not_to(receive(:new))
+ end
+ end
- it 'does not execute the webhook callback' do
- test_product.update(name: 'updated')
- expect(WebMock).to_not have_requested(:post, url)
- end
+ context 'after commit on update' do
+ it 'does not execute the webhook callback' do
+ product.update(name: 'updated')
+ expect(Spree::Webhooks::Endpoints::QueueRequests).not_to(receive(:new))
end
end
+ end
+
+ context 'with a non Spree::Webhooks class including the module' do
+ let(:product) { Test::Product.create(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
+ context 'after commit on create' do
+ it 'executes the webhook callback' do
+ expect(Spree::Webhooks::Endpoints::QueueRequests).to(
+ receive(:new).once.and_return(
+ double(:new).tap do |scope|
+ expect(scope).to receive(:call).with(event: 'product.create').once
+ end
)
- end
+ )
+ product
end
+ end
- context 'after commit on destroy' do
- let(:test_product) { Test::Product.create(name: 'test') }
+ context 'after commit on destroy' do
+ before { product }
- 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
+ it 'executes the webhook callback' do
+ expect(Spree::Webhooks::Endpoints::QueueRequests).to(
+ receive(:new).once.and_return(
+ double(:new).tap do |scope|
+ expect(scope).to receive(:call).with(event: 'product.destroy').once
+ end
)
- end
+ )
+ product.destroy
end
+ end
- context 'after commit on update' do
- let(:test_product) { Test::Product.create(name: 'test') }
+ context 'after commit on update' do
+ before { product }
- 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
+ it 'executes the webhook callback' do
+ expect(Spree::Webhooks::Endpoints::QueueRequests).to(
+ receive(:new).once.and_return(
+ double(:new).tap do |scope|
+ expect(scope).to receive(:call).with(event: 'product.update').once
+ end
)
- end
+ )
+ product.update(name: 'updated')
end
end
end