noticed 1.5.8 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7857214fa6a81b4daa79df4247b40d7a807c50963a30c03b728452aa4c7a98ee
4
- data.tar.gz: e5bb7f26fc03c44b71f130e14cba3d3b92e66f11c217cd48464967ee8e88f985
3
+ metadata.gz: eade1cdb53653ae77fa61a1348340402ea37625188677a16baddda26bc42bd49
4
+ data.tar.gz: 11d1d9ba60517005306714fc028fd312bbee7bef489d19218e7ba5c6938e22eb
5
5
  SHA512:
6
- metadata.gz: ce49d5a79dd42876bc83f5783e6b253a98edb1a21a6ddbe141bc3165aeff9efcd774bdfac6bf243643477b7401f547971f2eb9deb2c647ff162504ad62b5c0a7
7
- data.tar.gz: 32bcf1c228a088843d334296aea80025e5d6d3fb503d00773426a422701e64cf03357be08ded20f65b0cf763486bb0b7d9cbd779833611cc044b74c379ff28b2
6
+ metadata.gz: '08b57a6181fa23484fa7c21eb7153a75c2bb05750532d2c649a8bc46e30b57be8041039802396167e639fede5595c64bb09d683eaeb1f23a0b0d346f2e76ca5e'
7
+ data.tar.gz: cc01ee47a638c02f1e5311c1433735607052130be9b4beaf61ced7482fd31910ab8c8a17915ff9c60a50bccddbc86bcb14f44519d8119632a95a1eba214f22a4
data/README.md CHANGED
@@ -106,9 +106,10 @@ end
106
106
 
107
107
  **Shared Options**
108
108
 
109
- * `if: :method_name` - Calls `method_name`and cancels delivery method if `false` is returned
110
- * `unless: :method_name` - Calls `method_name`and cancels delivery method if `true` is returned
109
+ * `if: :method_name` - Calls `method_name` and cancels delivery method if `false` is returned
110
+ * `unless: :method_name` - Calls `method_name` and cancels delivery method if `true` is returned
111
111
  * `delay: ActiveSupport::Duration` - Delays the delivery for the given duration of time
112
+ * `delay: :method_name` - Calls `method_name which should return an `ActiveSupport::Duration` and delays the delivery for the given duration of time
112
113
 
113
114
  ##### Helper Methods
114
115
 
@@ -193,16 +194,18 @@ deliver_by :slack, debug: true
193
194
 
194
195
  ## ✅ Best Practices
195
196
 
197
+ ### Creating a notification from an Active Record callback
198
+
196
199
  A common use case is to trigger a notification when a record is created. For example,
197
200
 
198
201
  ```ruby
199
202
  class Message < ApplicationRecord
200
203
  belongs_to :recipient, class_name: "User"
201
-
204
+
202
205
  after_create_commit :notify_recipient
203
-
206
+
204
207
  private
205
-
208
+
206
209
  def notify_recipient
207
210
  NewMessageNotification.with(message: self).deliver_later(recipient)
208
211
  end
@@ -216,6 +219,23 @@ A common symptom of this problem is undelivered notifications and the following
216
219
 
217
220
  > `Discarded Noticed::DeliveryMethods::Email due to a ActiveJob::DeserializationError.`
218
221
 
222
+ ### Renaming notifications
223
+
224
+ If you rename the class of a notification object your existing queries can break. This is because Noticed serializes the class name and sets it to the `type` column on the `Notification` record.
225
+
226
+ You can catch these errors at runtime by using `YourNotificationClassName.name` instead of hardcoding the string when performing a query.
227
+
228
+ ```ruby
229
+ Notification.where(type: YourNotificationClassName.name) # good
230
+ Notification.where(type: "YourNotificationClassName") # bad
231
+ ```
232
+
233
+ When renaming a notification class you will need to backfill existing notifications to reference the new name.
234
+
235
+ ```ruby
236
+ Notification.where(type: "OldNotificationClassName").update_all(type: NewNotificationClassName.name)
237
+ ```
238
+
219
239
  ## 🚛 Delivery Methods
220
240
 
221
241
  The delivery methods are designed to be modular so you can customize the way each type gets delivered.
@@ -350,22 +370,6 @@ class DeliveryMethods::Discord < Noticed::DeliveryMethods::Base
350
370
  end
351
371
  ```
352
372
 
353
- #### Limitations
354
-
355
- Rails 6.1+ can serialize Class and Module objects as arguments to ActiveJob. The following syntax should work for Rails 6.1+:
356
-
357
- ```ruby
358
- deliver_by DeliveryMethods::Discord
359
- ```
360
-
361
- For Rails 5.2 and 6.0, you must pass strings of the class names in the `deliver_by` options.
362
-
363
- ```ruby
364
- deliver_by :discord, class: "DeliveryMethods::Discord"
365
- ```
366
-
367
- We recommend using a string in order to prevent confusion.
368
-
369
373
  ### 📦 Database Model
370
374
 
371
375
  The Notification database model includes several helpful features to make working with database notifications easier.
@@ -3,5 +3,5 @@
3
3
 
4
4
  Next steps:
5
5
  1. Run "rails db:migrate"
6
- 2. Add "has_many :notifications, as: :recipient" to your User model(s).
6
+ 2. Add "has_many :notifications, as: :recipient, dependent: :destroy" to your User model(s).
7
7
  3. Generate notifications with "rails g noticed:notification"
data/lib/noticed/base.rb CHANGED
@@ -75,6 +75,10 @@ module Noticed
75
75
  @params || {}
76
76
  end
77
77
 
78
+ def clear_recipient
79
+ self.recipient = nil
80
+ end
81
+
78
82
  private
79
83
 
80
84
  # Runs all delivery methods for a notification
@@ -112,6 +116,9 @@ module Noticed
112
116
 
113
117
  # Always perfrom later if a delay is present
114
118
  if (delay = delivery_method.dig(:options, :delay))
119
+ # Dynamic delays with metho calls or
120
+ delay = send(delay) if delay.is_a? Symbol
121
+
115
122
  method.set(wait: delay, queue: queue).perform_later(args)
116
123
  elsif enqueue
117
124
  method.set(queue: queue).perform_later(args)
data/lib/noticed/coder.rb CHANGED
@@ -3,6 +3,8 @@ module Noticed
3
3
  def self.load(data)
4
4
  return if data.nil?
5
5
  ActiveJob::Arguments.send(:deserialize_argument, data)
6
+ rescue ActiveRecord::RecordNotFound => error
7
+ {noticed_error: error.message, original_params: data}
6
8
  end
7
9
 
8
10
  def self.dump(data)
data/lib/noticed/model.rb CHANGED
@@ -72,5 +72,10 @@ module Noticed
72
72
  def read?
73
73
  read_at?
74
74
  end
75
+
76
+ # If a GlobalID record in params is no longer found, the params will default with a noticed_error key
77
+ def deserialize_error?
78
+ !!params[:noticed_error]
79
+ end
75
80
  end
76
81
  end
@@ -1,3 +1,3 @@
1
1
  module Noticed
2
- VERSION = "1.5.8"
2
+ VERSION = "1.6.0"
3
3
  end
data/lib/noticed.rb CHANGED
@@ -16,22 +16,13 @@ module Noticed
16
16
  autoload :Base, "noticed/delivery_methods/base"
17
17
  autoload :Database, "noticed/delivery_methods/database"
18
18
  autoload :Email, "noticed/delivery_methods/email"
19
+ autoload :Fcm, "noticed/delivery_methods/fcm"
19
20
  autoload :Ios, "noticed/delivery_methods/ios"
20
21
  autoload :MicrosoftTeams, "noticed/delivery_methods/microsoft_teams"
21
22
  autoload :Slack, "noticed/delivery_methods/slack"
22
23
  autoload :Test, "noticed/delivery_methods/test"
23
24
  autoload :Twilio, "noticed/delivery_methods/twilio"
24
25
  autoload :Vonage, "noticed/delivery_methods/vonage"
25
- autoload :Fcm, "noticed/delivery_methods/fcm"
26
- end
27
-
28
- def self.notify(recipients:, notification:)
29
- recipients.each do |recipient|
30
- notification.notify(recipient)
31
- end
32
-
33
- # Clear the recipient after sending to the group
34
- notification.recipient = nil
35
26
  end
36
27
 
37
28
  mattr_accessor :parent_class
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.5.8
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Oliver
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-03 00:00:00.000000000 Z
11
+ date: 2022-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -170,7 +170,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
170
170
  - !ruby/object:Gem::Version
171
171
  version: '0'
172
172
  requirements: []
173
- rubygems_version: 3.3.3
173
+ rubygems_version: 3.3.7
174
174
  signing_key:
175
175
  specification_version: 4
176
176
  summary: Notifications for Ruby on Rails applications