activity_notification 2.0.0 → 2.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (202) hide show
  1. checksums.yaml +4 -4
  2. data/.github/ISSUE_TEMPLATE/bug_report.md +22 -0
  3. data/.github/ISSUE_TEMPLATE/feature_request.md +17 -0
  4. data/.github/pull_request_template.md +13 -0
  5. data/.gitignore +10 -3
  6. data/.travis.yml +6 -5
  7. data/CHANGELOG.md +60 -0
  8. data/Gemfile +8 -3
  9. data/Procfile +1 -1
  10. data/README.md +153 -1510
  11. data/activity_notification.gemspec +4 -1
  12. data/app/channels/activity_notification/notification_api_channel.rb +12 -0
  13. data/app/channels/activity_notification/notification_api_with_devise_channel.rb +46 -0
  14. data/app/channels/activity_notification/notification_channel.rb +2 -2
  15. data/app/channels/activity_notification/notification_with_devise_channel.rb +2 -2
  16. data/app/controllers/activity_notification/apidocs_controller.rb +75 -0
  17. data/app/controllers/activity_notification/notifications_api_controller.rb +143 -0
  18. data/app/controllers/activity_notification/notifications_api_with_devise_controller.rb +7 -0
  19. data/app/controllers/activity_notification/notifications_controller.rb +79 -53
  20. data/app/controllers/activity_notification/subscriptions_api_controller.rb +197 -0
  21. data/app/controllers/activity_notification/subscriptions_api_with_devise_controller.rb +7 -0
  22. data/app/controllers/activity_notification/subscriptions_controller.rb +78 -69
  23. data/app/views/activity_notification/notifications/default/_default.html.erb +18 -18
  24. data/app/views/activity_notification/notifications/default/_default_without_grouping.html.erb +14 -14
  25. data/app/views/activity_notification/notifications/default/index.html.erb +6 -6
  26. data/app/views/activity_notification/optional_targets/default/action_cable_channel/_default.html.erb +176 -0
  27. data/app/views/activity_notification/subscriptions/default/_form.html.erb +1 -1
  28. data/app/views/activity_notification/subscriptions/default/_notification_keys.html.erb +3 -31
  29. data/app/views/activity_notification/subscriptions/default/_subscription.html.erb +7 -7
  30. data/app/views/activity_notification/subscriptions/default/index.html.erb +11 -7
  31. data/bin/deploy_on_heroku.sh +3 -1
  32. data/docs/CODE_OF_CONDUCT.md +76 -0
  33. data/docs/CONTRIBUTING.md +36 -0
  34. data/docs/Functions.md +1130 -0
  35. data/docs/Setup.md +801 -0
  36. data/docs/Testing.md +148 -0
  37. data/gemfiles/Gemfile.rails-4.2 +5 -1
  38. data/gemfiles/Gemfile.rails-5.0 +6 -1
  39. data/gemfiles/Gemfile.rails-5.1 +6 -1
  40. data/gemfiles/Gemfile.rails-5.2 +6 -1
  41. data/gemfiles/{Gemfile.rails-6.0.rc → Gemfile.rails-6.0} +6 -5
  42. data/lib/activity_notification.rb +13 -0
  43. data/lib/activity_notification/apis/notification_api.rb +37 -93
  44. data/lib/activity_notification/apis/subscription_api.rb +20 -8
  45. data/lib/activity_notification/apis/swagger.rb +6 -0
  46. data/lib/activity_notification/common.rb +4 -1
  47. data/lib/activity_notification/config.rb +41 -21
  48. data/lib/activity_notification/controllers/common_api_controller.rb +30 -0
  49. data/lib/activity_notification/controllers/common_controller.rb +45 -21
  50. data/lib/activity_notification/controllers/concerns/swagger/error_responses.rb +55 -0
  51. data/lib/activity_notification/controllers/concerns/swagger/notifications_api.rb +273 -0
  52. data/lib/activity_notification/controllers/concerns/swagger/notifications_parameters.rb +92 -0
  53. data/lib/activity_notification/controllers/concerns/swagger/subscriptions_api.rb +405 -0
  54. data/lib/activity_notification/controllers/concerns/swagger/subscriptions_parameters.rb +50 -0
  55. data/lib/activity_notification/controllers/devise_authentication_controller.rb +7 -6
  56. data/lib/activity_notification/gem_version.rb +14 -0
  57. data/lib/activity_notification/helpers/errors.rb +2 -0
  58. data/lib/activity_notification/helpers/view_helpers.rb +4 -0
  59. data/lib/activity_notification/mailers/helpers.rb +17 -10
  60. data/lib/activity_notification/models/concerns/notifiable.rb +31 -15
  61. data/lib/activity_notification/models/concerns/subscriber.rb +12 -1
  62. data/lib/activity_notification/models/concerns/swagger/error_schema.rb +36 -0
  63. data/lib/activity_notification/models/concerns/swagger/notification_schema.rb +209 -0
  64. data/lib/activity_notification/models/concerns/swagger/subscription_schema.rb +162 -0
  65. data/lib/activity_notification/models/concerns/target.rb +36 -10
  66. data/lib/activity_notification/models/notification.rb +1 -0
  67. data/lib/activity_notification/models/subscription.rb +1 -0
  68. data/lib/activity_notification/optional_targets/action_cable_api_channel.rb +69 -0
  69. data/lib/activity_notification/optional_targets/action_cable_channel.rb +68 -0
  70. data/lib/activity_notification/optional_targets/base.rb +7 -13
  71. data/lib/activity_notification/orm/active_record/notification.rb +17 -1
  72. data/lib/activity_notification/orm/active_record/subscription.rb +1 -1
  73. data/lib/activity_notification/orm/dynamoid.rb +38 -3
  74. data/lib/activity_notification/orm/dynamoid/extension.rb +79 -1
  75. data/lib/activity_notification/orm/dynamoid/notification.rb +49 -14
  76. data/lib/activity_notification/orm/dynamoid/subscription.rb +2 -2
  77. data/lib/activity_notification/orm/mongoid.rb +32 -3
  78. data/lib/activity_notification/orm/mongoid/notification.rb +24 -6
  79. data/lib/activity_notification/orm/mongoid/subscription.rb +1 -1
  80. data/lib/activity_notification/rails/routes.rb +132 -48
  81. data/lib/activity_notification/renderable.rb +13 -2
  82. data/lib/activity_notification/roles/acts_as_notifiable.rb +39 -20
  83. data/lib/activity_notification/version.rb +1 -1
  84. data/lib/generators/activity_notification/controllers_generator.rb +2 -1
  85. data/lib/generators/templates/activity_notification.rb +8 -0
  86. data/lib/generators/templates/controllers/notifications_api_controller.rb +31 -0
  87. data/lib/generators/templates/controllers/notifications_api_with_devise_controller.rb +31 -0
  88. data/lib/generators/templates/controllers/notifications_controller.rb +1 -37
  89. data/lib/generators/templates/controllers/notifications_with_devise_controller.rb +1 -45
  90. data/lib/generators/templates/controllers/subscriptions_api_controller.rb +61 -0
  91. data/lib/generators/templates/controllers/subscriptions_api_with_devise_controller.rb +61 -0
  92. data/lib/generators/templates/controllers/subscriptions_controller.rb +14 -37
  93. data/lib/generators/templates/controllers/subscriptions_with_devise_controller.rb +14 -45
  94. data/lib/generators/templates/models/README +8 -4
  95. data/lib/generators/templates/models/notification.rb +1 -1
  96. data/lib/generators/templates/models/subscription.rb +1 -1
  97. data/package.json +8 -0
  98. data/spec/channels/notification_api_channel_shared_examples.rb +59 -0
  99. data/spec/channels/notification_api_channel_spec.rb +51 -0
  100. data/spec/channels/notification_api_with_devise_channel_spec.rb +78 -0
  101. data/spec/concerns/apis/notification_api_spec.rb +38 -3
  102. data/spec/concerns/models/notifiable_spec.rb +82 -18
  103. data/spec/concerns/models/subscriber_spec.rb +13 -16
  104. data/spec/concerns/models/target_spec.rb +32 -0
  105. data/spec/concerns/renderable_spec.rb +2 -2
  106. data/spec/config_spec.rb +26 -15
  107. data/spec/controllers/controller_spec_utility.rb +136 -0
  108. data/spec/controllers/notifications_api_controller_shared_examples.rb +506 -0
  109. data/spec/controllers/notifications_api_controller_spec.rb +19 -0
  110. data/spec/controllers/notifications_api_with_devise_controller_spec.rb +60 -0
  111. data/spec/controllers/notifications_controller_shared_examples.rb +54 -79
  112. data/spec/controllers/notifications_controller_spec.rb +1 -2
  113. data/spec/controllers/notifications_with_devise_controller_spec.rb +3 -12
  114. data/spec/controllers/subscriptions_api_controller_shared_examples.rb +750 -0
  115. data/spec/controllers/subscriptions_api_controller_spec.rb +19 -0
  116. data/spec/controllers/subscriptions_api_with_devise_controller_spec.rb +60 -0
  117. data/spec/controllers/subscriptions_controller_shared_examples.rb +94 -121
  118. data/spec/controllers/subscriptions_controller_spec.rb +1 -2
  119. data/spec/controllers/subscriptions_with_devise_controller_spec.rb +3 -12
  120. data/spec/helpers/view_helpers_spec.rb +4 -11
  121. data/spec/mailers/mailer_spec.rb +41 -0
  122. data/spec/models/notification_spec.rb +17 -0
  123. data/spec/models/subscription_spec.rb +8 -13
  124. data/spec/optional_targets/action_cable_api_channel_spec.rb +37 -0
  125. data/spec/optional_targets/action_cable_channel_spec.rb +44 -0
  126. data/spec/optional_targets/amazon_sns_spec.rb +0 -2
  127. data/spec/optional_targets/slack_spec.rb +0 -2
  128. data/spec/rails_app/Rakefile +9 -0
  129. data/spec/rails_app/app/assets/config/manifest.js +3 -0
  130. data/spec/rails_app/app/assets/images/.keep +0 -0
  131. data/spec/rails_app/app/controllers/admins_controller.rb +21 -0
  132. data/spec/rails_app/app/controllers/application_controller.rb +1 -1
  133. data/spec/rails_app/app/controllers/articles_controller.rb +6 -3
  134. data/spec/rails_app/app/controllers/spa_controller.rb +7 -0
  135. data/spec/rails_app/app/controllers/users/notifications_controller.rb +0 -65
  136. data/spec/rails_app/app/controllers/users/notifications_with_devise_controller.rb +0 -73
  137. data/spec/rails_app/app/controllers/users/subscriptions_controller.rb +0 -77
  138. data/spec/rails_app/app/controllers/users/subscriptions_with_devise_controller.rb +0 -85
  139. data/spec/rails_app/app/controllers/users_controller.rb +26 -0
  140. data/spec/rails_app/app/javascript/App.vue +40 -0
  141. data/spec/rails_app/app/javascript/components/DeviseTokenAuth.vue +82 -0
  142. data/spec/rails_app/app/javascript/components/Top.vue +98 -0
  143. data/spec/rails_app/app/javascript/components/notifications/Index.vue +200 -0
  144. data/spec/rails_app/app/javascript/components/notifications/Notification.vue +133 -0
  145. data/spec/rails_app/app/javascript/components/notifications/NotificationContent.vue +122 -0
  146. data/spec/rails_app/app/javascript/components/subscriptions/Index.vue +279 -0
  147. data/spec/rails_app/app/javascript/components/subscriptions/NewSubscription.vue +112 -0
  148. data/spec/rails_app/app/javascript/components/subscriptions/NotificationKey.vue +141 -0
  149. data/spec/rails_app/app/javascript/components/subscriptions/Subscription.vue +226 -0
  150. data/spec/rails_app/app/javascript/config/development.js +5 -0
  151. data/spec/rails_app/app/javascript/config/environment.js +7 -0
  152. data/spec/rails_app/app/javascript/config/production.js +5 -0
  153. data/spec/rails_app/app/javascript/config/test.js +5 -0
  154. data/spec/rails_app/app/javascript/packs/application.js +18 -0
  155. data/spec/rails_app/app/javascript/packs/spa.js +14 -0
  156. data/spec/rails_app/app/javascript/router/index.js +73 -0
  157. data/spec/rails_app/app/javascript/store/index.js +37 -0
  158. data/spec/rails_app/app/models/admin.rb +16 -15
  159. data/spec/rails_app/app/models/article.rb +26 -21
  160. data/spec/rails_app/app/models/comment.rb +24 -71
  161. data/spec/rails_app/app/models/dummy/dummy_group.rb +8 -0
  162. data/spec/rails_app/app/models/dummy/dummy_notifiable_target.rb +8 -0
  163. data/spec/rails_app/app/models/user.rb +44 -20
  164. data/spec/rails_app/app/views/activity_notification/notifications/default/article/_update.html.erb +146 -0
  165. data/spec/rails_app/app/views/articles/index.html.erb +51 -7
  166. data/spec/rails_app/app/views/articles/show.html.erb +1 -1
  167. data/spec/rails_app/app/views/layouts/_header.html.erb +8 -10
  168. data/spec/rails_app/app/views/spa/index.html.erb +2 -0
  169. data/spec/rails_app/babel.config.js +72 -0
  170. data/spec/rails_app/bin/webpack +18 -0
  171. data/spec/rails_app/bin/webpack-dev-server +18 -0
  172. data/spec/rails_app/config/application.rb +18 -2
  173. data/spec/rails_app/config/dynamoid.rb +11 -3
  174. data/spec/rails_app/config/environment.rb +2 -1
  175. data/spec/rails_app/config/environments/development.rb +5 -0
  176. data/spec/rails_app/config/environments/production.rb +6 -0
  177. data/spec/rails_app/config/environments/test.rb +5 -0
  178. data/spec/rails_app/config/initializers/activity_notification.rb +11 -3
  179. data/spec/rails_app/config/initializers/copy_it.aws.rb.template +6 -0
  180. data/spec/rails_app/config/initializers/devise_token_auth.rb +55 -0
  181. data/spec/rails_app/config/initializers/mysql.rb +9 -0
  182. data/spec/rails_app/config/locales/activity_notification.en.yml +2 -2
  183. data/spec/rails_app/config/routes.rb +37 -1
  184. data/spec/rails_app/config/webpack/development.js +5 -0
  185. data/spec/rails_app/config/webpack/environment.js +7 -0
  186. data/spec/rails_app/config/webpack/loaders/vue.js +6 -0
  187. data/spec/rails_app/config/webpack/production.js +5 -0
  188. data/spec/rails_app/config/webpack/test.js +5 -0
  189. data/spec/rails_app/config/webpacker.yml +97 -0
  190. data/spec/rails_app/db/migrate/20191201000000_add_tokens_to_users.rb +10 -0
  191. data/spec/rails_app/db/schema.rb +4 -1
  192. data/spec/rails_app/db/seeds.rb +10 -2
  193. data/spec/rails_app/lib/custom_optional_targets/raise_error.rb +14 -0
  194. data/spec/rails_app/package.json +23 -0
  195. data/spec/rails_app/postcss.config.js +12 -0
  196. data/spec/roles/acts_as_group_spec.rb +0 -2
  197. data/spec/roles/acts_as_notifiable_spec.rb +6 -8
  198. data/spec/roles/acts_as_notifier_spec.rb +0 -2
  199. data/spec/roles/acts_as_target_spec.rb +0 -4
  200. data/spec/spec_helper.rb +7 -15
  201. data/spec/version_spec.rb +31 -0
  202. metadata +191 -13
@@ -0,0 +1,801 @@
1
+ ## Setup
2
+
3
+ ### Gem installation
4
+
5
+ You can install *activity_notification* as you would any other gem:
6
+
7
+ ```console
8
+ $ gem install activity_notification
9
+ ```
10
+ or in your Gemfile:
11
+
12
+ ```ruby
13
+ gem 'activity_notification'
14
+ ```
15
+
16
+ After you install *activity_notification* and add it to your Gemfile, you need to run the generator:
17
+
18
+ ```console
19
+ $ bin/rails generate activity_notification:install
20
+ ```
21
+
22
+ The generator will install an initializer which describes all configuration options of *activity_notification*.
23
+ It also generates a i18n based translation file which we can configure the presentation of notifications.
24
+
25
+ ### Database setup
26
+
27
+ #### Using ActiveRecord ORM
28
+
29
+ When you use *activity_notification* with ActiveRecord ORM as default configuration,
30
+ create migration for notifications and migrate the database in your Rails project:
31
+
32
+ ```console
33
+ $ bin/rails generate activity_notification:migration
34
+ $ bin/rake db:migrate
35
+ ```
36
+
37
+ If you are using a different table name from *"notifications"*, change the settings in your *config/initializers/activity_notification.rb* file, e.g., if you're using the table name *"activity_notifications"* instead of the default *"notifications"*:
38
+
39
+ ```ruby
40
+ config.notification_table_name = "activity_notifications"
41
+ ```
42
+
43
+ The same can be done for the subscription table name, e.g., if you're using the table name *"notifications_subscriptions"* instead of the default *"subscriptions"*:
44
+
45
+ ```ruby
46
+ config.subscription_table_name = "notifications_subscriptions"
47
+ ```
48
+
49
+ #### Using Mongoid ORM
50
+
51
+ When you use *activity_notification* with [Mongoid](http://mongoid.org) ORM, set **AN_ORM** environment variable to **mongoid**:
52
+
53
+ ```console
54
+ $ export AN_ORM=mongoid
55
+ ```
56
+
57
+ You can also configure ORM in initializer **activity_notification.rb**:
58
+
59
+ ```ruby
60
+ config.orm = :mongoid
61
+ ```
62
+
63
+ You need to configure Mongoid in your Rails application for your MongoDB environment. Then, your notifications and subscriptions will be stored in your MongoDB.
64
+
65
+ #### Using Dynamoid ORM
66
+
67
+ Currently, *activity_notification* only works with Dynamoid 3.1.0.
68
+
69
+ ```ruby
70
+ gem 'dynamoid', '3.1.0'
71
+ ```
72
+
73
+ When you use *activity_notification* with [Dynamoid](https://github.com/Dynamoid/dynamoid) ORM, set **AN_ORM** environment variable to **dynamoid**:
74
+
75
+ ```console
76
+ $ export AN_ORM=dynamoid
77
+ ```
78
+
79
+ You can also configure ORM in initializer **activity_notification.rb**:
80
+
81
+ ```ruby
82
+ config.orm = :dynamoid
83
+ ```
84
+
85
+ You need to configure Dynamoid in your Rails application for your Amazon DynamoDB environment.
86
+ Then, you can use this rake task to create DynamoDB tables used by *activity_notification* with Dynamoid:
87
+
88
+ ```console
89
+ $ bin/rake activity_notification:create_dynamodb_tables
90
+ ```
91
+
92
+ After these configurations, your notifications and subscriptions will be stored in your Amazon DynamoDB.
93
+
94
+ Note: Amazon DynamoDB integration using Dynamoid ORM is only supported with Rails 5.0+.
95
+
96
+ ##### Integration with DynamoDB Streams
97
+
98
+ You can capture *activity_notification*'s table activity with [DynamoDB Streams](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Streams.html).
99
+ Using DynamoDB Streams, activity notifications in your Rails application will be integrated into cloud computing and available as event stream processed by [DynamoDB Streams Kinesis Adapter](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Streams.KCLAdapter.html) or [AWS Lambda](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Streams.Lambda.html).
100
+
101
+ When you consume your activity notifications from DynamoDB Streams, sometimes you need to process notification records with associated target, notifiable or notifier record which is stored in database of your Rails application.
102
+ In such cases, you can use **store_with_associated_records** option in initializer **activity_notification.rb**:
103
+
104
+ ```ruby
105
+ config.store_with_associated_records = true
106
+ ```
107
+
108
+ When **store_with_associated_records** is set to *false* as default, *activity_notification* stores notificaion records with association like this:
109
+
110
+ ```json
111
+ {
112
+ "id": {
113
+ "S": "f05756ef-661e-4ef5-9e99-5af51243125c"
114
+ },
115
+ "target_key": {
116
+ "S": "User#1"
117
+ },
118
+ "notifiable_key": {
119
+ "S": "Comment#2"
120
+ },
121
+ "key": {
122
+ "S": "comment.default"
123
+ },
124
+ "group_key": {
125
+ "S": "Article#1"
126
+ },
127
+ "notifier_key": {
128
+ "S": "User#2"
129
+ },
130
+ "created_at": {
131
+ "S": "2020-03-08T08:22:53+00:00"
132
+ },
133
+ "updated_at": {
134
+ "S": "2020-03-08T08:22:53+00:00"
135
+ },
136
+ "parameters": {
137
+ "M": {}
138
+ }
139
+ }
140
+ ```
141
+
142
+ When you set **store_with_associated_records** to *true*, *activity_notification* stores notificaion records including associated target, notifiable, notifier and several instance methods like this:
143
+
144
+ ```json
145
+ {
146
+ "id": {
147
+ "S": "f05756ef-661e-4ef5-9e99-5af51243125c"
148
+ },
149
+ "target_key": {
150
+ "S": "User#1"
151
+ },
152
+ "notifiable_key": {
153
+ "S": "Comment#2"
154
+ },
155
+ "key": {
156
+ "S": "comment.default"
157
+ },
158
+ "group_key": {
159
+ "S": "Article#1"
160
+ },
161
+ "notifier_key": {
162
+ "S": "User#2"
163
+ },
164
+ "created_at": {
165
+ "S": "2020-03-08T08:22:53+00:00"
166
+ },
167
+ "updated_at": {
168
+ "S": "2020-03-08T08:22:53+00:00"
169
+ },
170
+ "parameters": {
171
+ "M": {}
172
+ },
173
+ "stored_target": {
174
+ "M": {
175
+ "id": {
176
+ "N": "1"
177
+ },
178
+ "email": {
179
+ "S": "ichiro@example.com"
180
+ },
181
+ "name": {
182
+ "S": "Ichiro"
183
+ },
184
+ "created_at": {
185
+ "S": "2020-03-08T08:22:23.451Z"
186
+ },
187
+ "updated_at": {
188
+ "S": "2020-03-08T08:22:23.451Z"
189
+ },
190
+ // { ... },
191
+ "printable_type": {
192
+ "S": "User"
193
+ },
194
+ "printable_target_name": {
195
+ "S": "Ichiro"
196
+ },
197
+ }
198
+ },
199
+ "stored_notifiable": {
200
+ "M": {
201
+ "id": {
202
+ "N": "2"
203
+ },
204
+ "user_id": {
205
+ "N": "2"
206
+ },
207
+ "article_id": {
208
+ "N": "1"
209
+ },
210
+ "body": {
211
+ "S": "This is the first Stephen's comment to Ichiro's article."
212
+ },
213
+ "created_at": {
214
+ "S": "2020-03-08T08:22:47.683Z"
215
+ },
216
+ "updated_at": {
217
+ "S": "2020-03-08T08:22:47.683Z"
218
+ },
219
+ "printable_type": {
220
+ "S": "Comment"
221
+ }
222
+ }
223
+ },
224
+ "stored_notifier": {
225
+ "M": {
226
+ "id": {
227
+ "N": "2"
228
+ },
229
+ "email": {
230
+ "S": "stephen@example.com"
231
+ },
232
+ "name": {
233
+ "S": "Stephen"
234
+ },
235
+ "created_at": {
236
+ "S": "2020-03-08T08:22:23.573Z"
237
+ },
238
+ "updated_at": {
239
+ "S": "2020-03-08T08:22:23.573Z"
240
+ },
241
+ // { ... },
242
+ "printable_type": {
243
+ "S": "User"
244
+ },
245
+ "printable_notifier_name": {
246
+ "S": "Stephen"
247
+ }
248
+ }
249
+ },
250
+ "stored_group": {
251
+ "M": {
252
+ "id": {
253
+ "N": "1"
254
+ },
255
+ "user_id": {
256
+ "N": "1"
257
+ },
258
+ "title": {
259
+ "S": "Ichiro's first article"
260
+ },
261
+ "body": {
262
+ "S": "This is the first Ichiro's article. Please read it!"
263
+ },
264
+ "created_at": {
265
+ "S": "2020-03-08T08:22:23.952Z"
266
+ },
267
+ "updated_at": {
268
+ "S": "2020-03-08T08:22:23.952Z"
269
+ },
270
+ "printable_type": {
271
+ "S": "Article"
272
+ },
273
+ "printable_group_name": {
274
+ "S": "article \"Ichiro's first article\""
275
+ }
276
+ }
277
+ },
278
+ "stored_notifiable_path": {
279
+ "S": "/articles/1"
280
+ },
281
+ "stored_printable_notifiable_name": {
282
+ "S": "comment \"This is the first Stephen's comment to Ichiro's article.\""
283
+ },
284
+ "stored_group_member_notifier_count": {
285
+ "N": "2"
286
+ },
287
+ "stored_group_notification_count": {
288
+ "N": "3"
289
+ },
290
+ "stored_group_members": {
291
+ "L": [
292
+ // { ... }, { ... }, ...
293
+ ]
294
+ }
295
+ }
296
+ ```
297
+
298
+ Then, you can process notification records with associated records in your DynamoDB Streams.
299
+
300
+ Note: This **store_with_associated_records** option can be set true only when you use mongoid or dynamoid ORM.
301
+
302
+ ### Configuring models
303
+
304
+ #### Configuring target models
305
+
306
+ Configure your target model (e.g. *app/models/user.rb*).
307
+ Add **acts_as_target** configuration to your target model to get notifications.
308
+
309
+ ##### Target as an ActiveRecord model
310
+
311
+ ```ruby
312
+ class User < ActiveRecord::Base
313
+ # acts_as_target configures your model as ActivityNotification::Target
314
+ # with parameters as value or custom methods defined in your model as lambda or symbol.
315
+ # This is an example without any options (default configuration) as the target.
316
+ acts_as_target
317
+ end
318
+ ```
319
+
320
+ ##### Target as a Mongoid model
321
+
322
+ ```ruby
323
+ require 'mongoid'
324
+ class User
325
+ include Mongoid::Document
326
+ include Mongoid::Timestamps
327
+ include GlobalID::Identification
328
+
329
+ # You need include ActivityNotification::Models except models which extend ActiveRecord::Base
330
+ include ActivityNotification::Models
331
+ acts_as_target
332
+ end
333
+ ```
334
+
335
+ *Note*: *acts_as_notification_target* is an alias for *acts_as_target* and does the same.
336
+
337
+ #### Configuring notifiable models
338
+
339
+ Configure your notifiable model (e.g. *app/models/comment.rb*).
340
+ Add **acts_as_notifiable** configuration to your notifiable model representing activity to notify for each of your target model.
341
+ You have to define notification targets for all notifications from this notifiable model by *:targets* option. Other configurations are optional. *:notifiable_path* option is a path to move when the notification is opened by the target user.
342
+
343
+ ##### Notifiable as an ActiveRecord model
344
+
345
+ ```ruby
346
+ class Article < ActiveRecord::Base
347
+ belongs_to :user
348
+ has_many :comments, dependent: :destroy
349
+ has_many :commented_users, through: :comments, source: :user
350
+ end
351
+
352
+ class Comment < ActiveRecord::Base
353
+ belongs_to :article
354
+ belongs_to :user
355
+
356
+ # acts_as_notifiable configures your model as ActivityNotification::Notifiable
357
+ # with parameters as value or custom methods defined in your model as lambda or symbol.
358
+ # The first argument is the plural symbol name of your target model.
359
+ acts_as_notifiable :users,
360
+ # Notification targets as :targets is a necessary option
361
+ # Set to notify to author and users commented to the article, except comment owner self
362
+ targets: ->(comment, key) {
363
+ ([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
364
+ },
365
+ # Path to move when the notification is opened by the target user
366
+ # This is an optional configuration since activity_notification uses polymorphic_path as default
367
+ notifiable_path: :article_notifiable_path
368
+
369
+ def article_notifiable_path
370
+ article_path(article)
371
+ end
372
+ end
373
+ ```
374
+
375
+ ##### Notifiable as a Mongoid model
376
+
377
+ ```ruby
378
+ require 'mongoid'
379
+ class Article
380
+ include Mongoid::Document
381
+ include Mongoid::Timestamps
382
+
383
+ belongs_to :user
384
+ has_many :comments, dependent: :destroy
385
+
386
+ def commented_users
387
+ User.where(:id.in => comments.pluck(:user_id))
388
+ end
389
+ end
390
+
391
+ require 'mongoid'
392
+ class Comment
393
+ include Mongoid::Document
394
+ include Mongoid::Timestamps
395
+ include GlobalID::Identification
396
+
397
+ # You need include ActivityNotification::Models except models which extend ActiveRecord::Base
398
+ include ActivityNotification::Models
399
+ acts_as_notifiable :users,
400
+ targets: ->(comment, key) {
401
+ ([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
402
+ },
403
+ notifiable_path: :article_notifiable_path
404
+
405
+ def article_notifiable_path
406
+ article_path(article)
407
+ end
408
+ end
409
+ ```
410
+
411
+ ##### Advanced notifiable path
412
+
413
+ Sometimes it might be necessary to provide extra information in the *notifiable_path*. In those cases, passing a lambda function to the *notifiable_path* will give you the notifiable object and the notifiable key to play around with:
414
+
415
+ ```ruby
416
+ acts_as_notifiable :users,
417
+ targets: ->(comment, key) {
418
+ ([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
419
+ },
420
+  notifiable_path: ->(comment, key) { "#{comment.article_notifiable_path}##{key}" }
421
+ ```
422
+
423
+ This will attach the key of the notification to the notifiable path.
424
+
425
+ ### Configuring views
426
+
427
+ *activity_notification* provides view templates to customize your notification views. The view generator can generate default views for all targets.
428
+
429
+ ```console
430
+ $ bin/rails generate activity_notification:views
431
+ ```
432
+
433
+ If you have multiple target models in your application, such as *User* and *Admin*, you will be able to have views based on the target like *notifications/users/index* and *notifications/admins/index*. If no view is found for the target, *activity_notification* will use the default view at *notifications/default/index*. You can also use the generator to generate views for the specified target:
434
+
435
+ ```console
436
+ $ bin/rails generate activity_notification:views users
437
+ ```
438
+
439
+ If you would like to generate only a few sets of views, like the ones for the *notifications* (for notification views) and *mailer* (for notification email views),
440
+ you can pass a list of modules to the generator with the *-v* flag.
441
+
442
+ ```console
443
+ $ bin/rails generate activity_notification:views -v notifications
444
+ ```
445
+
446
+ ### Configuring routes
447
+
448
+ *activity_notification* also provides routing helper for notifications. Add **notify_to** method to *config/routes.rb* for the target (e.g. *:users*):
449
+
450
+ ```ruby
451
+ Rails.application.routes.draw do
452
+ notify_to :users
453
+ end
454
+ ```
455
+
456
+ Then, you can access several pages like */users/1/notifications* and manage open/unopen of notifications using *[ActivityNotification::NotificationsController](/app/controllers/activity_notification/notifications_controller.rb)*.
457
+ If you use Devise integration and you want to configure simple default routes for authenticated users, see [Configuring simple default routes](#configuring-simple-default-routes).
458
+
459
+ #### Routes with namespaced model
460
+
461
+ It is possible to configure a target model as a submodule, e.g. if your target is `Entity::User`,
462
+ however by default the **ActivityNotification** controllers will be placed under the same namespace,
463
+ so it is mandatory to explicitly call the controllers this way
464
+
465
+ ```ruby
466
+ Rails.application.routes.draw do
467
+ notify_to :users, controller: '/activity_notification/notifications', target_type: 'entity/users'
468
+ end
469
+ ```
470
+
471
+ This will generate the necessary routes for the `Entity::User` target with parameters `:user_id`
472
+
473
+ #### Routes with scope
474
+
475
+ You can also configure *activity_notification* routes with scope like this:
476
+
477
+ ```ruby
478
+ Rails.application.routes.draw do
479
+ scope :myscope, as: :myscope do
480
+ notify_to :users, routing_scope: :myscope
481
+ end
482
+ end
483
+ ```
484
+
485
+ Then, pages are shown as */myscope/users/1/notifications*.
486
+
487
+ #### Routes as REST API backend
488
+
489
+ You can configure *activity_notification* routes as REST API backend with *api_mode* option like this:
490
+
491
+ ```ruby
492
+ Rails.application.routes.draw do
493
+ scope :api do
494
+ scope :"v2" do
495
+ notify_to :users, api_mode: true
496
+ end
497
+ end
498
+ end
499
+ ```
500
+
501
+ Then, you can call *activity_notification* REST API as */api/v2/notifications* from your frontend application. See [REST API backend](#rest-api-backend) for more details.
502
+
503
+ ### Creating notifications
504
+
505
+ #### Notification API
506
+
507
+ You can trigger notifications by setting all your required parameters and triggering **notify** on the notifiable model, like this:
508
+
509
+ ```ruby
510
+ @comment.notify :users, key: "comment.reply"
511
+ ```
512
+
513
+ Or, you can call public API as **ActivityNotification::Notification.notify**
514
+
515
+ ```ruby
516
+ ActivityNotification::Notification.notify :users, @comment, key: "comment.reply"
517
+ ```
518
+
519
+ The first argument is the plural symbol name of your target model, which is configured in notifiable model by *acts_as_notifiable*.
520
+ The new instances of **ActivityNotification::Notification** model will be generated for the specified targets.
521
+
522
+ *Hint*: *:key* is a option. Default key `#{notifiable_type}.default` which means *comment.default* will be used without specified key.
523
+ You can override it by *Notifiable#default_notification_key*.
524
+
525
+ #### Asynchronous notification API with ActiveJob
526
+
527
+ Using Notification API with default configurations, the notifications will be generated synchronously. *activity_notification* also supports **asynchronous notification API** with ActiveJob to improve application performance. You can use **notify_later** method on the notifiable model, like this:
528
+
529
+ ```ruby
530
+ @comment.notify_later :users, key: "comment.reply"
531
+ ```
532
+
533
+ You can also use *:notify_later* option in *notify* method. This is the same operation as calling *notify_later* method.
534
+
535
+ ```ruby
536
+ @comment.notify :users, key: "comment.reply", notify_later: true
537
+ ```
538
+
539
+ *Note*: *notify_now* is an alias for *notify* and does the same.
540
+
541
+ When you use asynchronous notification API, you should setup ActiveJob with background queuing service such as Sidekiq.
542
+ You can set *config.active_job_queue* in your initializer to specify a queue name *activity_notification* will use.
543
+ The default queue name is *:activity_notification*.
544
+
545
+ ```ruby
546
+ # Configure ActiveJob queue name for delayed notifications.
547
+ config.active_job_queue = :my_notification_queue
548
+ ```
549
+
550
+ #### Automatic tracked notifications
551
+
552
+ You can also generate automatic tracked notifications by **:tracked** option in *acts_as_notifiable*.
553
+ *:tracked* option adds required callbacks to generate notifications for creation and update of the notifiable model.
554
+ Set true to *:tracked* option to generate all tracked notifications, like this:
555
+
556
+ ```ruby
557
+ class Comment < ActiveRecord::Base
558
+ acts_as_notifiable :users,
559
+ targets: ->(comment, key) {
560
+ ([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
561
+ },
562
+ # Set true to :tracked option to generate automatic tracked notifications.
563
+ # It adds required callbacks to generate notifications for creation and update of the notifiable model.
564
+ tracked: true
565
+ end
566
+ ```
567
+
568
+ Or, set *:only* or *:except* option to generate specified tracked notifications, like this:
569
+
570
+ ```ruby
571
+ class Comment < ActiveRecord::Base
572
+ acts_as_notifiable :users,
573
+ targets: ->(comment, key) {
574
+ ([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
575
+ },
576
+ # Set { only: [:create] } to :tracked option to generate tracked notifications for creation only.
577
+ # It adds required callbacks to generate notifications for creation of the notifiable model.
578
+ tracked: { only: [:create] }
579
+ end
580
+ ```
581
+
582
+ ```ruby
583
+ class Comment < ActiveRecord::Base
584
+ acts_as_notifiable :users,
585
+ targets: ->(comment, key) {
586
+ ([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
587
+ },
588
+ # Set { except: [:update] } to :tracked option to generate tracked notifications except update (creation only).
589
+ # It adds required callbacks to generate notifications for creation of the notifiable model.
590
+ tracked: { except: [:update], key: 'comment.create.now', send_later: false }
591
+ end
592
+ ```
593
+
594
+ *Hint*: `#{notifiable_type}.create` and `#{notifiable_type}.update` will be used as the key of tracked notifications.
595
+ You can override them by *Notifiable#notification_key_for_tracked_creation* and *Notifiable#notification_key_for_tracked_update*.
596
+ You can also specify key option in the *:tracked* statement.
597
+
598
+ As a default, the notifications will be generated synchronously along with model creation or update. If you want to generate notifications asynchronously, use *:notify_later* option with the *:tracked* option, like this:
599
+
600
+ ```ruby
601
+ class Comment < ActiveRecord::Base
602
+ acts_as_notifiable :users,
603
+ targets: ->(comment, key) {
604
+ ([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
605
+ },
606
+ # It adds required callbacks to generate notifications asynchronously for creation of the notifiable model.
607
+ tracked: { only: [:create], key: 'comment.create.later', notify_later: true }
608
+ end
609
+ ```
610
+
611
+ ### Displaying notifications
612
+
613
+ #### Preparing target notifications
614
+
615
+ To display notifications, you can use **notifications** association of the target model:
616
+
617
+ ```ruby
618
+ # custom_notifications_controller.rb
619
+ def index
620
+ @notifications = @target.notifications
621
+ end
622
+ ```
623
+
624
+ You can also use several scope to filter notifications. For example, **unopened_only** to filter them unopened notifications only.
625
+
626
+ ```ruby
627
+ # custom_notifications_controller.rb
628
+ def index
629
+ @notifications = @target.notifications.unopened_only
630
+ end
631
+ ```
632
+
633
+ Moreover, you can use **notification_index** or **notification_index_with_attributes** methods to automatically prepare notification index for the target.
634
+
635
+ ```ruby
636
+ # custom_notifications_controller.rb
637
+ def index
638
+ @notifications = @target.notification_index_with_attributes
639
+ end
640
+ ```
641
+
642
+ #### Rendering notifications
643
+
644
+ You can use **render_notifications** helper in your views to show the notification index:
645
+
646
+ ```erb
647
+ <%= render_notifications(@notifications) %>
648
+ ```
649
+
650
+ We can set *:target* option to specify the target type of notifications:
651
+
652
+ ```erb
653
+ <%= render_notifications(@notifications, target: :users) %>
654
+ ```
655
+
656
+ *Note*: *render_notifications* is an alias for *render_notification* and does the same.
657
+
658
+ If you want to set notification index in the common layout, such as common header, you can use **render_notifications_of** helper like this:
659
+
660
+ ```shared/_header.html.erb
661
+ <%= render_notifications_of current_user, index_content: :with_attributes %>
662
+ ```
663
+
664
+ Then, content named **:notification_index** will be prepared and you can use it in your partial template.
665
+
666
+ ```activity_notifications/notifications/users/_index.html.erb
667
+ ...
668
+ <%= yield :notification_index %>
669
+ ...
670
+ ```
671
+
672
+ Sometimes, it's desirable to pass additional local variables to partials. It can be done this way:
673
+
674
+ ```erb
675
+ <%= render_notification(@notification, locals: { friends: current_user.friends }) %>
676
+ ```
677
+
678
+ #### Notification views
679
+
680
+ *activity_notification* looks for views in *app/views/activity_notification/notifications/:target* with **:key** of the notifications.
681
+
682
+ For example, if you have a notification with *:key* set to *"notification.comment.reply"* and rendered it with *:target* set to *:users*, the gem will look for a partial in *app/views/activity_notification/notifications/users/comment/_reply.html.(|erb|haml|slim|something_else)*.
683
+
684
+ *Hint*: the *"notification."* prefix in *:key* is completely optional, you can skip it in your projects or use this prefix only to make namespace.
685
+
686
+ If you would like to fallback to a partial, you can utilize the **:fallback** parameter to specify the path of a partial to use when one is missing:
687
+
688
+ ```erb
689
+ <%= render_notification(@notification, target: :users, fallback: :default) %>
690
+ ```
691
+
692
+ When used in this manner, if a partial with the specified *:key* cannot be located, it will use the partial defined in the *:fallback* instead. In the example above this would resolve to *activity_notification/notifications/users/_default.html.(|erb|haml|slim|something_else)*.
693
+
694
+ If you do not specify *:target* option like this,
695
+
696
+ ```erb
697
+ <%= render_notification(@notification, fallback: :default) %>
698
+ ```
699
+
700
+ the gem will look for a partial in *default* as the target type which means *activity_notification/notifications/default/_default.html.(|erb|haml|slim|something_else)*.
701
+
702
+ If a view file does not exist then *ActionView::MisingTemplate* will be raised. If you wish to fallback to the old behaviour and use an i18n based translation in this situation you can specify a *:fallback* parameter of *:text* to fallback to this mechanism like such:
703
+
704
+ ```erb
705
+ <%= render_notification(@notification, fallback: :text) %>
706
+ ```
707
+
708
+ Finally, default views of *activity_notification* depends on jQuery and you have to add requirements to *application.js* in your apps:
709
+
710
+ ```app/assets/javascripts/application.js
711
+ //= require jquery
712
+ //= require jquery_ujs
713
+ ```
714
+
715
+ #### i18n for notifications
716
+
717
+ Translations are used by the *#text* method, to which you can pass additional options in form of a hash. *#render* method uses translations when view templates have not been provided. You can render pure i18n strings by passing `{ i18n: true }` to *#render_notification* or *#render*.
718
+
719
+ Translations should be put in your locale *.yml* files as **text** field. To render pure strings from I18n example structure:
720
+
721
+ ```yaml
722
+ notification:
723
+ user:
724
+ article:
725
+ create:
726
+ text: 'Article has been created'
727
+ update:
728
+ text: 'Article %{article_title} has been updated'
729
+ destroy:
730
+ text: 'Some user removed an article!'
731
+ comment:
732
+ create:
733
+ text: '%{notifier_name} posted a comment on the article "%{article_title}"'
734
+ post:
735
+ text:
736
+ one: "<p>%{notifier_name} posted a comment on your article %{article_title}</p>"
737
+ other: "<p>%{notifier_name} posted %{count} comments on your article %{article_title}</p>"
738
+ reply:
739
+ text: "<p>%{notifier_name} and %{group_member_count} other people replied %{group_notification_count} times to your comment</p>"
740
+ mail_subject: 'New comment on your article'
741
+ admin:
742
+ article:
743
+ post:
744
+ text: '[Admin] Article has been created'
745
+ ```
746
+
747
+ This structure is valid for notifications with keys *"notification.comment.reply"* or *"comment.reply"*. As mentioned before, *"notification."* part of the key is optional. In addition for above example, `%{notifier_name}` and `%{article_title}` are used from parameter field in the notification record. Pluralization is supported (but optional) for grouped notifications using the `%{group_notification_count}` value.
748
+
749
+ ### Customizing controllers (optional)
750
+
751
+ If the customization at the views level is not enough, you can customize each controller by following these steps:
752
+
753
+ 1. Create your custom controllers using the generator with a target:
754
+
755
+ ```console
756
+ $ bin/rails generate activity_notification:controllers users
757
+ ```
758
+
759
+ If you specify *users* as the target, controllers will be created in *app/controllers/users*.
760
+ And the notifications controller will look like this:
761
+
762
+ ```ruby
763
+ class Users::NotificationsController < ActivityNotification::NotificationsController
764
+ # GET /:target_type/:target_id/notifications
765
+ # def index
766
+ # super
767
+ # end
768
+
769
+ # ...
770
+
771
+ # PUT /:target_type/:target_id/notifications/:id/open
772
+ # def open
773
+ # super
774
+ # end
775
+
776
+ # ...
777
+ end
778
+ ```
779
+
780
+ 2. Tell the router to use this controller:
781
+
782
+ ```ruby
783
+ notify_to :users, controller: 'users/notifications'
784
+ ```
785
+
786
+ 3. Finally, change or extend the desired controller actions.
787
+
788
+ You can completely override a controller action
789
+ ```ruby
790
+ class Users::NotificationsController < ActivityNotification::NotificationsController
791
+ # ...
792
+
793
+ # PUT /:target_type/:target_id/notifications/:id/open
794
+ def open
795
+ # Custom code to open notification here
796
+
797
+ # super
798
+ end
799
+
800
+ # ...
801
+ end