Module: Spree::Event

Extended by:
Event
Included in:
Event
Defined in:
core/lib/spree/event.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#subscribersObject (readonly)



103
104
105
# File 'core/lib/spree/event.rb', line 103

def subscribers
  Spree::Config.events.subscribers
end

Instance Method Details

#adapterObject

The adapter used by Spree::Event, defaults to Spree::Event::Adapters::ActiveSupportNotifications

Examples:

Change the adapter

Spree::Config.events.adapter = "Spree::EventBus.new"

See Also:



89
90
91
# File 'core/lib/spree/event.rb', line 89

def adapter
  Spree::Config.events.adapter
end

#fire(event_name, opts = {}) ⇒ Object

Allows to trigger events that can be subscribed using #subscribe. An optional block can be passed that will be executed immediately. The actual code implementation is delegated to the adapter.

Examples:

Trigger an event named 'order_finalized'

Spree::Event.fire 'order_finalized', order: @order do
  @order.finalize!
end

Parameters:

  • event_name (String)

    the name of the event. The suffix “.spree” will be added automatically if not present

  • opts (Hash) (defaults to: {})

    a list of options to be passed to the triggered event



23
24
25
26
27
# File 'core/lib/spree/event.rb', line 23

def fire(event_name, opts = {})
  adapter.fire name_with_suffix(event_name.to_s), opts do
    yield opts if block_given?
  end
end

#listenersHash

Lists all subscriptions currently registered under the “.spree” namespace. Actual implementation is delegated to the adapter

Examples:

Current subscriptions

Spree::Event.listeners
  # => {"order_finalized.spree"=> [#<ActiveSupport...>],
    "reimbursement_reimbursed.spree"=> [#<ActiveSupport...>]}

Returns:

  • (Hash)

    an hash with event names as keys and arrays of subscriptions as values



78
79
80
# File 'core/lib/spree/event.rb', line 78

def listeners
  adapter.listeners_for(listener_names)
end

#subscribe(event_name, &block) ⇒ Object

Subscribe to an event with the given name. The provided block is executed every time the subscribed event is fired.

Examples:

Subscribe to the `order_finalized` event

Spree::Event.subscribe 'order_finalized' do |event|
  order = event.payload[:order]
  Spree::Mailer.order_finalized(order).deliver_later
end

Parameters:

  • event_name (String)

    the name of the event. The suffix “.spree” will be added automatically if not present

Returns:

  • a subscription object that can be used as reference in order to remove the subscription

See Also:



45
46
47
48
49
# File 'core/lib/spree/event.rb', line 45

def subscribe(event_name, &block)
  name = name_with_suffix(event_name)
  listener_names << name
  adapter.subscribe(name, &block)
end

#suffixObject

The suffix used for namespacing Solidus events, defaults to `.spree`

See Also:

  • Spree::Event::Configuration#suffix


97
98
99
# File 'core/lib/spree/event.rb', line 97

def suffix
  Spree::Config.events.suffix
end

#unsubscribe(subscriber) ⇒ Object

Unsubscribes a whole event or a specific subscription object

Examples:

Unsubscribe a single subscription

subscription = Spree::Event.fire 'order_finalized'
Spree::Event.unsubscribe(subscription)

Unsubscribe all `order_finalized` event subscriptions

Spree::Event.unsubscribe('order_finalized')

Unsubscribe an event by name with explicit prefix

Spree::Event.unsubscribe('order_finalized.spree')

Parameters:

  • subscriber (String, Object)

    the event name as a string (with or without the “.spree” suffix) or the subscription object



63
64
65
66
# File 'core/lib/spree/event.rb', line 63

def unsubscribe(subscriber)
  name_or_subscriber = subscriber.is_a?(String) ? name_with_suffix(subscriber) : subscriber
  adapter.unsubscribe(name_or_subscriber)
end