noticed 1.2.1 → 1.2.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a50bd43b89379867d1a76114bf3d306a9f7bb983e87ba9d8c293b34c5ed5fe7f
4
- data.tar.gz: 2ac5422b2de7a9b4a7cde0ceddb2fb89eef13ed641fba3d4bff2d251a34fbfba
3
+ metadata.gz: ac839e0194ed6ffcb49cdded96384b5e47aeed9dbd407e92b7e18d67d700efcb
4
+ data.tar.gz: 4f8bea6c1e9b7b7c1d841a4110bce06a6db56aaa36db358d6ed129294268e6a8
5
5
  SHA512:
6
- metadata.gz: 856584e1f6045ca6db3badc0611066fcb709754008ec28806b4cf56b6be1f5ad62d7f6e8ee47ee181b446c19af1080e6352e2b551a8adf65e51a4d05e25d1c22
7
- data.tar.gz: 2f4b8aa9183bbca49af5228f309a48448ffda1abf5ad302b405c1cafb43b82039a25b485c8d59dab4fdfe69242f247867cbdc6d4dad6fb21d7e4f09a7e1c015e
6
+ metadata.gz: 5d5b49fa5aabd5e94ad370938f157e0ee55e28727b1f1da221924c2d5de40e629f29f909e9f77caa2908dad316777e6f7a66be1c6c290f5f013d3e9407781b28
7
+ data.tar.gz: 5db5ca0a303953ea1b82d6bf2d1ab6948e4bcbde8b6cb56c73e1a286732239cdeeb7d77b677d40ed77fa6d801de1069c3882b2006616f99f288f97c2e0d1a958
data/README.md CHANGED
@@ -4,18 +4,27 @@
4
4
 
5
5
  ### 🎉 Notifications for your Ruby on Rails app.
6
6
 
7
- [![Build Status](https://github.com/excid3/noticed/workflows/Tests/badge.svg)](https://github.com/excid3/noticed/actions)
7
+ [![Build Status](https://github.com/excid3/noticed/workflows/Tests/badge.svg)](https://github.com/excid3/noticed/actions) [![Gem Version](https://badge.fury.io/rb/noticed.svg)](https://badge.fury.io/rb/noticed)
8
8
 
9
9
  Currently, we support these notification delivery methods out of the box:
10
10
 
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
@@ -134,7 +144,7 @@ Defining custom delivery methods allows you to add callbacks that run inside the
134
144
 
135
145
  ##### Translations
136
146
 
137
- We've added `translate` and `t` helpers like Rails has to provide an easy way of scoping translations. If the key starts with a period, it will automatically scope the key under `notifications` and the underscored name of the notification class it is used in.
147
+ We've added `translate` and `t` helpers like Rails has to provide an easy way of scoping translations. If the key starts with a period, it will automatically scope the key under `notifications` and the underscored name of the notification class it is used in.
138
148
 
139
149
  For example:
140
150
 
@@ -176,6 +186,10 @@ Writes notification to the database.
176
186
 
177
187
  The name of the database association to use. Defaults to `:notifications`
178
188
 
189
+ * `format: :format_for_database` - *Optional*
190
+
191
+ Use a custom method to define the attributes saved to the database
192
+
179
193
  ### Email
180
194
 
181
195
  Sends an email notification. Emails will always be sent with `deliver_later`
@@ -192,6 +206,10 @@ Sends an email notification. Emails will always be sent with `deliver_later`
192
206
 
193
207
  Used to customize the method on the mailer that is called
194
208
 
209
+ * `format: :format_for_email` - *Optional*
210
+
211
+ Use a custom method to define the params sent to the mailer. `recipient` will be merged into the params.
212
+
195
213
  ### ActionCable
196
214
 
197
215
  Sends a notification to the browser via websockets (ActionCable channel by default).
@@ -264,7 +282,7 @@ Sends an SMS notification via Twilio.
264
282
 
265
283
  ### Vonage SMS
266
284
 
267
- Sends an SMS notification vai Vonage / Nexmo.
285
+ Sends an SMS notification via Vonage / Nexmo.
268
286
 
269
287
  `deliver_by :vonage`
270
288
 
@@ -344,6 +362,45 @@ For Rails 6.0 and earlier, you must pass strings of the class names in the `deli
344
362
 
345
363
  We recommend the Rails 6.0 compatible options to prevent confusion.
346
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
+
347
404
  ## 🙏 Contributing
348
405
 
349
406
  This project uses [Standard](https://github.com/testdouble/standard) for formatting Ruby code. Please make sure to run `standardrb` before submitting pull requests.
@@ -22,6 +22,6 @@ class <%= class_name %> < Noticed::Base
22
22
  # end
23
23
  #
24
24
  # def url
25
- # posts_path(params[:post])
25
+ # post_path(params[:post])
26
26
  # end
27
27
  end
@@ -1,3 +1,4 @@
1
+ require "active_job/arguments"
1
2
  require "http"
2
3
  require "noticed/engine"
3
4
 
@@ -9,7 +9,8 @@ module Noticed
9
9
  class_attribute :delivery_methods, instance_writer: false, default: []
10
10
  class_attribute :param_names, instance_writer: false, default: []
11
11
 
12
- attr_accessor :record
12
+ # Gives notifications access to the record and recipient when formatting for delivery
13
+ attr_accessor :record, :recipient
13
14
 
14
15
  class << self
15
16
  def deliver_by(name, options = {})
@@ -41,7 +42,7 @@ module Noticed
41
42
  validate!
42
43
 
43
44
  run_callbacks :deliver do
44
- Array.wrap(recipients).each do |recipient|
45
+ Array.wrap(recipients).uniq.each do |recipient|
45
46
  run_delivery(recipient, enqueue: false)
46
47
  end
47
48
  end
@@ -51,7 +52,7 @@ module Noticed
51
52
  validate!
52
53
 
53
54
  run_callbacks :deliver do
54
- Array.wrap(recipients).each do |recipient|
55
+ Array.wrap(recipients).uniq.each do |recipient|
55
56
  run_delivery(recipient, enqueue: true)
56
57
  end
57
58
  end
@@ -1,11 +1,13 @@
1
1
  module Noticed
2
2
  class Coder
3
3
  def self.load(data)
4
- ActiveJob::Arguments.send(:deserialize_argument, data)
4
+ return if data.nil?
5
+ ActiveJob::Arguments.send(:deserialize_argument, JSON.parse(data))
5
6
  end
6
7
 
7
8
  def self.dump(data)
8
- ActiveJob::Arguments.send(:serialize_argument, data)
9
+ return if data.nil?
10
+ ActiveJob::Arguments.send(:serialize_argument, data).to_json
9
11
  end
10
12
  end
11
13
  end
@@ -4,15 +4,17 @@ 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
- # Keep track of the database record for rendering
15
+ # Make notification aware of database record and recipient during delivery
15
16
  @notification.record = record
17
+ @notification.recipient = recipient
16
18
 
17
19
  run_callbacks :deliver do
18
20
  deliver
@@ -2,7 +2,7 @@ module Noticed
2
2
  module DeliveryMethods
3
3
  class Email < Base
4
4
  def deliver
5
- mailer.with(notification.params).send(method.to_sym).deliver_later
5
+ mailer.with(format).send(method.to_sym).deliver_later
6
6
  end
7
7
 
8
8
  private
@@ -14,6 +14,17 @@ module Noticed
14
14
  def method
15
15
  options[:method] || notification.class.name.underscore
16
16
  end
17
+
18
+ def format
19
+ if (method = options[:format])
20
+ notification.send(method)
21
+ else
22
+ notification.params.merge(
23
+ recipient: recipient,
24
+ record: record
25
+ )
26
+ end
27
+ end
17
28
  end
18
29
  end
19
30
  end
@@ -11,7 +11,7 @@ module Noticed
11
11
  if (method = options[:format])
12
12
  notification.send(method)
13
13
  else
14
- params
14
+ notification.params
15
15
  end
16
16
  end
17
17
 
@@ -1,3 +1,3 @@
1
1
  module Noticed
2
- VERSION = "1.2.1"
2
+ VERSION = "1.2.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: noticed
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Oliver
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-03 00:00:00.000000000 Z
11
+ date: 2020-08-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails