noticed 1.2.3 → 1.2.8

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: a17ac9f27bd6d18637bb5f033f47bf19b452b33c77647729c27ad2f38239c9b6
4
- data.tar.gz: ba7b0c12028132a9234ad32566dd005f870d8398db28287a7bdad6d53f59d79d
3
+ metadata.gz: 8612d56239114aba89f0a57365165e8e4eed4ca1a6291392099aede62ff5a852
4
+ data.tar.gz: 102c543b03ce37815165f056ce90c6c8ccb42e4cbe771a2e10b7d9c2d69d9245
5
5
  SHA512:
6
- metadata.gz: af37b4d82f2f327d57cd0cc18271f8d84f7e38842f868c7ace8bb0c9a626161d4eec53e0c00363f1d4b21aaf8a8793eb197af88b76ab5630987366aa4e1c78e1
7
- data.tar.gz: 733f9a0a51226f2ffce597c3f9e2ba36cb2befb62359c39f5f1b208e262d4900062753477bd5993f751db90e7c11218af050e1bd7c8295b9c7098eb0fb2907d7
6
+ metadata.gz: '094d06187629f0ea3faa41ccd570da239fcad6f94a5de59235d4afa87c623d62aa4635aadde71b7e3f8e6956e57b10e17efd0387005b779ca613e45f57918693'
7
+ data.tar.gz: 9b84a30978138d9bf9dbbb440f921fde3bd6b503055eb100d556247e4ca4e7dfdfa775912323f9df6a5103962b994d821ed17367c4e2357dd254567b7a6a1eda
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/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
 
@@ -29,9 +29,10 @@ module Noticed
29
29
  new(params)
30
30
  end
31
31
 
32
- def param(name)
33
- param_names.push(name)
32
+ def params(*names)
33
+ param_names.concat Array.wrap(names)
34
34
  end
35
+ alias param params
35
36
  end
36
37
 
37
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,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
@@ -19,7 +19,10 @@ module Noticed
19
19
  if (method = options[:format])
20
20
  notification.send(method)
21
21
  else
22
- notification.params.merge(recipient: recipient)
22
+ notification.params.merge(
23
+ recipient: recipient,
24
+ record: record
25
+ )
23
26
  end
24
27
  end
25
28
  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
 
@@ -2,7 +2,7 @@ module Noticed
2
2
  module DeliveryMethods
3
3
  class Twilio < Base
4
4
  def deliver
5
- HTTP.basic_auth(user: account_sid, pass: auth_token).post(url, json: format)
5
+ HTTP.basic_auth(user: account_sid, pass: auth_token).post(url, form: format)
6
6
  end
7
7
 
8
8
  private
@@ -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.3"
2
+ VERSION = "1.2.8"
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.3
4
+ version: 1.2.8
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-04 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