Skip to content

Commit

Permalink
Rename Spree::Event::Subscriber::subscribe! and ::unsubscribe!
Browse files Browse the repository at this point in the history
`Spree::Event::Subscriber.subscribe!` is deprecated and replaced by
`::activate`.

`Spree::Event::Subscriber.unsubscribe!` is deprecated and replaced by
`::deactivate`.

`::subscribe` and `::unsubscribe` are already used by `Spree::Event`, so
the naming was a bit confusing.
  • Loading branch information
DanielePalombo authored and spaghetticode committed Oct 16, 2020
1 parent bf879ec commit 996bd4f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 34 deletions.
47 changes: 29 additions & 18 deletions core/lib/spree/event/subscriber.rb
Expand Up @@ -19,15 +19,20 @@ module Event
# end
# end
#
# EmailSender.subscribe!
# # Optional, required only when the subscriber needs to be loaded manually.
# #
# # If Spree::Config.events.autoload_subscribers is set to `true` and the module
# # file matches the pattern `app/subscribers/**/*_subscriber.rb` then it will
# # be loaded automatically at boot and this line can be removed:
# EmailSender.activate
module Subscriber
def self.included(base)
base.extend base

base.mattr_accessor :event_actions
base.event_actions = {}

Spree::Event.subscribers << base.name
Spree::Event.subscriber_registry.register(base)
end

# Declares a method name in the including module that can be subscribed/unsubscribed
Expand Down Expand Up @@ -65,28 +70,34 @@ def event_action(method_name, event_name: nil)
event_actions[method_name] = (event_name || method_name).to_s
end

# Subscribes all declared event actions to their events. Only actions that are subscribed
# Activates all declared event actions to their events. Only actions that are activated
# will be called when their event fires.
#
# @example subscribe all event actions for module 'EmailSender'
# EmailSender.subscribe!
# @example activate all event actions for module 'EmailSender'
# EmailSender.activate
def activate
Spree::Event.subscriber_registry.activate_subscriber(self)
end

# Deactivates all declared event actions (or a single specific one) from their events.
# This means that when an event fires then none of its unsubscribed event actions will
# be called.
# @example deactivate all event actions for module 'EmailSender'
# EmailSender.deactivate
# @example deactivate only order_finalized for module 'EmailSender'
# EmailSender.deactivate(:order_finalized)
def deactivate(event_action_name = nil)
Spree::Event.subscriber_registry.deactivate_subscriber(self, event_action_name)
end

def subscribe!
unsubscribe!
event_actions.each do |event_action, event_name|
send "#{event_action}_handler=", Spree::Event.subscribe(event_name) { |event|
send event_action, event
}
end
Spree::Deprecation.warn("#{self}.subscribe! is deprecated. Please use `#{self}.activate`.")
activate
end

# Unsubscribes all declared event actions from their events. This means that when an event
# fires then none of its unsubscribed event actions will be called.
# @example unsubscribe all event actions for module 'EmailSender'
# EmailSender.unsubscribe!
def unsubscribe!
event_actions.keys.each do |event_action|
Spree::Event.unsubscribe send("#{event_action}_handler")
end
Spree::Deprecation.warn("#{self}.unsubscribe! is deprecated. Please use `#{self}.deactivate`.")
deactivate
end
end
end
Expand Down
24 changes: 12 additions & 12 deletions core/spec/lib/spree/event/subscriber_spec.rb
Expand Up @@ -14,15 +14,15 @@ def event_name(event)
end

def other_event(event)
# ...
# not registered via event_action
end
end

describe '::subscribe!' do
before { M.unsubscribe! }
describe '::activate' do
before { M.deactivate }

it 'adds new listeners to Spree::Event' do
expect { M.subscribe! }.to change { Spree::Event.listeners }
expect { M.activate }.to change { Spree::Event.listeners }
end

context 'when subscriptions are not registered' do
Expand All @@ -33,31 +33,31 @@ def other_event(event)
end

it 'subscribes event actions' do
M.subscribe!
M.activate
expect(M).to receive(:event_name)
Spree::Event.fire 'event_name'
end

it 'does not subscribe event actions more than once' do
2.times { M.subscribe! }
2.times { M.activate }
expect(M).to receive(:event_name).once
Spree::Event.fire 'event_name'
end
end

describe '::unsubscribe' do
before { M.subscribe! }
describe '::deactivate' do
before { M.activate }

it 'removes the subscription' do
expect(M).not_to receive(:event_name)
M.unsubscribe!
M.deactivate
Spree::Event.fire 'event_name'
end
end

describe '::event_action' do
context 'when the action has not been declared' do
before { M.subscribe! }
before { M.activate }

it 'does not subscribe the action' do
expect(M).not_to receive(:other_event)
Expand All @@ -68,11 +68,11 @@ def other_event(event)
context 'when the action is declared' do
before do
M.event_action :other_event
M.subscribe!
M.activate
end

after do
M.unsubscribe!
M.deactivate
M.event_actions.delete(:other_event)
end

Expand Down
13 changes: 9 additions & 4 deletions core/spec/models/spree/order_spec.rb
Expand Up @@ -28,15 +28,20 @@
end.to change(order, :confirmation_delivered).to true
end

it 'sends the email' do
expect(Spree::Config.order_mailer_class).to receive(:confirm_email).and_call_original
order.finalize!
end

# These specs show how notifications can be removed, one at a time or
# all the ones set by MailerSubscriber module
context 'when removing the default email notification subscription' do
before do
Spree::Event.unsubscribe Spree::MailerSubscriber.order_finalized_handler
Spree::MailerSubscriber.deactivate(:order_finalized)
end

after do
Spree::MailerSubscriber.subscribe!
Spree::MailerSubscriber.activate
end

it 'does not send the email' do
Expand All @@ -47,11 +52,11 @@

context 'when removing all the email notification subscriptions' do
before do
Spree::MailerSubscriber.unsubscribe!
Spree::MailerSubscriber.deactivate
end

after do
Spree::MailerSubscriber.subscribe!
Spree::MailerSubscriber.activate
end

it 'does not send the email' do
Expand Down

0 comments on commit 996bd4f

Please sign in to comment.