notification_manager 0.1.0 → 0.1.2
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 +4 -4
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +8 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +21 -0
- data/README.md +288 -0
- data/Rakefile +12 -0
- data/bin/console +11 -0
- data/bin/setup +8 -0
- data/lib/generators/notification_manager/notification/notification_generator.rb +34 -0
- data/lib/generators/notification_manager/notifications_generator.rb +34 -0
- data/lib/generators/notification_manager/templates/create_notifications.rb +14 -0
- data/lib/generators/notification_manager/templates/notification_model.rb +6 -0
- data/lib/notification_manager/notification_definitions/base.rb +64 -0
- data/lib/notification_manager/notification_managers/base.rb +24 -0
- data/lib/notification_manager/notification_sources/active_record_notifiable.rb +25 -0
- data/lib/notification_manager/version.rb +5 -0
- data/lib/notification_manager.rb +10 -0
- data/notification_manager.gemspec +31 -0
- data/sig/notification_manager.rbs +4 -0
- data/spec/notification_manager_spec.rb +7 -0
- data/spec/spec_helper.rb +15 -0
- metadata +30 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e376c2270a096ff8d19676ce7487dfb9740acc3659ed7a41c789ce32417b3dd3
|
|
4
|
+
data.tar.gz: 17b18422bc6b2f193e53796d25d9dd5eccb8b5644641bbabb2dda30dde404d49
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '0749cbc681d8526d3f7a593aaf4b9f62323a60cd1880792c269a14b8ff9d6b488fad3a6b665d0b3999752744a3e89412f1c33f2e11494cf95b562cc37d1197ee'
|
|
7
|
+
data.tar.gz: acb945c0b0a496740a79535e97b315a91df1ff6a795ed6c2427bcf5e5c6414038591d8daa4ed641068b76773e90d3a88c74eba37876c8dcb684dd4646f3aaf4d
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
|
10
|
+
identity and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
|
26
|
+
community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
|
31
|
+
any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
|
35
|
+
without their explicit permission
|
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
+
decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official email address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
[INSERT CONTACT METHOD].
|
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
|
86
|
+
actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
|
93
|
+
ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
+
Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
|
113
|
+
community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.1, available at
|
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by
|
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
|
123
|
+
|
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
|
127
|
+
|
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 TODO: Write your name
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
# NotificationManager
|
|
2
|
+
|
|
3
|
+
A flexible notification management system for Rails applications that automatically triggers notifications based on ActiveRecord model lifecycle events and attribute changes.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🔔 Trigger notifications on model lifecycle events (create, update, destroy)
|
|
8
|
+
- 🎯 Trigger notifications based on attribute status changes
|
|
9
|
+
- 🏷️ Tag-based notification clearing to prevent duplicate notifications
|
|
10
|
+
- 👥 Support for multiple recipients per notification
|
|
11
|
+
- 🔄 Polymorphic notification system supporting any notifiable model
|
|
12
|
+
- ⏭️ Conditional notification skipping
|
|
13
|
+
- 🎨 Extensible architecture with customizable notification managers
|
|
14
|
+
|
|
15
|
+
## Requirements
|
|
16
|
+
|
|
17
|
+
- Ruby >= 3.2.0
|
|
18
|
+
- Rails >= 6.1
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
Add this line to your application's Gemfile:
|
|
23
|
+
|
|
24
|
+
```ruby
|
|
25
|
+
gem 'notification_manager'
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
And then execute:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
$ bundle install
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Usage
|
|
35
|
+
|
|
36
|
+
### 1. Generate the Notification Model
|
|
37
|
+
|
|
38
|
+
First, generate the notification model and migration:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
$ rails generate notification_manager:notifications
|
|
42
|
+
$ rails db:migrate
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
This creates:
|
|
46
|
+
- `app/models/notification.rb` - The Notification model
|
|
47
|
+
- `db/migrate/[timestamp]_create_notifications.rb` - The database migration
|
|
48
|
+
|
|
49
|
+
The notifications table includes:
|
|
50
|
+
- `user_id` - Reference to the user receiving the notification
|
|
51
|
+
- `notifiable` - Polymorphic reference to any notifiable model
|
|
52
|
+
- `description` - Notification message
|
|
53
|
+
- `link` - Optional link for the notification
|
|
54
|
+
- `tag` - Tag for grouping/clearing related notifications
|
|
55
|
+
- `opened` - Boolean flag to track read/unread status
|
|
56
|
+
|
|
57
|
+
### 2. Include the Notifiable Concern
|
|
58
|
+
|
|
59
|
+
Add the `ActiveRecordNotifiable` concern to any model you want to send notifications from:
|
|
60
|
+
|
|
61
|
+
```ruby
|
|
62
|
+
class Order < ApplicationRecord
|
|
63
|
+
include NotificationManager::NotificationSources::ActiveRecordNotifiable
|
|
64
|
+
|
|
65
|
+
# Your model code...
|
|
66
|
+
end
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
This adds an `after_commit` callback that automatically triggers notifications based on your notification manager configuration.
|
|
70
|
+
|
|
71
|
+
### 3. Create a Notification Manager
|
|
72
|
+
|
|
73
|
+
Create a notification manager class for your notifiable model. The class name should follow the pattern `[ModelName]NotificationManager`:
|
|
74
|
+
|
|
75
|
+
```ruby
|
|
76
|
+
# app/notification_managers/order_notification_manager.rb
|
|
77
|
+
class OrderNotificationManager < NotificationManager::NotificationManagers::Base
|
|
78
|
+
def notification_steps
|
|
79
|
+
[
|
|
80
|
+
order_created_notification,
|
|
81
|
+
order_shipped_notification,
|
|
82
|
+
order_delivered_notification
|
|
83
|
+
]
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
private
|
|
87
|
+
|
|
88
|
+
def order_created_notification
|
|
89
|
+
NotificationManager::NotificationDefinitions::Base.new(
|
|
90
|
+
lifecycle_triggers: [:created],
|
|
91
|
+
status_triggers: [],
|
|
92
|
+
notification_tags_to_clear: [],
|
|
93
|
+
notifications_to_send: [
|
|
94
|
+
{
|
|
95
|
+
recipients: :seller_recipient,
|
|
96
|
+
notification: :send_order_created_notification
|
|
97
|
+
}
|
|
98
|
+
]
|
|
99
|
+
)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def order_shipped_notification
|
|
103
|
+
NotificationManager::NotificationDefinitions::Base.new(
|
|
104
|
+
lifecycle_triggers: [:updated],
|
|
105
|
+
status_triggers: [
|
|
106
|
+
{
|
|
107
|
+
attr: :status,
|
|
108
|
+
triggers: -> { ['shipped'] }
|
|
109
|
+
}
|
|
110
|
+
],
|
|
111
|
+
notification_tags_to_clear: ['order_pending'],
|
|
112
|
+
notifications_to_send: [
|
|
113
|
+
{
|
|
114
|
+
recipients: :buyer_recipient,
|
|
115
|
+
notification: :send_order_shipped_notification
|
|
116
|
+
}
|
|
117
|
+
]
|
|
118
|
+
)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def order_delivered_notification
|
|
122
|
+
NotificationManager::NotificationDefinitions::Base.new(
|
|
123
|
+
lifecycle_triggers: [:updated],
|
|
124
|
+
status_triggers: [
|
|
125
|
+
{
|
|
126
|
+
attr: :status,
|
|
127
|
+
triggers: -> { ['delivered'] }
|
|
128
|
+
}
|
|
129
|
+
],
|
|
130
|
+
notification_tags_to_clear: ['order_shipped'],
|
|
131
|
+
notifications_to_send: [
|
|
132
|
+
{
|
|
133
|
+
recipients: :buyer_recipient,
|
|
134
|
+
notification: :send_order_delivered_notification
|
|
135
|
+
}
|
|
136
|
+
]
|
|
137
|
+
)
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# Recipient methods return arrays of users
|
|
141
|
+
def seller_recipient
|
|
142
|
+
[notifiable.seller]
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def buyer_recipient
|
|
146
|
+
[notifiable.buyer]
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# Notification sender methods
|
|
150
|
+
def send_order_created_notification(order, recipient)
|
|
151
|
+
Notification.create!(
|
|
152
|
+
user: recipient,
|
|
153
|
+
notifiable: order,
|
|
154
|
+
description: "New order ##{order.id} received",
|
|
155
|
+
link: "/orders/#{order.id}",
|
|
156
|
+
tag: 'order_created'
|
|
157
|
+
)
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def send_order_shipped_notification(order, recipient)
|
|
161
|
+
Notification.create!(
|
|
162
|
+
user: recipient,
|
|
163
|
+
notifiable: order,
|
|
164
|
+
description: "Your order ##{order.id} has been shipped",
|
|
165
|
+
link: "/orders/#{order.id}",
|
|
166
|
+
tag: 'order_shipped'
|
|
167
|
+
)
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def send_order_delivered_notification(order, recipient)
|
|
171
|
+
Notification.create!(
|
|
172
|
+
user: recipient,
|
|
173
|
+
notifiable: order,
|
|
174
|
+
description: "Your order ##{order.id} has been delivered",
|
|
175
|
+
link: "/orders/#{order.id}",
|
|
176
|
+
tag: 'order_delivered'
|
|
177
|
+
)
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### 4. Notification Definition Options
|
|
183
|
+
|
|
184
|
+
Each notification definition accepts the following options:
|
|
185
|
+
|
|
186
|
+
#### `lifecycle_triggers` (Array)
|
|
187
|
+
Specifies which lifecycle events should trigger the notification:
|
|
188
|
+
- `:created` - Triggers when the record is created
|
|
189
|
+
- `:updated` - Triggers when the record is updated
|
|
190
|
+
- `:destroyed` - Triggers when the record is destroyed
|
|
191
|
+
|
|
192
|
+
#### `status_triggers` (Array of Hashes)
|
|
193
|
+
Defines attribute-based conditions for triggering notifications:
|
|
194
|
+
```ruby
|
|
195
|
+
status_triggers: [
|
|
196
|
+
{
|
|
197
|
+
attr: :status, # The attribute to watch
|
|
198
|
+
triggers: -> { ['value'] } # Lambda returning array of trigger values
|
|
199
|
+
}
|
|
200
|
+
]
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
Use `-> { '*' }` to trigger on any change to the attribute.
|
|
204
|
+
|
|
205
|
+
#### `notification_tags_to_clear` (Array)
|
|
206
|
+
Array of notification tags to clear before sending new notifications. This prevents duplicate or outdated notifications:
|
|
207
|
+
```ruby
|
|
208
|
+
notification_tags_to_clear: ['order_pending', 'order_processing']
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
#### `notifications_to_send` (Array of Hashes)
|
|
212
|
+
Defines which notifications to send:
|
|
213
|
+
```ruby
|
|
214
|
+
notifications_to_send: [
|
|
215
|
+
{
|
|
216
|
+
recipients: :method_name, # Method that returns array of users
|
|
217
|
+
notification: :method_name # Method that creates the notification
|
|
218
|
+
}
|
|
219
|
+
]
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### 5. Skipping Notifications
|
|
223
|
+
|
|
224
|
+
You can temporarily skip notifications for a specific operation:
|
|
225
|
+
|
|
226
|
+
```ruby
|
|
227
|
+
order = Order.new(...)
|
|
228
|
+
order.skip_notifications = true
|
|
229
|
+
order.save
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
## Advanced Examples
|
|
233
|
+
|
|
234
|
+
### Multiple Recipients
|
|
235
|
+
|
|
236
|
+
```ruby
|
|
237
|
+
def admin_recipients
|
|
238
|
+
User.where(role: 'admin').to_a
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
def stakeholder_recipients
|
|
242
|
+
[notifiable.owner, notifiable.manager].compact
|
|
243
|
+
end
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
### Conditional Status Triggers
|
|
247
|
+
|
|
248
|
+
```ruby
|
|
249
|
+
status_triggers: [
|
|
250
|
+
{
|
|
251
|
+
attr: :priority,
|
|
252
|
+
triggers: -> { ['high', 'critical'] }
|
|
253
|
+
},
|
|
254
|
+
{
|
|
255
|
+
attr: :assigned_to_id,
|
|
256
|
+
triggers: -> { '*' } # Trigger on any assignment change
|
|
257
|
+
}
|
|
258
|
+
]
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### Chaining Notifications
|
|
262
|
+
|
|
263
|
+
Clear previous notifications when sending new ones to maintain a clean notification state:
|
|
264
|
+
|
|
265
|
+
```ruby
|
|
266
|
+
NotificationManager::NotificationDefinitions::Base.new(
|
|
267
|
+
lifecycle_triggers: [:updated],
|
|
268
|
+
status_triggers: [{ attr: :status, triggers: -> { ['completed'] } }],
|
|
269
|
+
notification_tags_to_clear: ['task_pending', 'task_in_progress'],
|
|
270
|
+
notifications_to_send: [
|
|
271
|
+
{ recipients: :task_owner, notification: :send_completion_notification }
|
|
272
|
+
]
|
|
273
|
+
)
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
## Development
|
|
277
|
+
|
|
278
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
279
|
+
|
|
280
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
|
281
|
+
|
|
282
|
+
## Contributing
|
|
283
|
+
|
|
284
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/CodeTectonics/notification-manager. This project is intended to be a safe, welcoming space for collaboration.
|
|
285
|
+
|
|
286
|
+
## License
|
|
287
|
+
|
|
288
|
+
The gem is available as open source under the terms of the [MIT License](LICENSE.txt).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "bundler/setup"
|
|
5
|
+
require "notification_manager"
|
|
6
|
+
|
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
9
|
+
|
|
10
|
+
require "irb"
|
|
11
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
require "rails/generators"
|
|
2
|
+
require "rails/generators/named_base"
|
|
3
|
+
require "rails/generators/migration"
|
|
4
|
+
|
|
5
|
+
module AnalyticsPlane
|
|
6
|
+
module Generators
|
|
7
|
+
class NotificationGenerator < Rails::Generators::NamedBase
|
|
8
|
+
include Rails::Generators::Migration
|
|
9
|
+
|
|
10
|
+
source_root File.expand_path("../templates", __dir__)
|
|
11
|
+
|
|
12
|
+
desc "Creates a Notification model and migration for NotificationManager"
|
|
13
|
+
|
|
14
|
+
def create_model
|
|
15
|
+
template "notification_model.rb", "app/models/#{file_name}.rb"
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def create_notification_migration
|
|
19
|
+
migration_template(
|
|
20
|
+
"create_notifications.rb",
|
|
21
|
+
"db/migrate/create_#{file_name.pluralize}.rb"
|
|
22
|
+
)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.next_migration_number(dirname)
|
|
26
|
+
if @next_migration_number
|
|
27
|
+
@next_migration_number += 1
|
|
28
|
+
else
|
|
29
|
+
@next_migration_number = Time.now.utc.strftime("%Y%m%d%H%M%S")
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
require "rails/generators"
|
|
2
|
+
require "rails/generators/named_base"
|
|
3
|
+
require "rails/generators/migration"
|
|
4
|
+
|
|
5
|
+
module NotificationManager
|
|
6
|
+
module Generators
|
|
7
|
+
class NotificationsGenerator < Rails::Generators::Base
|
|
8
|
+
include Rails::Generators::Migration
|
|
9
|
+
|
|
10
|
+
source_root File.expand_path("templates", __dir__)
|
|
11
|
+
|
|
12
|
+
desc "Creates the Notification model and miration for use with Notification Managers."
|
|
13
|
+
|
|
14
|
+
def create_models
|
|
15
|
+
template "notification_model.rb", "app/models/notification.rb"
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def create_migrations
|
|
19
|
+
migration_template "create_notifications.rb",
|
|
20
|
+
"db/migrate/create_notifications.rb"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.next_migration_number(dirname)
|
|
24
|
+
if ActiveRecord::Base.timestamped_migrations
|
|
25
|
+
@@migration_number ||= Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
|
|
26
|
+
@@migration_number += 1
|
|
27
|
+
@@migration_number.to_s
|
|
28
|
+
else
|
|
29
|
+
format("%03d", current_migration_number(dirname) + 1)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
class CreateNotifications < ActiveRecord::Migration[7.0]
|
|
2
|
+
def change
|
|
3
|
+
create_table :notifications do |t|
|
|
4
|
+
t.references :user, foreign_key: true
|
|
5
|
+
t.references :notifiable, polymorphic: true
|
|
6
|
+
t.string :description
|
|
7
|
+
t.string :link
|
|
8
|
+
t.string :tag
|
|
9
|
+
t.boolean :opened, default: false, null: false
|
|
10
|
+
|
|
11
|
+
t.timestamps
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
module NotificationManager
|
|
2
|
+
module NotificationDefinitions
|
|
3
|
+
class Base
|
|
4
|
+
attr_accessor :lifecycle_triggers, :status_triggers, :notification_tags_to_clear,
|
|
5
|
+
:notifications_to_send
|
|
6
|
+
|
|
7
|
+
def initialize(options)
|
|
8
|
+
@lifecycle_triggers = options[:lifecycle_triggers] || []
|
|
9
|
+
@status_triggers = options[:status_triggers]
|
|
10
|
+
@notification_tags_to_clear = options[:notification_tags_to_clear] || []
|
|
11
|
+
@notifications_to_send = options[:notifications_to_send] || []
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def triggered?(notifiable)
|
|
15
|
+
lifecycle_event_reached?(notifiable) && status_reached?(notifiable)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def lifecycle_event_reached?(notifiable)
|
|
19
|
+
if notifiable.created_at_previously_changed?
|
|
20
|
+
lifecycle_triggers.include?(:created)
|
|
21
|
+
elsif notifiable.updated_at_previously_changed?
|
|
22
|
+
lifecycle_triggers.include?(:updated)
|
|
23
|
+
elsif notifiable.destroyed?
|
|
24
|
+
lifecycle_triggers.include?(:destroyed)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def status_reached?(notifiable)
|
|
29
|
+
return true if status_triggers.empty?
|
|
30
|
+
|
|
31
|
+
status_triggers.each do |event|
|
|
32
|
+
next unless notifiable.send("#{event[:attr]}_previously_changed?")
|
|
33
|
+
|
|
34
|
+
triggers = event[:triggers].yield
|
|
35
|
+
|
|
36
|
+
return true if triggers == '*'
|
|
37
|
+
|
|
38
|
+
return true if notifiable[event[:attr]].in?(triggers)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
false
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def clear_notifications(notifiable)
|
|
45
|
+
return if notification_tags_to_clear.empty?
|
|
46
|
+
|
|
47
|
+
Notification.where(
|
|
48
|
+
notifiable_id: notifiable.id,
|
|
49
|
+
notifiable_type: notifiable.class.name,
|
|
50
|
+
tag: notification_tags_to_clear
|
|
51
|
+
).destroy_all
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def send_notifications(notification_manager, notifiable)
|
|
55
|
+
notifications_to_send.each do |notification|
|
|
56
|
+
recipients = notification_manager.send(notification[:recipients])
|
|
57
|
+
recipients.compact.each do |recipient|
|
|
58
|
+
notification_manager.send(notification[:notification], notifiable, recipient)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module NotificationManager
|
|
2
|
+
module NotificationManagers
|
|
3
|
+
class Base
|
|
4
|
+
attr_accessor :notifiable
|
|
5
|
+
|
|
6
|
+
def initialize(notifiable)
|
|
7
|
+
@notifiable = notifiable
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def run_notification_flow
|
|
11
|
+
notification_steps.each do |step|
|
|
12
|
+
next unless step.triggered?(@notifiable)
|
|
13
|
+
|
|
14
|
+
step.clear_notifications(@notifiable)
|
|
15
|
+
step.send_notifications(self, @notifiable)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def notification_steps
|
|
20
|
+
raise "Implement in subclass"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module NotificationManager
|
|
2
|
+
module NotificationSources
|
|
3
|
+
module ActiveRecordNotifiable
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
|
|
6
|
+
included do
|
|
7
|
+
attr_accessor :skip_notifications
|
|
8
|
+
|
|
9
|
+
after_commit :send_notifications
|
|
10
|
+
|
|
11
|
+
def send_notifications
|
|
12
|
+
return if skip_notifications
|
|
13
|
+
return if notification_manager_class_name.nil?
|
|
14
|
+
|
|
15
|
+
notification_manager = notification_manager_class_name.constantize.new(self)
|
|
16
|
+
notification_manager.run_notification_flow
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def notification_manager_class_name
|
|
20
|
+
"#{self.class.name}NotificationManager"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "notification_manager/version"
|
|
4
|
+
require_relative "notification_manager/notification_definitions/base"
|
|
5
|
+
require_relative "notification_manager/notification_managers/base"
|
|
6
|
+
require_relative "notification_manager/notification_sources/active_record_notifiable"
|
|
7
|
+
|
|
8
|
+
module NotificationManager
|
|
9
|
+
class Error < StandardError; end
|
|
10
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "lib/notification_manager/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "notification_manager"
|
|
7
|
+
spec.version = NotificationManager::VERSION
|
|
8
|
+
spec.authors = ["Mark Harbison"]
|
|
9
|
+
spec.email = ["mark@tyne-solutions.com"]
|
|
10
|
+
|
|
11
|
+
spec.summary = "Notification manager."
|
|
12
|
+
spec.description = "Notification manager."
|
|
13
|
+
spec.homepage = "https://github.com/CodeTectonics/notification-manager"
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
spec.required_ruby_version = ">= 3.2.0"
|
|
16
|
+
|
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
18
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
|
19
|
+
spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
|
|
20
|
+
|
|
21
|
+
spec.files = `git ls-files`.split("\n")
|
|
22
|
+
spec.test_files = `git ls-files -- spec/*`.split("\n")
|
|
23
|
+
spec.require_paths = ["lib"]
|
|
24
|
+
|
|
25
|
+
# Uncomment to register a new dependency of your gem
|
|
26
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
|
27
|
+
spec.add_dependency "rails", ">= 6.1"
|
|
28
|
+
|
|
29
|
+
# For more information and examples about making a new gem, check out our
|
|
30
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
|
31
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "notification_manager"
|
|
4
|
+
|
|
5
|
+
RSpec.configure do |config|
|
|
6
|
+
# Enable flags like --only-failures and --next-failure
|
|
7
|
+
config.example_status_persistence_file_path = ".rspec_status"
|
|
8
|
+
|
|
9
|
+
# Disable RSpec exposing methods globally on `Module` and `main`
|
|
10
|
+
config.disable_monkey_patching!
|
|
11
|
+
|
|
12
|
+
config.expect_with :rspec do |c|
|
|
13
|
+
c.syntax = :expect
|
|
14
|
+
end
|
|
15
|
+
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: notification_manager
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Mark Harbison
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-09-
|
|
11
|
+
date: 2026-09-02 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rails
|
|
@@ -30,7 +30,31 @@ email:
|
|
|
30
30
|
executables: []
|
|
31
31
|
extensions: []
|
|
32
32
|
extra_rdoc_files: []
|
|
33
|
-
files:
|
|
33
|
+
files:
|
|
34
|
+
- ".gitignore"
|
|
35
|
+
- ".rspec"
|
|
36
|
+
- ".rubocop.yml"
|
|
37
|
+
- CHANGELOG.md
|
|
38
|
+
- CODE_OF_CONDUCT.md
|
|
39
|
+
- Gemfile
|
|
40
|
+
- LICENSE.txt
|
|
41
|
+
- README.md
|
|
42
|
+
- Rakefile
|
|
43
|
+
- bin/console
|
|
44
|
+
- bin/setup
|
|
45
|
+
- lib/generators/notification_manager/notification/notification_generator.rb
|
|
46
|
+
- lib/generators/notification_manager/notifications_generator.rb
|
|
47
|
+
- lib/generators/notification_manager/templates/create_notifications.rb
|
|
48
|
+
- lib/generators/notification_manager/templates/notification_model.rb
|
|
49
|
+
- lib/notification_manager.rb
|
|
50
|
+
- lib/notification_manager/notification_definitions/base.rb
|
|
51
|
+
- lib/notification_manager/notification_managers/base.rb
|
|
52
|
+
- lib/notification_manager/notification_sources/active_record_notifiable.rb
|
|
53
|
+
- lib/notification_manager/version.rb
|
|
54
|
+
- notification_manager.gemspec
|
|
55
|
+
- sig/notification_manager.rbs
|
|
56
|
+
- spec/notification_manager_spec.rb
|
|
57
|
+
- spec/spec_helper.rb
|
|
34
58
|
homepage: https://github.com/CodeTectonics/notification-manager
|
|
35
59
|
licenses:
|
|
36
60
|
- MIT
|
|
@@ -57,4 +81,6 @@ rubygems_version: 3.4.19
|
|
|
57
81
|
signing_key:
|
|
58
82
|
specification_version: 4
|
|
59
83
|
summary: Notification manager.
|
|
60
|
-
test_files:
|
|
84
|
+
test_files:
|
|
85
|
+
- spec/notification_manager_spec.rb
|
|
86
|
+
- spec/spec_helper.rb
|