Skip to content

Commit

Permalink
Add methods for subscribing event subscribers
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
spaghetticode committed Sep 18, 2020
1 parent 9048a8f commit 1304862
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 2 additions & 6 deletions core/lib/spree/core/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions core/lib/spree/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1304862

Please sign in to comment.