notification-settings 1.2.6 → 2.0.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: 4193e47d53d1953047f50b4e5d47cced6258e9bfb55a95f65e8bf387025cce93
4
- data.tar.gz: 967ff77853953c85928e67b1ba76091c8ddd97111f7207ed5c53271dfe1b594f
3
+ metadata.gz: 1be12c7320f2f51d85d54709ccec7f39fd32e9ec3f678c0635f845b47a282a77
4
+ data.tar.gz: dc137bbc872c9a9316c1b0f89a2fc227cb12e05d259f1fa5382fdd11d8598cd2
5
5
  SHA512:
6
- metadata.gz: 7e744b92cb72fc7b3bc1c4e3b278a9e31c0a3cd915986cd913b7adcf49db1809e70569ef88c9719dfeed66ffeaf527ad5fe0517acaf3190f31bdcc0556ca9af9
7
- data.tar.gz: 163007c12e9559d559defa7fe71982c58fc6a2f7b46887843cbe7fd06f3e0375e1ffda7e062c3adcf6f7b8b397b5607afccb7dfbaa7219b7644e159623acf326
6
+ metadata.gz: efa0e738bc0e76b48f27698665a37fcebed7bb771719673ccca50a23668824a3626ad75436f141db27d781d4e9205f7c1a6dfbc4e3d6812bb848eed8612b1d2d
7
+ data.tar.gz: 79303e8bc9c434431e57b3cf6992b6f856108e72552503251b46b9b205c4bfdc16cf4efcc6e7cf69c7a242eb3425eea436df0cd8fb6a85defa05b0faa4377295
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # NotificationSettings
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/notification-settings.svg)](https://badge.fury.io/rb/notification-settings) <img src="https://travis-ci.org/jonhue/notifications-rails.svg?branch=master" />
3
+ [![Gem Version](https://badge.fury.io/rb/notification-settings.svg)](https://badge.fury.io/rb/notification-settings) ![Travis](https://travis-ci.com/jonhue/notifications-rails.svg?branch=master)
4
4
 
5
5
  Integrates with your authentication solution to craft a personalized user notification platform.
6
6
 
@@ -10,20 +10,18 @@ Integrates with your authentication solution to craft a personalized user notifi
10
10
 
11
11
  * [Installation](#installation)
12
12
  * [Usage](#usage)
13
- * [Categories](#categories)
14
- * [Settings](#settings)
15
- * [Category specific settings](#category-specific-settings)
16
- * [Pusher specific settings](#pusher-specific-settings)
17
- * [Updating settings](#updating-settings)
18
- * [Subscriptions](#subscriptions)
19
- * [Status](#status)
13
+ * [Categories](#categories)
14
+ * [Settings](#settings)
15
+ * [Category-specific settings](#category-specific-settings)
16
+ * [Pusher-specific settings](#pusher-specific-settings)
17
+ * [Updating settings](#updating-settings)
18
+ * [Subscriptions](#subscriptions)
19
+ * [Status](#status)
20
20
  * [Configuration](#configuration)
21
- * [Status](#status)
21
+ * [Status](#status)
22
22
  * [To Do](#to-do)
23
23
  * [Contributing](#contributing)
24
- * [Contributors](#contributors)
25
- * [Semantic versioning](#semantic-versioning)
26
- * [License](#license)
24
+ * [Semantic versioning](#semantic-versioning)
27
25
 
28
26
  ---
29
27
 
@@ -43,14 +41,9 @@ Or install it yourself as:
43
41
 
44
42
  $ gem install notification-settings
45
43
 
46
- If you always want to be up to date fetch the latest from GitHub in your `Gemfile`:
47
-
48
- ```ruby
49
- gem 'notification-settings', github: 'jonhue/notifications-rails'
50
- ```
51
-
52
44
  Now run the generator:
53
45
 
46
+ $ rails g notification_handler:install
54
47
  $ rails g notification_settings:install
55
48
 
56
49
  To wrap things up, migrate the changes to your database:
@@ -61,10 +54,22 @@ To wrap things up, migrate the changes to your database:
61
54
 
62
55
  ## Usage
63
56
 
64
- NotificationSettings will create a `NotificationSettings::Setting` record for every newly created `notification_target`-object. It is accessible by calling:
57
+ First, you have to add some attributes to every model that acts as a `notification_target`:
65
58
 
66
59
  ```ruby
67
- User.first.notification_setting
60
+ class User
61
+ # ...
62
+
63
+ notification_target
64
+ end
65
+ ```
66
+
67
+ ```ruby
68
+ # A Hashie::Mash object that stores all the settings of a notification target.
69
+ add_column :users, :settings, :text
70
+
71
+ # A string that describes a notification-relevant state of a notification target.
72
+ add_column :users, :status, :string
68
73
  ```
69
74
 
70
75
  ### Categories
@@ -72,7 +77,7 @@ User.first.notification_setting
72
77
  NotificationSettings uses categories to allow your notification targets to define specific preferences. This is how you are able to specify the `category` of a `Notification` record:
73
78
 
74
79
  ```ruby
75
- notification = Notification.create target: User.first, object: Recipe.first, category: 'notification'
80
+ notification = Notification.create(target: User.first, object: Recipe.first, category: :notification)
76
81
  ```
77
82
 
78
83
  **Note:** The `category` attribute of any new `Notification` record will default to the [`default_category` configuration](#configuration).
@@ -89,35 +94,82 @@ Notification.follow_category
89
94
 
90
95
  ### Settings
91
96
 
92
- You can disable notifications for a given notification target:
97
+ You can completely disable notifications for a given notification target:
93
98
 
94
99
  ```ruby
95
- s = User.first.notification_setting
96
- s.settings[:enabled] = false
100
+ settings = User.first.settings
101
+
102
+ settings.enabled = false
97
103
  ```
98
104
 
99
105
  This will prevent you from creating any new notifications with this user as target.
100
106
 
101
- #### Category specific settings
107
+ The default is `true` (enabled) for this setting and the other settings.
108
+
109
+ #### Category-specific settings
110
+
111
+ A user can also have category-specific settings:
102
112
 
103
- A user can also have category specific settings:
113
+ ```ruby
114
+ settings.categories!.send("#{category}!").enabled = false
115
+ ```
116
+
117
+ #### Delivery-method-specific settings
118
+
119
+ He can have global or category-specific delivery method settings:
104
120
 
105
121
  ```ruby
106
- s.category_settings[:category] = { enabled: false }
122
+ settings.delivery_methods!.enabled = false # Prevent delivering via *any* delivery method
123
+ settings.delivery_methods!.email!.enabled = false # Prevent delivering via the :email delivery method
124
+ settings.categories!.category!.delivery_methods!.email!.enabled = false # Prevent delivering via the :email delivery method for :category
107
125
  ```
108
126
 
109
- #### Pusher specific settings
127
+ #### Form objects
110
128
 
111
- He can have global or category specific pusher settings:
129
+ NotificationSettings comes with three form objects that simplify building forms for updating settings.
130
+
131
+ ##### PreferencesForm
132
+
133
+ The preferences form has just one attribute: `enabled`. It can be used to update the global notification setting.
112
134
 
113
135
  ```ruby
114
- s.settings[:ActionMailer] = false
115
- s.category_settings[:category] = { ActionMailer: false }
136
+ user = User.first
137
+
138
+ form = NotificationSettings::PreferencesForm.new(enabled: user.settings.enabled)
139
+ form.valid?
140
+
141
+ user.settings.enabled = form.enabled
116
142
  ```
117
143
 
118
- #### Updating settings
144
+ ##### CategoryPreferencesForm
145
+
146
+ The category preferences form has one attribute per category. It can be used to update category-specific settings.
147
+
148
+ ```ruby
149
+ user = User.first
119
150
 
120
- ...
151
+ form = NotificationSettings::CategoryPreferencesForm.new(user.settings.categories_.to_h)
152
+ form.valid?
153
+
154
+ form.changed_attributes.each do |category|
155
+ user.settings.categories!.send(category, form.send(category))
156
+ end
157
+ ```
158
+
159
+ ##### DeliveryMethodPreferencesForm
160
+
161
+ The delivery method preferences form has one attribute per category and a general `enabled` attribute. It can be used to update delivery-method-specific settings or to disable all delivery methods.
162
+
163
+ ```ruby
164
+ user = User.first
165
+
166
+ form = NotificationSettings::DeliveryMethodPreferencesForm.new(user.settings.delivery_methods_.to_h)
167
+ form.valid?
168
+
169
+ form.changed_attributes.each do |delivery_method|
170
+ user.settings.delivery_methods!.send(delivery_method, form.send(delivery_method))
171
+ end
172
+ ```
121
173
 
122
174
  ### Subscriptions
123
175
 
@@ -126,14 +178,14 @@ Subscriptions are a way to better handle settings for notifications from differe
126
178
  This is how to subscribe/unsubscribe a target to an object:
127
179
 
128
180
  ```ruby
129
- User.first.subscribe Recipe.first
130
- User.first.unsubscribe Recipe.first
181
+ User.first.subscribe(Recipe.first)
182
+ User.first.unsubscribe(Recipe.first)
131
183
  ```
132
184
 
133
185
  Now you can easily notify all subscribers from the subscribable object:
134
186
 
135
187
  ```ruby
136
- Recipe.first.notify_subscribers push: :ActionMailer
188
+ Recipe.first.notify_subscribers(delivery_method: :ActionMailer)
137
189
  ```
138
190
 
139
191
  Let's assume that we have a group which has multiple chats. When sending notifications to subscribers of a given chat, we only want them to get notified. But when sending notifications about the group, we want to have everyone notified, that is either subscribed to the group or subscribed to one of its chats. To do that you have to add the `private` method `notification_dependents` to your model (in this case `Group`) and return an array of ActiveRecord objects whose subscribers should receive notifications for objects of this class.
@@ -145,7 +197,7 @@ has_many :talks
145
197
  private
146
198
 
147
199
  def notification_dependents
148
- self.chats
200
+ self.chats
149
201
  end
150
202
  ```
151
203
 
@@ -153,21 +205,41 @@ It is possible to override that behavior when notifying subscribers:
153
205
 
154
206
  ```ruby
155
207
  # Disable notification dependents
156
- Group.first.notify_subscribers dependents: nil
208
+ Group.first.notify_subscribers(dependents: nil)
157
209
 
158
210
  # Override notification dependents
159
- Group.first.notify_subscribers dependents: Group.first.chats + Group.first.talks
211
+ Group.first.notify_subscribers(dependents: Group.first.chats + Group.first.talks)
160
212
  ```
161
213
 
162
- You can customize settings for a single subscription just as you would for a notification target:
214
+ You can customize settings & status for a single subscription just as you would for a notification target:
163
215
 
164
216
  ```ruby
165
- s = User.first.notification_subscriptions.first.notification_setting
166
- s.settings[:enabled] = false
217
+ user = User.first
218
+ subscription = user.notification_subscriptions.first
219
+
220
+ subscription.settings.enabled = false
221
+ subscription.status = 'online'
222
+ subscription.save
167
223
  ```
168
224
 
169
225
  [Learn more](#settings)
170
226
 
227
+ You can add associations to your subscriber/subscribable classes if you need easy access to all their subscriptions:
228
+
229
+ ```ruby
230
+ # For subscribers:
231
+ # List all subscriptions of a subscriber
232
+ subscriber.notification_subscribables
233
+ # List all subscribables of a specific type a subscriber subscribed to
234
+ has_many :subscribed_products, through: :notification_subscribables, source: :subscribable, source_type: 'Product'
235
+
236
+ # For subscribables:
237
+ # List all subscriptions of a subscriber
238
+ subscribable.notification_subscribers
239
+ # List all subscribers of a specific type that subscribed to a subscribable
240
+ has_many :subscribed_users, through: :notification_subscribers, source: :subscriber, source_type: 'User'
241
+ ```
242
+
171
243
  ### Status
172
244
 
173
245
  NotificationSettings comes with a handy feature called Status. The status of a record can temporarily disable the ability to create notifications for or to push notifications of a target.
@@ -175,8 +247,7 @@ NotificationSettings comes with a handy feature called Status. The status of a r
175
247
  This is how to define a status:
176
248
 
177
249
  ```ruby
178
- User.first.notification_setting.status = 'do not disturb'
179
- User.first.notification_setting.save
250
+ User.first.update(status: 'do not disturb')
180
251
  ```
181
252
 
182
253
  **Note:** You can set `status` to any string you like.
@@ -185,12 +256,12 @@ User.first.notification_setting.save
185
256
 
186
257
  If you have set `status` to a custom value, you can get back to using the defaults by setting it back to `nil`.
187
258
 
188
- You can define statuses that prevent creating new notifications for a target and statuses that just prevent pushing them:
259
+ You can define statuses that prevent creating new notifications for a target and statuses that just prevent delivering them:
189
260
 
190
261
  ```ruby
191
262
  NotificationSettings.configure do |config|
192
- config.do_not_notify_statuses = ['do not notify']
193
- config.do_not_push_statuses = ['do not disturb']
263
+ config.do_not_notify_statuses = ['do not notify']
264
+ config.do_not_deliver_statuses = ['do not disturb']
194
265
  end
195
266
  ```
196
267
 
@@ -202,11 +273,13 @@ You can configure NotificationSettings by passing a block to `configure`. This c
202
273
 
203
274
  ```ruby
204
275
  NotificationSettings.configure do |config|
205
- config.default_category = 'notification'
276
+ config.categories = [:notification]
206
277
  end
207
278
  ```
208
279
 
209
- **`default_category`** Choose your default notification category. Takes a string. Defaults to `'notification'`.
280
+ **`categories`** Choose your default notification category. Takes an array of symbols. Defaults to `[:notification]`.
281
+
282
+ **`default_category`** Choose your default notification category. Takes a symbol. Defaults to `:notification`.
210
283
 
211
284
  ### Status
212
285
 
@@ -214,17 +287,19 @@ end
214
287
 
215
288
  **`offline_after`** Time duration without activity after which the status defaults to `'offline'`. Takes a time. Defaults to `3.hours`.
216
289
 
217
- **`last_seen`** Stringified datetime attribute name of `object` that defines the time of the last activity. Takes a string. Defaults to `'last_seen'`.
290
+ **`last_seen`** Stringified datetime attribute name of `object` that defines the time of the last activity. Takes a symbol. Defaults to `:last_seen`.
291
+
292
+ **`statuses`** Array of all possible statuses. Takes an array of strings. Defaults to `['online', 'idle', 'offline', 'do not notify', 'do not disturb']`.
218
293
 
219
- **`do_not_notify_statuses`** Array of possible statuses that will prevent creating notifications for a target. Takes an array of strings. Defaults to `[]`.
294
+ **`do_not_notify_statuses`** Array of possible statuses that will prevent creating notifications for a target. Takes an array of strings. Defaults to `['do not notify']`.
220
295
 
221
- **`do_not_push_statuses`** Array of possible statuses that will prevent pushing notifications of a target. Takes an array of strings. Defaults to `['do not disturb']`
296
+ **`do_not_deliver_statuses`** Array of possible statuses that will prevent delivering notifications of a target. Takes an array of strings. Defaults to `['do not disturb']`
222
297
 
223
298
  ---
224
299
 
225
300
  ## To Do
226
301
 
227
- [Here](https://github.com/jonhue/notifications-rails/projects/4) is the full list of current projects.
302
+ We use [GitHub projects](https://github.com/jonhue/notifications-rails/projects/4) to coordinate the work on this project.
228
303
 
229
304
  To propose your ideas, initiate the discussion by adding a [new issue](https://github.com/jonhue/notifications-rails/issues/new).
230
305
 
@@ -236,36 +311,6 @@ We hope that you will consider contributing to NotificationSettings. Please read
236
311
 
237
312
  [Learn more about contributing to this repository](https://github.com/jonhue/notifications-rails/blob/master/CONTRIBUTING.md), [Code of Conduct](https://github.com/jonhue/notifications-rails/blob/master/CODE_OF_CONDUCT.md)
238
313
 
239
- ### Contributors
240
-
241
- Give the people some :heart: who are working on this project. See them all at:
242
-
243
- https://github.com/jonhue/notifications-rails/graphs/contributors
244
-
245
314
  ### Semantic Versioning
246
315
 
247
316
  NotificationSettings follows Semantic Versioning 2.0 as defined at http://semver.org.
248
-
249
- ## License
250
-
251
- MIT License
252
-
253
- Copyright (c) 2017 Jonas Hübotter
254
-
255
- Permission is hereby granted, free of charge, to any person obtaining a copy
256
- of this software and associated documentation files (the "Software"), to deal
257
- in the Software without restriction, including without limitation the rights
258
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
259
- copies of the Software, and to permit persons to whom the Software is
260
- furnished to do so, subject to the following conditions:
261
-
262
- The above copyright notice and this permission notice shall be included in all
263
- copies or substantial portions of the Software.
264
-
265
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
266
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
267
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
268
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
269
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
270
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
271
- SOFTWARE.
@@ -4,12 +4,11 @@ module NotificationSettings
4
4
  class Subscription < ActiveRecord::Base
5
5
  self.table_name = 'notification_settings_subscriptions'
6
6
 
7
- include NotificationSettings::SubscriptionLibrary
7
+ include NotificationSettings::Settings
8
8
 
9
9
  belongs_to :subscriber, polymorphic: true
10
10
  belongs_to :subscribable, polymorphic: true
11
11
 
12
12
  has_many :notifications, class_name: '::Notification'
13
- has_one :notification_setting, class_name: 'Setting'
14
13
  end
15
14
  end
@@ -19,7 +19,7 @@ module NotificationSettings
19
19
  end
20
20
 
21
21
  def create_initializer
22
- template 'initializer.rb', 'config/initializers/notification-settings.rb'
22
+ template 'initializer.rb', 'config/initializers/notification_settings.rb'
23
23
  end
24
24
 
25
25
  def create_notifications_migration_file
@@ -34,6 +34,7 @@ module NotificationSettings
34
34
 
35
35
  def migration_version
36
36
  return unless Rails.version >= '5.0.0'
37
+
37
38
  "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
38
39
  end
39
40
  end
@@ -1,8 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  NotificationSettings.configure do |config|
4
- # Choose your default notification category. Takes a string.
5
- # config.default_category = 'notification'
4
+ # An array of all notification categories. Takes an array of symbols.
5
+ # config.categories = [:notification]
6
+
7
+ # Choose your default notification category. Takes a symbol.
8
+ # config.default_category = :notification
6
9
 
7
10
  ### STATUS ###
8
11
 
@@ -15,14 +18,23 @@ NotificationSettings.configure do |config|
15
18
  # config.offline_after = 3.hours
16
19
 
17
20
  # Stringified datetime attribute name of `object` that defines the time of the
18
- # last activity. Takes a string.
19
- # config.last_seen = 'last_seen'
21
+ # last activity. Takes a symbol.
22
+ # config.last_seen = :last_seen
23
+
24
+ # Array of all possible statuses. Takes an array of strings.
25
+ # config.statuses = [
26
+ # 'online',
27
+ # 'idle',
28
+ # 'offline',
29
+ # 'do not notify',
30
+ # 'do not disturb'
31
+ # ]
20
32
 
21
33
  # Array of possible statuses that will prevent creating notifications for a
22
34
  # target. Takes an array of strings.
23
- # config.do_not_notify_statuses = []
35
+ # config.do_not_notify_statuses = ['do not notify']
24
36
 
25
37
  # Array of possible statuses that will prevent pushing notifications of a
26
38
  # target. Takes an array of strings.
27
- # config.do_not_push_statuses = ['do not disturb']
39
+ # config.do_not_deliver_statuses = ['do not disturb']
28
40
  end
@@ -5,21 +5,19 @@ class NotificationSettingsMigration < ActiveRecord::Migration<%= migration_versi
5
5
  add_column :notifications, :subscription_id, :bigint, index: true
6
6
  add_column :notifications, :category, :string, index: true
7
7
 
8
- create_table :notification_settings_settings do |t|
9
- t.references :object, polymorphic: true, index: { name: 'idx_settings_object_type_object_id' }
10
- t.references :subscription, index: true
11
-
12
- t.string :status
8
+ create_table :notification_settings_subscriptions do |t|
9
+ t.references :subscriber,
10
+ polymorphic: true,
11
+ index: {
12
+ name: 'idx_subscriptions_subscriber_type_subscriber_id'
13
+ }
14
+ t.references :subscribable,
15
+ polymorphic: true,
16
+ index: {
17
+ name: 'idx_subscriptions_subscribable_type_subscribable_id'
18
+ }
13
19
 
14
20
  t.text :settings
15
- t.text :category_settings
16
-
17
- t.timestamps
18
- end
19
-
20
- create_table :notification_settings_subscriptions do |t|
21
- t.references :subscriber, polymorphic: true, index: { name: 'idx_subscriptions_subscriber_type_subscriber_id' }
22
- t.references :subscribable, polymorphic: true, index: { name: 'idx_subscriptions_subscribable_type_subscribable_id' }
23
21
 
24
22
  t.timestamps
25
23
  end