activity_notification 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/ISSUE_TEMPLATE/bug_report.md +22 -0
- data/.github/ISSUE_TEMPLATE/feature_request.md +17 -0
- data/.github/pull_request_template.md +13 -0
- data/.gitignore +10 -3
- data/.travis.yml +6 -5
- data/CHANGELOG.md +22 -0
- data/Gemfile +8 -2
- data/Procfile +1 -1
- data/README.md +153 -1510
- data/activity_notification.gemspec +4 -1
- data/app/channels/activity_notification/notification_api_channel.rb +12 -0
- data/app/channels/activity_notification/notification_api_with_devise_channel.rb +46 -0
- data/app/channels/activity_notification/notification_channel.rb +1 -1
- data/app/channels/activity_notification/notification_with_devise_channel.rb +1 -1
- data/app/controllers/activity_notification/apidocs_controller.rb +75 -0
- data/app/controllers/activity_notification/notifications_api_controller.rb +143 -0
- data/app/controllers/activity_notification/notifications_api_with_devise_controller.rb +7 -0
- data/app/controllers/activity_notification/notifications_controller.rb +79 -53
- data/app/controllers/activity_notification/subscriptions_api_controller.rb +197 -0
- data/app/controllers/activity_notification/subscriptions_api_with_devise_controller.rb +7 -0
- data/app/controllers/activity_notification/subscriptions_controller.rb +78 -69
- data/app/views/activity_notification/notifications/default/_default.html.erb +18 -18
- data/app/views/activity_notification/notifications/default/_default_without_grouping.html.erb +14 -14
- data/app/views/activity_notification/notifications/default/index.html.erb +6 -6
- data/app/views/activity_notification/optional_targets/default/action_cable_channel/_default.html.erb +176 -0
- data/app/views/activity_notification/subscriptions/default/_form.html.erb +1 -1
- data/app/views/activity_notification/subscriptions/default/_notification_keys.html.erb +3 -31
- data/app/views/activity_notification/subscriptions/default/_subscription.html.erb +7 -7
- data/app/views/activity_notification/subscriptions/default/index.html.erb +11 -7
- data/bin/deploy_on_heroku.sh +3 -1
- data/docs/CODE_OF_CONDUCT.md +76 -0
- data/docs/CONTRIBUTING.md +36 -0
- data/docs/Functions.md +1130 -0
- data/docs/Setup.md +674 -0
- data/docs/Testing.md +148 -0
- data/gemfiles/Gemfile.rails-4.2 +3 -0
- data/gemfiles/Gemfile.rails-5.0 +3 -0
- data/gemfiles/Gemfile.rails-5.1 +3 -0
- data/gemfiles/Gemfile.rails-5.2 +3 -0
- data/gemfiles/{Gemfile.rails-6.0.rc → Gemfile.rails-6.0} +5 -3
- data/lib/activity_notification.rb +13 -1
- data/lib/activity_notification/apis/notification_api.rb +29 -92
- data/lib/activity_notification/apis/subscription_api.rb +20 -8
- data/lib/activity_notification/apis/swagger.rb +6 -0
- data/lib/activity_notification/config.rb +41 -21
- data/lib/activity_notification/controllers/common_api_controller.rb +30 -0
- data/lib/activity_notification/controllers/common_controller.rb +44 -20
- data/lib/activity_notification/controllers/concerns/swagger/error_responses.rb +55 -0
- data/lib/activity_notification/controllers/concerns/swagger/notifications_api.rb +273 -0
- data/lib/activity_notification/controllers/concerns/swagger/notifications_parameters.rb +92 -0
- data/lib/activity_notification/controllers/concerns/swagger/subscriptions_api.rb +405 -0
- data/lib/activity_notification/controllers/concerns/swagger/subscriptions_parameters.rb +50 -0
- data/lib/activity_notification/controllers/devise_authentication_controller.rb +7 -6
- data/lib/activity_notification/gem_version.rb +14 -0
- data/lib/activity_notification/helpers/errors.rb +2 -0
- data/lib/activity_notification/helpers/view_helpers.rb +4 -0
- data/lib/activity_notification/mailers/helpers.rb +17 -10
- data/lib/activity_notification/models/concerns/notifiable.rb +26 -10
- data/lib/activity_notification/models/concerns/subscriber.rb +12 -1
- data/lib/activity_notification/models/concerns/swagger/error_schema.rb +36 -0
- data/lib/activity_notification/models/concerns/swagger/notification_schema.rb +209 -0
- data/lib/activity_notification/models/concerns/swagger/subscription_schema.rb +162 -0
- data/lib/activity_notification/models/concerns/target.rb +36 -10
- data/lib/activity_notification/models/notification.rb +1 -0
- data/lib/activity_notification/models/subscription.rb +1 -0
- data/lib/activity_notification/optional_targets/action_cable_api_channel.rb +69 -0
- data/lib/activity_notification/optional_targets/action_cable_channel.rb +68 -0
- data/lib/activity_notification/optional_targets/base.rb +7 -13
- data/lib/activity_notification/orm/active_record/notification.rb +17 -1
- data/lib/activity_notification/orm/active_record/subscription.rb +1 -1
- data/lib/activity_notification/orm/dynamoid.rb +28 -0
- data/lib/activity_notification/orm/dynamoid/extension.rb +79 -1
- data/lib/activity_notification/orm/dynamoid/notification.rb +1 -1
- data/lib/activity_notification/orm/dynamoid/subscription.rb +1 -1
- data/lib/activity_notification/orm/mongoid.rb +22 -0
- data/lib/activity_notification/orm/mongoid/notification.rb +17 -1
- data/lib/activity_notification/orm/mongoid/subscription.rb +1 -1
- data/lib/activity_notification/rails/routes.rb +132 -48
- data/lib/activity_notification/renderable.rb +13 -2
- data/lib/activity_notification/roles/acts_as_notifiable.rb +38 -20
- data/lib/activity_notification/version.rb +1 -1
- data/lib/generators/activity_notification/controllers_generator.rb +2 -1
- data/lib/generators/templates/activity_notification.rb +8 -0
- data/lib/generators/templates/controllers/notifications_api_controller.rb +31 -0
- data/lib/generators/templates/controllers/notifications_api_with_devise_controller.rb +31 -0
- data/lib/generators/templates/controllers/notifications_controller.rb +1 -37
- data/lib/generators/templates/controllers/notifications_with_devise_controller.rb +1 -45
- data/lib/generators/templates/controllers/subscriptions_api_controller.rb +61 -0
- data/lib/generators/templates/controllers/subscriptions_api_with_devise_controller.rb +61 -0
- data/lib/generators/templates/controllers/subscriptions_controller.rb +14 -37
- data/lib/generators/templates/controllers/subscriptions_with_devise_controller.rb +14 -45
- data/lib/generators/templates/models/README +8 -4
- data/lib/generators/templates/models/notification.rb +1 -1
- data/lib/generators/templates/models/subscription.rb +1 -1
- data/package.json +8 -0
- data/spec/channels/notification_api_channel_shared_examples.rb +59 -0
- data/spec/channels/notification_api_channel_spec.rb +51 -0
- data/spec/channels/notification_api_with_devise_channel_spec.rb +78 -0
- data/spec/concerns/apis/notification_api_spec.rb +37 -2
- data/spec/concerns/models/notifiable_spec.rb +64 -0
- data/spec/concerns/models/subscriber_spec.rb +13 -16
- data/spec/concerns/models/target_spec.rb +32 -0
- data/spec/concerns/renderable_spec.rb +2 -2
- data/spec/controllers/controller_spec_utility.rb +136 -0
- data/spec/controllers/notifications_api_controller_shared_examples.rb +506 -0
- data/spec/controllers/notifications_api_controller_spec.rb +19 -0
- data/spec/controllers/notifications_api_with_devise_controller_spec.rb +60 -0
- data/spec/controllers/notifications_controller_shared_examples.rb +54 -79
- data/spec/controllers/notifications_controller_spec.rb +1 -2
- data/spec/controllers/notifications_with_devise_controller_spec.rb +3 -12
- data/spec/controllers/subscriptions_api_controller_shared_examples.rb +750 -0
- data/spec/controllers/subscriptions_api_controller_spec.rb +19 -0
- data/spec/controllers/subscriptions_api_with_devise_controller_spec.rb +60 -0
- data/spec/controllers/subscriptions_controller_shared_examples.rb +94 -121
- data/spec/controllers/subscriptions_controller_spec.rb +1 -2
- data/spec/controllers/subscriptions_with_devise_controller_spec.rb +3 -12
- data/spec/helpers/view_helpers_spec.rb +4 -11
- data/spec/mailers/mailer_spec.rb +41 -0
- data/spec/models/notification_spec.rb +17 -0
- data/spec/models/subscription_spec.rb +0 -13
- data/spec/optional_targets/action_cable_api_channel_spec.rb +37 -0
- data/spec/optional_targets/action_cable_channel_spec.rb +44 -0
- data/spec/optional_targets/amazon_sns_spec.rb +0 -2
- data/spec/optional_targets/slack_spec.rb +0 -2
- data/spec/rails_app/Rakefile +9 -0
- data/spec/rails_app/app/assets/config/manifest.js +3 -0
- data/spec/rails_app/app/assets/images/.keep +0 -0
- data/spec/rails_app/app/controllers/admins_controller.rb +21 -0
- data/spec/rails_app/app/controllers/application_controller.rb +1 -1
- data/spec/rails_app/app/controllers/articles_controller.rb +6 -3
- data/spec/rails_app/app/controllers/spa_controller.rb +7 -0
- data/spec/rails_app/app/controllers/users/notifications_controller.rb +0 -65
- data/spec/rails_app/app/controllers/users/notifications_with_devise_controller.rb +0 -73
- data/spec/rails_app/app/controllers/users/subscriptions_controller.rb +0 -77
- data/spec/rails_app/app/controllers/users/subscriptions_with_devise_controller.rb +0 -85
- data/spec/rails_app/app/controllers/users_controller.rb +21 -0
- data/spec/rails_app/app/javascript/App.vue +104 -0
- data/spec/rails_app/app/javascript/components/DeviseTokenAuth.vue +83 -0
- data/spec/rails_app/app/javascript/components/Top.vue +99 -0
- data/spec/rails_app/app/javascript/components/notifications/Index.vue +200 -0
- data/spec/rails_app/app/javascript/components/notifications/Notification.vue +133 -0
- data/spec/rails_app/app/javascript/components/notifications/NotificationContent.vue +122 -0
- data/spec/rails_app/app/javascript/components/subscriptions/Index.vue +279 -0
- data/spec/rails_app/app/javascript/components/subscriptions/NewSubscription.vue +112 -0
- data/spec/rails_app/app/javascript/components/subscriptions/NotificationKey.vue +141 -0
- data/spec/rails_app/app/javascript/components/subscriptions/Subscription.vue +226 -0
- data/spec/rails_app/app/javascript/config/development.js +5 -0
- data/spec/rails_app/app/javascript/config/environment.js +7 -0
- data/spec/rails_app/app/javascript/config/production.js +5 -0
- data/spec/rails_app/app/javascript/config/test.js +5 -0
- data/spec/rails_app/app/javascript/packs/application.js +18 -0
- data/spec/rails_app/app/javascript/packs/spa.js +11 -0
- data/spec/rails_app/app/javascript/store/auth.js +37 -0
- data/spec/rails_app/app/models/admin.rb +16 -15
- data/spec/rails_app/app/models/article.rb +26 -21
- data/spec/rails_app/app/models/comment.rb +24 -71
- data/spec/rails_app/app/models/user.rb +43 -20
- data/spec/rails_app/app/views/activity_notification/notifications/default/article/_update.html.erb +146 -0
- data/spec/rails_app/app/views/articles/index.html.erb +51 -7
- data/spec/rails_app/app/views/articles/show.html.erb +1 -1
- data/spec/rails_app/app/views/layouts/_header.html.erb +8 -10
- data/spec/rails_app/app/views/spa/index.html.erb +2 -0
- data/spec/rails_app/babel.config.js +72 -0
- data/spec/rails_app/bin/webpack +18 -0
- data/spec/rails_app/bin/webpack-dev-server +18 -0
- data/spec/rails_app/config/application.rb +15 -2
- data/spec/rails_app/config/environment.rb +2 -1
- data/spec/rails_app/config/environments/development.rb +5 -0
- data/spec/rails_app/config/environments/production.rb +3 -0
- data/spec/rails_app/config/environments/test.rb +5 -0
- data/spec/rails_app/config/initializers/activity_notification.rb +8 -0
- data/spec/rails_app/config/initializers/devise_token_auth.rb +55 -0
- data/spec/rails_app/config/initializers/mysql.rb +9 -0
- data/spec/rails_app/config/locales/activity_notification.en.yml +2 -2
- data/spec/rails_app/config/routes.rb +33 -1
- data/spec/rails_app/config/webpack/development.js +5 -0
- data/spec/rails_app/config/webpack/environment.js +7 -0
- data/spec/rails_app/config/webpack/loaders/vue.js +6 -0
- data/spec/rails_app/config/webpack/production.js +5 -0
- data/spec/rails_app/config/webpack/test.js +5 -0
- data/spec/rails_app/config/webpacker.yml +97 -0
- data/spec/rails_app/db/migrate/20191201000000_add_tokens_to_users.rb +10 -0
- data/spec/rails_app/db/schema.rb +4 -1
- data/spec/rails_app/db/seeds.rb +1 -0
- data/spec/rails_app/lib/custom_optional_targets/raise_error.rb +14 -0
- data/spec/rails_app/package.json +23 -0
- data/spec/rails_app/postcss.config.js +12 -0
- data/spec/roles/acts_as_group_spec.rb +0 -2
- data/spec/roles/acts_as_notifiable_spec.rb +1 -3
- data/spec/roles/acts_as_notifier_spec.rb +0 -2
- data/spec/roles/acts_as_target_spec.rb +0 -4
- data/spec/spec_helper.rb +7 -15
- data/spec/version_spec.rb +31 -0
- metadata +187 -13
data/docs/Setup.md
ADDED
@@ -0,0 +1,674 @@
|
|
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
|
+
"N": "1560085332.689929"
|
132
|
+
},
|
133
|
+
"updated_at": {
|
134
|
+
"N": "1560085332.695515"
|
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 and notifier 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
|
+
"target_record": {
|
153
|
+
"S": "{\"id\":1,\"email\":\"ichiro@example.com\",\"name\":\"Ichiro\",\"created_at\":\"2019-06-09T13:10:44.853Z\",\"updated_at\":\"2019-06-09T13:10:44.853Z\"}"
|
154
|
+
},
|
155
|
+
"notifiable_key": {
|
156
|
+
"S": "Comment#2"
|
157
|
+
},
|
158
|
+
"notifiable_record": {
|
159
|
+
"S": "{\"id\":2,\"user_id\":2,\"article_id\":1,\"body\":\"This is the first Stephen's comment to Ichiro's article.\",\"created_at\":\"2019-06-09T13:10:45.677Z\",\"updated_at\":\"2019-06-09T13:10:45.677Z\"}"
|
160
|
+
},
|
161
|
+
"key": {
|
162
|
+
"S": "comment.default"
|
163
|
+
},
|
164
|
+
"group_key": {
|
165
|
+
"S": "Article#1"
|
166
|
+
},
|
167
|
+
"notifier_key": {
|
168
|
+
"S": "User#2"
|
169
|
+
},
|
170
|
+
"notifier_record": {
|
171
|
+
"S": "{\"id\":2,\"email\":\"stephen@example.com\",\"name\":\"Stephen\",\"created_at\":\"2019-06-09T13:10:45.006Z\",\"updated_at\":\"2019-06-09T13:10:45.006Z\"}"
|
172
|
+
},
|
173
|
+
"created_at": {
|
174
|
+
"N": "1560085332.689929"
|
175
|
+
},
|
176
|
+
"updated_at": {
|
177
|
+
"N": "1560085332.695515"
|
178
|
+
},
|
179
|
+
"parameters": {
|
180
|
+
"M": {}
|
181
|
+
}
|
182
|
+
}
|
183
|
+
```
|
184
|
+
|
185
|
+
Then, you can process notification records with associated records in your DynamoDB Streams.
|
186
|
+
|
187
|
+
Note: This **store_with_associated_records** option can be set true only when you use mongoid or dynamoid ORM.
|
188
|
+
|
189
|
+
### Configuring models
|
190
|
+
|
191
|
+
#### Configuring target models
|
192
|
+
|
193
|
+
Configure your target model (e.g. *app/models/user.rb*).
|
194
|
+
Add **acts_as_target** configuration to your target model to get notifications.
|
195
|
+
|
196
|
+
##### Target as an ActiveRecord model
|
197
|
+
|
198
|
+
```ruby
|
199
|
+
class User < ActiveRecord::Base
|
200
|
+
# acts_as_target configures your model as ActivityNotification::Target
|
201
|
+
# with parameters as value or custom methods defined in your model as lambda or symbol.
|
202
|
+
# This is an example without any options (default configuration) as the target.
|
203
|
+
acts_as_target
|
204
|
+
end
|
205
|
+
```
|
206
|
+
|
207
|
+
##### Target as a Mongoid model
|
208
|
+
|
209
|
+
```ruby
|
210
|
+
require 'mongoid'
|
211
|
+
class User
|
212
|
+
include Mongoid::Document
|
213
|
+
include Mongoid::Timestamps
|
214
|
+
include GlobalID::Identification
|
215
|
+
|
216
|
+
# You need include ActivityNotification::Models except models which extend ActiveRecord::Base
|
217
|
+
include ActivityNotification::Models
|
218
|
+
acts_as_target
|
219
|
+
end
|
220
|
+
```
|
221
|
+
|
222
|
+
*Note*: *acts_as_notification_target* is an alias for *acts_as_target* and does the same.
|
223
|
+
|
224
|
+
#### Configuring notifiable models
|
225
|
+
|
226
|
+
Configure your notifiable model (e.g. *app/models/comment.rb*).
|
227
|
+
Add **acts_as_notifiable** configuration to your notifiable model representing activity to notify for each of your target model.
|
228
|
+
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.
|
229
|
+
|
230
|
+
##### Notifiable as an ActiveRecord model
|
231
|
+
|
232
|
+
```ruby
|
233
|
+
class Article < ActiveRecord::Base
|
234
|
+
belongs_to :user
|
235
|
+
has_many :comments, dependent: :destroy
|
236
|
+
has_many :commented_users, through: :comments, source: :user
|
237
|
+
end
|
238
|
+
|
239
|
+
class Comment < ActiveRecord::Base
|
240
|
+
belongs_to :article
|
241
|
+
belongs_to :user
|
242
|
+
|
243
|
+
# acts_as_notifiable configures your model as ActivityNotification::Notifiable
|
244
|
+
# with parameters as value or custom methods defined in your model as lambda or symbol.
|
245
|
+
# The first argument is the plural symbol name of your target model.
|
246
|
+
acts_as_notifiable :users,
|
247
|
+
# Notification targets as :targets is a necessary option
|
248
|
+
# Set to notify to author and users commented to the article, except comment owner self
|
249
|
+
targets: ->(comment, key) {
|
250
|
+
([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
|
251
|
+
},
|
252
|
+
# Path to move when the notification is opened by the target user
|
253
|
+
# This is an optional configuration since activity_notification uses polymorphic_path as default
|
254
|
+
notifiable_path: :article_notifiable_path
|
255
|
+
|
256
|
+
def article_notifiable_path
|
257
|
+
article_path(article)
|
258
|
+
end
|
259
|
+
end
|
260
|
+
```
|
261
|
+
|
262
|
+
##### Notifiable as a Mongoid model
|
263
|
+
|
264
|
+
```ruby
|
265
|
+
require 'mongoid'
|
266
|
+
class Article
|
267
|
+
include Mongoid::Document
|
268
|
+
include Mongoid::Timestamps
|
269
|
+
|
270
|
+
belongs_to :user
|
271
|
+
has_many :comments, dependent: :destroy
|
272
|
+
|
273
|
+
def commented_users
|
274
|
+
User.where(:id.in => comments.pluck(:user_id))
|
275
|
+
end
|
276
|
+
end
|
277
|
+
|
278
|
+
require 'mongoid'
|
279
|
+
class Comment
|
280
|
+
include Mongoid::Document
|
281
|
+
include Mongoid::Timestamps
|
282
|
+
include GlobalID::Identification
|
283
|
+
|
284
|
+
# You need include ActivityNotification::Models except models which extend ActiveRecord::Base
|
285
|
+
include ActivityNotification::Models
|
286
|
+
acts_as_notifiable :users,
|
287
|
+
targets: ->(comment, key) {
|
288
|
+
([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
|
289
|
+
},
|
290
|
+
notifiable_path: :article_notifiable_path
|
291
|
+
|
292
|
+
def article_notifiable_path
|
293
|
+
article_path(article)
|
294
|
+
end
|
295
|
+
end
|
296
|
+
```
|
297
|
+
|
298
|
+
##### Advanced notifiable path
|
299
|
+
|
300
|
+
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:
|
301
|
+
|
302
|
+
```ruby
|
303
|
+
acts_as_notifiable :users,
|
304
|
+
targets: ->(comment, key) {
|
305
|
+
([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
|
306
|
+
},
|
307
|
+
notifiable_path: ->(comment, key) { "#{comment.article_notifiable_path}##{key}" }
|
308
|
+
```
|
309
|
+
|
310
|
+
This will attach the key of the notification to the notifiable path.
|
311
|
+
|
312
|
+
### Configuring views
|
313
|
+
|
314
|
+
*activity_notification* provides view templates to customize your notification views. The view generator can generate default views for all targets.
|
315
|
+
|
316
|
+
```console
|
317
|
+
$ bin/rails generate activity_notification:views
|
318
|
+
```
|
319
|
+
|
320
|
+
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:
|
321
|
+
|
322
|
+
```console
|
323
|
+
$ bin/rails generate activity_notification:views users
|
324
|
+
```
|
325
|
+
|
326
|
+
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),
|
327
|
+
you can pass a list of modules to the generator with the *-v* flag.
|
328
|
+
|
329
|
+
```console
|
330
|
+
$ bin/rails generate activity_notification:views -v notifications
|
331
|
+
```
|
332
|
+
|
333
|
+
### Configuring routes
|
334
|
+
|
335
|
+
*activity_notification* also provides routing helper for notifications. Add **notify_to** method to *config/routes.rb* for the target (e.g. *:users*):
|
336
|
+
|
337
|
+
```ruby
|
338
|
+
Rails.application.routes.draw do
|
339
|
+
notify_to :users
|
340
|
+
end
|
341
|
+
```
|
342
|
+
|
343
|
+
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)*.
|
344
|
+
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).
|
345
|
+
|
346
|
+
#### Routes with scope
|
347
|
+
|
348
|
+
You can also configure *activity_notification* routes with scope like this:
|
349
|
+
|
350
|
+
```ruby
|
351
|
+
Rails.application.routes.draw do
|
352
|
+
scope :myscope, as: :myscope do
|
353
|
+
notify_to :users, routing_scope: :myscope
|
354
|
+
end
|
355
|
+
end
|
356
|
+
```
|
357
|
+
|
358
|
+
Then, pages are shown as */myscope/users/1/notifications*.
|
359
|
+
|
360
|
+
#### Routes as REST API backend
|
361
|
+
|
362
|
+
You can configure *activity_notification* routes as REST API backend with *api_mode* option like this:
|
363
|
+
|
364
|
+
```ruby
|
365
|
+
Rails.application.routes.draw do
|
366
|
+
scope :api do
|
367
|
+
scope :"v2" do
|
368
|
+
notify_to :users, api_mode: true
|
369
|
+
end
|
370
|
+
end
|
371
|
+
end
|
372
|
+
```
|
373
|
+
|
374
|
+
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.
|
375
|
+
|
376
|
+
### Creating notifications
|
377
|
+
|
378
|
+
#### Notification API
|
379
|
+
|
380
|
+
You can trigger notifications by setting all your required parameters and triggering **notify** on the notifiable model, like this:
|
381
|
+
|
382
|
+
```ruby
|
383
|
+
@comment.notify :users, key: "comment.reply"
|
384
|
+
```
|
385
|
+
|
386
|
+
Or, you can call public API as **ActivityNotification::Notification.notify**
|
387
|
+
|
388
|
+
```ruby
|
389
|
+
ActivityNotification::Notification.notify :users, @comment, key: "comment.reply"
|
390
|
+
```
|
391
|
+
|
392
|
+
The first argument is the plural symbol name of your target model, which is configured in notifiable model by *acts_as_notifiable*.
|
393
|
+
The new instances of **ActivityNotification::Notification** model will be generated for the specified targets.
|
394
|
+
|
395
|
+
*Hint*: *:key* is a option. Default key `#{notifiable_type}.default` which means *comment.default* will be used without specified key.
|
396
|
+
You can override it by *Notifiable#default_notification_key*.
|
397
|
+
|
398
|
+
#### Asynchronous notification API with ActiveJob
|
399
|
+
|
400
|
+
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:
|
401
|
+
|
402
|
+
```ruby
|
403
|
+
@comment.notify_later :users, key: "comment.reply"
|
404
|
+
```
|
405
|
+
|
406
|
+
You can also use *:notify_later* option in *notify* method. This is the same operation as calling *notify_later* method.
|
407
|
+
|
408
|
+
```ruby
|
409
|
+
@comment.notify :users, key: "comment.reply", notify_later: true
|
410
|
+
```
|
411
|
+
|
412
|
+
*Note*: *notify_now* is an alias for *notify* and does the same.
|
413
|
+
|
414
|
+
When you use asynchronous notification API, you should setup ActiveJob with background queuing service such as Sidekiq.
|
415
|
+
You can set *config.active_job_queue* in your initializer to specify a queue name *activity_notification* will use.
|
416
|
+
The default queue name is *:activity_notification*.
|
417
|
+
|
418
|
+
```ruby
|
419
|
+
# Configure ActiveJob queue name for delayed notifications.
|
420
|
+
config.active_job_queue = :my_notification_queue
|
421
|
+
```
|
422
|
+
|
423
|
+
#### Automatic tracked notifications
|
424
|
+
|
425
|
+
You can also generate automatic tracked notifications by **:tracked** option in *acts_as_notifiable*.
|
426
|
+
*:tracked* option adds required callbacks to generate notifications for creation and update of the notifiable model.
|
427
|
+
Set true to *:tracked* option to generate all tracked notifications, like this:
|
428
|
+
|
429
|
+
```ruby
|
430
|
+
class Comment < ActiveRecord::Base
|
431
|
+
acts_as_notifiable :users,
|
432
|
+
targets: ->(comment, key) {
|
433
|
+
([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
|
434
|
+
},
|
435
|
+
# Set true to :tracked option to generate automatic tracked notifications.
|
436
|
+
# It adds required callbacks to generate notifications for creation and update of the notifiable model.
|
437
|
+
tracked: true
|
438
|
+
end
|
439
|
+
```
|
440
|
+
|
441
|
+
Or, set *:only* or *:except* option to generate specified tracked notifications, like this:
|
442
|
+
|
443
|
+
```ruby
|
444
|
+
class Comment < ActiveRecord::Base
|
445
|
+
acts_as_notifiable :users,
|
446
|
+
targets: ->(comment, key) {
|
447
|
+
([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
|
448
|
+
},
|
449
|
+
# Set { only: [:create] } to :tracked option to generate tracked notifications for creation only.
|
450
|
+
# It adds required callbacks to generate notifications for creation of the notifiable model.
|
451
|
+
tracked: { only: [:create] }
|
452
|
+
end
|
453
|
+
```
|
454
|
+
|
455
|
+
```ruby
|
456
|
+
class Comment < ActiveRecord::Base
|
457
|
+
acts_as_notifiable :users,
|
458
|
+
targets: ->(comment, key) {
|
459
|
+
([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
|
460
|
+
},
|
461
|
+
# Set { except: [:update] } to :tracked option to generate tracked notifications except update (creation only).
|
462
|
+
# It adds required callbacks to generate notifications for creation of the notifiable model.
|
463
|
+
tracked: { except: [:update], key: 'comment.create.now', send_later: false }
|
464
|
+
end
|
465
|
+
```
|
466
|
+
|
467
|
+
*Hint*: `#{notifiable_type}.create` and `#{notifiable_type}.update` will be used as the key of tracked notifications.
|
468
|
+
You can override them by *Notifiable#notification_key_for_tracked_creation* and *Notifiable#notification_key_for_tracked_update*.
|
469
|
+
You can also specify key option in the *:tracked* statement.
|
470
|
+
|
471
|
+
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:
|
472
|
+
|
473
|
+
```ruby
|
474
|
+
class Comment < ActiveRecord::Base
|
475
|
+
acts_as_notifiable :users,
|
476
|
+
targets: ->(comment, key) {
|
477
|
+
([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
|
478
|
+
},
|
479
|
+
# It adds required callbacks to generate notifications asynchronously for creation of the notifiable model.
|
480
|
+
tracked: { only: [:create], key: 'comment.create.later', notify_later: true }
|
481
|
+
end
|
482
|
+
```
|
483
|
+
|
484
|
+
### Displaying notifications
|
485
|
+
|
486
|
+
#### Preparing target notifications
|
487
|
+
|
488
|
+
To display notifications, you can use **notifications** association of the target model:
|
489
|
+
|
490
|
+
```ruby
|
491
|
+
# custom_notifications_controller.rb
|
492
|
+
def index
|
493
|
+
@notifications = @target.notifications
|
494
|
+
end
|
495
|
+
```
|
496
|
+
|
497
|
+
You can also use several scope to filter notifications. For example, **unopened_only** to filter them unopened notifications only.
|
498
|
+
|
499
|
+
```ruby
|
500
|
+
# custom_notifications_controller.rb
|
501
|
+
def index
|
502
|
+
@notifications = @target.notifications.unopened_only
|
503
|
+
end
|
504
|
+
```
|
505
|
+
|
506
|
+
Moreover, you can use **notification_index** or **notification_index_with_attributes** methods to automatically prepare notification index for the target.
|
507
|
+
|
508
|
+
```ruby
|
509
|
+
# custom_notifications_controller.rb
|
510
|
+
def index
|
511
|
+
@notifications = @target.notification_index_with_attributes
|
512
|
+
end
|
513
|
+
```
|
514
|
+
|
515
|
+
#### Rendering notifications
|
516
|
+
|
517
|
+
You can use **render_notifications** helper in your views to show the notification index:
|
518
|
+
|
519
|
+
```erb
|
520
|
+
<%= render_notifications(@notifications) %>
|
521
|
+
```
|
522
|
+
|
523
|
+
We can set *:target* option to specify the target type of notifications:
|
524
|
+
|
525
|
+
```erb
|
526
|
+
<%= render_notifications(@notifications, target: :users) %>
|
527
|
+
```
|
528
|
+
|
529
|
+
*Note*: *render_notifications* is an alias for *render_notification* and does the same.
|
530
|
+
|
531
|
+
If you want to set notification index in the common layout, such as common header, you can use **render_notifications_of** helper like this:
|
532
|
+
|
533
|
+
```shared/_header.html.erb
|
534
|
+
<%= render_notifications_of current_user, index_content: :with_attributes %>
|
535
|
+
```
|
536
|
+
|
537
|
+
Then, content named **:notification_index** will be prepared and you can use it in your partial template.
|
538
|
+
|
539
|
+
```activity_notifications/notifications/users/_index.html.erb
|
540
|
+
...
|
541
|
+
<%= yield :notification_index %>
|
542
|
+
...
|
543
|
+
```
|
544
|
+
|
545
|
+
Sometimes, it's desirable to pass additional local variables to partials. It can be done this way:
|
546
|
+
|
547
|
+
```erb
|
548
|
+
<%= render_notification(@notification, locals: { friends: current_user.friends }) %>
|
549
|
+
```
|
550
|
+
|
551
|
+
#### Notification views
|
552
|
+
|
553
|
+
*activity_notification* looks for views in *app/views/activity_notification/notifications/:target* with **:key** of the notifications.
|
554
|
+
|
555
|
+
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)*.
|
556
|
+
|
557
|
+
*Hint*: the *"notification."* prefix in *:key* is completely optional, you can skip it in your projects or use this prefix only to make namespace.
|
558
|
+
|
559
|
+
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:
|
560
|
+
|
561
|
+
```erb
|
562
|
+
<%= render_notification(@notification, target: :users, fallback: :default) %>
|
563
|
+
```
|
564
|
+
|
565
|
+
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)*.
|
566
|
+
|
567
|
+
If you do not specify *:target* option like this,
|
568
|
+
|
569
|
+
```erb
|
570
|
+
<%= render_notification(@notification, fallback: :default) %>
|
571
|
+
```
|
572
|
+
|
573
|
+
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)*.
|
574
|
+
|
575
|
+
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:
|
576
|
+
|
577
|
+
```erb
|
578
|
+
<%= render_notification(@notification, fallback: :text) %>
|
579
|
+
```
|
580
|
+
|
581
|
+
Finally, default views of *activity_notification* depends on jQuery and you have to add requirements to *application.js* in your apps:
|
582
|
+
|
583
|
+
```app/assets/javascripts/application.js
|
584
|
+
//= require jquery
|
585
|
+
//= require jquery_ujs
|
586
|
+
```
|
587
|
+
|
588
|
+
#### i18n for notifications
|
589
|
+
|
590
|
+
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*.
|
591
|
+
|
592
|
+
Translations should be put in your locale *.yml* files as **text** field. To render pure strings from I18n example structure:
|
593
|
+
|
594
|
+
```yaml
|
595
|
+
notification:
|
596
|
+
user:
|
597
|
+
article:
|
598
|
+
create:
|
599
|
+
text: 'Article has been created'
|
600
|
+
update:
|
601
|
+
text: 'Article %{article_title} has been updated'
|
602
|
+
destroy:
|
603
|
+
text: 'Some user removed an article!'
|
604
|
+
comment:
|
605
|
+
create:
|
606
|
+
text: '%{notifier_name} posted a comment on the article "%{article_title}"'
|
607
|
+
post:
|
608
|
+
text:
|
609
|
+
one: "<p>%{notifier_name} posted a comment on your article %{article_title}</p>"
|
610
|
+
other: "<p>%{notifier_name} posted %{count} comments on your article %{article_title}</p>"
|
611
|
+
reply:
|
612
|
+
text: "<p>%{notifier_name} and %{group_member_count} other people replied %{group_notification_count} times to your comment</p>"
|
613
|
+
mail_subject: 'New comment on your article'
|
614
|
+
admin:
|
615
|
+
article:
|
616
|
+
post:
|
617
|
+
text: '[Admin] Article has been created'
|
618
|
+
```
|
619
|
+
|
620
|
+
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.
|
621
|
+
|
622
|
+
### Customizing controllers (optional)
|
623
|
+
|
624
|
+
If the customization at the views level is not enough, you can customize each controller by following these steps:
|
625
|
+
|
626
|
+
1. Create your custom controllers using the generator with a target:
|
627
|
+
|
628
|
+
```console
|
629
|
+
$ bin/rails generate activity_notification:controllers users
|
630
|
+
```
|
631
|
+
|
632
|
+
If you specify *users* as the target, controllers will be created in *app/controllers/users*.
|
633
|
+
And the notifications controller will look like this:
|
634
|
+
|
635
|
+
```ruby
|
636
|
+
class Users::NotificationsController < ActivityNotification::NotificationsController
|
637
|
+
# GET /:target_type/:target_id/notifications
|
638
|
+
# def index
|
639
|
+
# super
|
640
|
+
# end
|
641
|
+
|
642
|
+
# ...
|
643
|
+
|
644
|
+
# PUT /:target_type/:target_id/notifications/:id/open
|
645
|
+
# def open
|
646
|
+
# super
|
647
|
+
# end
|
648
|
+
|
649
|
+
# ...
|
650
|
+
end
|
651
|
+
```
|
652
|
+
|
653
|
+
2. Tell the router to use this controller:
|
654
|
+
|
655
|
+
```ruby
|
656
|
+
notify_to :users, controller: 'users/notifications'
|
657
|
+
```
|
658
|
+
|
659
|
+
3. Finally, change or extend the desired controller actions.
|
660
|
+
|
661
|
+
You can completely override a controller action
|
662
|
+
```ruby
|
663
|
+
class Users::NotificationsController < ActivityNotification::NotificationsController
|
664
|
+
# ...
|
665
|
+
|
666
|
+
# PUT /:target_type/:target_id/notifications/:id/open
|
667
|
+
def open
|
668
|
+
# Custom code to open notification here
|
669
|
+
|
670
|
+
# super
|
671
|
+
end
|
672
|
+
|
673
|
+
# ...
|
674
|
+
end
|