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.

Make has_spree_webhooks available for every Spree::Base inheriting class.

Sebastian Palma6a2495e1d438
diff --git a/api/lib/spree/api/engine.rb b/api/lib/spree/api/engine.rb
index 89298616bf..109073ed3a 100644
--- a/api/lib/spree/api/engine.rb
+++ b/api/lib/spree/api/engine.rb
@@ -1,4 +1,5 @@
require 'rails/engine'
+require 'spree/webhooks/has_webhooks'
module Spree
module Api
@@ -22,6 +23,12 @@ module Spree
end
end
+ 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__))
end
diff --git a/api/lib/spree/webhooks/has_webhooks.rb b/api/lib/spree/webhooks/has_webhooks.rb
new file mode 100644
index 0000000000..25173b0c1d
--- /dev/null
+++ b/api/lib/spree/webhooks/has_webhooks.rb
@@ -0,0 +1,24 @@
+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
+end
diff --git a/api/spec/models/spree/webhooks/endpoint_spec.rb b/api/spec/models/spree/webhooks/endpoint_spec.rb
index 0fc1ea81a6..02bc624d1e 100644
--- a/api/spec/models/spree/webhooks/endpoint_spec.rb
+++ b/api/spec/models/spree/webhooks/endpoint_spec.rb
@@ -13,12 +13,11 @@ module Spree
end
context 'mysql', if: ActiveRecord::Base.connection.adapter_name == 'Mysql2' do
-
context 'without subscriptions when initializing an endpoint' do
let(:subscriptions) { nil }
it 'sets the default subscriptions' do
- expect(subject.subscriptions).to eq(default_all_subscriptions)
+ expect(subject.subscriptions).to eq(default_all_subscriptions)
end
end
@@ -28,7 +27,7 @@ module Spree
let(:subscriptions) { ['model.event'] }
it 'after-save does not set default subscriptions' do
- expect(subject.subscriptions).to eq(subscriptions)
+ expect(subject.subscriptions).to eq(subscriptions)
end
end
end
@@ -40,7 +39,7 @@ module Spree
# Although an empty string isn't a working subscription
# what's being tested is setting default susbcriptions,
# since MySQL does not support default values for JSON.
- expect(subject.subscriptions).to eq(subscriptions)
+ expect(subject.subscriptions).to eq(subscriptions)
end
end
end
diff --git a/api/spec/models/spree/webhooks/has_webhooks_spec.rb b/api/spec/models/spree/webhooks/has_webhooks_spec.rb
new file mode 100644
index 0000000000..43b60c1fc5
--- /dev/null
+++ b/api/spec/models/spree/webhooks/has_webhooks_spec.rb
@@ -0,0 +1,33 @@
+require 'spec_helper'
+
+module Spree
+ describe Webhooks::HasSpreeWebhooks do
+ before(:all) do
+ ActiveRecord::Base.connection.create_table(:test_spree_products, force: true) do |table|
+ table.string :name
+ end
+ Spree.const_set(
+ "TestSpreeProduct",
+ Class.new(Spree::Base) do
+ self.table_name = 'test_spree_products'
+
+ has_spree_webhooks(on: :create)
+ end
+ )
+ end
+
+ it 'implements has_spree_webhooks' do
+ expect(TestSpreeProduct).to respond_to(:has_spree_webhooks)
+ end
+
+ context 'after commit on :create' do
+ let(:test_spree_product) { TestSpreeProduct.new(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!')
+ end
+ end
+ end
+end