noticed 1.5.9 → 1.6.1

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: 367ce1b20a44bfc15ebf8aeaa0328ef90ad0673edd076e977d0e647a3a1b13b7
4
- data.tar.gz: 437627e876b7e42243626bab168b9fd37d7da49c54309e2f8f9ef91dfb5e541b
3
+ metadata.gz: dec51920d13ba117cce592dc0545e22d5618528033e736b267238a8456cf8341
4
+ data.tar.gz: 503d20af8d1640173e066e99fe3c8794155666d1f84456731253de6524f1d8e0
5
5
  SHA512:
6
- metadata.gz: 5991aa837054391b01f5614617db15951279d4015c96eab52194ad7a0ab05cf3d586353cb5968bb04584246b23682c75365858464062c852b70f9553f0be8ad5
7
- data.tar.gz: ff4adba6ae61a08bf8dab9a4b27899c3e5dd3dea8e1475fa7f1da589db85ce17b264d02ed523ea38d905bd53016f2e1d483cf4a7c18ea9461e1b12ef056868e6
6
+ metadata.gz: 3d05f0f58b8d10e06782472cca617de0ba0140e42ffa0a7995adf80467fa88ae49f7656e0dd3c52d4e3130e9c3908543e5b72d05884431940422b092c50b7258
7
+ data.tar.gz: 602e15164492cf2de75d6b3b43a33d67312fdd18fa0cd42fd1b3a6d4f3397ab5d283b2346a3b019dfb9dd7f79b265387b927e135fb41e560fb3fa13ba0d99d7a
data/README.md CHANGED
@@ -194,6 +194,8 @@ deliver_by :slack, debug: true
194
194
 
195
195
  ## ✅ Best Practices
196
196
 
197
+ ### Creating a notification from an Active Record callback
198
+
197
199
  A common use case is to trigger a notification when a record is created. For example,
198
200
 
199
201
  ```ruby
@@ -217,6 +219,23 @@ A common symptom of this problem is undelivered notifications and the following
217
219
 
218
220
  > `Discarded Noticed::DeliveryMethods::Email due to a ActiveJob::DeserializationError.`
219
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
+
220
239
  ## 🚛 Delivery Methods
221
240
 
222
241
  The delivery methods are designed to be modular so you can customize the way each type gets delivered.
@@ -351,22 +370,6 @@ class DeliveryMethods::Discord < Noticed::DeliveryMethods::Base
351
370
  end
352
371
  ```
353
372
 
354
- #### Limitations
355
-
356
- Rails 6.1+ can serialize Class and Module objects as arguments to ActiveJob. The following syntax should work for Rails 6.1+:
357
-
358
- ```ruby
359
- deliver_by DeliveryMethods::Discord
360
- ```
361
-
362
- For Rails 5.2 and 6.0, you must pass strings of the class names in the `deliver_by` options.
363
-
364
- ```ruby
365
- deliver_by :discord, class: "DeliveryMethods::Discord"
366
- ```
367
-
368
- We recommend using a string in order to prevent confusion.
369
-
370
373
  ### 📦 Database Model
371
374
 
372
375
  The Notification database model includes several helpful features to make working with database notifications easier.
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators/named_base"
4
+
5
+ module Noticed
6
+ module Generators
7
+ module Model
8
+ class BaseGenerator < Rails::Generators::NamedBase
9
+ include Rails::Generators::ResourceHelpers
10
+
11
+ source_root File.expand_path("../templates", __FILE__)
12
+
13
+ desc "Generates a Notification model for storing notifications."
14
+
15
+ argument :name, type: :string, default: "Notification", banner: "Notification"
16
+ argument :attributes, type: :array, default: [], banner: "field:type field:type"
17
+
18
+ def generate_notification
19
+ binding.irb
20
+ generate :model, name, "recipient:references{polymorphic}", "type", "params:#{json_column_type}", "read_at:datetime:index", *attributes
21
+ end
22
+
23
+ def add_noticed_model
24
+ inject_into_class model_path, class_name, " include Noticed::Model\n"
25
+ end
26
+
27
+ def add_not_nullable
28
+ migration_path = Dir.glob(Rails.root.join("db/migrate/*")).max_by { |f| File.mtime(f) }
29
+
30
+ # Force is required because null: false already exists in the file and Thor isn't smart enough to tell the difference
31
+ insert_into_file migration_path, after: "t.string :type", force: true do
32
+ ", null: false"
33
+ end
34
+ end
35
+
36
+ def done
37
+ readme "README" if behavior == :invoke
38
+ end
39
+
40
+ private
41
+
42
+ def model_path
43
+ @model_path ||= File.join("app", "models", "#{file_path}.rb")
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators/named_base"
4
+ require_relative "base_generator"
5
+
6
+ module Noticed
7
+ module Generators
8
+ module Model
9
+ class MysqlGenerator < BaseGenerator
10
+ private
11
+
12
+ def json_column_type
13
+ "json"
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators/named_base"
4
+ require_relative "base_generator"
5
+
6
+ module Noticed
7
+ module Generators
8
+ module Model
9
+ class PostgresqlGenerator < BaseGenerator
10
+ private
11
+
12
+ def json_column_type
13
+ "jsonb"
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators/named_base"
4
+ require_relative "base_generator"
5
+
6
+ module Noticed
7
+ module Generators
8
+ module Model
9
+ class Sqlite3Generator < BaseGenerator
10
+ private
11
+
12
+ def json_column_type
13
+ "json"
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -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
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)
@@ -5,9 +5,9 @@ module Noticed
5
5
 
6
6
  def deliver
7
7
  if options[:enqueue]
8
- mailer.with(format).send(method.to_sym).deliver_later
8
+ mailer.with(format).send(mailer_method.to_sym).deliver_later
9
9
  else
10
- mailer.with(format).send(method.to_sym).deliver_now
10
+ mailer.with(format).send(mailer_method.to_sym).deliver_now
11
11
  end
12
12
  end
13
13
 
@@ -33,7 +33,7 @@ module Noticed
33
33
  # If notification responds to symbol, call that method and use return value
34
34
  # If notification does not respond to symbol, use the symbol for the mailer method
35
35
  # Otherwise, use the underscored notification class name as the mailer method
36
- def method
36
+ def mailer_method
37
37
  method_name = options[:method]&.to_sym
38
38
  if method_name.present?
39
39
  notification.respond_to?(method_name) ? notification.send(method_name) : method_name
data/lib/noticed/model.rb CHANGED
@@ -13,7 +13,11 @@ module Noticed
13
13
  included do
14
14
  self.inheritance_column = nil
15
15
 
16
- serialize :params, noticed_coder
16
+ if Rails.gem_version >= Gem::Version.new("7.1")
17
+ serialize :params, coder: noticed_coder
18
+ else
19
+ serialize :params, noticed_coder
20
+ end
17
21
 
18
22
  belongs_to :recipient, polymorphic: true
19
23
 
@@ -72,5 +76,10 @@ module Noticed
72
76
  def read?
73
77
  read_at?
74
78
  end
79
+
80
+ # If a GlobalID record in params is no longer found, the params will default with a noticed_error key
81
+ def deserialize_error?
82
+ !!params[:noticed_error]
83
+ end
75
84
  end
76
85
  end
@@ -1,3 +1,3 @@
1
1
  module Noticed
2
- VERSION = "1.5.9"
2
+ VERSION = "1.6.1"
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
@@ -5,7 +5,7 @@ module ActionCable
5
5
  module Server
6
6
  class Configuration
7
7
  def pubsub_adapter
8
- cable["adapter"] == "test" ? ActionCable::SubscriptionAdapter::Test : super
8
+ (cable["adapter"] == "test") ? ActionCable::SubscriptionAdapter::Test : super
9
9
  end
10
10
  end
11
11
  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.5.9
4
+ version: 1.6.1
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-11 00:00:00.000000000 Z
11
+ date: 2023-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,76 +38,6 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 4.0.0
41
- - !ruby/object:Gem::Dependency
42
- name: pg
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: standard
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: webmock
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: mysql2
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: '0'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- version: '0'
97
- - !ruby/object:Gem::Dependency
98
- name: sqlite3
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: '0'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: '0'
111
41
  description: Database, browser, realtime ActionCable, Email, SMS, Slack notifications,
112
42
  and more for Rails apps
113
43
  email:
@@ -120,6 +50,10 @@ files:
120
50
  - README.md
121
51
  - Rakefile
122
52
  - lib/generators/noticed/delivery_method_generator.rb
53
+ - lib/generators/noticed/model/base_generator.rb
54
+ - lib/generators/noticed/model/mysql_generator.rb
55
+ - lib/generators/noticed/model/postgresql_generator.rb
56
+ - lib/generators/noticed/model/sqlite3_generator.rb
123
57
  - lib/generators/noticed/model_generator.rb
124
58
  - lib/generators/noticed/notification_generator.rb
125
59
  - lib/generators/noticed/templates/README
@@ -170,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
170
104
  - !ruby/object:Gem::Version
171
105
  version: '0'
172
106
  requirements: []
173
- rubygems_version: 3.3.3
107
+ rubygems_version: 3.4.12
174
108
  signing_key:
175
109
  specification_version: 4
176
110
  summary: Notifications for Ruby on Rails applications