From 13048629142b7da5da46e69ec8f0cead0f59d147 Mon Sep 17 00:00:00 2001 From: andrea longhi Date: Fri, 18 Sep 2020 17:43:40 +0200 Subject: [PATCH] Add methods for subscribing event subscribers The new methods `Spree::Event.unsubscribe_subscribers!` and `Spree::Event.subscribe_subscribers!` encapsulate the logic for subscribing and unsubscribing all event subscribers at once. The logic was previously scattered in engine.rb, exposing too much of the subscription internals. --- core/lib/spree/core/engine.rb | 8 ++------ core/lib/spree/event.rb | 8 ++++++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/core/lib/spree/core/engine.rb b/core/lib/spree/core/engine.rb index d8129e715f0..37b4a4d2751 100644 --- a/core/lib/spree/core/engine.rb +++ b/core/lib/spree/core/engine.rb @@ -48,15 +48,11 @@ class Engine < ::Rails::Engine initializer 'spree.core.initialize_subscribers' do |app| app.reloader.to_prepare do Spree::Event.require_subscriber_files - Spree::Event.subscribers.each_key do |klass| - klass.constantize.subscribe! - end + Spree::Event.subscribe_subscribers! end app.reloader.before_class_unload do - Spree::Event.subscribers.each_key do |klass| - klass.constantize.unsubscribe! - end + Spree::Event.unsubscribe_subscribers! end end diff --git a/core/lib/spree/event.rb b/core/lib/spree/event.rb index 35a0f00be39..171eb6d7862 100644 --- a/core/lib/spree/event.rb +++ b/core/lib/spree/event.rb @@ -136,6 +136,14 @@ def subscribers Spree::Config.events.subscribers end + def subscribe_subscribers! + subscribers.each_key { |klass| klass.constantize.subscribe! } + end + + def unsubscribe_subscribers! + subscribers.each_key { |klass| klass.constantize.unsubscribe! } + end + private def normalize_name(name)