noticed 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +50 -5
- data/lib/noticed/base.rb +20 -17
- data/lib/noticed/delivery_methods/base.rb +6 -2
- data/lib/noticed/delivery_methods/database.rb +1 -1
- data/lib/noticed/delivery_methods/test.rb +6 -0
- data/lib/noticed/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b8ce628657c8b6011616fbcf075869fe902ad33f19569acadf88e52aef89fb79
|
4
|
+
data.tar.gz: f92330969818566ce0d4080341f19631be41fd5bb4c45f94dfb3ab5714c3c663
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8945414df8ed4cdf89bbf654171e686b8c655ae91542177207f134052d0d09794df146192694775f9e755a3b0a65fafbe180130d41590f186b53837c9f114961
|
7
|
+
data.tar.gz: c78d5048f9fb95b6982dc44edd7fa03d8f2cf8cf093884d059668fb2aed3258d9f2992b84b2843ac61e39d3e3ce42818fc48c583520d2097cb89bf8260b1aa30
|
data/README.md
CHANGED
@@ -42,6 +42,10 @@ class CommentNotification < Noticed::Base
|
|
42
42
|
def email_notifications?
|
43
43
|
!!recipient.preferences[:email]
|
44
44
|
end
|
45
|
+
|
46
|
+
after_deliver do
|
47
|
+
# Anything you want
|
48
|
+
end
|
45
49
|
end
|
46
50
|
```
|
47
51
|
|
@@ -66,6 +70,35 @@ Each delivery method is able to transfrom this metadata that's best for the form
|
|
66
70
|
* `if: :method_name` - Calls `method_name`and cancels delivery method if `false` is returned
|
67
71
|
* `unless: :method_name` - Calls `method_name`and cancels delivery method if `true` is returned
|
68
72
|
|
73
|
+
**Callbacks**
|
74
|
+
|
75
|
+
Like ActiveRecord, notifications have several different types of callbacks.
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
class CommentNotification < Noticed::Base
|
79
|
+
deliver_by :database
|
80
|
+
deliver_by :email
|
81
|
+
|
82
|
+
# Callbacks for the entire delivery
|
83
|
+
before_deliver :whatever
|
84
|
+
around_deliver :whatever
|
85
|
+
after_deliver :whatever
|
86
|
+
|
87
|
+
# Callbacks for each delivery method
|
88
|
+
before_database :whatever
|
89
|
+
around_database :whatever
|
90
|
+
after_database :whatever
|
91
|
+
|
92
|
+
before_email :whatever
|
93
|
+
around_email :whatever
|
94
|
+
after_email :whatever
|
95
|
+
end
|
96
|
+
```
|
97
|
+
|
98
|
+
When using `deliver_later` callbacks will be run around queuing the delivery method jobs (not inside the jobs as they actually execute).
|
99
|
+
|
100
|
+
Defining custom delivery methods allows you to add callbacks that run inside the background job as each individual delivery is executed. See the Custom Delivery Methods section for more information.
|
101
|
+
|
69
102
|
## Delivery Methods
|
70
103
|
|
71
104
|
The delivery methods are designed to be overriden so that you can customi1ze the notification for each medium.
|
@@ -86,7 +119,7 @@ Sends an email notification. Emails will always be sent with `deliver_later`
|
|
86
119
|
|
87
120
|
`deliver_by :email, mailer: "UserMailer"`
|
88
121
|
|
89
|
-
|
122
|
+
##### Options
|
90
123
|
|
91
124
|
* `mailer` - **Required**
|
92
125
|
|
@@ -102,7 +135,7 @@ Sends a notification to the browser via websockets (ActionCable channel by defau
|
|
102
135
|
|
103
136
|
`deliver_by :action_cable`
|
104
137
|
|
105
|
-
|
138
|
+
##### Options
|
106
139
|
|
107
140
|
* `format: :format_for_action_cable` - *Optional*
|
108
141
|
|
@@ -120,7 +153,7 @@ Sends a Slack notification via webhook.
|
|
120
153
|
|
121
154
|
`deliver_by :slack`
|
122
155
|
|
123
|
-
|
156
|
+
##### Options
|
124
157
|
|
125
158
|
* `format: :format_for_slack` - *Optional*
|
126
159
|
|
@@ -138,7 +171,7 @@ Sends an SMS notification via Twilio.
|
|
138
171
|
|
139
172
|
`deliver_by :twilio`
|
140
173
|
|
141
|
-
|
174
|
+
##### Options
|
142
175
|
|
143
176
|
* `credentials: :get_twilio_credentials` - *Optional*
|
144
177
|
|
@@ -172,7 +205,7 @@ Sends an SMS notification vai Vonage / Nexmo.
|
|
172
205
|
|
173
206
|
`deliver_by :vonage`
|
174
207
|
|
175
|
-
|
208
|
+
##### Options
|
176
209
|
|
177
210
|
* `credentials: :get_credentials` - *Optional*
|
178
211
|
|
@@ -236,6 +269,18 @@ Delivery methods have access to the following methods and attributes:
|
|
236
269
|
* `recipient` - The object who should receive the notification. This is typically a User, Account, or other ActiveRecord model.
|
237
270
|
* `params` - The params passed into the notification. This is details about the event that happened. For example, a user commenting on a post would have params of `{ user: User.first }`
|
238
271
|
|
272
|
+
#### Callbacks
|
273
|
+
|
274
|
+
Callbacks for delivery methods wrap the *actual* delivery of the notification. You can use `before_deliver`, `around_deliver` and `after_deliver` in your custom delivery methods.
|
275
|
+
|
276
|
+
```ruby
|
277
|
+
class DiscordNotification < Noticed::DeliveryMethods::Base
|
278
|
+
after_deliver do
|
279
|
+
# Do whatever you want
|
280
|
+
end
|
281
|
+
end
|
282
|
+
```
|
283
|
+
|
239
284
|
#### Limitations
|
240
285
|
|
241
286
|
Rails 6.1+ can serialize Class and Module objects as arguments to ActiveJob. The following syntax should work for Rails 6.1+:
|
data/lib/noticed/base.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
module Noticed
|
2
2
|
class Base
|
3
|
+
extend ActiveModel::Callbacks
|
4
|
+
define_model_callbacks :deliver
|
5
|
+
|
3
6
|
class_attribute :delivery_methods, instance_writer: false, default: []
|
4
7
|
class_attribute :param_names, instance_writer: false, default: []
|
5
8
|
|
@@ -7,10 +10,8 @@ module Noticed
|
|
7
10
|
|
8
11
|
class << self
|
9
12
|
def deliver_by(name, options = {})
|
10
|
-
delivery_methods.push(
|
11
|
-
|
12
|
-
options: options
|
13
|
-
)
|
13
|
+
delivery_methods.push(name: name, options: options)
|
14
|
+
define_model_callbacks(name)
|
14
15
|
end
|
15
16
|
|
16
17
|
# Copy delivery methods from parent
|
@@ -51,16 +52,18 @@ module Noticed
|
|
51
52
|
|
52
53
|
# Runs all delivery methods for a notification
|
53
54
|
def run_delivery(recipient, enqueue: true)
|
54
|
-
|
55
|
+
run_callbacks :deliver do
|
56
|
+
delivery_methods = self.class.delivery_methods.dup
|
55
57
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
58
|
+
# Run database delivery inline first if it exists so other methods have access to the record
|
59
|
+
if (index = delivery_methods.find_index { |m| m[:name] == :database })
|
60
|
+
delivery_method = delivery_methods.delete_at(index)
|
61
|
+
@record = run_delivery_method(delivery_method, recipient: recipient, enqueue: false)
|
62
|
+
end
|
61
63
|
|
62
|
-
|
63
|
-
|
64
|
+
delivery_methods.each do |delivery_method|
|
65
|
+
run_delivery_method(delivery_method, recipient: recipient, enqueue: enqueue)
|
66
|
+
end
|
64
67
|
end
|
65
68
|
end
|
66
69
|
|
@@ -77,15 +80,15 @@ module Noticed
|
|
77
80
|
record: record
|
78
81
|
}
|
79
82
|
|
80
|
-
|
81
|
-
|
83
|
+
run_callbacks delivery_method[:name] do
|
84
|
+
klass = get_class(delivery_method[:name], delivery_method[:options])
|
85
|
+
enqueue ? klass.perform_later(args) : klass.perform_now(args)
|
86
|
+
end
|
82
87
|
end
|
83
88
|
|
84
89
|
# Retrieves the correct class for a delivery method
|
85
90
|
def get_class(name, options)
|
86
|
-
if
|
87
|
-
name
|
88
|
-
elsif options[:class]
|
91
|
+
if options[:class]
|
89
92
|
options[:class].constantize
|
90
93
|
else
|
91
94
|
"Noticed::DeliveryMethods::#{name.to_s.classify}".constantize
|
@@ -1,8 +1,10 @@
|
|
1
1
|
module Noticed
|
2
2
|
module DeliveryMethods
|
3
3
|
class Base < Noticed.parent_class.constantize
|
4
|
+
extend ActiveModel::Callbacks
|
5
|
+
define_model_callbacks :deliver
|
6
|
+
|
4
7
|
attr_reader :notification, :options, :recipient
|
5
|
-
delegate :params, to: :notification
|
6
8
|
|
7
9
|
def perform(notification_class:, options:, params:, recipient:, record:)
|
8
10
|
@notification = notification_class.constantize.new(params)
|
@@ -12,7 +14,9 @@ module Noticed
|
|
12
14
|
# Keep track of the database record for rendering
|
13
15
|
@notification.record = record
|
14
16
|
|
15
|
-
deliver
|
17
|
+
run_callbacks :deliver do
|
18
|
+
deliver
|
19
|
+
end
|
16
20
|
end
|
17
21
|
|
18
22
|
def deliver
|
@@ -2,9 +2,15 @@ module Noticed
|
|
2
2
|
module DeliveryMethods
|
3
3
|
class Test < Base
|
4
4
|
class_attribute :delivered, default: []
|
5
|
+
class_attribute :callbacks, default: []
|
6
|
+
|
7
|
+
after_deliver do
|
8
|
+
self.class.callbacks << :after
|
9
|
+
end
|
5
10
|
|
6
11
|
def self.clear!
|
7
12
|
delivered.clear
|
13
|
+
callbacks.clear
|
8
14
|
end
|
9
15
|
|
10
16
|
def deliver
|
data/lib/noticed/version.rb
CHANGED