noticed 1.2.12 → 1.2.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +37 -3
- data/lib/generators/noticed/model_generator.rb +9 -0
- data/lib/generators/noticed/templates/delivery_method.rb.tt +7 -0
- data/lib/noticed/base.rb +16 -5
- data/lib/noticed/delivery_methods/base.rb +5 -0
- data/lib/noticed/delivery_methods/email.rb +6 -0
- data/lib/noticed/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 177af67cbc18228368b1046461e97ec0046ab7be37dcd528d0834456184d3dbc
|
4
|
+
data.tar.gz: 4b68f5ad47ac4cde2fb48f0738792c8d70332a0bf3b2a5bc4c6c0a830bd9b030
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 127fb40a8e97d833c01d8f35586aee3ae5be6b673e20670306a5c94982ea4b2f358d7d471020460689483c326c7caf5a9f9bd3854b2c4052f47f2714a4aa8b92
|
7
|
+
data.tar.gz: 5f285005c9e5553e9cde3203a7b7b646c3448edb9fdb0335e0286dc2bee14d09f7001644733caa8a5795f5291380fc4e65aa37ab5c89e9c752a54d2dd35a8a61
|
data/README.md
CHANGED
@@ -78,7 +78,7 @@ To add delivery methods, simply `include` the module for the delivery methods yo
|
|
78
78
|
class CommentNotification < Noticed::Base
|
79
79
|
deliver_by :database
|
80
80
|
deliver_by :action_cable
|
81
|
-
deliver_by :email, if: :email_notifications?
|
81
|
+
deliver_by :email, mailer: 'CommentMailer', if: :email_notifications?
|
82
82
|
|
83
83
|
# I18n helpers
|
84
84
|
def message
|
@@ -120,7 +120,7 @@ Like ActiveRecord, notifications have several different types of callbacks.
|
|
120
120
|
```ruby
|
121
121
|
class CommentNotification < Noticed::Base
|
122
122
|
deliver_by :database
|
123
|
-
deliver_by :email
|
123
|
+
deliver_by :email, mailer: 'CommentMailer'
|
124
124
|
|
125
125
|
# Callbacks for the entire delivery
|
126
126
|
before_deliver :whatever
|
@@ -158,7 +158,7 @@ For example:
|
|
158
158
|
|
159
159
|
```ruby
|
160
160
|
class CommentNotification < Noticed::Base
|
161
|
-
deliver_by :email, if: :email_notifications?
|
161
|
+
deliver_by :email, mailer: 'CommentMailer', if: :email_notifications?
|
162
162
|
|
163
163
|
def email_notifications?
|
164
164
|
recipient.email_notifications?
|
@@ -340,6 +340,40 @@ Delivery methods have access to the following methods and attributes:
|
|
340
340
|
* `recipient` - The object who should receive the notification. This is typically a User, Account, or other ActiveRecord model.
|
341
341
|
* `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 }`
|
342
342
|
|
343
|
+
#### Validating options passed to Custom Delivery methods
|
344
|
+
|
345
|
+
You can validate the options passed to the custom delivery method and raise validation errors. This is helpful for debugging to make sure valid and required options were passed in.
|
346
|
+
|
347
|
+
To do this, simply override the `self.validate!(options)` method from the `Noticed::DeliveryMethods::Base` class in your Custom Delivery method.
|
348
|
+
|
349
|
+
```ruby
|
350
|
+
class DeliveryMethods::Discord < Noticed::DeliveryMethods::Base
|
351
|
+
def deliver
|
352
|
+
# Logic for sending a Discord notification
|
353
|
+
end
|
354
|
+
|
355
|
+
def self.validate!(options)
|
356
|
+
unless options.key?(:sent_by)
|
357
|
+
raise Noticed::ValidationError, 'the `sent_by` option is missing'
|
358
|
+
end
|
359
|
+
end
|
360
|
+
end
|
361
|
+
|
362
|
+
class CommentNotification < Noticed::Base
|
363
|
+
deliver_by :discord, class: 'DeliveryMethods::Discord'
|
364
|
+
end
|
365
|
+
```
|
366
|
+
|
367
|
+
Now it will raise an error because a required argument is missing.
|
368
|
+
|
369
|
+
To fix the error, the argument has to be passed correctly. For example:
|
370
|
+
|
371
|
+
```ruby
|
372
|
+
class CommentNotification < Noticed::Base
|
373
|
+
deliver_by :discord, class: 'DeliveryMethods::Discord', sent_by: User.admin.first
|
374
|
+
end
|
375
|
+
```
|
376
|
+
|
343
377
|
#### Callbacks
|
344
378
|
|
345
379
|
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.
|
@@ -22,6 +22,15 @@ module Noticed
|
|
22
22
|
inject_into_class model_path, class_name, " include Noticed::Model\n"
|
23
23
|
end
|
24
24
|
|
25
|
+
def add_not_nullable
|
26
|
+
migration_path = Dir.glob(Rails.root.join("db/migrate/*")).max_by { |f| File.mtime(f) }
|
27
|
+
|
28
|
+
# Force is required because null: false already exists in the file and Thor isn't smart enough to tell the difference
|
29
|
+
insert_into_file migration_path, after: "t.string :type", force: true do
|
30
|
+
", null: false"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
25
34
|
def done
|
26
35
|
readme "README" if behavior == :invoke
|
27
36
|
end
|
@@ -2,4 +2,11 @@ class DeliveryMethods::<%= class_name %> < Noticed::DeliveryMethods::Base
|
|
2
2
|
def deliver
|
3
3
|
# Logic for sending the notification
|
4
4
|
end
|
5
|
+
|
6
|
+
# You may override this method to validate options for the delivery method
|
7
|
+
# Invalid options should raise a ValidationError
|
8
|
+
#
|
9
|
+
# def self.validate!(options)
|
10
|
+
# raise ValidationError, "required_option missing" unless options[:required_option]
|
11
|
+
# end
|
5
12
|
end
|
data/lib/noticed/base.rb
CHANGED
@@ -97,13 +97,12 @@ module Noticed
|
|
97
97
|
}
|
98
98
|
|
99
99
|
run_callbacks delivery_method[:name] do
|
100
|
-
|
101
|
-
enqueue ?
|
100
|
+
method = delivery_method_for(delivery_method[:name], delivery_method[:options])
|
101
|
+
enqueue ? method.perform_later(args) : method.perform_now(args)
|
102
102
|
end
|
103
103
|
end
|
104
104
|
|
105
|
-
|
106
|
-
def get_class(name, options)
|
105
|
+
def delivery_method_for(name, options)
|
107
106
|
if options[:class]
|
108
107
|
options[:class].constantize
|
109
108
|
else
|
@@ -111,13 +110,25 @@ module Noticed
|
|
111
110
|
end
|
112
111
|
end
|
113
112
|
|
114
|
-
# Validates that all params are present
|
115
113
|
def validate!
|
114
|
+
validate_params_present!
|
115
|
+
validate_options_of_delivery_methods!
|
116
|
+
end
|
117
|
+
|
118
|
+
# Validates that all params are present
|
119
|
+
def validate_params_present!
|
116
120
|
self.class.param_names.each do |param_name|
|
117
121
|
if params[param_name].nil?
|
118
122
|
raise ValidationError, "#{param_name} is missing."
|
119
123
|
end
|
120
124
|
end
|
121
125
|
end
|
126
|
+
|
127
|
+
def validate_options_of_delivery_methods!
|
128
|
+
delivery_methods.each do |delivery_method|
|
129
|
+
method = delivery_method_for(delivery_method[:name], delivery_method[:options])
|
130
|
+
method.validate!(delivery_method[:options])
|
131
|
+
end
|
132
|
+
end
|
122
133
|
end
|
123
134
|
end
|
@@ -25,6 +25,11 @@ module Noticed
|
|
25
25
|
raise NotImplementedError, "Delivery methods must implement a deliver method"
|
26
26
|
end
|
27
27
|
|
28
|
+
def self.validate!(options)
|
29
|
+
# Override this method in your custom DeliveryMethod class to validate the options
|
30
|
+
# and raise error, if invalid.
|
31
|
+
end
|
32
|
+
|
28
33
|
private
|
29
34
|
|
30
35
|
# Helper method for making POST requests from delivery methods
|
@@ -5,6 +5,12 @@ module Noticed
|
|
5
5
|
mailer.with(format).send(method.to_sym).deliver_later
|
6
6
|
end
|
7
7
|
|
8
|
+
def self.validate!(options)
|
9
|
+
unless options.key?(:mailer)
|
10
|
+
raise ValidationError, "email delivery method requires a 'mailer' to be specified"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
8
14
|
private
|
9
15
|
|
10
16
|
def mailer
|
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.2.
|
4
|
+
version: 1.2.13
|
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-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|