notification-renderer 1.2.5 → 1.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +21 -21
- data/README.md +328 -328
- data/app/helpers/notification_renderer_helper.rb +80 -63
- data/lib/generators/notification_renderer/install_generator.rb +33 -36
- data/lib/generators/notification_renderer/type_generator.rb +23 -14
- data/lib/generators/templates/install/initializer.rb +12 -12
- data/lib/generators/templates/install/notifications_migration.rb.erb +5 -3
- data/lib/generators/templates/type/_template.html +7 -7
- data/lib/notification-renderer.rb +10 -10
- data/lib/notification_renderer/configuration.rb +24 -26
- data/lib/notification_renderer/engine.rb +9 -7
- data/lib/notification_renderer/notification_library.rb +49 -45
- data/lib/notification_renderer/notification_scopes.rb +24 -26
- metadata +46 -34
- data/CHANGELOG.md +0 -46
- data/lib/generators/templates/install/README.md +0 -1
data/README.md
CHANGED
@@ -1,328 +1,328 @@
|
|
1
|
-
# NotificationRenderer
|
2
|
-
|
3
|
-
[![Gem Version](https://badge.fury.io/rb/notification-renderer.svg)](https://badge.fury.io/rb/notification-renderer) <img src="https://travis-ci.org/jonhue/notifications-rails.svg?branch=master" />
|
4
|
-
|
5
|
-
Render your notifications on multiple platforms by specifying notification types.
|
6
|
-
|
7
|
-
---
|
8
|
-
|
9
|
-
## Table of Contents
|
10
|
-
|
11
|
-
* [Installation](#installation)
|
12
|
-
* [Usage](#usage)
|
13
|
-
* [Types](#types)
|
14
|
-
* [Generating a new type](#generating-a-new-type)
|
15
|
-
* [Using a type](#using-a-type)
|
16
|
-
* [Renderers](#renderers)
|
17
|
-
* [View helpers](#view-helpers)
|
18
|
-
* [`render_notification`](#render_notification)
|
19
|
-
* [`render_notifications`](#render_notifications)
|
20
|
-
* [Grouping](#grouping)
|
21
|
-
* [Grouping by notification types](#grouping-by-notification-types)
|
22
|
-
* [Grouping by notification dates](#grouping-by-notification-dates)
|
23
|
-
* [Configuration](#configuration)
|
24
|
-
* [To Do](#to-do)
|
25
|
-
* [Contributing](#contributing)
|
26
|
-
* [Contributors](#contributors)
|
27
|
-
* [Semantic versioning](#semantic-versioning)
|
28
|
-
* [License](#license)
|
29
|
-
|
30
|
-
---
|
31
|
-
|
32
|
-
## Installation
|
33
|
-
|
34
|
-
NotificationRenderer works with Rails 5 onwards. You can add it to your `Gemfile` with:
|
35
|
-
|
36
|
-
```ruby
|
37
|
-
gem 'notification-renderer'
|
38
|
-
```
|
39
|
-
|
40
|
-
And then execute:
|
41
|
-
|
42
|
-
$ bundle
|
43
|
-
|
44
|
-
Or install it yourself as:
|
45
|
-
|
46
|
-
$ gem install notification-renderer
|
47
|
-
|
48
|
-
If you always want to be up to date fetch the latest from GitHub in your `Gemfile`:
|
49
|
-
|
50
|
-
```ruby
|
51
|
-
gem 'notification-renderer', github: 'jonhue/notifications-rails'
|
52
|
-
```
|
53
|
-
|
54
|
-
Now run the generator:
|
55
|
-
|
56
|
-
$ rails g notification_renderer:install
|
57
|
-
|
58
|
-
To wrap things up, migrate the changes to your database:
|
59
|
-
|
60
|
-
$ rails db:migrate
|
61
|
-
|
62
|
-
---
|
63
|
-
|
64
|
-
## Usage
|
65
|
-
|
66
|
-
### Types
|
67
|
-
|
68
|
-
NotificationRenderer uses templates to render your notifications.
|
69
|
-
|
70
|
-
The `type` of a notification determines which template gets utilized for rendering. Each notification type has multiple templates each of which responsible for rendering a notification in another scenario. The default template for a given type is `index`.
|
71
|
-
|
72
|
-
#### Generating a new type
|
73
|
-
|
74
|
-
This gem comes with a generator to make adding new types a whole lot easier. Run in your terminal:
|
75
|
-
|
76
|
-
$ rails g notification_renderer:type -t notification
|
77
|
-
|
78
|
-
This will create the following structure in your application:
|
79
|
-
|
80
|
-
* `views`
|
81
|
-
* `notifications`
|
82
|
-
* `notification`
|
83
|
-
* `_index.html.erb`
|
84
|
-
|
85
|
-
You can also customize the generated templates (renderers):
|
86
|
-
|
87
|
-
$ rails g notification_renderer:type -t notification -r index feed
|
88
|
-
|
89
|
-
This command will also create a custom renderer called `feed` for the notification type `notification`:
|
90
|
-
|
91
|
-
* `views`
|
92
|
-
* `notifications`
|
93
|
-
* `notification`
|
94
|
-
* `_feed.html.erb`
|
95
|
-
* `_index.html.erb`
|
96
|
-
|
97
|
-
#### Using a type
|
98
|
-
|
99
|
-
You are able to specify the `type` of a `Notification` record:
|
100
|
-
|
101
|
-
```ruby
|
102
|
-
notification = Notification.create target: User.first, object: Recipe.first, type: 'notification'
|
103
|
-
```
|
104
|
-
|
105
|
-
**Note:** The `type` attribute of any new `Notification` record will default to the [`default_type` configuration](#configuration).
|
106
|
-
|
107
|
-
You can also scope records by their type:
|
108
|
-
|
109
|
-
```ruby
|
110
|
-
# Return records with `'notification'` as type
|
111
|
-
Notification.notification_type
|
112
|
-
|
113
|
-
# Return records with `'follow'` as type
|
114
|
-
Notification.follow_type
|
115
|
-
```
|
116
|
-
|
117
|
-
### Renderers
|
118
|
-
|
119
|
-
In your renderers you can access the `Notification` record through the `notification` variable. This is how a renderer could look like:
|
120
|
-
|
121
|
-
```erb
|
122
|
-
<%= notification.target.name %> commented on <%= notification.object.article.title %>.
|
123
|
-
```
|
124
|
-
|
125
|
-
### View helpers
|
126
|
-
|
127
|
-
NotificationRenderer introduces some view helpers to assist you in embedding notifications.
|
128
|
-
|
129
|
-
#### `render_notification`
|
130
|
-
|
131
|
-
`render_notification` renders a single `Notification` record:
|
132
|
-
|
133
|
-
```erb
|
134
|
-
<%= render_notification Notification.first %>
|
135
|
-
```
|
136
|
-
|
137
|
-
Rendering a notification will set its `read` attribute to `true`. This behavior can be [configured](#configuration).
|
138
|
-
|
139
|
-
You can also specify a renderer. It defaults to `'index'`.
|
140
|
-
|
141
|
-
```erb
|
142
|
-
<%= render_notification Notification.first, 'feed' %>
|
143
|
-
```
|
144
|
-
|
145
|
-
#### `render_notifications`
|
146
|
-
|
147
|
-
`render_notifications` takes an ActiveRecord array of `Notification` records and renders each of them in order:
|
148
|
-
|
149
|
-
```erb
|
150
|
-
<%= render_notifications Notification.all %>
|
151
|
-
```
|
152
|
-
|
153
|
-
You can also specify a renderer. It defaults to `'index'`.
|
154
|
-
|
155
|
-
```erb
|
156
|
-
<%= render_notifications Notification.all, 'feed' %>
|
157
|
-
```
|
158
|
-
|
159
|
-
It wraps the rendered notifications in a `div`:
|
160
|
-
|
161
|
-
```html
|
162
|
-
<div class="notification-renderer notifications">
|
163
|
-
<!-- ... -->
|
164
|
-
</div>
|
165
|
-
```
|
166
|
-
|
167
|
-
### Grouping
|
168
|
-
|
169
|
-
You can group any ActiveRecord array of `Notification` records by an attribute value:
|
170
|
-
|
171
|
-
```ruby
|
172
|
-
Notification.all.grouping(['object.article'])
|
173
|
-
Notification.all.grouping(['object.article', 'metadata[:title]'])
|
174
|
-
```
|
175
|
-
|
176
|
-
**Note:** Notifications will be grouped in order.
|
177
|
-
|
178
|
-
When rendering notifications you often want to group them by the object they belong to. This is how to group notifications by the associated object:
|
179
|
-
|
180
|
-
```erb
|
181
|
-
<%= render_notifications_grouped Notification.all, ['object'], renderer: 'feed' %>
|
182
|
-
```
|
183
|
-
|
184
|
-
You can also group notifications by nested attributes:
|
185
|
-
|
186
|
-
```erb
|
187
|
-
<%= render_notifications_grouped Notification.all, ['object.article'] %>
|
188
|
-
<%= render_notifications_grouped Notification.all, ['metadata[:title]'] %>
|
189
|
-
```
|
190
|
-
|
191
|
-
It is also possible to group notifications for just one object:
|
192
|
-
|
193
|
-
```erb
|
194
|
-
<%= render_notifications_grouped Notification.where(object_id: 1, object_type: 'Comment'), ['object'] %>
|
195
|
-
```
|
196
|
-
|
197
|
-
This will render the last notification for every group and pass the attributes value grouped by to your renderer:
|
198
|
-
|
199
|
-
```erb
|
200
|
-
<!-- View -->
|
201
|
-
|
202
|
-
<%= render_notifications_grouped Notification.notification_type, ['object.article'] %>
|
203
|
-
```
|
204
|
-
|
205
|
-
```erb
|
206
|
-
<!-- Renderer -->
|
207
|
-
|
208
|
-
<% if notification_grouped? %>
|
209
|
-
<%= notification.target.name %> and <%= (notifications.count - 1).to_s %> others commented on <%= attributes['object.article'].title %>.
|
210
|
-
<% else %>
|
211
|
-
<%= notification.target.name %> commented on <%= notification.object.article.title %>.
|
212
|
-
<% end %>
|
213
|
-
```
|
214
|
-
|
215
|
-
Grouping makes the following two methods available in your renderer:
|
216
|
-
|
217
|
-
**`attributes`** Hash of attributes grouped by and their values.
|
218
|
-
|
219
|
-
**`notifications`** The ActiveRecord array of notifications including the currently rendered notification.
|
220
|
-
|
221
|
-
You may check whether a template is being used for grouping by using the `notification_grouped?` helper method.
|
222
|
-
|
223
|
-
#### Grouping by notification types
|
224
|
-
|
225
|
-
It is common, if rendering multiple notification types at once, to group the notifications by their type:
|
226
|
-
|
227
|
-
```erb
|
228
|
-
<%= render_notifications_grouped Notification.all, ['object.article'], group_by_type: true %>
|
229
|
-
```
|
230
|
-
|
231
|
-
This is identical to the following:
|
232
|
-
|
233
|
-
```erb
|
234
|
-
<%= render_notifications_grouped Notification.all, [:type, 'object.article'] %>
|
235
|
-
```
|
236
|
-
|
237
|
-
#### Grouping by notification dates
|
238
|
-
|
239
|
-
It is also often required to group notifications by their date of creation:
|
240
|
-
|
241
|
-
```erb
|
242
|
-
<%= render_notifications_grouped Notification.all, ['object.article'], group_by_date: :month %>
|
243
|
-
```
|
244
|
-
|
245
|
-
This is identical to the following:
|
246
|
-
|
247
|
-
```erb
|
248
|
-
<%= render_notifications_grouped Notification.all, ['created_at.beginning_of_month', 'object.article'] %>
|
249
|
-
```
|
250
|
-
|
251
|
-
Accepted values are:
|
252
|
-
|
253
|
-
* `:minute`
|
254
|
-
* `:hour`
|
255
|
-
* `:day`
|
256
|
-
* `:week`
|
257
|
-
* `:month`
|
258
|
-
* `:year`
|
259
|
-
|
260
|
-
**Note:** If used together with `group_by_type`, notifications will be grouped first by creation date and then by `:type`.
|
261
|
-
|
262
|
-
---
|
263
|
-
|
264
|
-
## Configuration
|
265
|
-
|
266
|
-
You can configure NotificationRenderer by passing a block to `configure`. This can be done in `config/initializers/notification-renderer.rb`:
|
267
|
-
|
268
|
-
```ruby
|
269
|
-
NotificationRenderer.configure do |config|
|
270
|
-
config.default_type = 'notification'
|
271
|
-
end
|
272
|
-
```
|
273
|
-
|
274
|
-
**`default_type`** Choose your default notification type. Takes a string. Defaults to `'notification'`.
|
275
|
-
|
276
|
-
**`default_renderer`** Choose your default renderer. Takes a string. Defaults to `'index'`.
|
277
|
-
|
278
|
-
**`auto_read`** Automatically mark rendered notifications as read. Takes a boolean. Defaults to `true`.
|
279
|
-
|
280
|
-
---
|
281
|
-
|
282
|
-
## To Do
|
283
|
-
|
284
|
-
[Here](https://github.com/jonhue/notifications-rails/projects/7) is the full list of current projects.
|
285
|
-
|
286
|
-
To propose your ideas, initiate the discussion by adding a [new issue](https://github.com/jonhue/notifications-rails/issues/new).
|
287
|
-
|
288
|
-
---
|
289
|
-
|
290
|
-
## Contributing
|
291
|
-
|
292
|
-
We hope that you will consider contributing to NotificationRenderer. Please read this short overview for some information about how to get started:
|
293
|
-
|
294
|
-
[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)
|
295
|
-
|
296
|
-
### Contributors
|
297
|
-
|
298
|
-
Give the people some :heart: who are working on this project. See them all at:
|
299
|
-
|
300
|
-
https://github.com/jonhue/notifications-rails/graphs/contributors
|
301
|
-
|
302
|
-
### Semantic Versioning
|
303
|
-
|
304
|
-
NotificationRenderer follows Semantic Versioning 2.0 as defined at http://semver.org.
|
305
|
-
|
306
|
-
## License
|
307
|
-
|
308
|
-
MIT License
|
309
|
-
|
310
|
-
Copyright (c) 2017 Jonas Hübotter
|
311
|
-
|
312
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
313
|
-
of this software and associated documentation files (the "Software"), to deal
|
314
|
-
in the Software without restriction, including without limitation the rights
|
315
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
316
|
-
copies of the Software, and to permit persons to whom the Software is
|
317
|
-
furnished to do so, subject to the following conditions:
|
318
|
-
|
319
|
-
The above copyright notice and this permission notice shall be included in all
|
320
|
-
copies or substantial portions of the Software.
|
321
|
-
|
322
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
323
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
324
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
325
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
326
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
327
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
328
|
-
SOFTWARE.
|
1
|
+
# NotificationRenderer
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/notification-renderer.svg)](https://badge.fury.io/rb/notification-renderer) <img src="https://travis-ci.org/jonhue/notifications-rails.svg?branch=master" />
|
4
|
+
|
5
|
+
Render your notifications on multiple platforms by specifying notification types.
|
6
|
+
|
7
|
+
---
|
8
|
+
|
9
|
+
## Table of Contents
|
10
|
+
|
11
|
+
* [Installation](#installation)
|
12
|
+
* [Usage](#usage)
|
13
|
+
* [Types](#types)
|
14
|
+
* [Generating a new type](#generating-a-new-type)
|
15
|
+
* [Using a type](#using-a-type)
|
16
|
+
* [Renderers](#renderers)
|
17
|
+
* [View helpers](#view-helpers)
|
18
|
+
* [`render_notification`](#render_notification)
|
19
|
+
* [`render_notifications`](#render_notifications)
|
20
|
+
* [Grouping](#grouping)
|
21
|
+
* [Grouping by notification types](#grouping-by-notification-types)
|
22
|
+
* [Grouping by notification dates](#grouping-by-notification-dates)
|
23
|
+
* [Configuration](#configuration)
|
24
|
+
* [To Do](#to-do)
|
25
|
+
* [Contributing](#contributing)
|
26
|
+
* [Contributors](#contributors)
|
27
|
+
* [Semantic versioning](#semantic-versioning)
|
28
|
+
* [License](#license)
|
29
|
+
|
30
|
+
---
|
31
|
+
|
32
|
+
## Installation
|
33
|
+
|
34
|
+
NotificationRenderer works with Rails 5 onwards. You can add it to your `Gemfile` with:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
gem 'notification-renderer'
|
38
|
+
```
|
39
|
+
|
40
|
+
And then execute:
|
41
|
+
|
42
|
+
$ bundle
|
43
|
+
|
44
|
+
Or install it yourself as:
|
45
|
+
|
46
|
+
$ gem install notification-renderer
|
47
|
+
|
48
|
+
If you always want to be up to date fetch the latest from GitHub in your `Gemfile`:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
gem 'notification-renderer', github: 'jonhue/notifications-rails'
|
52
|
+
```
|
53
|
+
|
54
|
+
Now run the generator:
|
55
|
+
|
56
|
+
$ rails g notification_renderer:install
|
57
|
+
|
58
|
+
To wrap things up, migrate the changes to your database:
|
59
|
+
|
60
|
+
$ rails db:migrate
|
61
|
+
|
62
|
+
---
|
63
|
+
|
64
|
+
## Usage
|
65
|
+
|
66
|
+
### Types
|
67
|
+
|
68
|
+
NotificationRenderer uses templates to render your notifications.
|
69
|
+
|
70
|
+
The `type` of a notification determines which template gets utilized for rendering. Each notification type has multiple templates each of which responsible for rendering a notification in another scenario. The default template for a given type is `index`.
|
71
|
+
|
72
|
+
#### Generating a new type
|
73
|
+
|
74
|
+
This gem comes with a generator to make adding new types a whole lot easier. Run in your terminal:
|
75
|
+
|
76
|
+
$ rails g notification_renderer:type -t notification
|
77
|
+
|
78
|
+
This will create the following structure in your application:
|
79
|
+
|
80
|
+
* `views`
|
81
|
+
* `notifications`
|
82
|
+
* `notification`
|
83
|
+
* `_index.html.erb`
|
84
|
+
|
85
|
+
You can also customize the generated templates (renderers):
|
86
|
+
|
87
|
+
$ rails g notification_renderer:type -t notification -r index feed
|
88
|
+
|
89
|
+
This command will also create a custom renderer called `feed` for the notification type `notification`:
|
90
|
+
|
91
|
+
* `views`
|
92
|
+
* `notifications`
|
93
|
+
* `notification`
|
94
|
+
* `_feed.html.erb`
|
95
|
+
* `_index.html.erb`
|
96
|
+
|
97
|
+
#### Using a type
|
98
|
+
|
99
|
+
You are able to specify the `type` of a `Notification` record:
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
notification = Notification.create target: User.first, object: Recipe.first, type: 'notification'
|
103
|
+
```
|
104
|
+
|
105
|
+
**Note:** The `type` attribute of any new `Notification` record will default to the [`default_type` configuration](#configuration).
|
106
|
+
|
107
|
+
You can also scope records by their type:
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
# Return records with `'notification'` as type
|
111
|
+
Notification.notification_type
|
112
|
+
|
113
|
+
# Return records with `'follow'` as type
|
114
|
+
Notification.follow_type
|
115
|
+
```
|
116
|
+
|
117
|
+
### Renderers
|
118
|
+
|
119
|
+
In your renderers you can access the `Notification` record through the `notification` variable. This is how a renderer could look like:
|
120
|
+
|
121
|
+
```erb
|
122
|
+
<%= notification.target.name %> commented on <%= notification.object.article.title %>.
|
123
|
+
```
|
124
|
+
|
125
|
+
### View helpers
|
126
|
+
|
127
|
+
NotificationRenderer introduces some view helpers to assist you in embedding notifications.
|
128
|
+
|
129
|
+
#### `render_notification`
|
130
|
+
|
131
|
+
`render_notification` renders a single `Notification` record:
|
132
|
+
|
133
|
+
```erb
|
134
|
+
<%= render_notification Notification.first %>
|
135
|
+
```
|
136
|
+
|
137
|
+
Rendering a notification will set its `read` attribute to `true`. This behavior can be [configured](#configuration).
|
138
|
+
|
139
|
+
You can also specify a renderer. It defaults to `'index'`.
|
140
|
+
|
141
|
+
```erb
|
142
|
+
<%= render_notification Notification.first, 'feed' %>
|
143
|
+
```
|
144
|
+
|
145
|
+
#### `render_notifications`
|
146
|
+
|
147
|
+
`render_notifications` takes an ActiveRecord array of `Notification` records and renders each of them in order:
|
148
|
+
|
149
|
+
```erb
|
150
|
+
<%= render_notifications Notification.all %>
|
151
|
+
```
|
152
|
+
|
153
|
+
You can also specify a renderer. It defaults to `'index'`.
|
154
|
+
|
155
|
+
```erb
|
156
|
+
<%= render_notifications Notification.all, 'feed' %>
|
157
|
+
```
|
158
|
+
|
159
|
+
It wraps the rendered notifications in a `div`:
|
160
|
+
|
161
|
+
```html
|
162
|
+
<div class="notification-renderer notifications">
|
163
|
+
<!-- ... -->
|
164
|
+
</div>
|
165
|
+
```
|
166
|
+
|
167
|
+
### Grouping
|
168
|
+
|
169
|
+
You can group any ActiveRecord array of `Notification` records by an attribute value:
|
170
|
+
|
171
|
+
```ruby
|
172
|
+
Notification.all.grouping(['object.article'])
|
173
|
+
Notification.all.grouping(['object.article', 'metadata[:title]'])
|
174
|
+
```
|
175
|
+
|
176
|
+
**Note:** Notifications will be grouped in order.
|
177
|
+
|
178
|
+
When rendering notifications you often want to group them by the object they belong to. This is how to group notifications by the associated object:
|
179
|
+
|
180
|
+
```erb
|
181
|
+
<%= render_notifications_grouped Notification.all, ['object'], renderer: 'feed' %>
|
182
|
+
```
|
183
|
+
|
184
|
+
You can also group notifications by nested attributes:
|
185
|
+
|
186
|
+
```erb
|
187
|
+
<%= render_notifications_grouped Notification.all, ['object.article'] %>
|
188
|
+
<%= render_notifications_grouped Notification.all, ['metadata[:title]'] %>
|
189
|
+
```
|
190
|
+
|
191
|
+
It is also possible to group notifications for just one object:
|
192
|
+
|
193
|
+
```erb
|
194
|
+
<%= render_notifications_grouped Notification.where(object_id: 1, object_type: 'Comment'), ['object'] %>
|
195
|
+
```
|
196
|
+
|
197
|
+
This will render the last notification for every group and pass the attributes value grouped by to your renderer:
|
198
|
+
|
199
|
+
```erb
|
200
|
+
<!-- View -->
|
201
|
+
|
202
|
+
<%= render_notifications_grouped Notification.notification_type, ['object.article'] %>
|
203
|
+
```
|
204
|
+
|
205
|
+
```erb
|
206
|
+
<!-- Renderer -->
|
207
|
+
|
208
|
+
<% if notification_grouped? %>
|
209
|
+
<%= notification.target.name %> and <%= (notifications.count - 1).to_s %> others commented on <%= attributes['object.article'].title %>.
|
210
|
+
<% else %>
|
211
|
+
<%= notification.target.name %> commented on <%= notification.object.article.title %>.
|
212
|
+
<% end %>
|
213
|
+
```
|
214
|
+
|
215
|
+
Grouping makes the following two methods available in your renderer:
|
216
|
+
|
217
|
+
**`attributes`** Hash of attributes grouped by and their values.
|
218
|
+
|
219
|
+
**`notifications`** The ActiveRecord array of notifications including the currently rendered notification.
|
220
|
+
|
221
|
+
You may check whether a template is being used for grouping by using the `notification_grouped?` helper method.
|
222
|
+
|
223
|
+
#### Grouping by notification types
|
224
|
+
|
225
|
+
It is common, if rendering multiple notification types at once, to group the notifications by their type:
|
226
|
+
|
227
|
+
```erb
|
228
|
+
<%= render_notifications_grouped Notification.all, ['object.article'], group_by_type: true %>
|
229
|
+
```
|
230
|
+
|
231
|
+
This is identical to the following:
|
232
|
+
|
233
|
+
```erb
|
234
|
+
<%= render_notifications_grouped Notification.all, [:type, 'object.article'] %>
|
235
|
+
```
|
236
|
+
|
237
|
+
#### Grouping by notification dates
|
238
|
+
|
239
|
+
It is also often required to group notifications by their date of creation:
|
240
|
+
|
241
|
+
```erb
|
242
|
+
<%= render_notifications_grouped Notification.all, ['object.article'], group_by_date: :month %>
|
243
|
+
```
|
244
|
+
|
245
|
+
This is identical to the following:
|
246
|
+
|
247
|
+
```erb
|
248
|
+
<%= render_notifications_grouped Notification.all, ['created_at.beginning_of_month', 'object.article'] %>
|
249
|
+
```
|
250
|
+
|
251
|
+
Accepted values are:
|
252
|
+
|
253
|
+
* `:minute`
|
254
|
+
* `:hour`
|
255
|
+
* `:day`
|
256
|
+
* `:week`
|
257
|
+
* `:month`
|
258
|
+
* `:year`
|
259
|
+
|
260
|
+
**Note:** If used together with `group_by_type`, notifications will be grouped first by creation date and then by `:type`.
|
261
|
+
|
262
|
+
---
|
263
|
+
|
264
|
+
## Configuration
|
265
|
+
|
266
|
+
You can configure NotificationRenderer by passing a block to `configure`. This can be done in `config/initializers/notification-renderer.rb`:
|
267
|
+
|
268
|
+
```ruby
|
269
|
+
NotificationRenderer.configure do |config|
|
270
|
+
config.default_type = 'notification'
|
271
|
+
end
|
272
|
+
```
|
273
|
+
|
274
|
+
**`default_type`** Choose your default notification type. Takes a string. Defaults to `'notification'`.
|
275
|
+
|
276
|
+
**`default_renderer`** Choose your default renderer. Takes a string. Defaults to `'index'`.
|
277
|
+
|
278
|
+
**`auto_read`** Automatically mark rendered notifications as read. Takes a boolean. Defaults to `true`.
|
279
|
+
|
280
|
+
---
|
281
|
+
|
282
|
+
## To Do
|
283
|
+
|
284
|
+
[Here](https://github.com/jonhue/notifications-rails/projects/7) is the full list of current projects.
|
285
|
+
|
286
|
+
To propose your ideas, initiate the discussion by adding a [new issue](https://github.com/jonhue/notifications-rails/issues/new).
|
287
|
+
|
288
|
+
---
|
289
|
+
|
290
|
+
## Contributing
|
291
|
+
|
292
|
+
We hope that you will consider contributing to NotificationRenderer. Please read this short overview for some information about how to get started:
|
293
|
+
|
294
|
+
[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)
|
295
|
+
|
296
|
+
### Contributors
|
297
|
+
|
298
|
+
Give the people some :heart: who are working on this project. See them all at:
|
299
|
+
|
300
|
+
https://github.com/jonhue/notifications-rails/graphs/contributors
|
301
|
+
|
302
|
+
### Semantic Versioning
|
303
|
+
|
304
|
+
NotificationRenderer follows Semantic Versioning 2.0 as defined at http://semver.org.
|
305
|
+
|
306
|
+
## License
|
307
|
+
|
308
|
+
MIT License
|
309
|
+
|
310
|
+
Copyright (c) 2017 Jonas Hübotter
|
311
|
+
|
312
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
313
|
+
of this software and associated documentation files (the "Software"), to deal
|
314
|
+
in the Software without restriction, including without limitation the rights
|
315
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
316
|
+
copies of the Software, and to permit persons to whom the Software is
|
317
|
+
furnished to do so, subject to the following conditions:
|
318
|
+
|
319
|
+
The above copyright notice and this permission notice shall be included in all
|
320
|
+
copies or substantial portions of the Software.
|
321
|
+
|
322
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
323
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
324
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
325
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
326
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
327
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
328
|
+
SOFTWARE.
|