notification-renderer 1.2.3 → 1.2.4

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: 3cd8ff29aaeda72cef91cd077b82d33b388c0ade6560db4eb5ff16d39cb89354
4
- data.tar.gz: 1b93f84fac881b91e3d4e9ef41fb317d03cbd3bba3bcfa1fe49588482aeb1132
3
+ metadata.gz: 712f319a90e8085272bd6e7e2064a1b1f5e736a9235703640ccc9f7dcad23335
4
+ data.tar.gz: fff7dc09ad9e69bfc29ccdf8c85a6fb60dae9c18dbfc6d82ee84648ea44c4beb
5
5
  SHA512:
6
- metadata.gz: 5be66082f05cc424b0aec23a13a2f057ac11ed270368305df73a0ab77fae97a413e1a883662d57bdf9ee4f7ac4bd6a99703a9de17dbec6022b0e0aaa4d05d1ec
7
- data.tar.gz: 7c03ff84089edfb9bac28a939e3d30731e8fb7dd401cda18bb5d7086fe4713e9b46b5517247d2f79f68a916c684b1f57eb12e3deffc65cd9c2c7b2bd6becd9bc
6
+ metadata.gz: 84df50521bd7d113bd023ac9b1f58603f87ae0c2378d89e7250742043fb8506cc593acf030ecd287dac39bf0b5500b25c502b0349f4991fb8aba9a2b7646cac1
7
+ data.tar.gz: e52ea677458828dcd1fb9d0a812e2b6d10fc7e5ba166f41afbb588f82937b4d5c467bd57f8eccd74bc8745a8ee99db04c63f04c5ae015f76c2f5e533fb545641
data/README.md CHANGED
@@ -18,6 +18,8 @@ Render your notifications on multiple platforms by specifying notification types
18
18
  * [`render_notification`](#render_notification)
19
19
  * [`render_notifications`](#render_notifications)
20
20
  * [Grouping](#grouping)
21
+ * [Grouping by notification types](#grouping-by-notification-types)
22
+ * [Grouping by notification dates](#grouping-by-notification-dates)
21
23
  * [Configuration](#configuration)
22
24
  * [To Do](#to-do)
23
25
  * [Contributing](#contributing)
@@ -167,27 +169,29 @@ It wraps the rendered notifications in a `div`:
167
169
  You can group any ActiveRecord array of `Notification` records by an attribute value:
168
170
 
169
171
  ```ruby
170
- Notification.grouping('object.article')
171
- Notification.grouping('metadata[:title]')
172
+ Notification.all.grouping(['object.article'])
173
+ Notification.all.grouping(['object.article', 'metadata[:title]'])
172
174
  ```
173
175
 
176
+ **Note:** Notifications will be grouped in order.
177
+
174
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:
175
179
 
176
180
  ```erb
177
- <%= render_notifications_grouped Notification.all, 'object', 'feed' %>
181
+ <%= render_notifications_grouped Notification.all, ['object'], renderer: 'feed' %>
178
182
  ```
179
183
 
180
184
  You can also group notifications by nested attributes:
181
185
 
182
186
  ```erb
183
- <%= render_notifications_grouped Notification.all, 'object.article' %>
184
- <%= render_notifications_grouped Notification.all, 'metadata[:title]' %>
187
+ <%= render_notifications_grouped Notification.all, ['object.article'] %>
188
+ <%= render_notifications_grouped Notification.all, ['metadata[:title]'] %>
185
189
  ```
186
190
 
187
191
  It is also possible to group notifications for just one object:
188
192
 
189
193
  ```erb
190
- <%= render_notifications_grouped Notification.where(object_id: 1, object_type: 'Comment'), 'object' %>
194
+ <%= render_notifications_grouped Notification.where(object_id: 1, object_type: 'Comment'), ['object'] %>
191
195
  ```
192
196
 
193
197
  This will render the last notification for every group and pass the attributes value grouped by to your renderer:
@@ -195,13 +199,14 @@ This will render the last notification for every group and pass the attributes v
195
199
  ```erb
196
200
  <!-- View -->
197
201
 
198
- <%= render_notifications_grouped Notification.all, 'object.article' %>
199
-
202
+ <%= render_notifications_grouped Notification.notification_type, ['object.article'] %>
203
+ ```
200
204
 
205
+ ```erb
201
206
  <!-- Renderer -->
202
207
 
203
208
  <% if notification_grouped? %>
204
- <%= notification.target.name %> and <%= (notification_count - 1).to_s %> others commented on <%= attribute.title %>.
209
+ <%= notification.target.name %> and <%= (notifications.count - 1).to_s %> others commented on <%= attributes['object.article'].title %>.
205
210
  <% else %>
206
211
  <%= notification.target.name %> commented on <%= notification.object.article.title %>.
207
212
  <% end %>
@@ -209,12 +214,51 @@ This will render the last notification for every group and pass the attributes v
209
214
 
210
215
  Grouping makes the following two methods available in your renderer:
211
216
 
212
- **`attribute`** The value of the attribute used for grouping.
217
+ **`attributes`** Hash of attributes grouped by and their values.
213
218
 
214
- **`notification_count`** The notification count for the group of the current notification.
219
+ **`notifications`** The ActiveRecord array of notifications including the currently rendered notification.
215
220
 
216
221
  You may check whether a template is being used for grouping by using the `notification_grouped?` helper method.
217
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
+
218
262
  ---
219
263
 
220
264
  ## Configuration
@@ -1,28 +1,63 @@
1
1
  module NotificationRendererHelper
2
2
 
3
- def render_notification notification, renderer = NotificationRenderer.configuration.default_renderer, attribute = nil, notifications_count = nil
3
+ def render_notification notification, options = {}
4
+ defaults = {
5
+ renderer: NotificationRenderer.configuration.default_renderer,
6
+ attributes: nil,
7
+ notifications: nil
8
+ }
9
+ options = defaults.merge options
10
+
4
11
  notification.update_attributes read: true if NotificationRenderer.configuration.auto_read
5
- render "notifications/#{notification.type}/#{renderer}", notification: notification, attribute: attribute, notifications_count: notifications_count
12
+ render "notifications/#{notification.type}/#{options[:renderer]}", notification: notification, attributes: options[:attributes], notifications: options[:notifications]
6
13
  end
7
14
 
8
- def render_notifications notifications, renderer = NotificationRenderer.configuration.default_renderer
15
+ def render_notifications notifications, options = {}
16
+ defaults = {
17
+ renderer: NotificationRenderer.configuration.default_renderer
18
+ }
19
+ options = defaults.merge options
20
+
9
21
  content_tag :div, class: 'notification-renderer notifications' do
10
22
  notifications.each do |notification|
11
- render_notification notification, renderer
23
+ render_notification notification, renderer: options[:renderer]
12
24
  end
13
25
  end
14
26
  end
15
27
 
16
- def render_notifications_grouped notifications, group_by, renderer = NotificationRenderer.configuration.default_renderer
28
+ def render_notifications_grouped notifications, group_by, options = {}
29
+ defaults = {
30
+ renderer: NotificationRenderer.configuration.default_renderer,
31
+ group_by_date: false,
32
+ group_by_type: false
33
+ }
34
+ options = defaults.merge options
35
+
36
+ group_by.unshift :type if options[:group_by_type]
37
+ group_by.unshift "created_at.beginning_of_#{options[:group_by_date]}" if options[:group_by_date]
38
+
17
39
  content_tag :div, class: 'notification-renderer notifications' do
18
- notifications.grouping(group_by)&.each do |attribute, notifications|
19
- render_notification notifications.last, renderer, attribute, notifications.count
20
- end
40
+ recursive_render_notifications_grouped notifications.grouping(options[:group_by]), options
21
41
  end
22
42
  end
23
43
 
24
44
  def notification_grouped?
25
- local_assigns[:attribute]
45
+ local_assigns[:attributes] && local_assigns[:notifications]
46
+ end
47
+
48
+ private
49
+
50
+ def recursive_render_notifications_grouped notifications, i = 0, options = {}
51
+ options[:attributes] = {} unless options.has_key? :attributes
52
+ notifications.each_pair do |k, v|
53
+ options[:attributes][options[:group_by][i]] = k
54
+ i += 1
55
+ if v.is_a? Hash
56
+ recursive_render_notifications_grouped v, options
57
+ else
58
+ render_notification v.last, renderer: options[:renderer], attributes: options[:attributes], notifications: v
59
+ end
60
+ end
26
61
  end
27
62
 
28
63
  end
@@ -6,21 +6,40 @@ module NotificationRenderer
6
6
 
7
7
  extend ActiveSupport::Concern
8
8
 
9
- included do
10
- include NotificationRenderer::NotificationLibrary::InstanceMethods
11
- end
12
-
13
- module InstanceMethods
9
+ module ClassMethods
10
+
11
+ def grouping group_by
12
+ notifications = all
13
+ group_by.each do |method|
14
+ notifications = recursive_grouping notifications, method
15
+ end
16
+ notifications
17
+ end
14
18
 
15
- def self.grouping group_by
16
- group_by{ |notification| notification.send(group_by) }
19
+ def grouping_by group_by
20
+ all.group_by{ |notification| notification.send(group_by) }
17
21
  end
18
22
 
19
- def type
20
- self[:type] || NotificationRenderer.configuration.default_type
23
+ def recursive_grouping notifications, group_by
24
+ if notifications.is_a? Hash
25
+ notifications.each_pair do |k, v|
26
+ if v.is_a? Hash
27
+ recursive_grouping v, group_by
28
+ else
29
+ k = v.grouping_by group_by
30
+ end
31
+ end
32
+ else
33
+ notifications.grouping_by group_by
34
+ end
35
+ notifications
21
36
  end
22
37
 
23
38
  end
24
39
 
40
+ def type
41
+ self[:type] || NotificationRenderer.configuration.default_type
42
+ end
43
+
25
44
  end
26
45
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: notification-renderer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Hübotter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-23 00:00:00.000000000 Z
11
+ date: 2018-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - '='
60
60
  - !ruby/object:Gem::Version
61
- version: 1.2.3
61
+ version: 1.2.4
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - '='
67
67
  - !ruby/object:Gem::Version
68
- version: 1.2.3
68
+ version: 1.2.4
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec
71
71
  requirement: !ruby/object:Gem::Requirement