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.

Fixed spree events logger in development environment

Damian Legawiecb5e97052556f
diff --git a/core/app/subscribers/spree/event_log_subscriber.rb b/core/app/subscribers/spree/event_log_subscriber.rb
index 3a5151463e..667195d8da 100644
--- a/core/app/subscribers/spree/event_log_subscriber.rb
+++ b/core/app/subscribers/spree/event_log_subscriber.rb
@@ -16,9 +16,10 @@ module Spree
class << self
def attach_to_notifications
- return if @attached
+ # Always detach first to ensure clean state after code reload
+ detach_from_notifications
- @subscription = ActiveSupport::Notifications.subscribe(/\.#{NAMESPACE}$/) do |name, start, finish, id, payload|
+ @subscription = ActiveSupport::Notifications.subscribe(/\.#{NAMESPACE}$/) do |name, start, finish, _id, payload|
log_event(name, start, finish, payload)
end
@@ -27,8 +28,6 @@ module Spree
end
def detach_from_notifications
- return unless @attached
-
ActiveSupport::Notifications.unsubscribe(@subscription) if @subscription
@subscription = nil
@attached = false
diff --git a/core/lib/spree/core/engine.rb b/core/lib/spree/core/engine.rb
index 04c4d45616..ee365f40ba 100644
--- a/core/lib/spree/core/engine.rb
+++ b/core/lib/spree/core/engine.rb
@@ -386,10 +386,21 @@ module Spree
Rails.configuration.cache_classes ? require(c) : load(c)
end
+ # Re-enable lifecycle events for configured models after code reload
+ # This is needed because after_commit callbacks are lost when classes are reloaded
+ Rails.application.config.spree.eventable_models&.each do |model|
+ model.publishes_lifecycle_events if model.respond_to?(:publishes_lifecycle_events)
+ end
+
# Reset and re-activate event subscribers on code reload
# activate! will register all subscribers from Spree.subscribers
Spree::Events.reset!
Spree::Events.activate!
+
+ # Re-attach event log subscriber if enabled
+ if Spree::Config.events_log_enabled
+ Spree::EventLogSubscriber.attach_to_notifications
+ end
end
end
end
diff --git a/core/spec/subscribers/spree/event_log_subscriber_spec.rb b/core/spec/subscribers/spree/event_log_subscriber_spec.rb
index 810acd1ae7..322eabfef5 100644
--- a/core/spec/subscribers/spree/event_log_subscriber_spec.rb
+++ b/core/spec/subscribers/spree/event_log_subscriber_spec.rb
@@ -19,10 +19,13 @@ RSpec.describe Spree::EventLogSubscriber do
expect(described_class.attached?).to be true
end
- it 'only attaches once' do
+ it 'can be called multiple times safely (for code reload support)' do
described_class.attach_to_notifications
- expect(ActiveSupport::Notifications).not_to receive(:subscribe)
+ expect(described_class.attached?).to be true
+
+ # Calling again should still work (detaches and re-attaches)
described_class.attach_to_notifications
+ expect(described_class.attached?).to be true
end
end