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
Only inject Webhooks concern into models that makes sense
Damian Legawieccb79a331ac3d
diff --git a/api/app/models/concerns/spree/webhooks/has_webhooks.rb b/api/app/models/concerns/spree/webhooks/has_webhooks.rbindex 4f29489202..f6d57e566c 100644--- a/api/app/models/concerns/spree/webhooks/has_webhooks.rb+++ b/api/app/models/concerns/spree/webhooks/has_webhooks.rb@@ -9,7 +9,7 @@ module Spreeafter_update_commit(proc { queue_webhooks_requests!(event_name(:update)) })def queue_webhooks_requests!(event)- return if disable_spree_webhooks? || webhooks_descendant? || body.blank?+ return if disable_spree_webhooks? || body.blank?Spree::Webhooks::Subscribers::QueueRequests.call(event: event, body: body)end@@ -21,14 +21,6 @@ module Spree"#{self.class.name.demodulize.tableize.singularize}.#{operation}"end- def webhooks_descendant?- if Rails::VERSION::MAJOR >= 6- self.class.module_parent == Spree::Webhooks- else- self.class.parent == Spree::Webhooks- end- end-def bodyresource_serializer.new(self).serializable_hash.to_jsonend@@ -44,5 +36,3 @@ module Spreeendendend--Spree::Base.include(Spree::Webhooks::HasWebhooks)diff --git a/api/lib/spree/api/engine.rb b/api/lib/spree/api/engine.rbindex 0f32b04e0f..6e5722fde2 100644--- a/api/lib/spree/api/engine.rb+++ b/api/lib/spree/api/engine.rb@@ -23,7 +23,19 @@ module Spreeenddef self.activate- Spree::Base.include(Spree::Webhooks::HasWebhooks)+ [+ Spree::Address, Spree::Asset, Spree::CmsPage, Spree::CreditCard, Spree::CustomerReturn,+ Spree::DigitalLink, Spree::Digital, Spree::InventoryUnit, Spree::LineItem, Spree::MenuItem,+ Spree::Menu, Spree::OptionType, Spree::OptionValue, Spree::Order, Spree::PaymentCaptureEvent,+ Spree::Payment, Spree::Price, Spree::Product, Spree::Promotion, Spree::Property, Spree::Prototype,+ Spree::Refund, Spree::Reimbursement, Spree::ReturnAuthorization, Spree::ReturnItem, Spree::Role,+ Spree::Shipment, Spree::ShippingCategory, Spree::ShippingMethod, Spree::ShippingRate,+ Spree::StockItem, Spree::StockLocation, Spree::StockMovement, Spree::StockTransfer,+ Spree::StoreCredit, Spree::Store, Spree::TaxCategory, Spree::TaxRate, Spree::Taxonomy,+ Spree::Taxon, Spree::Variant, Spree::WishedItem, Spree::Wishlist, Spree::Zone+ ].each do |webhookable_class|+ webhookable_class.include(Spree::Webhooks::HasWebhooks)+ endDir.glob(File.join(File.dirname(__FILE__), '../../../app/models/spree/api/webhooks/*_decorator*.rb')) do |c|Rails.application.config.cache_classes ? require(c) : load(c)diff --git a/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb b/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rbindex dcd50f7929..8232b2b448 100644--- a/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb+++ b/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb@@ -1,34 +1,6 @@require 'spec_helper'-module Spree- module Webhooks- class TestProduct < ActiveRecord::Base- self.table_name = 'test_products'-- include Spree::Webhooks::HasWebhooks- end- end-end--module Spree- class TestProduct < ActiveRecord::Base- self.table_name = 'test_products'-- include Spree::Webhooks::HasWebhooks- end-end-describe Spree::Webhooks::HasWebhooks do- before(:all) do- ActiveRecord::Base.connection.create_table :test_products, force: true do |t|- t.string :name- end- end-- after(:all) do- ActiveRecord::Base.connection.drop_table :test_products, if_exists: true- end-before doallow(Spree::Webhooks::Subscribers::QueueRequests).to receive(:new).and_return(queue_requests)allow(queue_requests).to receive(:call).with(any_args)@@ -72,103 +44,42 @@ describe Spree::Webhooks::HasWebhooks doendcontext 'without DISABLE_SPREE_WEBHOOKS' do- context 'with a Spree::Webhooks descendant class' do- let(:product) { Spree::Webhooks::TestProduct.new(name: 'test') }+ let(:body) { Spree::Api::V2::Platform::ProductSerializer.new(product).serializable_hash.to_json }- before do- ENV['DISABLE_SPREE_WEBHOOKS'] = nil- product.save- end+ before do+ ENV['DISABLE_SPREE_WEBHOOKS'] = nil+ product.save+ end- context 'after_create_commit' do- it 'does not queue a request' do- expect(queue_requests).not_to have_received(:call).with(hash_including(event: 'test_product.create'))- end+ context 'after_create_commit' do+ it 'queues a request through QueueRequests for product.create' do+ expect(queue_requests).to have_received(:call).with(event: 'product.create', body: body).onceend+ end- context 'after_destroy_commit' do- it 'does not queue a request' do- product.destroy- expect(queue_requests).not_to have_received(:call).with(hash_including(event: 'test_product.destroy'))- end+ context 'after_destroy_commit' do+ it 'queues a request through QueueRequests for product.destroy' do+ product.destroy+ expect(queue_requests).to have_received(:call).with(event: 'product.destroy', body: body).onceend+ end- context 'after_update_commit' do- it 'does not queue a request' do- product.update(name: 'updated')- expect(queue_requests).not_to have_received(:call).with(hash_including(event: 'test_product.update'))- end+ context 'after_update_commit' do+ it 'queues a request through QueueRequests for product.update' do+ product.update(name: 'updated')+ expect(queue_requests).to have_received(:call).with(event: 'product.update', body: body).onceendend- context 'without a Spree::Webhooks descendant class' do- context 'with a resource serializer' do- let(:body) { Spree::Api::V2::Platform::ProductSerializer.new(product).serializable_hash.to_json }-- before do- ENV['DISABLE_SPREE_WEBHOOKS'] = nil- product.save- end-- context 'after_create_commit' do- it 'queues a request through QueueRequests for product.create' do- expect(queue_requests).to have_received(:call).with(event: 'product.create', body: body).once- end- end-- context 'after_destroy_commit' do- it 'queues a request through QueueRequests for product.destroy' do- product.destroy- expect(queue_requests).to have_received(:call).with(event: 'product.destroy', body: body).once- end- end-- context 'after_update_commit' do- it 'queues a request through QueueRequests for product.update' do- product.update(name: 'updated')- expect(queue_requests).to have_received(:call).with(event: 'product.update', body: body).once- end- end-- context 'with a class name with multiple words' do- let!(:cms_page) { create(:cms_homepage, store: store, locale: 'en') }- let(:body) do- Spree::Api::V2::Platform::CmsPageSerializer.new(cms_page).serializable_hash.to_json- end- let(:underscore_event) { 'cms_page.create' }-- it 'underscorize the event name' do- expect(queue_requests).to have_received(:call).with(event: underscore_event, body: body).once- end- end+ context 'with a class name with multiple words' do+ let!(:cms_page) { create(:cms_homepage, store: store, locale: 'en') }+ let(:body) do+ Spree::Api::V2::Platform::CmsPageSerializer.new(cms_page).serializable_hash.to_jsonend+ let(:underscore_event) { 'cms_page.create' }- context 'without a resource serializer' do- let(:product) { Spree::TestProduct.new(name: 'test') }-- let(:exception_msg) { /uninitialized constant Spree::Api::V2::Platform::TestProductSerializer/ }-- context 'after_create_commit' do- before { ENV['DISABLE_SPREE_WEBHOOKS'] = nil }-- it { expect { product.save }.to raise_error(NameError, exception_msg) }- end-- context 'update/destroy' do- before do- ENV['DISABLE_SPREE_WEBHOOKS'] = 'true'- product.save- ENV['DISABLE_SPREE_WEBHOOKS'] = nil- end-- context 'after_destroy_commit' do- it { expect { product.destroy }.to raise_error(NameError, exception_msg) }- end-- context 'after_update_commit' do- it { expect { product.update(name: 'new name') }.to raise_error(NameError, exception_msg) }- end- end+ it 'underscorize the event name' do+ expect(queue_requests).to have_received(:call).with(event: underscore_event, body: body).onceendendend