noticed 1.2.2 → 1.2.7

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: 534fd7403e6eb6ad98672132d5c2b3ebf6de0eff7ada3a40ad9acc2bea2b7f2b
4
- data.tar.gz: 18973d1ff94fee48cc3daadd7a1e5df9bc104153dcd6ee10d9b5cfe98df832be
3
+ metadata.gz: fcd5afdca1ab2862bca9259b3015f86d93ab323eb64b6f8de666f9067216b202
4
+ data.tar.gz: 8c6a53d533eb2667c44804b4a7a5e518b4ab7938b345c6105490145bb4fdb75a
5
5
  SHA512:
6
- metadata.gz: 67cc5fe29f00612c81b64f70fd0b72d264d7f9d3486b14d0892a6482ac1ca75f06303d2bfd8aebfa349d145a269295b06e9530105ca2578d85d148ac5665eefb
7
- data.tar.gz: b66fa2922599a8edb40bdddf3a9a2a05debdfde299877140909fcb339895a0c94b2fa30c2a9d693026a4c040fcbd0c2cb1fdacaefcb328c845783694b04c4100
6
+ metadata.gz: 21ea1e549dcbd0857fea6538a0fc2d17ba5f0d249b7f39d1e1d33a56cbece35a3413605d8c1a6e71a381a424b6db93c82bfe7de19d87e3e669c569926fa4a42b
7
+ data.tar.gz: 1e85eb2edfbbac61fa9bcacf91a4fe041adc945c54ad029a6e8ab75e3d4fc64d52fbcf8527e43da9d0fd3c30e267c2c1e13bafbdf10662f6f826758a1a4f3f41
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.
data/Rakefile CHANGED
@@ -18,6 +18,10 @@ require "bundler/gem_tasks"
18
18
 
19
19
  require "rake/testtask"
20
20
 
21
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
22
+ load "rails/tasks/engine.rake"
23
+ load "rails/tasks/statistics.rake"
24
+
21
25
  Rake::TestTask.new(:test) do |t|
22
26
  t.libs << "test"
23
27
  t.pattern = "test/**/*_test.rb"
@@ -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 = {})
@@ -28,9 +29,10 @@ module Noticed
28
29
  new(params)
29
30
  end
30
31
 
31
- def param(name)
32
- param_names.push(name)
32
+ def params(*names)
33
+ param_names.concat Array.wrap(names)
33
34
  end
35
+ alias param params
34
36
  end
35
37
 
36
38
  def initialize(params = {})
@@ -1,11 +1,19 @@
1
1
  module Noticed
2
2
  class Coder
3
3
  def self.load(data)
4
+ return if data.nil?
5
+
6
+ # Text columns need JSON parsing
7
+ if data.is_a?(String)
8
+ data = JSON.parse(data)
9
+ end
10
+
4
11
  ActiveJob::Arguments.send(:deserialize_argument, data)
5
12
  end
6
13
 
7
14
  def self.dump(data)
8
- ActiveJob::Arguments.send(:serialize_argument, data)
15
+ return if data.nil?
16
+ ActiveJob::Arguments.send(:serialize_argument, data).to_json
9
17
  end
10
18
  end
11
19
  end
@@ -16,11 +16,19 @@ module Noticed
16
16
  end
17
17
 
18
18
  def channel
19
- if (method = options[:channel])
20
- notification.send(method)
21
- else
22
- Noticed::NotificationChannel
23
- end
19
+ @channel ||= begin
20
+ value = options[:channel]
21
+ case value
22
+ when String
23
+ value.constantize
24
+ when Symbol
25
+ notification.send(value)
26
+ when Class
27
+ value
28
+ else
29
+ Noticed::NotificationChannel
30
+ end
31
+ end
24
32
  end
25
33
  end
26
34
  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
 
@@ -13,7 +13,7 @@ module Translation
13
13
 
14
14
  def scope_translation_key(key)
15
15
  if key.to_s.start_with?(".")
16
- "notifications.#{self.class.name.underscore}#{key}"
16
+ "#{i18n_scope}.#{self.class.name.underscore}#{key}"
17
17
  else
18
18
  key
19
19
  end
@@ -1,3 +1,3 @@
1
1
  module Noticed
2
- VERSION = "1.2.2"
2
+ VERSION = "1.2.7"
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.2
4
+ version: 1.2.7
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-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails