Skip to content

Commit

Permalink
Deprecate 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`.

The method names `subscribe` and `unsubscribe` are already used by
`Spree::Event`, so the naming was a bit confusing; `activate` and
`deactivate` should help in making this a bit more clear.

`::activate` and `::deacticate` are now using the new subscriber
registry for activation/deactivation, as `Spree::Event.subscribers`
is now deprecated.
  • Loading branch information
spaghetticode committed Oct 16, 2020
1 parent 026af1b commit 754346e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 31 deletions.
38 changes: 22 additions & 16 deletions core/lib/spree/event/subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,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`.", caller)
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`.", caller)
deactivate
end
end
end
Expand Down
40 changes: 29 additions & 11 deletions core/spec/lib/spree/event/subscriber_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,33 @@ def event_name(event)
end

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

describe '::subscribe!' do
before { M.unsubscribe! }
it 'is deprecated in favor of ::activate' do
allow(M).to receive(:activate)
expect(Spree::Deprecation).to receive(:warn)

M.subscribe!
end
end

describe '::unsubscribe!' do
it 'is deprecated in favor of ::unsubscribe' do
allow(M).to receive(:deactivate)
expect(Spree::Deprecation).to receive(:warn)

M.unsubscribe!
end
end

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 +51,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 +86,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
Original file line number Diff line number Diff line change
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 754346e

Please sign in to comment.