@spree/docs 0.1.49 → 0.1.51
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -61,7 +61,7 @@ Create a subscriber class in `app/subscribers/` that inherits from `Spree::Subsc
|
|
|
61
61
|
```ruby app/subscribers/my_app/order_completed_subscriber.rb
|
|
62
62
|
module MyApp
|
|
63
63
|
class OrderCompletedSubscriber < Spree::Subscriber
|
|
64
|
-
subscribes_to 'order.
|
|
64
|
+
subscribes_to 'order.completed'
|
|
65
65
|
|
|
66
66
|
def handle(event)
|
|
67
67
|
order_id = event.payload['id']
|
|
@@ -82,17 +82,17 @@ The `Spree::Subscriber` class provides a clean DSL for declaring subscriptions:
|
|
|
82
82
|
```ruby
|
|
83
83
|
class MySubscriber < Spree::Subscriber
|
|
84
84
|
# Subscribe to a single event
|
|
85
|
-
subscribes_to 'order.
|
|
85
|
+
subscribes_to 'order.completed'
|
|
86
86
|
|
|
87
87
|
# Subscribe to multiple events
|
|
88
|
-
subscribes_to 'order.
|
|
88
|
+
subscribes_to 'order.completed', 'order.canceled', 'order.resumed'
|
|
89
89
|
|
|
90
90
|
# Subscribe to all events matching a pattern
|
|
91
91
|
subscribes_to 'order.*' # All order events
|
|
92
92
|
subscribes_to '*.*' # All events (use sparingly!)
|
|
93
93
|
|
|
94
94
|
# Run synchronously instead of via background job
|
|
95
|
-
subscribes_to 'order.
|
|
95
|
+
subscribes_to 'order.completed', async: false
|
|
96
96
|
end
|
|
97
97
|
```
|
|
98
98
|
|
|
@@ -103,11 +103,11 @@ When subscribing to multiple events, use the `on` DSL to route events to specifi
|
|
|
103
103
|
```ruby app/subscribers/my_app/order_audit_subscriber.rb
|
|
104
104
|
module MyApp
|
|
105
105
|
class OrderAuditSubscriber < Spree::Subscriber
|
|
106
|
-
subscribes_to 'order.
|
|
106
|
+
subscribes_to 'order.completed', 'order.canceled', 'order.resumed'
|
|
107
107
|
|
|
108
|
-
on 'order.
|
|
109
|
-
on 'order.
|
|
110
|
-
on 'order.
|
|
108
|
+
on 'order.completed', :log_order_completed
|
|
109
|
+
on 'order.canceled', :log_order_canceled
|
|
110
|
+
on 'order.resumed', :log_order_resumed
|
|
111
111
|
|
|
112
112
|
private
|
|
113
113
|
|
|
@@ -144,7 +144,7 @@ When your subscriber receives an event, you get a `Spree::Event` object with:
|
|
|
144
144
|
```ruby
|
|
145
145
|
def handle(event)
|
|
146
146
|
event.id # => "550e8400-e29b-41d4-a716-446655440000" (UUID)
|
|
147
|
-
event.name # => "order.
|
|
147
|
+
event.name # => "order.completed"
|
|
148
148
|
event.store_id # => 1 (ID of the store where the event originated)
|
|
149
149
|
event.payload # => { "id" => 1, "number" => "R123456", ... }
|
|
150
150
|
event.metadata # => { "spree_version" => "5.1.0" }
|
|
@@ -153,7 +153,7 @@ def handle(event)
|
|
|
153
153
|
# Helper methods
|
|
154
154
|
event.store # => Spree::Store instance (lazy loaded)
|
|
155
155
|
event.resource_type # => "order" (extracted from name)
|
|
156
|
-
event.action # => "
|
|
156
|
+
event.action # => "completed" (extracted from name)
|
|
157
157
|
end
|
|
158
158
|
```
|
|
159
159
|
|
|
@@ -421,7 +421,7 @@ For critical operations that must complete before the request finishes, use sync
|
|
|
421
421
|
|
|
422
422
|
```ruby
|
|
423
423
|
class CriticalOrderHandler < Spree::Subscriber
|
|
424
|
-
subscribes_to 'order.
|
|
424
|
+
subscribes_to 'order.completed', async: false
|
|
425
425
|
|
|
426
426
|
def handle(event)
|
|
427
427
|
# This runs immediately, blocking the request
|
|
@@ -458,7 +458,7 @@ RSpec.describe MyApp::OrderCompletedSubscriber do
|
|
|
458
458
|
let(:order) { create(:completed_order_with_totals) }
|
|
459
459
|
let(:event) do
|
|
460
460
|
Spree::Event.new(
|
|
461
|
-
name: 'order.
|
|
461
|
+
name: 'order.completed',
|
|
462
462
|
payload: order.event_payload
|
|
463
463
|
)
|
|
464
464
|
end
|
|
@@ -477,9 +477,9 @@ end
|
|
|
477
477
|
Use the `emit_webhook_event` matcher (if available) or stub the events:
|
|
478
478
|
|
|
479
479
|
```ruby
|
|
480
|
-
it 'publishes order.
|
|
480
|
+
it 'publishes order.completed event' do
|
|
481
481
|
expect(Spree::Events).to receive(:publish).with(
|
|
482
|
-
'order.
|
|
482
|
+
'order.completed',
|
|
483
483
|
hash_including('id' => order.id)
|
|
484
484
|
)
|
|
485
485
|
|
|
@@ -506,7 +506,7 @@ Here's a complete example of a subscriber that sends alerts when inventory is lo
|
|
|
506
506
|
```ruby app/subscribers/my_app/inventory_alert_subscriber.rb
|
|
507
507
|
module MyApp
|
|
508
508
|
class InventoryAlertSubscriber < Spree::Subscriber
|
|
509
|
-
subscribes_to 'stock_item.
|
|
509
|
+
subscribes_to 'stock_item.updated'
|
|
510
510
|
|
|
511
511
|
LOW_STOCK_THRESHOLD = 10
|
|
512
512
|
|
|
@@ -79,9 +79,9 @@ If you previously added Subscribers for Events, eg. `order.completed` with code
|
|
|
79
79
|
```ruby
|
|
80
80
|
module MyApp
|
|
81
81
|
class OrderAuditSubscriber < Spree::Subscriber
|
|
82
|
-
subscribes_to 'order.
|
|
82
|
+
subscribes_to 'order.completed'
|
|
83
83
|
|
|
84
|
-
on 'order.
|
|
84
|
+
on 'order.completed', :log_order_completed
|
|
85
85
|
|
|
86
86
|
private
|
|
87
87
|
|