noticed 1.2.21 → 1.3.0

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: e648889dba7414cf25efd56b6b39aeeef03c44febfd7bbfccf47151cb940736e
4
- data.tar.gz: 0ecb213d7913181f686bbd5cd716e617f0cb6da58a87a4c3185d8e69dcf114c0
3
+ metadata.gz: 83635d722a21df622c90c4e8ded293206242be768b30b2139c41f13bc4d6712c
4
+ data.tar.gz: 707ff4e0e8d09ed4c1fa1b0a7228353de637b070e849e7eb234b58b6fb491e40
5
5
  SHA512:
6
- metadata.gz: 65a6fdd5fa8ea6cea59201358b62a93c5b98af509d8edd0694afda809887367baf973173ecc1bf401e54cb9dc0700989cb7e6e8ff56aa0978b130a8c10b2250b
7
- data.tar.gz: e873f202212c36f16b2a97ab7fc678498ded0383bc2ac0ca1af9c647806c0c827be8c85833199af34823202bb8b029fffd66d60903928377890bbcd1e0b4209f
6
+ metadata.gz: 85ddb6e11858df2b21b4dc098096c78465f7ac3dcb7a804037f3cf1855735b954cccb1cb0ad3227d1ebd89cb98e2e6e5d0138af27e73e52d2feeb35ac85ccdec
7
+ data.tar.gz: 80d3485a086e8203397f83bf3269e057ec63c9da89b3016d29a8395499447419336d89e7d01daff911a838159b963423dd45f6161b80c0dfab608f2f4e11c662
data/README.md CHANGED
@@ -265,7 +265,7 @@ Sends a Teams notification via webhook.
265
265
 
266
266
  * `format: :format_for_teams` - *Optional*
267
267
 
268
- Use a custom method to define the payload sent to slack. Method should return a Hash.
268
+ Use a custom method to define the payload sent to Microsoft Teams. Method should return a Hash.
269
269
  Documentation for posting via Webhooks available at: https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook
270
270
 
271
271
  ```ruby
@@ -379,7 +379,7 @@ end
379
379
 
380
380
  In this scenario, you can create an escalating notification that starts with a ping in Slack, then emails the team, and then finally sends an SMS to the on-call phone.
381
381
 
382
- You can mix and match the options and delivery methods to suit your application specific needs.
382
+ You can mix and match the options and delivery methods to suit your application specific needs.
383
383
 
384
384
  ### 🚚 Custom Delivery Methods
385
385
 
@@ -534,53 +534,43 @@ Adding notification associations to your models makes querying and deleting noti
534
534
 
535
535
  For example, in most cases, you'll want to delete notifications for records that are destroyed.
536
536
 
537
- ##### JSON Columns
537
+ We'll need two associations for this:
538
538
 
539
- If you're using MySQL or Postgresql, the `params` column on the notifications table is in `json` or `jsonb` format and can be queried against directly.
539
+ 1. Notifications where the record is the recipient
540
+ 2. Notifications where the record is in the notification params
540
541
 
541
542
  For example, we can query the notifications and delete them on destroy like so:
542
543
 
543
544
  ```ruby
544
545
  class Post < ApplicationRecord
545
- def notifications
546
- # Exact match
547
- @notifications ||= Notification.where(params: { post: self })
546
+ # Standard association for deleting notifications when you're the recipient
547
+ has_many :notifications, as: :recipient, dependent: :destroy
548
548
 
549
- # Or Postgres syntax to query the post key in the JSON column
550
- # @notifications ||= Notification.where("params->'post' = ?", Noticed::Coder.dump(self).to_json)
551
- end
552
-
553
- before_destroy :destroy_notifications
549
+ # Helper for associating and destroying Notification records where(params: {post: self})
550
+ has_noticed_notifications
554
551
 
555
- def destroy_notifications
556
- notifications.destroy_all
557
- end
552
+ # You can override the param_name, the notification model name, or disable the before_destroy callback
553
+ has_noticed_notifications param_name: :parent, destroy: false, model: "Notification"
558
554
  end
559
- ```
560
-
561
- ##### Polymorphic Association
562
555
 
563
- If your notification is only associated with one model or you're using a `text` column for your params column , then a polymorphic association is what you'll want to use.
556
+ # Create a CommentNotification with a post param
557
+ CommentNotification.with(post: @post).deliver(user)
558
+ # Lookup Notifications where params: {post: @post}
559
+ @post.notifications_as_post
564
560
 
565
- 1. Add a polymorphic association to the Notification model. `rails g migration AddNotifiableToNotifications notifiable:belongs_to{polymorphic}`
566
-
567
- 2. Add `has_many :notifications, as: :notifiable, dependent: :destroy` to each model
561
+ CommentNotification.with(parent: @post).deliver(user)
562
+ @post.notifications_as_parent
563
+ ```
568
564
 
569
- 3. Customize database `format: ` option to write the `notifiable` attribute(s) when saving the notification
565
+ #### Handling Deleted Records
570
566
 
571
- ```ruby
572
- class ExampleNotification < Noticed::Base
573
- deliver_by :database, format: :format_for_database
567
+ If you create a notification but delete the associated record and forgot `has_noticed_notifications` on the model, the jobs for sending the notification will not be able to find the record when ActiveJob deserializes. You can discord the job on these errors by adding the following to `ApplicationJob`:
574
568
 
575
- def format_for_database
576
- {
577
- notifiable: params.delete(:post),
578
- type: self.class.name,
579
- params: params
580
- }
581
- end
582
- end
583
- ```
569
+ ```ruby
570
+ class ApplicationJob < ActiveJob::Base
571
+ discard_on ActiveJob::DeserializationError
572
+ end
573
+ ```
584
574
 
585
575
  ## 🙏 Contributing
586
576
 
@@ -43,12 +43,11 @@ module Noticed
43
43
 
44
44
  def params_column
45
45
  case ActiveRecord::Base.configurations.configs_for(spec_name: "primary").config["adapter"]
46
- when "mysql2"
47
- "params:json"
48
46
  when "postgresql"
49
47
  "params:jsonb"
50
48
  else
51
- "params:text"
49
+ # MySQL and SQLite both support json
50
+ "params:json"
52
51
  end
53
52
  end
54
53
  end
data/lib/noticed.rb CHANGED
@@ -5,6 +5,7 @@ require "noticed/engine"
5
5
  module Noticed
6
6
  autoload :Base, "noticed/base"
7
7
  autoload :Coder, "noticed/coder"
8
+ autoload :HasNotifications, "noticed/has_notifications"
8
9
  autoload :Model, "noticed/model"
9
10
  autoload :TextCoder, "noticed/text_coder"
10
11
  autoload :Translation, "noticed/translation"
@@ -1,4 +1,9 @@
1
1
  module Noticed
2
2
  class Engine < ::Rails::Engine
3
+ initializer "noticed.has_notifications" do
4
+ ActiveSupport.on_load(:active_record) do
5
+ include Noticed::HasNotifications
6
+ end
7
+ end
3
8
  end
4
9
  end
@@ -0,0 +1,32 @@
1
+ module Noticed
2
+ module HasNotifications
3
+ # Defines a method for the association and a before_destory callback to remove notifications
4
+ # where this record is a param
5
+ #
6
+ # class User < ApplicationRecord
7
+ # has_noticed_notifications
8
+ # has_noticed_notifications param_name: :owner, destroy: false, model: "Notification"
9
+ # end
10
+ #
11
+ # @user.notifications_as_user
12
+ # @user.notifications_as_owner
13
+
14
+ extend ActiveSupport::Concern
15
+
16
+ class_methods do
17
+ def has_noticed_notifications(param_name: model_name.singular, **options)
18
+ model = options.fetch(:model_name, "Notification").constantize
19
+
20
+ define_method "notifications_as_#{param_name}" do
21
+ model.where(params: {param_name.to_sym => self})
22
+ end
23
+
24
+ if options.fetch(:destroy, true)
25
+ before_destroy do
26
+ send("notifications_as_#{param_name}").destroy_all
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,3 +1,3 @@
1
1
  module Noticed
2
- VERSION = "1.2.21"
2
+ VERSION = "1.3.0"
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.21
4
+ version: 1.3.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: 2020-12-07 00:00:00.000000000 Z
11
+ date: 2021-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
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'
97
111
  description: Database, browser, realtime ActionCable, Email, SMS, Slack notifications,
98
112
  and more for Rails apps
99
113
  email:
@@ -124,6 +138,7 @@ files:
124
138
  - lib/noticed/delivery_methods/twilio.rb
125
139
  - lib/noticed/delivery_methods/vonage.rb
126
140
  - lib/noticed/engine.rb
141
+ - lib/noticed/has_notifications.rb
127
142
  - lib/noticed/model.rb
128
143
  - lib/noticed/notification_channel.rb
129
144
  - lib/noticed/text_coder.rb
@@ -149,7 +164,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
149
164
  - !ruby/object:Gem::Version
150
165
  version: '0'
151
166
  requirements: []
152
- rubygems_version: 3.1.4
167
+ rubygems_version: 3.2.3
153
168
  signing_key:
154
169
  specification_version: 4
155
170
  summary: Notifications for Ruby on Rails applications