noticed 1.0.0 → 1.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +146 -45
- data/lib/generators/noticed/model_generator.rb +36 -0
- data/lib/generators/noticed/notification_generator.rb +19 -0
- data/lib/generators/noticed/templates/README +7 -0
- data/lib/generators/noticed/templates/notification.rb.tt +27 -0
- data/lib/noticed.rb +2 -0
- data/lib/noticed/base.rb +29 -14
- data/lib/noticed/delivery_methods/base.rb +8 -3
- data/lib/noticed/delivery_methods/database.rb +6 -2
- data/lib/noticed/delivery_methods/email.rb +9 -1
- data/lib/noticed/delivery_methods/test.rb +6 -0
- data/lib/noticed/model.rb +42 -0
- data/lib/noticed/translation.rb +21 -0
- data/lib/noticed/version.rb +1 -1
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a17ac9f27bd6d18637bb5f033f47bf19b452b33c77647729c27ad2f38239c9b6
|
4
|
+
data.tar.gz: ba7b0c12028132a9234ad32566dd005f870d8398db28287a7bdad6d53f59d79d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af37b4d82f2f327d57cd0cc18271f8d84f7e38842f868c7ace8bb0c9a626161d4eec53e0c00363f1d4b21aaf8a8793eb197af88b76ab5630987366aa4e1c78e1
|
7
|
+
data.tar.gz: 733f9a0a51226f2ffce597c3f9e2ba36cb2befb62359c39f5f1b208e262d4900062753477bd5993f751db90e7c11218af050e1bd7c8295b9c7098eb0fb2907d7
|
data/README.md
CHANGED
@@ -1,37 +1,47 @@
|
|
1
|
-
|
1
|
+
<p align="center">
|
2
|
+
<h1>Noticed</h1>
|
3
|
+
</p>
|
2
4
|
|
3
|
-
|
5
|
+
### 🎉 Notifications for your Ruby on Rails app.
|
6
|
+
|
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)
|
4
8
|
|
5
9
|
Currently, we support these notification delivery methods out of the box:
|
6
10
|
|
7
11
|
* Database
|
8
12
|
* Email
|
9
|
-
*
|
13
|
+
* ActionCable channels
|
10
14
|
* Twilio (SMS)
|
11
15
|
* Vonage / Nexmo (SMS)
|
12
16
|
|
13
17
|
And you can easily add new notification types for any other delivery methods.
|
14
18
|
|
15
|
-
## Installation
|
16
|
-
|
19
|
+
## 🚀 Installation
|
20
|
+
Run the following command to add Noticed to your Gemfile
|
17
21
|
|
18
22
|
```ruby
|
19
|
-
|
23
|
+
bundle add "noticed"
|
20
24
|
```
|
21
25
|
|
22
|
-
|
23
|
-
```bash
|
24
|
-
$ bundle
|
25
|
-
```
|
26
|
+
To save notifications to your database, use the following command to generate a Notification model.
|
26
27
|
|
27
|
-
|
28
|
-
|
29
|
-
$ gem install noticed
|
28
|
+
```ruby
|
29
|
+
rails generate noticed:model
|
30
30
|
```
|
31
31
|
|
32
|
-
|
32
|
+
This will generate a Notification model and instructions for associating User models with the notifications table.
|
33
|
+
|
34
|
+
## 📝 Usage
|
35
|
+
|
36
|
+
To generate a notification object, simply run:
|
37
|
+
|
38
|
+
`rails generate noticed:notification CommentNotification`
|
33
39
|
|
34
|
-
|
40
|
+
#### Notification Objects
|
41
|
+
|
42
|
+
Notifications inherit from `Noticed::Base`. This provides all their functionality and allows them to be delivered.
|
43
|
+
|
44
|
+
To add delivery methods, simply `include` the module for the delivery methods you would like to use.
|
35
45
|
|
36
46
|
```ruby
|
37
47
|
class CommentNotification < Noticed::Base
|
@@ -42,9 +52,25 @@ class CommentNotification < Noticed::Base
|
|
42
52
|
def email_notifications?
|
43
53
|
!!recipient.preferences[:email]
|
44
54
|
end
|
55
|
+
|
56
|
+
# I18n helpers
|
57
|
+
def message
|
58
|
+
t(".message")
|
59
|
+
end
|
60
|
+
|
61
|
+
# URL helpers are accessible in notifications
|
62
|
+
def url
|
63
|
+
post_path(params[:post])
|
64
|
+
end
|
65
|
+
|
66
|
+
after_deliver do
|
67
|
+
# Anything you want
|
68
|
+
end
|
45
69
|
end
|
46
70
|
```
|
47
71
|
|
72
|
+
#### Sending Notifications
|
73
|
+
|
48
74
|
To send a notification to a user:
|
49
75
|
|
50
76
|
```ruby
|
@@ -55,20 +81,84 @@ notification.deliver_later(@comment.post.author)
|
|
55
81
|
|
56
82
|
# Deliver notification immediately
|
57
83
|
notification.deliver(@comment.post.author)
|
84
|
+
|
85
|
+
# Deliver notification to multiple recipients
|
86
|
+
notification.deliver_later(User.all)
|
58
87
|
```
|
59
88
|
|
60
|
-
This will instantiate a new notification with the `comment`
|
89
|
+
This will instantiate a new notification with the `comment` stored in the notification's params.
|
61
90
|
|
62
|
-
Each delivery method is able to
|
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.
|
63
92
|
|
64
93
|
**Shared Options**
|
65
94
|
|
66
95
|
* `if: :method_name` - Calls `method_name`and cancels delivery method if `false` is returned
|
67
96
|
* `unless: :method_name` - Calls `method_name`and cancels delivery method if `true` is returned
|
68
97
|
|
69
|
-
|
98
|
+
##### Helper Methods
|
99
|
+
|
100
|
+
You can define helper methods inside your Notification object to make it easier to render.
|
101
|
+
|
102
|
+
##### URL Helpers
|
103
|
+
|
104
|
+
Rails url helpers are included in notification classes by default so you have full access to them just like you would in your controllers and views.
|
105
|
+
|
106
|
+
**Callbacks**
|
107
|
+
|
108
|
+
Like ActiveRecord, notifications have several different types of callbacks.
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
class CommentNotification < Noticed::Base
|
112
|
+
deliver_by :database
|
113
|
+
deliver_by :email
|
114
|
+
|
115
|
+
# Callbacks for the entire delivery
|
116
|
+
before_deliver :whatever
|
117
|
+
around_deliver :whatever
|
118
|
+
after_deliver :whatever
|
119
|
+
|
120
|
+
# Callbacks for each delivery method
|
121
|
+
before_database :whatever
|
122
|
+
around_database :whatever
|
123
|
+
after_database :whatever
|
124
|
+
|
125
|
+
before_email :whatever
|
126
|
+
around_email :whatever
|
127
|
+
after_email :whatever
|
128
|
+
end
|
129
|
+
```
|
130
|
+
|
131
|
+
When using `deliver_later` callbacks will be run around queuing the delivery method jobs (not inside the jobs as they actually execute).
|
132
|
+
|
133
|
+
Defining custom delivery methods allows you to add callbacks that run inside the background job as each individual delivery is executed. See the Custom Delivery Methods section for more information.
|
134
|
+
|
135
|
+
##### Translations
|
136
|
+
|
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.
|
138
|
+
|
139
|
+
For example:
|
140
|
+
|
141
|
+
`t(".message")` looks up `en.notifications.new_comment.message`
|
142
|
+
|
143
|
+
##### User Preferences
|
144
|
+
|
145
|
+
You can use the `if:` and `unless: ` options on your delivery methods to check the user's preferences and skip processing if they have disabled that type of notification.
|
146
|
+
|
147
|
+
For example:
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
class CommentNotification < Noticed::Base
|
151
|
+
deliver_by :email, if: :email_notifications?
|
152
|
+
|
153
|
+
def email_notifications?
|
154
|
+
recipient.email_notifications?
|
155
|
+
end
|
156
|
+
end
|
157
|
+
```
|
158
|
+
|
159
|
+
## 🚛 Delivery Methods
|
70
160
|
|
71
|
-
The delivery methods are designed to be
|
161
|
+
The delivery methods are designed to be modular so you can customize the way each type gets delivered.
|
72
162
|
|
73
163
|
For example, emails will require a subject, body, and email address while an SMS requires a phone number and simple message. You can define the formats for each of these in your Notification and the delivery method will handle the processing of it.
|
74
164
|
|
@@ -80,13 +170,23 @@ Writes notification to the database.
|
|
80
170
|
|
81
171
|
**Note:** Database notifications are special in that they will run before the other delivery methods. We do this so you can reference the database record ID in other delivery methods.
|
82
172
|
|
173
|
+
##### Options
|
174
|
+
|
175
|
+
* `association` - *Optional*
|
176
|
+
|
177
|
+
The name of the database association to use. Defaults to `:notifications`
|
178
|
+
|
179
|
+
* `format: :format_for_database` - *Optional*
|
180
|
+
|
181
|
+
Use a custom method to define the attributes saved to the database
|
182
|
+
|
83
183
|
### Email
|
84
184
|
|
85
185
|
Sends an email notification. Emails will always be sent with `deliver_later`
|
86
186
|
|
87
187
|
`deliver_by :email, mailer: "UserMailer"`
|
88
188
|
|
89
|
-
|
189
|
+
##### Options
|
90
190
|
|
91
191
|
* `mailer` - **Required**
|
92
192
|
|
@@ -96,13 +196,17 @@ Sends an email notification. Emails will always be sent with `deliver_later`
|
|
96
196
|
|
97
197
|
Used to customize the method on the mailer that is called
|
98
198
|
|
199
|
+
* `format: :format_for_email` - *Optional*
|
200
|
+
|
201
|
+
Use a custom method to define the params sent to the mailer. `recipient` will be merged into the params.
|
202
|
+
|
99
203
|
### ActionCable
|
100
204
|
|
101
205
|
Sends a notification to the browser via websockets (ActionCable channel by default).
|
102
206
|
|
103
207
|
`deliver_by :action_cable`
|
104
208
|
|
105
|
-
|
209
|
+
##### Options
|
106
210
|
|
107
211
|
* `format: :format_for_action_cable` - *Optional*
|
108
212
|
|
@@ -120,7 +224,7 @@ Sends a Slack notification via webhook.
|
|
120
224
|
|
121
225
|
`deliver_by :slack`
|
122
226
|
|
123
|
-
|
227
|
+
##### Options
|
124
228
|
|
125
229
|
* `format: :format_for_slack` - *Optional*
|
126
230
|
|
@@ -138,7 +242,7 @@ Sends an SMS notification via Twilio.
|
|
138
242
|
|
139
243
|
`deliver_by :twilio`
|
140
244
|
|
141
|
-
|
245
|
+
##### Options
|
142
246
|
|
143
247
|
* `credentials: :get_twilio_credentials` - *Optional*
|
144
248
|
|
@@ -168,11 +272,11 @@ Sends an SMS notification via Twilio.
|
|
168
272
|
|
169
273
|
### Vonage SMS
|
170
274
|
|
171
|
-
Sends an SMS notification
|
275
|
+
Sends an SMS notification via Vonage / Nexmo.
|
172
276
|
|
173
277
|
`deliver_by :vonage`
|
174
278
|
|
175
|
-
|
279
|
+
##### Options
|
176
280
|
|
177
281
|
* `credentials: :get_credentials` - *Optional*
|
178
282
|
|
@@ -195,23 +299,7 @@ Sends an SMS notification vai Vonage / Nexmo.
|
|
195
299
|
}
|
196
300
|
```
|
197
301
|
|
198
|
-
###
|
199
|
-
|
200
|
-
Each delivery method implements a `deliver_with_#{name}` method that receives the recipient as the first argument. You can override this method to check the user's preferences and skip processing if they have disabled that type of notification.
|
201
|
-
|
202
|
-
For example:
|
203
|
-
|
204
|
-
```ruby
|
205
|
-
class CommentNotification < Noticed::Base
|
206
|
-
deliver_by :email, if: :email_notifications?
|
207
|
-
|
208
|
-
def email_notifications?
|
209
|
-
recipient.email_notifications?
|
210
|
-
end
|
211
|
-
end
|
212
|
-
```
|
213
|
-
|
214
|
-
### Custom Delivery Methods
|
302
|
+
### 🚚 Custom Delivery Methods
|
215
303
|
|
216
304
|
You can define a custom delivery method easily by adding a `deliver_by` line with a unique name and class option. The class will be instantiated and should inherit from `Noticed::DeliveryMethods::Base`.
|
217
305
|
|
@@ -236,6 +324,18 @@ Delivery methods have access to the following methods and attributes:
|
|
236
324
|
* `recipient` - The object who should receive the notification. This is typically a User, Account, or other ActiveRecord model.
|
237
325
|
* `params` - The params passed into the notification. This is details about the event that happened. For example, a user commenting on a post would have params of `{ user: User.first }`
|
238
326
|
|
327
|
+
#### Callbacks
|
328
|
+
|
329
|
+
Callbacks for delivery methods wrap the *actual* delivery of the notification. You can use `before_deliver`, `around_deliver` and `after_deliver` in your custom delivery methods.
|
330
|
+
|
331
|
+
```ruby
|
332
|
+
class DiscordNotification < Noticed::DeliveryMethods::Base
|
333
|
+
after_deliver do
|
334
|
+
# Do whatever you want
|
335
|
+
end
|
336
|
+
end
|
337
|
+
```
|
338
|
+
|
239
339
|
#### Limitations
|
240
340
|
|
241
341
|
Rails 6.1+ can serialize Class and Module objects as arguments to ActiveJob. The following syntax should work for Rails 6.1+:
|
@@ -252,8 +352,9 @@ For Rails 6.0 and earlier, you must pass strings of the class names in the `deli
|
|
252
352
|
|
253
353
|
We recommend the Rails 6.0 compatible options to prevent confusion.
|
254
354
|
|
255
|
-
## Contributing
|
256
|
-
|
355
|
+
## 🙏 Contributing
|
356
|
+
|
357
|
+
This project uses [Standard](https://github.com/testdouble/standard) for formatting Ruby code. Please make sure to run `standardrb` before submitting pull requests.
|
257
358
|
|
258
|
-
## License
|
359
|
+
## 📝 License
|
259
360
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rails/generators/named_base"
|
4
|
+
|
5
|
+
module Noticed
|
6
|
+
module Generators
|
7
|
+
class ModelGenerator < Rails::Generators::NamedBase
|
8
|
+
include Rails::Generators::ResourceHelpers
|
9
|
+
|
10
|
+
source_root File.expand_path("../templates", __FILE__)
|
11
|
+
|
12
|
+
desc "Generates a Notification model for storing notifications."
|
13
|
+
|
14
|
+
argument :name, type: :string, default: "Notification", banner: "Notification"
|
15
|
+
argument :attributes, type: :array, default: [], banner: "field:type field:type"
|
16
|
+
|
17
|
+
def generate_notification
|
18
|
+
generate :model, name, "recipient:references{polymorphic}", "type", "params:text", "read_at:datetime", *attributes
|
19
|
+
end
|
20
|
+
|
21
|
+
def add_noticed_model
|
22
|
+
inject_into_class model_path, class_name, " include Noticed::Model\n"
|
23
|
+
end
|
24
|
+
|
25
|
+
def done
|
26
|
+
readme "README" if behavior == :invoke
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def model_path
|
32
|
+
@model_path ||= File.join("app", "models", "#{file_path}.rb")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rails/generators/named_base"
|
4
|
+
|
5
|
+
module Noticed
|
6
|
+
module Generators
|
7
|
+
class NotificationGenerator < Rails::Generators::NamedBase
|
8
|
+
include Rails::Generators::ResourceHelpers
|
9
|
+
|
10
|
+
source_root File.expand_path("../templates", __FILE__)
|
11
|
+
|
12
|
+
desc "Generates a notification with the given NAME."
|
13
|
+
|
14
|
+
def generate_notification
|
15
|
+
template "notification.rb", "app/notifications/#{singular_name}.rb"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# To deliver this notification:
|
2
|
+
#
|
3
|
+
# <%= class_name %>.with(post: @post).deliver_later(current_user)
|
4
|
+
# <%= class_name %>.with(post: @post).deliver(current_user)
|
5
|
+
|
6
|
+
class <%= class_name %> < Noticed::Base
|
7
|
+
# Add your delivery methods
|
8
|
+
#
|
9
|
+
# deliver_by :database
|
10
|
+
# deliver_by :email, mailer: "UserMailer"
|
11
|
+
# deliver_by :slack
|
12
|
+
# deliver_by :custom, class: "MyDeliveryMethod"
|
13
|
+
|
14
|
+
# Add required params
|
15
|
+
#
|
16
|
+
# param :post
|
17
|
+
|
18
|
+
# Define helper methods to make rendering easier.
|
19
|
+
#
|
20
|
+
# def message
|
21
|
+
# t(".message")
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# def url
|
25
|
+
# posts_path(params[:post])
|
26
|
+
# end
|
27
|
+
end
|
data/lib/noticed.rb
CHANGED
@@ -4,6 +4,8 @@ require "noticed/engine"
|
|
4
4
|
module Noticed
|
5
5
|
autoload :Base, "noticed/base"
|
6
6
|
autoload :Coder, "noticed/coder"
|
7
|
+
autoload :Model, "noticed/model"
|
8
|
+
autoload :Translation, "noticed/translation"
|
7
9
|
|
8
10
|
module DeliveryMethods
|
9
11
|
autoload :Base, "noticed/delivery_methods/base"
|
data/lib/noticed/base.rb
CHANGED
@@ -1,16 +1,21 @@
|
|
1
1
|
module Noticed
|
2
2
|
class Base
|
3
|
+
include Translation
|
4
|
+
include Rails.application.routes.url_helpers
|
5
|
+
|
6
|
+
extend ActiveModel::Callbacks
|
7
|
+
define_model_callbacks :deliver
|
8
|
+
|
3
9
|
class_attribute :delivery_methods, instance_writer: false, default: []
|
4
10
|
class_attribute :param_names, instance_writer: false, default: []
|
5
11
|
|
6
|
-
|
12
|
+
# Gives notifications access to the record and recipient when formatting for delivery
|
13
|
+
attr_accessor :record, :recipient
|
7
14
|
|
8
15
|
class << self
|
9
16
|
def deliver_by(name, options = {})
|
10
|
-
delivery_methods.push(
|
11
|
-
|
12
|
-
options: options
|
13
|
-
)
|
17
|
+
delivery_methods.push(name: name, options: options)
|
18
|
+
define_model_callbacks(name)
|
14
19
|
end
|
15
20
|
|
16
21
|
# Copy delivery methods from parent
|
@@ -33,14 +38,24 @@ module Noticed
|
|
33
38
|
@params = params
|
34
39
|
end
|
35
40
|
|
36
|
-
def deliver(
|
41
|
+
def deliver(recipients)
|
37
42
|
validate!
|
38
|
-
|
43
|
+
|
44
|
+
run_callbacks :deliver do
|
45
|
+
Array.wrap(recipients).uniq.each do |recipient|
|
46
|
+
run_delivery(recipient, enqueue: false)
|
47
|
+
end
|
48
|
+
end
|
39
49
|
end
|
40
50
|
|
41
|
-
def deliver_later(
|
51
|
+
def deliver_later(recipients)
|
42
52
|
validate!
|
43
|
-
|
53
|
+
|
54
|
+
run_callbacks :deliver do
|
55
|
+
Array.wrap(recipients).uniq.each do |recipient|
|
56
|
+
run_delivery(recipient, enqueue: true)
|
57
|
+
end
|
58
|
+
end
|
44
59
|
end
|
45
60
|
|
46
61
|
def params
|
@@ -77,15 +92,15 @@ module Noticed
|
|
77
92
|
record: record
|
78
93
|
}
|
79
94
|
|
80
|
-
|
81
|
-
|
95
|
+
run_callbacks delivery_method[:name] do
|
96
|
+
klass = get_class(delivery_method[:name], delivery_method[:options])
|
97
|
+
enqueue ? klass.perform_later(args) : klass.perform_now(args)
|
98
|
+
end
|
82
99
|
end
|
83
100
|
|
84
101
|
# Retrieves the correct class for a delivery method
|
85
102
|
def get_class(name, options)
|
86
|
-
if
|
87
|
-
name
|
88
|
-
elsif options[:class]
|
103
|
+
if options[:class]
|
89
104
|
options[:class].constantize
|
90
105
|
else
|
91
106
|
"Noticed::DeliveryMethods::#{name.to_s.classify}".constantize
|
@@ -1,18 +1,23 @@
|
|
1
1
|
module Noticed
|
2
2
|
module DeliveryMethods
|
3
3
|
class Base < Noticed.parent_class.constantize
|
4
|
+
extend ActiveModel::Callbacks
|
5
|
+
define_model_callbacks :deliver
|
6
|
+
|
4
7
|
attr_reader :notification, :options, :recipient
|
5
|
-
delegate :params, to: :notification
|
6
8
|
|
7
9
|
def perform(notification_class:, options:, params:, recipient:, record:)
|
8
10
|
@notification = notification_class.constantize.new(params)
|
9
11
|
@options = options
|
10
12
|
@recipient = recipient
|
11
13
|
|
12
|
-
#
|
14
|
+
# Make notification aware of database record and recipient during delivery
|
13
15
|
@notification.record = record
|
16
|
+
@notification.recipient = recipient
|
14
17
|
|
15
|
-
deliver
|
18
|
+
run_callbacks :deliver do
|
19
|
+
deliver
|
20
|
+
end
|
16
21
|
end
|
17
22
|
|
18
23
|
def deliver
|
@@ -3,18 +3,22 @@ module Noticed
|
|
3
3
|
class Database < Base
|
4
4
|
# Must return the database record
|
5
5
|
def deliver
|
6
|
-
recipient.
|
6
|
+
recipient.send(association_name).create!(attributes)
|
7
7
|
end
|
8
8
|
|
9
9
|
private
|
10
10
|
|
11
|
+
def association_name
|
12
|
+
options[:association] || :notifications
|
13
|
+
end
|
14
|
+
|
11
15
|
def attributes
|
12
16
|
if (method = options[:format])
|
13
17
|
notification.send(method)
|
14
18
|
else
|
15
19
|
{
|
16
20
|
type: notification.class.name,
|
17
|
-
params: params
|
21
|
+
params: notification.params
|
18
22
|
}
|
19
23
|
end
|
20
24
|
end
|
@@ -2,7 +2,7 @@ module Noticed
|
|
2
2
|
module DeliveryMethods
|
3
3
|
class Email < Base
|
4
4
|
def deliver
|
5
|
-
mailer.with(
|
5
|
+
mailer.with(format).send(method.to_sym).deliver_later
|
6
6
|
end
|
7
7
|
|
8
8
|
private
|
@@ -14,6 +14,14 @@ 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(recipient: recipient)
|
23
|
+
end
|
24
|
+
end
|
17
25
|
end
|
18
26
|
end
|
19
27
|
end
|
@@ -2,9 +2,15 @@ module Noticed
|
|
2
2
|
module DeliveryMethods
|
3
3
|
class Test < Base
|
4
4
|
class_attribute :delivered, default: []
|
5
|
+
class_attribute :callbacks, default: []
|
6
|
+
|
7
|
+
after_deliver do
|
8
|
+
self.class.callbacks << :after
|
9
|
+
end
|
5
10
|
|
6
11
|
def self.clear!
|
7
12
|
delivered.clear
|
13
|
+
callbacks.clear
|
8
14
|
end
|
9
15
|
|
10
16
|
def deliver
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Noticed
|
2
|
+
module Model
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
self.inheritance_column = nil
|
7
|
+
|
8
|
+
serialize :params, Noticed::Coder
|
9
|
+
|
10
|
+
belongs_to :recipient, polymorphic: true
|
11
|
+
|
12
|
+
scope :newest_first, -> { order(created_at: :desc) }
|
13
|
+
end
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
def mark_as_read!
|
17
|
+
update_all(read_at: Time.current, updated_at: Time.current)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Rehydrate the database notification into the Notification object for rendering
|
22
|
+
def to_notification
|
23
|
+
@_notification ||= begin
|
24
|
+
instance = type.constantize.with(params)
|
25
|
+
instance.record = self
|
26
|
+
instance
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def mark_as_read!
|
31
|
+
update(read_at: Time.current)
|
32
|
+
end
|
33
|
+
|
34
|
+
def unread?
|
35
|
+
!read?
|
36
|
+
end
|
37
|
+
|
38
|
+
def read?
|
39
|
+
read_at?
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Translation
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
# Returns the +i18n_scope+ for the class. Overwrite if you want custom lookup.
|
5
|
+
def i18n_scope
|
6
|
+
:notifications
|
7
|
+
end
|
8
|
+
|
9
|
+
def translate(key, **options)
|
10
|
+
I18n.translate(scope_translation_key(key), **options)
|
11
|
+
end
|
12
|
+
alias t translate
|
13
|
+
|
14
|
+
def scope_translation_key(key)
|
15
|
+
if key.to_s.start_with?(".")
|
16
|
+
"notifications.#{self.class.name.underscore}#{key}"
|
17
|
+
else
|
18
|
+
key
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/noticed/version.rb
CHANGED
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.
|
4
|
+
version: 1.2.3
|
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-
|
11
|
+
date: 2020-08-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -92,6 +92,10 @@ files:
|
|
92
92
|
- README.md
|
93
93
|
- Rakefile
|
94
94
|
- app/channels/noticed/notification_channel.rb
|
95
|
+
- lib/generators/noticed/model_generator.rb
|
96
|
+
- lib/generators/noticed/notification_generator.rb
|
97
|
+
- lib/generators/noticed/templates/README
|
98
|
+
- lib/generators/noticed/templates/notification.rb.tt
|
95
99
|
- lib/noticed.rb
|
96
100
|
- lib/noticed/base.rb
|
97
101
|
- lib/noticed/coder.rb
|
@@ -104,6 +108,8 @@ files:
|
|
104
108
|
- lib/noticed/delivery_methods/twilio.rb
|
105
109
|
- lib/noticed/delivery_methods/vonage.rb
|
106
110
|
- lib/noticed/engine.rb
|
111
|
+
- lib/noticed/model.rb
|
112
|
+
- lib/noticed/translation.rb
|
107
113
|
- lib/noticed/version.rb
|
108
114
|
- lib/tasks/noticed_tasks.rake
|
109
115
|
homepage: https://github.com/excid3/noticed
|