Module: Spree::Event::Subscriber

Included in:
MailerSubscriber
Defined in:
lib/spree/event/subscriber.rb

Overview

This module simplifies adding and removing subscriptions to Spree::Event events. Here’s a complete example:

 module EmailSender
   include Spree::Event::Subscriber

   event_action :order_finalized
   event_action :confirm_reimbursement, event_name: :reimbursement_reimbursed

   def order_finalized(event)
     Mailer.send_email(event.payload[:order])
   end

   def confirm_reimbursement(event)
     Mailer.send_email(event.payload[:reimbursement])
   end
 end

EmailSender.subscribe!

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



24
25
26
27
28
29
# File 'lib/spree/event/subscriber.rb', line 24

def self.included(base)
  base.extend base

  base.mattr_accessor :event_actions
  base.event_actions = {}
end

Instance Method Details

#event_action(method_name, event_name: nil) ⇒ Object

Declares a method name in the including module that can be subscribed/unsubscribed to an event.

Examples:

Declares ‘send_email’ as an event action that can subscribe the event ‘order_finalized’

module EmailSender
  event_action :send_email, event_name: :order_finalized

  def send_email(event)
    Mailer.send_email(event.payload[:order])
  end
end

Same as above, but the method name is same as the event name:

module EmailSender
  event_action :order_completed

  def order_completed(event)
    Mailer.send_email(event.payload[:order])
  end
end

Parameters:

  • method_name (String, Symbol)

    the method that will be called when the subscribed event is fired

  • event_name (String, Symbol) (defaults to: nil)

    the name of the event to be subscribed



54
55
56
57
# File 'lib/spree/event/subscriber.rb', line 54

def event_action(method_name, event_name: nil)
  mattr_accessor "#{method_name}_handler"
  event_actions[method_name] = (event_name || method_name).to_s
end

#subscribe!Object

Subscribes all declared event actions to their events. Only actions that are subscribed will be called when their event fires.

Examples:

subscribe all event actions for module ‘EmailSender’

EmailSender.subscribe!


64
65
66
67
68
69
70
71
# File 'lib/spree/event/subscriber.rb', line 64

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
end

#unsubscribe!Object

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.

Examples:

unsubscribe all event actions for module ‘EmailSender’

EmailSender.unsubscribe!


77
78
79
80
81
# File 'lib/spree/event/subscriber.rb', line 77

def unsubscribe!
  event_actions.keys.each do |event_action|
    Spree::Event.unsubscribe send("#{event_action}_handler")
  end
end