noticed 1.2.3 → 1.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +74 -25
- data/lib/generators/noticed/templates/notification.rb.tt +1 -1
- data/lib/noticed.rb +1 -0
- data/lib/noticed/delivery_methods/base.rb +2 -1
- data/lib/noticed/delivery_methods/email.rb +4 -1
- 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: 2936d217d3d881874888d8dd68280ecc9888f41fc1edf3f195e713bf863d2233
|
4
|
+
data.tar.gz: a73fba77cb4eebead7c3d343c501cbe7ca5b19db5a24b11b6d33e6674c41a1f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 67f055f8a05d3418caf3c21c2a3938341705752e6154284bbe596ac31563fd715c7d29d5651f690ce310433d762b7dca05e99ec8326b93d477a85db91cddfd10
|
7
|
+
data.tar.gz: 3fbaf13b4dd6f7f3fa9f50a52849ef0ff112268c730cdca202f2e421fd7f66cdc797147c549911addb2161ed2c40d7738d6d85e5d420427f46658eaef5b13b81
|
data/README.md
CHANGED
@@ -11,11 +11,20 @@ Currently, we support these notification delivery methods out of the box:
|
|
11
11
|
* Database
|
12
12
|
* Email
|
13
13
|
* ActionCable channels
|
14
|
+
* Slack
|
14
15
|
* Twilio (SMS)
|
15
16
|
* Vonage / Nexmo (SMS)
|
16
17
|
|
17
18
|
And you can easily add new notification types for any other delivery methods.
|
18
19
|
|
20
|
+
## 🎬 Screencast
|
21
|
+
|
22
|
+
<div style="width:50%">
|
23
|
+
<a href="https://www.youtube.com/watch?v=Scffi4otlFc"><img src="https://i.imgur.com/UvVKWwD.png" title="How to add Notifications to Rails with Noticed" /></a>
|
24
|
+
</div>
|
25
|
+
|
26
|
+
[Watch Screencast](https://www.youtube.com/watch?v=Scffi4otlFc)
|
27
|
+
|
19
28
|
## 🚀 Installation
|
20
29
|
Run the following command to add Noticed to your Gemfile
|
21
30
|
|
@@ -37,6 +46,28 @@ To generate a notification object, simply run:
|
|
37
46
|
|
38
47
|
`rails generate noticed:notification CommentNotification`
|
39
48
|
|
49
|
+
#### Sending Notifications
|
50
|
+
|
51
|
+
To send a notification to a user:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
# Instantiate a new notification
|
55
|
+
notification = CommentNotification.with(comment: @comment)
|
56
|
+
|
57
|
+
# Deliver notification in background job
|
58
|
+
notification.deliver_later(@comment.post.author)
|
59
|
+
|
60
|
+
# Deliver notification immediately
|
61
|
+
notification.deliver(@comment.post.author)
|
62
|
+
|
63
|
+
# Deliver notification to multiple recipients
|
64
|
+
notification.deliver_later(User.all)
|
65
|
+
```
|
66
|
+
|
67
|
+
This will instantiate a new notification with the `comment` stored in the notification's params.
|
68
|
+
|
69
|
+
Each delivery method is able to transform this metadata that's best for the format. For example, the database may simply store the comment so it can be linked when rendering in the navbar. The websocket mechanism may transform this into a browser notification or insert it into the navbar.
|
70
|
+
|
40
71
|
#### Notification Objects
|
41
72
|
|
42
73
|
Notifications inherit from `Noticed::Base`. This provides all their functionality and allows them to be delivered.
|
@@ -49,10 +80,6 @@ class CommentNotification < Noticed::Base
|
|
49
80
|
deliver_by :action_cable
|
50
81
|
deliver_by :email, if: :email_notifications?
|
51
82
|
|
52
|
-
def email_notifications?
|
53
|
-
!!recipient.preferences[:email]
|
54
|
-
end
|
55
|
-
|
56
83
|
# I18n helpers
|
57
84
|
def message
|
58
85
|
t(".message")
|
@@ -63,33 +90,16 @@ class CommentNotification < Noticed::Base
|
|
63
90
|
post_path(params[:post])
|
64
91
|
end
|
65
92
|
|
93
|
+
def email_notifications?
|
94
|
+
!!recipient.preferences[:email]
|
95
|
+
end
|
96
|
+
|
66
97
|
after_deliver do
|
67
98
|
# Anything you want
|
68
99
|
end
|
69
100
|
end
|
70
101
|
```
|
71
102
|
|
72
|
-
#### Sending Notifications
|
73
|
-
|
74
|
-
To send a notification to a user:
|
75
|
-
|
76
|
-
```ruby
|
77
|
-
notification = CommentNotification.with(comment: @comment.to_gid)
|
78
|
-
|
79
|
-
# Deliver notification in background job
|
80
|
-
notification.deliver_later(@comment.post.author)
|
81
|
-
|
82
|
-
# Deliver notification immediately
|
83
|
-
notification.deliver(@comment.post.author)
|
84
|
-
|
85
|
-
# Deliver notification to multiple recipients
|
86
|
-
notification.deliver_later(User.all)
|
87
|
-
```
|
88
|
-
|
89
|
-
This will instantiate a new notification with the `comment` stored in the notification's params.
|
90
|
-
|
91
|
-
Each delivery method is able to transform this metadata that's best for the format. For example, the database may simply store the comment so it can be linked when rendering in the navbar. The websocket mechanism may transform this into a browser notification or insert it into the navbar.
|
92
|
-
|
93
103
|
**Shared Options**
|
94
104
|
|
95
105
|
* `if: :method_name` - Calls `method_name`and cancels delivery method if `false` is returned
|
@@ -352,6 +362,45 @@ For Rails 6.0 and earlier, you must pass strings of the class names in the `deli
|
|
352
362
|
|
353
363
|
We recommend the Rails 6.0 compatible options to prevent confusion.
|
354
364
|
|
365
|
+
### 📦 Database Model
|
366
|
+
|
367
|
+
The Notification database model includes several helpful features to make working with database notifications easier.
|
368
|
+
|
369
|
+
#### Class methods
|
370
|
+
|
371
|
+
Sorting notifications by newest first:
|
372
|
+
|
373
|
+
```ruby
|
374
|
+
user.notifications.newest_first
|
375
|
+
```
|
376
|
+
|
377
|
+
Marking all notifications as read:
|
378
|
+
|
379
|
+
```ruby
|
380
|
+
user.notifications.mark_as_read!
|
381
|
+
```
|
382
|
+
|
383
|
+
#### Instance methods
|
384
|
+
|
385
|
+
Convert back into a Noticed notification object:
|
386
|
+
|
387
|
+
```ruby
|
388
|
+
@notification.to_notification
|
389
|
+
```
|
390
|
+
|
391
|
+
Mark notification as read:
|
392
|
+
|
393
|
+
```ruby
|
394
|
+
@notification.mark_as_read!
|
395
|
+
```
|
396
|
+
|
397
|
+
Check if read / unread:
|
398
|
+
|
399
|
+
```ruby
|
400
|
+
@notification.read?
|
401
|
+
@notification.unread?
|
402
|
+
```
|
403
|
+
|
355
404
|
## 🙏 Contributing
|
356
405
|
|
357
406
|
This project uses [Standard](https://github.com/testdouble/standard) for formatting Ruby code. Please make sure to run `standardrb` before submitting pull requests.
|
data/lib/noticed.rb
CHANGED
@@ -4,12 +4,13 @@ module Noticed
|
|
4
4
|
extend ActiveModel::Callbacks
|
5
5
|
define_model_callbacks :deliver
|
6
6
|
|
7
|
-
attr_reader :notification, :options, :recipient
|
7
|
+
attr_reader :notification, :options, :recipient, :record
|
8
8
|
|
9
9
|
def perform(notification_class:, options:, params:, recipient:, record:)
|
10
10
|
@notification = notification_class.constantize.new(params)
|
11
11
|
@options = options
|
12
12
|
@recipient = recipient
|
13
|
+
@record = record
|
13
14
|
|
14
15
|
# Make notification aware of database record and recipient during delivery
|
15
16
|
@notification.record = record
|
data/lib/noticed/version.rb
CHANGED