noticed 1.2.0 → 1.2.5

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: 5c8f1abe201518056e73f223b840bb30f44e173181116c632217f5f5a4a4b4d5
4
- data.tar.gz: ba23af57eba01de7aecafd23c1ed00efacb8f354402592c68967f9f13c83a376
3
+ metadata.gz: 3a1534dc87f0c5a0ff1178c1eafc8babae3e4cd49f3f902311a4e7db9672ed77
4
+ data.tar.gz: a5e7f177a02369bc82a1ba3c5989df6b86ecfca2274c9f6905c8f14ca2e7537e
5
5
  SHA512:
6
- metadata.gz: 32fcb3681980b2e0ad9db0cbd26e47c8ff8c4b4524f5c1068da010b967c46524dcee78ec3809e622791768a1be08793fc7c159dae061e028ff3e707f89fe6f47
7
- data.tar.gz: f1d9d8318f4acb4ea77c8940f4e71bd91dd3ea40b70873741246109c472fc5164a5aa6be67fb22089b2bc6868601497c417ffb88cdb2e6ab92d1c195ab69ee58
6
+ metadata.gz: b6d5ae8fc80b975db5aaa09d33e9c35b55a0f8b02aee37cfc73c226357d3f80068d109b034006c563a19b581aa3a8b8d8600070e52a2e1eabe5d83ab76beb33e
7
+ data.tar.gz: 4f78b2bb15d8bf64f094b60905b5225abd431104bfb25271163748e9702cedac64bfd15640e9b3898f35f7375c57f8619ab48ccedc8d1f0cba6483fd683e9463
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,30 +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
-
86
- This will instantiate a new notification with the `comment` stored in the notification's params.
87
-
88
- 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.
89
-
90
103
  **Shared Options**
91
104
 
92
105
  * `if: :method_name` - Calls `method_name`and cancels delivery method if `false` is returned
@@ -131,7 +144,7 @@ Defining custom delivery methods allows you to add callbacks that run inside the
131
144
 
132
145
  ##### Translations
133
146
 
134
- 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.
135
148
 
136
149
  For example:
137
150
 
@@ -153,8 +166,6 @@ class CommentNotification < Noticed::Base
153
166
  end
154
167
  ```
155
168
 
156
- ###
157
-
158
169
  ## 🚛 Delivery Methods
159
170
 
160
171
  The delivery methods are designed to be modular so you can customize the way each type gets delivered.
@@ -175,6 +186,10 @@ Writes notification to the database.
175
186
 
176
187
  The name of the database association to use. Defaults to `:notifications`
177
188
 
189
+ * `format: :format_for_database` - *Optional*
190
+
191
+ Use a custom method to define the attributes saved to the database
192
+
178
193
  ### Email
179
194
 
180
195
  Sends an email notification. Emails will always be sent with `deliver_later`
@@ -191,6 +206,10 @@ Sends an email notification. Emails will always be sent with `deliver_later`
191
206
 
192
207
  Used to customize the method on the mailer that is called
193
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
+
194
213
  ### ActionCable
195
214
 
196
215
  Sends a notification to the browser via websockets (ActionCable channel by default).
@@ -263,7 +282,7 @@ Sends an SMS notification via Twilio.
263
282
 
264
283
  ### Vonage SMS
265
284
 
266
- Sends an SMS notification vai Vonage / Nexmo.
285
+ Sends an SMS notification via Vonage / Nexmo.
267
286
 
268
287
  `deliver_by :vonage`
269
288
 
@@ -343,6 +362,45 @@ For Rails 6.0 and earlier, you must pass strings of the class names in the `deli
343
362
 
344
363
  We recommend the Rails 6.0 compatible options to prevent confusion.
345
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
+
346
404
  ## 🙏 Contributing
347
405
 
348
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 = {})
@@ -37,14 +38,24 @@ module Noticed
37
38
  @params = params
38
39
  end
39
40
 
40
- def deliver(recipient)
41
+ def deliver(recipients)
41
42
  validate!
42
- run_delivery(recipient, enqueue: false)
43
+
44
+ run_callbacks :deliver do
45
+ Array.wrap(recipients).uniq.each do |recipient|
46
+ run_delivery(recipient, enqueue: false)
47
+ end
48
+ end
43
49
  end
44
50
 
45
- def deliver_later(recipient)
51
+ def deliver_later(recipients)
46
52
  validate!
47
- run_delivery(recipient, enqueue: true)
53
+
54
+ run_callbacks :deliver do
55
+ Array.wrap(recipients).uniq.each do |recipient|
56
+ run_delivery(recipient, enqueue: true)
57
+ end
58
+ end
48
59
  end
49
60
 
50
61
  def params
@@ -55,18 +66,16 @@ module Noticed
55
66
 
56
67
  # Runs all delivery methods for a notification
57
68
  def run_delivery(recipient, enqueue: true)
58
- run_callbacks :deliver do
59
- delivery_methods = self.class.delivery_methods.dup
69
+ delivery_methods = self.class.delivery_methods.dup
60
70
 
61
- # Run database delivery inline first if it exists so other methods have access to the record
62
- if (index = delivery_methods.find_index { |m| m[:name] == :database })
63
- delivery_method = delivery_methods.delete_at(index)
64
- @record = run_delivery_method(delivery_method, recipient: recipient, enqueue: false)
65
- end
71
+ # Run database delivery inline first if it exists so other methods have access to the record
72
+ if (index = delivery_methods.find_index { |m| m[:name] == :database })
73
+ delivery_method = delivery_methods.delete_at(index)
74
+ @record = run_delivery_method(delivery_method, recipient: recipient, enqueue: false)
75
+ end
66
76
 
67
- delivery_methods.each do |delivery_method|
68
- run_delivery_method(delivery_method, recipient: recipient, enqueue: enqueue)
69
- end
77
+ delivery_methods.each do |delivery_method|
78
+ run_delivery_method(delivery_method, recipient: recipient, enqueue: enqueue)
70
79
  end
71
80
  end
72
81
 
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Noticed
2
- VERSION = "1.2.0"
2
+ VERSION = "1.2.5"
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.0
4
+ version: 1.2.5
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