activity_notification 1.7.1 → 2.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- 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 +13 -3
- data/.travis.yml +21 -6
- data/CHANGELOG.md +70 -2
- data/Gemfile +16 -2
- data/Procfile +2 -0
- data/README.md +170 -1193
- data/Rakefile +19 -10
- data/activity_notification.gemspec +9 -4
- 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 +37 -0
- data/app/channels/activity_notification/notification_with_devise_channel.rb +51 -0
- 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 +80 -54
- 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 +79 -70
- data/app/jobs/activity_notification/notify_all_job.rb +16 -0
- data/app/jobs/activity_notification/notify_job.rb +17 -0
- data/app/jobs/activity_notification/notify_to_job.rb +16 -0
- 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 +55 -2
- 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/_dynamodblocal +4 -0
- data/{scripts → bin}/bundle_update.sh +1 -0
- data/bin/deploy_on_heroku.sh +16 -0
- data/bin/install_dynamodblocal.sh +5 -0
- data/bin/start_dynamodblocal.sh +47 -0
- data/bin/stop_dynamodblocal.sh +34 -0
- data/docs/CODE_OF_CONDUCT.md +76 -0
- data/docs/CONTRIBUTING.md +36 -0
- data/docs/Functions.md +1130 -0
- data/docs/Setup.md +688 -0
- data/docs/Testing.md +148 -0
- data/gemfiles/Gemfile.rails-4.2 +6 -1
- data/gemfiles/Gemfile.rails-5.0 +7 -1
- data/gemfiles/Gemfile.rails-5.1 +6 -1
- data/gemfiles/Gemfile.rails-5.2 +6 -1
- data/gemfiles/Gemfile.rails-6.0 +24 -0
- data/lib/activity_notification.rb +14 -0
- data/lib/activity_notification/apis/notification_api.rb +233 -143
- data/lib/activity_notification/apis/subscription_api.rb +92 -53
- data/lib/activity_notification/apis/swagger.rb +6 -0
- data/lib/activity_notification/common.rb +3 -3
- data/lib/activity_notification/config.rb +109 -33
- data/lib/activity_notification/controllers/common_api_controller.rb +30 -0
- data/lib/activity_notification/controllers/common_controller.rb +48 -12
- 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 +6 -0
- data/lib/activity_notification/helpers/view_helpers.rb +5 -1
- data/lib/activity_notification/mailers/helpers.rb +17 -10
- data/lib/activity_notification/models/concerns/notifiable.rb +78 -54
- data/lib/activity_notification/models/concerns/subscriber.rb +19 -7
- 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 +100 -29
- 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 +9 -15
- data/lib/activity_notification/orm/active_record/notification.rb +20 -23
- data/lib/activity_notification/orm/active_record/subscription.rb +1 -1
- data/lib/activity_notification/orm/dynamoid.rb +523 -0
- data/lib/activity_notification/orm/dynamoid/extension.rb +262 -0
- data/lib/activity_notification/orm/dynamoid/notification.rb +189 -0
- data/lib/activity_notification/orm/dynamoid/subscription.rb +82 -0
- data/lib/activity_notification/orm/mongoid.rb +26 -1
- data/lib/activity_notification/orm/mongoid/notification.rb +27 -26
- data/lib/activity_notification/orm/mongoid/subscription.rb +2 -2
- 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 +51 -5
- data/lib/activity_notification/roles/acts_as_target.rb +62 -9
- 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 +38 -7
- 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/lib/tasks/activity_notification_tasks.rake +14 -4
- 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/channels/notification_channel_shared_examples.rb +59 -0
- data/spec/channels/notification_channel_spec.rb +50 -0
- data/spec/channels/notification_with_devise_channel_spec.rb +99 -0
- data/spec/concerns/apis/notification_api_spec.rb +39 -4
- data/spec/concerns/apis/subscription_api_spec.rb +2 -2
- data/spec/concerns/models/notifiable_spec.rb +136 -7
- data/spec/concerns/models/subscriber_spec.rb +63 -62
- data/spec/concerns/models/target_spec.rb +167 -13
- data/spec/concerns/renderable_spec.rb +2 -2
- data/spec/config_spec.rb +41 -1
- 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 +55 -76
- 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 +95 -118
- 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 +16 -21
- data/spec/mailers/mailer_spec.rb +41 -0
- data/spec/models/dummy/dummy_group_spec.rb +4 -0
- data/spec/models/dummy/dummy_notifiable_spec.rb +4 -0
- data/spec/models/dummy/dummy_notifier_spec.rb +4 -0
- data/spec/models/dummy/dummy_subscriber_spec.rb +3 -0
- data/spec/models/dummy/dummy_target_spec.rb +4 -0
- data/spec/models/notification_spec.rb +181 -45
- data/spec/models/subscription_spec.rb +77 -27
- 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/orm/dynamoid_spec.rb +115 -0
- data/spec/rails_app/Rakefile +9 -0
- data/spec/rails_app/app/assets/config/manifest.js +3 -0
- data/spec/rails_app/app/{models → assets/images}/.keep +0 -0
- data/spec/rails_app/app/assets/javascripts/application.js +2 -1
- data/spec/rails_app/app/assets/javascripts/cable.js +12 -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/comments_controller.rb +3 -4
- 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 +14 -11
- data/spec/rails_app/app/models/article.rb +25 -20
- data/spec/rails_app/app/models/comment.rb +27 -62
- data/spec/rails_app/app/models/user.rb +43 -18
- data/spec/rails_app/app/views/activity_notification/notifications/default/article/_update.html.erb +146 -0
- data/spec/rails_app/app/views/activity_notification/notifications/users/overridden/custom/_test.html.erb +1 -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 +21 -3
- data/spec/rails_app/config/cable.yml +8 -0
- data/spec/rails_app/config/dynamoid.rb +5 -0
- data/spec/rails_app/config/environment.rb +5 -1
- data/spec/rails_app/config/environments/development.rb +5 -0
- data/spec/rails_app/config/environments/production.rb +4 -1
- data/spec/rails_app/config/environments/test.rb +5 -0
- data/spec/rails_app/config/initializers/activity_notification.rb +38 -7
- data/spec/rails_app/config/initializers/copy_it.aws.rb.template +6 -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 +4 -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 +22 -5
- data/spec/rails_app/lib/custom_optional_targets/raise_error.rb +14 -0
- data/spec/rails_app/lib/mailer_previews/mailer_preview.rb +12 -4
- 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 +2 -4
- data/spec/roles/acts_as_notifier_spec.rb +0 -2
- data/spec/roles/acts_as_target_spec.rb +1 -5
- data/spec/spec_helper.rb +15 -16
- data/spec/version_spec.rb +31 -0
- metadata +251 -22
- data/spec/rails_app/app/views/activity_notification/notifications/users/overriden/custom/_test.html.erb +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9a517c56b317fe1705d3666039793aee93ed55c77c2493b70018eb1da4268f97
|
4
|
+
data.tar.gz: ba26afa004950dc8ec175abb6f36a948014f3ff7be81b8d185665f25f22137fe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 88b587552a4c2e1bfeda92759b44f7611309409b3bfb11c7a777739a30e3af4f28f96d532b39c42edfed0b07de274239014d6d117590f51dc1ac09fc8898e8de
|
7
|
+
data.tar.gz: c9e40bf9e14e6c5c0f4898161748e2ab07e9b1e95c8fbb2daaf151a5d94bab9727280f840216c7375819aac2f6564659d9fa9b05ce8a320dd2e3c7b42269acb3
|
@@ -0,0 +1,22 @@
|
|
1
|
+
---
|
2
|
+
name: Bug report
|
3
|
+
about: Create a report to help us improve
|
4
|
+
title: ''
|
5
|
+
labels: ''
|
6
|
+
assignees: ''
|
7
|
+
|
8
|
+
---
|
9
|
+
|
10
|
+
### Steps to reproduce
|
11
|
+
<!-- Tell us how to reproduce the issue -->
|
12
|
+
|
13
|
+
### Expected behavior
|
14
|
+
<!-- Tell us what should happen -->
|
15
|
+
|
16
|
+
### Actual behavior
|
17
|
+
<!-- Tell us what happens instead -->
|
18
|
+
|
19
|
+
### System configuration
|
20
|
+
**activity_notification gem version**:
|
21
|
+
**Rails version**:
|
22
|
+
**ORM (ActiveRecord, Mongoid or Dynamoid)**:
|
@@ -0,0 +1,17 @@
|
|
1
|
+
---
|
2
|
+
name: Feature request
|
3
|
+
about: Suggest an idea for this project
|
4
|
+
title: ''
|
5
|
+
labels: ''
|
6
|
+
assignees: ''
|
7
|
+
|
8
|
+
---
|
9
|
+
|
10
|
+
### Problem or use case
|
11
|
+
<!-- Tell us what the problem is if your feature request is related to a problem -->
|
12
|
+
|
13
|
+
### Expected solution
|
14
|
+
<!-- Tell us what you want to happen -->
|
15
|
+
|
16
|
+
### Alternatives
|
17
|
+
<!-- Tell us any alternative solutions or features you've considered -->
|
@@ -0,0 +1,13 @@
|
|
1
|
+
**Issue #, if available**:
|
2
|
+
|
3
|
+
### Summary
|
4
|
+
|
5
|
+
<!-- Provide a general description of the code changes in your pull request.
|
6
|
+
Were there any bugs you had fixed? If so, mention them.
|
7
|
+
If these bugs have open GitHub issues, be sure to tag them here as well, to keep the conversation linked together. -->
|
8
|
+
|
9
|
+
### Other Information
|
10
|
+
|
11
|
+
<!-- If there's anything else that's important and relevant to your pull request, mention that information here.
|
12
|
+
|
13
|
+
Thank you for contributing to activity_notification! -->
|
data/.gitignore
CHANGED
@@ -7,9 +7,12 @@
|
|
7
7
|
/InstalledFiles
|
8
8
|
/pkg/
|
9
9
|
/spec/reports/
|
10
|
+
/spec/openapi.json
|
10
11
|
/spec/examples.txt
|
11
12
|
/spec/rails_app/log/*
|
12
13
|
/spec/rails_app/tmp/*
|
14
|
+
/spec/rails_app/public/assets/
|
15
|
+
/spec/DynamoDBLocal-latest/
|
13
16
|
/test/tmp/
|
14
17
|
/test/version_tmp/
|
15
18
|
/tmp/
|
@@ -17,6 +20,7 @@
|
|
17
20
|
*~
|
18
21
|
*.sqlite3
|
19
22
|
.project
|
23
|
+
.DS_Store
|
20
24
|
|
21
25
|
# Used by dotenv library to load environment variables.
|
22
26
|
# .env
|
@@ -51,11 +55,17 @@ build-iPhoneSimulator/
|
|
51
55
|
/gemfiles/vendor/bundle
|
52
56
|
/lib/bundler/man/
|
53
57
|
|
58
|
+
# Ignore webpacker files
|
59
|
+
/spec/rails_app/node_modules
|
60
|
+
/spec/rails_app/yarn.lock
|
61
|
+
/spec/rails_app/yarn-error.log
|
62
|
+
/spec/rails_app/public/packs
|
63
|
+
/spec/rails_app/public/packs-test
|
64
|
+
|
54
65
|
# for a library or gem, you might want to ignore these files since the code is
|
55
66
|
# intended to run in multiple environments; otherwise, check them in:
|
56
|
-
|
57
|
-
|
58
|
-
# .ruby-gemset
|
67
|
+
.ruby-version
|
68
|
+
.ruby-gemset
|
59
69
|
|
60
70
|
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
61
71
|
.rvmrc
|
data/.travis.yml
CHANGED
@@ -5,24 +5,31 @@ branches:
|
|
5
5
|
- images
|
6
6
|
|
7
7
|
rvm:
|
8
|
-
- 2.
|
9
|
-
|
10
|
-
# - 2.
|
11
|
-
# - 2.
|
8
|
+
# - 2.7.0 # Need to respond to warnings for Ruby 3.0
|
9
|
+
- 2.6.5
|
10
|
+
# - 2.5.7
|
11
|
+
# - 2.4.9
|
12
|
+
# - 2.3.8 #EOL
|
12
13
|
|
13
14
|
gemfile:
|
14
15
|
- gemfiles/Gemfile.rails-4.2
|
15
16
|
- gemfiles/Gemfile.rails-5.0
|
16
17
|
- gemfiles/Gemfile.rails-5.1
|
17
18
|
- gemfiles/Gemfile.rails-5.2
|
19
|
+
- gemfiles/Gemfile.rails-6.0
|
18
20
|
|
19
21
|
env:
|
20
22
|
- AN_ORM=active_record
|
21
23
|
- AN_ORM=active_record AN_TEST_DB=mysql
|
22
24
|
- AN_ORM=active_record AN_TEST_DB=postgresql
|
25
|
+
- AN_ORM=mongoid
|
23
26
|
- AN_ORM=mongoid AN_TEST_DB=mongodb
|
27
|
+
- AN_ORM=dynamoid
|
24
28
|
|
25
29
|
matrix:
|
30
|
+
exclude:
|
31
|
+
- gemfile: gemfiles/Gemfile.rails-4.2
|
32
|
+
env: AN_ORM=dynamoid
|
26
33
|
include:
|
27
34
|
- rvm: ruby-head
|
28
35
|
gemfile: Gemfile
|
@@ -30,11 +37,16 @@ matrix:
|
|
30
37
|
- rvm: ruby-head
|
31
38
|
gemfile: Gemfile
|
32
39
|
env: AN_ORM=mongoid
|
40
|
+
- rvm: ruby-head
|
41
|
+
gemfile: Gemfile
|
42
|
+
env: AN_ORM=dynamoid
|
33
43
|
allow_failures:
|
34
44
|
- rvm: ruby-head
|
35
45
|
fast_finish: true
|
36
46
|
|
37
47
|
services:
|
48
|
+
- mysql
|
49
|
+
- postgresql
|
38
50
|
- mongodb
|
39
51
|
|
40
52
|
sudo: false
|
@@ -48,12 +60,15 @@ before_install:
|
|
48
60
|
install:
|
49
61
|
# Specify bundler version as '< 2.0' for Rails 4.2
|
50
62
|
- bundle _1.17.3_ install
|
63
|
+
- if [ "$AN_ORM" = "dynamoid" ]; then bin/install_dynamodblocal.sh; fi
|
51
64
|
|
52
65
|
before_script:
|
53
66
|
# Specify bundler version as '< 2.0' for Rails 4.2
|
54
67
|
- bundle _1.17.3_ update
|
55
|
-
- mysql -e 'create database activity_notification_test'
|
56
|
-
- psql -c 'create database activity_notification_test' -U postgres
|
68
|
+
- if [ "$AN_TEST_DB" = "mysql" ]; then mysql -e 'create database activity_notification_test'; fi
|
69
|
+
- if [ "$AN_TEST_DB" = "postgresql" ]; then psql -c 'create database activity_notification_test' -U postgres; fi
|
70
|
+
- if [ "$AN_ORM" = "dynamoid" ]; then bin/start_dynamodblocal.sh; fi
|
71
|
+
- if [ "$AN_ORM" = "dynamoid" ]; then export AWS_DEFAULT_REGION=ap-northeast-1 AWS_ACCESS_KEY_ID=dummy AWS_SECRET_ACCESS_KEY=dummy; fi
|
57
72
|
|
58
73
|
script: bundle exec rspec
|
59
74
|
|
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,72 @@
|
|
1
|
-
## 1.
|
1
|
+
## 2.1.3 / 2020-08-1
|
2
|
+
[Full Changelog](http://github.com/simukappu/activity_notification/compare/v2.1.2...v2.1.3)
|
3
|
+
|
4
|
+
Enhancements:
|
5
|
+
|
6
|
+
* Enable to use namespaced model - [#132](https://github.com/simukappu/activity_notification/pull/132)
|
7
|
+
|
8
|
+
Bug Fixes:
|
9
|
+
|
10
|
+
* Fix mongoid any_of selector error in filtered_by_group scope - [MONGOID-4887](https://jira.mongodb.org/browse/MONGOID-4887)
|
11
|
+
|
12
|
+
## 2.1.2 / 2020-02-24
|
13
|
+
[Full Changelog](http://github.com/simukappu/activity_notification/compare/v2.1.1...v2.1.2)
|
14
|
+
|
15
|
+
Bug Fixes:
|
16
|
+
|
17
|
+
* Fix scope of uniqueness validation in subscription model with mongoid - [#126](https://github.com/simukappu/activity_notification/issues/126) [#128](https://github.com/simukappu/activity_notification/pull/128)
|
18
|
+
* Fix uninitialized constant DeviseTokenAuth when *config.eager_load = true* - [#129](https://github.com/simukappu/activity_notification/issues/129)
|
19
|
+
|
20
|
+
## 2.1.1 / 2020-02-11
|
21
|
+
[Full Changelog](http://github.com/simukappu/activity_notification/compare/v2.1.0...v2.1.1)
|
22
|
+
|
23
|
+
Bug Fixes:
|
24
|
+
|
25
|
+
* Fix eager_load by autoloading VERSION - [#124](https://github.com/simukappu/activity_notification/issues/124) [#125](https://github.com/simukappu/activity_notification/pull/125)
|
26
|
+
|
27
|
+
## 2.1.0 / 2020-02-04
|
28
|
+
[Full Changelog](http://github.com/simukappu/activity_notification/compare/v2.0.0...v2.1.0)
|
29
|
+
|
30
|
+
Enhancements:
|
31
|
+
|
32
|
+
* Add API mode using notification and subscription API controllers - [#108](https://github.com/simukappu/activity_notification/issues/108) [#113](https://github.com/simukappu/activity_notification/issues/113)
|
33
|
+
* Add API controllers integrated with Devise Token Auth - [#108](https://github.com/simukappu/activity_notification/issues/108) [#113](https://github.com/simukappu/activity_notification/issues/113)
|
34
|
+
* Add sample single page application working with REST API backend - [#108](https://github.com/simukappu/activity_notification/issues/108) [#113](https://github.com/simukappu/activity_notification/issues/113)
|
35
|
+
* Move Action Cable broadcasting to optional targets - [#111](https://github.com/simukappu/activity_notification/issues/111)
|
36
|
+
* Add Action Cable API channels publishing formatted JSON - [#111](https://github.com/simukappu/activity_notification/issues/111)
|
37
|
+
* Rescue and skip error in optional_targets - [#103](https://github.com/simukappu/activity_notification/issues/103)
|
38
|
+
* Add *later_than* and *earlier_than* filter options to notification index API - [#108](https://github.com/simukappu/activity_notification/issues/108)
|
39
|
+
* Add key uniqueness validation to subscription model - [#119](https://github.com/simukappu/activity_notification/issues/119)
|
40
|
+
* Make mailer headers more configurable to set custom *from*, *reply_to* and *message_id* - [#116](https://github.com/simukappu/activity_notification/pull/116)
|
41
|
+
* Allow use and test with Rails 6.0 release - [#102](https://github.com/simukappu/activity_notification/issues/102)
|
42
|
+
|
43
|
+
Breaking Changes:
|
44
|
+
|
45
|
+
* Change HTTP POST method of open notification and subscription methods into PUT method
|
46
|
+
* Make *Target#open_all_notifications* return opened notification records instead of their count
|
47
|
+
* Make *Subscriber#create_subscription* raise *ActivityNotification::RecordInvalidError* when the request is invalid - [#119](https://github.com/simukappu/activity_notification/pull/119)
|
48
|
+
|
49
|
+
## 2.0.0 / 2019-08-09
|
50
|
+
[Full Changelog](http://github.com/simukappu/activity_notification/compare/v1.7.1...v2.0.0)
|
51
|
+
|
52
|
+
Enhancements:
|
53
|
+
|
54
|
+
* Add push notification with Action Cable - [#101](https://github.com/simukappu/activity_notification/issues/101)
|
55
|
+
* Allow use with Rails 6.0 - [#102](https://github.com/simukappu/activity_notification/issues/102)
|
56
|
+
* Add Amazon DynamoDB support using Dynamoid
|
57
|
+
* Add *ActivityNotification.config.store_with_associated_records* option
|
58
|
+
* Add test case using Mongoid orm with ActiveRecord application
|
59
|
+
* Publish demo application on Heroku
|
60
|
+
|
61
|
+
Bug Fixes:
|
62
|
+
|
63
|
+
* Fix syntax error of a default view *_default_without_grouping.html.erb*
|
64
|
+
|
65
|
+
Deprecated:
|
66
|
+
|
67
|
+
* Remove deprecated *ActivityNotification.config.table_name* option
|
68
|
+
|
69
|
+
## 1.7.1 / 2019-04-30
|
2
70
|
[Full Changelog](http://github.com/simukappu/activity_notification/compare/v1.7.0...v1.7.1)
|
3
71
|
|
4
72
|
Enhancements:
|
@@ -184,7 +252,7 @@ Bug Fixes:
|
|
184
252
|
|
185
253
|
Breaking Changes:
|
186
254
|
|
187
|
-
* Remove *notifiable_type* from the argument of
|
255
|
+
* Remove *notifiable_type* from the argument of overridden method or configured lambda function with *:batch_email_allowed* option in *acts_as_target* role
|
188
256
|
|
189
257
|
## 1.0.2 / 2016-11-14
|
190
258
|
[Full Changelog](http://github.com/simukappu/activity_notification/compare/v1.0.1...v1.0.2)
|
data/Gemfile
CHANGED
@@ -2,17 +2,31 @@ source 'https://rubygems.org'
|
|
2
2
|
|
3
3
|
gemspec
|
4
4
|
|
5
|
-
gem 'rails', '~>
|
5
|
+
gem 'rails', '~> 6.0.0'
|
6
|
+
|
7
|
+
group :production do
|
8
|
+
gem 'puma'
|
9
|
+
gem 'pg'
|
10
|
+
gem 'devise'
|
11
|
+
gem 'devise_token_auth'
|
12
|
+
end
|
6
13
|
|
7
14
|
group :development do
|
8
15
|
gem 'bullet'
|
9
16
|
end
|
10
17
|
|
11
18
|
group :test do
|
19
|
+
#TODO https://github.com/rails/rails/issues/35417
|
20
|
+
gem 'rspec-rails', '4.0.0.beta4'
|
12
21
|
gem 'rails-controller-testing'
|
13
22
|
gem 'ammeter'
|
14
23
|
gem 'timecop'
|
15
|
-
gem '
|
24
|
+
gem 'committee'
|
25
|
+
gem 'committee-rails'
|
26
|
+
# gem 'coveralls', require: false
|
27
|
+
gem 'coveralls_reborn', require: false
|
16
28
|
end
|
17
29
|
|
30
|
+
gem 'webpacker', groups: [:production, :development]
|
31
|
+
gem 'rack-cors', groups: [:production, :development]
|
18
32
|
gem 'dotenv-rails', groups: [:development, :test]
|
data/Procfile
ADDED
data/README.md
CHANGED
@@ -8,105 +8,158 @@
|
|
8
8
|
[![Gem Downloads](https://img.shields.io/gem/dt/activity_notification.svg)](https://rubygems.org/gems/activity_notification)
|
9
9
|
[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](MIT-LICENSE)
|
10
10
|
|
11
|
-
*activity_notification* provides integrated user activity notifications for Ruby on Rails. You can easily use it to configure multiple notification targets and make activity notifications with notifiable models, like adding comments, responding etc.
|
11
|
+
*activity_notification* provides integrated user activity notifications for [Ruby on Rails](https://rubyonrails.org). You can easily use it to configure multiple notification targets and make activity notifications with notifiable models, like adding comments, responding etc.
|
12
12
|
|
13
|
-
*activity_notification* supports Rails 4.2+ with ActiveRecord
|
13
|
+
*activity_notification* supports Rails 4.2+ with [ActiveRecord](https://guides.rubyonrails.org/active_record_basics.html), [Mongoid](https://mongoid.org) and [Dynamoid](https://github.com/Dynamoid/dynamoid) ORM. It is tested for [MySQL](https://www.mysql.com), [PostgreSQL](https://www.postgresql.org), [SQLite3](https://www.sqlite.org) with ActiveRecord, [MongoDB](https://www.mongodb.com) with Mongoid and [Amazon DynamoDB](https://aws.amazon.com/dynamodb) with Dynamoid. The latest *activity_notification* [v2](https://rubygems.org/gems/activity_notification) is almost compatible with [v1](https://rubygems.org/gems/activity_notification/versions/1.7.1).
|
14
14
|
|
15
15
|
|
16
16
|
## About
|
17
17
|
|
18
18
|
*activity_notification* provides following functions:
|
19
|
-
* Notification API (creating notifications, query for notifications
|
20
|
-
* Notification models (stored with ActiveRecord or
|
19
|
+
* Notification API for your Rails application (creating and managing notifications, query for notifications)
|
20
|
+
* Notification models (stored with ActiveRecord, Mongoid or Dynamoid ORM)
|
21
21
|
* Notification controllers (managing open/unopen of notifications, providing link to notifiable activity page)
|
22
22
|
* Notification views (presentation of notifications)
|
23
23
|
* Automatic tracked notifications (generating notifications along with the lifecycle of notifiable models)
|
24
24
|
* Grouping notifications (grouping like *"Kevin and 7 other users posted comments to this article"*)
|
25
25
|
* Email notification
|
26
26
|
* Batch email notification (event driven or periodical email notification, daily or weekly etc)
|
27
|
+
* Push notification with [Action Cable](https://guides.rubyonrails.org/action_cable_overview.html)
|
27
28
|
* Subscription management (subscribing and unsubscribing for each target and notification type)
|
29
|
+
* REST API backend and [OpenAPI Specification](https://github.com/OAI/OpenAPI-Specification)
|
28
30
|
* Integration with [Devise](https://github.com/plataformatec/devise) authentication
|
29
|
-
*
|
31
|
+
* Activity notifications stream integrated into cloud computing using [Amazon DynamoDB Streams](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Streams.html)
|
32
|
+
* Optional notification targets (Configurable optional notification targets like [Amazon SNS](https://aws.amazon.com/sns), [Slack](https://slack.com), SMS and so on)
|
33
|
+
|
34
|
+
### Online Demo
|
35
|
+
|
36
|
+
You can see an actual application using this gem here:
|
37
|
+
* **https://activity-notification-example.herokuapp.com/**
|
38
|
+
|
39
|
+
Login as the following test users to experience user activity notifications:
|
40
|
+
|
41
|
+
| Email | Password | Admin? |
|
42
|
+
|:---:|:---:|:---:|
|
43
|
+
| ichiro@example.com | changeit | Yes |
|
44
|
+
| stephen@example.com | changeit | |
|
45
|
+
| klay@example.com | changeit | |
|
46
|
+
| kevin@example.com | changeit | |
|
47
|
+
|
48
|
+
The deployed demo application is included in this gem's source code as a test application here: *[/spec/rails_app](/spec/rails_app/)*
|
30
49
|
|
31
50
|
### Notification index and plugin notifications
|
51
|
+
|
32
52
|
<kbd>![plugin-notifications-image](https://raw.githubusercontent.com/simukappu/activity_notification/images/activity_notification_plugin_focus_with_subscription.png)</kbd>
|
33
53
|
|
34
54
|
*activity_notification* deeply uses [PublicActivity](https://github.com/pokonski/public_activity) as reference in presentation layer.
|
35
55
|
|
36
56
|
### Subscription management of notifications
|
57
|
+
|
37
58
|
<kbd>![subscription-management-image](https://raw.githubusercontent.com/simukappu/activity_notification/images/activity_notification_subscription_management_with_optional_targets.png)</kbd>
|
38
59
|
|
39
60
|
### Amazon SNS as optional notification target
|
61
|
+
|
40
62
|
<kbd>![optional-target-amazon-sns-email-image](https://raw.githubusercontent.com/simukappu/activity_notification/images/activity_notification_optional_target_amazon_sns.png)</kbd>
|
41
63
|
|
42
64
|
### Slack as optional notification target
|
65
|
+
|
43
66
|
<kbd>![optional-target-slack-image](https://raw.githubusercontent.com/simukappu/activity_notification/images/activity_notification_optional_target_slack.png)</kbd>
|
44
67
|
|
68
|
+
### Public REST API reference as OpenAPI Specification
|
69
|
+
|
70
|
+
REST API reference as OpenAPI Specification is published in SwaggerHub here:
|
71
|
+
* **https://app.swaggerhub.com/apis-docs/simukappu/activity-notification/**
|
72
|
+
|
73
|
+
You can see sample single page application using [Vue.js](https://vuejs.org) as a part of example Rails application here:
|
74
|
+
* **https://activity-notification-example.herokuapp.com/spa/**
|
75
|
+
|
76
|
+
This sample application works with *activity_notification* REST API backend.
|
45
77
|
|
46
|
-
|
78
|
+
|
79
|
+
## Table of Contents
|
47
80
|
|
48
81
|
- [About](#about)
|
49
|
-
- [
|
50
|
-
- [
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
- [
|
55
|
-
- [
|
56
|
-
- [
|
57
|
-
|
58
|
-
|
59
|
-
- [Configuring
|
60
|
-
- [
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
- [
|
67
|
-
|
68
|
-
- [Notification
|
69
|
-
- [
|
70
|
-
|
71
|
-
- [
|
72
|
-
|
73
|
-
- [
|
74
|
-
- [
|
75
|
-
- [
|
76
|
-
|
77
|
-
|
78
|
-
- [
|
79
|
-
- [
|
80
|
-
- [
|
81
|
-
- [
|
82
|
-
- [
|
83
|
-
- [
|
84
|
-
|
85
|
-
- [
|
86
|
-
- [
|
87
|
-
- [
|
88
|
-
- [
|
89
|
-
|
90
|
-
- [
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
- [
|
95
|
-
- [Customizing
|
96
|
-
|
97
|
-
- [
|
98
|
-
- [
|
99
|
-
- [
|
100
|
-
|
101
|
-
|
82
|
+
- [Online Demo](#online-demo)
|
83
|
+
- [Public REST API reference as OpenAPI Specification](#public-rest-apu-reference-as-openapi-specification)
|
84
|
+
- [Getting Started](#getting-started)
|
85
|
+
- [Setup](/docs/Setup.md#Setup)
|
86
|
+
- [Gem installation](/docs/Setup.md#gem-installation)
|
87
|
+
- [Database setup](/docs/Setup.md#database-setup)
|
88
|
+
- [Using ActiveRecord ORM](/docs/Setup.md#using-activerecord-orm)
|
89
|
+
- [Using Mongoid ORM](/docs/Setup.md#using-mongoid-orm)
|
90
|
+
- [Using Dynamoid ORM](/docs/Setup.md#using-dynamoid-orm)
|
91
|
+
- [Integration with DynamoDB Streams](/docs/Setup.md#integration-with-dynamodb-streams)
|
92
|
+
- [Configuring models](/docs/Setup.md#configuring-models)
|
93
|
+
- [Configuring target models](/docs/Setup.md#configuring-target-models)
|
94
|
+
- [Configuring notifiable models](/docs/Setup.md#configuring-notifiable-models)
|
95
|
+
- [Advanced notifiable path](/docs/Setup.md#advanced-notifiable-path)
|
96
|
+
- [Configuring views](/docs/Setup.md#configuring-views)
|
97
|
+
- [Configuring routes](/docs/Setup.md#configuring-routes)
|
98
|
+
- [Routes with scope](/docs/Setup.md#routes-with-scope)
|
99
|
+
- [Routes as REST API backend](/docs/Setup.md#routes-as-rest-api-backend)
|
100
|
+
- [Creating notifications](/docs/Setup.md#creating-notifications)
|
101
|
+
- [Notification API](/docs/Setup.md#notification-api)
|
102
|
+
- [Asynchronous notification API with ActiveJob](/docs/Setup.md#asynchronous-notification-api-with-activejob)
|
103
|
+
- [Automatic tracked notifications](/docs/Setup.md#automatic-tracked-notifications)
|
104
|
+
- [Displaying notifications](/docs/Setup.md#displaying-notifications)
|
105
|
+
- [Preparing target notifications](/docs/Setup.md#preparing-target-notifications)
|
106
|
+
- [Rendering notifications](/docs/Setup.md#rendering-notifications)
|
107
|
+
- [Notification views](/docs/Setup.md#notification-views)
|
108
|
+
- [i18n for notifications](/docs/Setup.md#i18n-for-notifications)
|
109
|
+
- [Customizing controllers (optional)](/docs/Setup.md#customizing-controllers-optional)
|
110
|
+
- [Functions](/docs/Functions.md#Functions)
|
111
|
+
- [Email notification](/docs/Functions.md#email-notification)
|
112
|
+
- [Mailer setup](/docs/Functions.md#mailer-setup)
|
113
|
+
- [Sender configuration](/docs/Functions.md#sender-configuration)
|
114
|
+
- [Email templates](/docs/Functions.md#email-templates)
|
115
|
+
- [Email subject](/docs/Functions.md#email-subject)
|
116
|
+
- [Other header fields](/docs/Functions.md#other-header-fields)
|
117
|
+
- [i18n for email](/docs/Functions.md#i18n-for-email)
|
118
|
+
- [Batch email notification](/docs/Functions.md#batch-email-notification)
|
119
|
+
- [Batch mailer setup](/docs/Functions.md#batch-mailer-setup)
|
120
|
+
- [Batch sender configuration](/docs/Functions.md#batch-sender-configuration)
|
121
|
+
- [Batch email templates](/docs/Functions.md#batch-email-templates)
|
122
|
+
- [Batch email subject](/docs/Functions.md#batch-email-subject)
|
123
|
+
- [i18n for batch email](/docs/Functions.md#i18n-for-batch-email)
|
124
|
+
- [Grouping notifications](/docs/Functions.md#grouping-notifications)
|
125
|
+
- [Subscription management](/docs/Functions.md#subscription-management)
|
126
|
+
- [Configuring subscriptions](/docs/Functions.md#configuring-subscriptions)
|
127
|
+
- [Managing subscriptions](/docs/Functions.md#managing-subscriptions)
|
128
|
+
- [Customizing subscriptions](/docs/Functions.md#customizing-subscriptions)
|
129
|
+
- [REST API backend](/docs/Functions.md#rest-api-backend)
|
130
|
+
- [Configuring REST API backend](/docs/Functions.md#configuring-rest-api-backend)
|
131
|
+
- [API reference as OpenAPI Specification](/docs/Functions.md#api-reference-as-openapi-specification)
|
132
|
+
- [Integration with Devise](/docs/Functions.md#integration-with-devise)
|
133
|
+
- [Configuring integration with Devise authentication](/docs/Functions.md#configuring-integration-with-devise-authentication)
|
134
|
+
- [Using different model as target](/docs/Functions.md#using-different-model-as-target)
|
135
|
+
- [Configuring simple default routes](/docs/Functions.md#configuring-simple-default-routes)
|
136
|
+
- [REST API backend with Devise Token Auth](/docs/Functions.md#rest-api-backend-with-devise-token-auth)
|
137
|
+
- [Push notification with Action Cable](/docs/Functions.md#push-notification-with-action-cable)
|
138
|
+
- [Enabling broadcasting notifications to channels](/docs/Functions.md#enabling-broadcasting-notifications-to-channels)
|
139
|
+
- [Subscribing notifications from channels](/docs/Functions.md#subscribing-notifications-from-channels)
|
140
|
+
- [Subscribing notifications with Devise authentication](/docs/Functions.md#subscribing-notifications-with-devise-authentication)
|
141
|
+
- [Subscribing notifications API with Devise Token Auth](/docs/Functions.md#subscribing-notifications-api-with-devise-token-auth)
|
142
|
+
- [Subscription management of Action Cable channels](/docs/Functions.md#subscription-management-of-action-cable-channels)
|
143
|
+
- [Optional notification targets](/docs/Functions.md#optional-notification-targets)
|
144
|
+
- [Configuring optional targets](/docs/Functions.md#configuring-optional-targets)
|
145
|
+
- [Customizing message format](/docs/Functions.md#customizing-message-format)
|
146
|
+
- [Action Cable channels as optional target](/docs/Functions.md#action-cable-channels-as-optional-target)
|
147
|
+
- [Amazon SNS as optional target](/docs/Functions.md#amazon-sns-as-optional-target)
|
148
|
+
- [Slack as optional target](/docs/Functions.md#slack-as-optional-target)
|
149
|
+
- [Developing custom optional targets](/docs/Functions.md#developing-custom-optional-targets)
|
150
|
+
- [Subscription management of optional targets](/docs/Functions.md#subscription-management-of-optional-targets)
|
151
|
+
- [Testing](/docs/Testing.md#Testing)
|
152
|
+
- [Testing your application](/docs/Testing.md#testing-your-application)
|
153
|
+
- [Testing gem alone](/docs/Testing.md#testing-gem-alone)
|
102
154
|
- [Documentation](#documentation)
|
103
|
-
-
|
104
|
-
- [Help](#help)
|
155
|
+
- [Common Examples](#common-examples)
|
105
156
|
- [Contributing](#contributing)
|
106
157
|
- [License](#license)
|
107
158
|
|
108
159
|
|
109
|
-
##
|
160
|
+
## Getting Started
|
161
|
+
|
162
|
+
This getting started shows easy setup description of *activity_notification*. See [Setup](/docs/Setup.md#Setup) for more details.
|
110
163
|
|
111
164
|
### Gem installation
|
112
165
|
|
@@ -128,12 +181,9 @@ $ bin/rails generate activity_notification:install
|
|
128
181
|
```
|
129
182
|
|
130
183
|
The generator will install an initializer which describes all configuration options of *activity_notification*.
|
131
|
-
It also generates a i18n based translation file which we can configure the presentation of notifications.
|
132
184
|
|
133
185
|
### Database setup
|
134
186
|
|
135
|
-
#### Using ActiveRecord ORM
|
136
|
-
|
137
187
|
When you use *activity_notification* with ActiveRecord ORM as default configuration,
|
138
188
|
create migration for notifications and migrate the database in your Rails project:
|
139
189
|
|
@@ -142,77 +192,22 @@ $ bin/rails generate activity_notification:migration
|
|
142
192
|
$ bin/rake db:migrate
|
143
193
|
```
|
144
194
|
|
145
|
-
|
146
|
-
|
147
|
-
```ruby
|
148
|
-
config.notification_table_name = "activity_notifications"
|
149
|
-
```
|
150
|
-
|
151
|
-
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"*:
|
152
|
-
|
153
|
-
```ruby
|
154
|
-
config.subscription_table_name = "notifications_subscriptions"
|
155
|
-
```
|
156
|
-
|
157
|
-
|
158
|
-
#### Using Mongoid ORM
|
159
|
-
|
160
|
-
When you use *activity_notification* with [Mongoid](http://mongoid.org) ORM, set **AN_ORM** environment variable to **mongoid**:
|
161
|
-
|
162
|
-
```console
|
163
|
-
$ export AN_ORM=mongoid
|
164
|
-
```
|
165
|
-
|
166
|
-
You can also configure ORM in initializer **activity_notification.rb**:
|
167
|
-
|
168
|
-
```ruby
|
169
|
-
config.orm = :mongoid
|
170
|
-
```
|
171
|
-
|
172
|
-
You need to configure Mongoid in your Rails application for your MongoDB environment. Then, your notifications and subscriptions will be stored in your MongoDB.
|
195
|
+
See [Database setup](/docs/Setup.md#database-setup) for other ORMs.
|
173
196
|
|
174
197
|
### Configuring models
|
175
198
|
|
176
|
-
#### Configuring target models
|
177
|
-
|
178
199
|
Configure your target model (e.g. *app/models/user.rb*).
|
179
200
|
Add **acts_as_target** configuration to your target model to get notifications.
|
180
201
|
|
181
|
-
##### Target as an ActiveRecord model
|
182
|
-
|
183
202
|
```ruby
|
184
203
|
class User < ActiveRecord::Base
|
185
|
-
# acts_as_target configures your model as ActivityNotification::Target
|
186
|
-
# with parameters as value or custom methods defined in your model as lambda or symbol.
|
187
|
-
# This is an example without any options (default configuration) as the target.
|
188
|
-
acts_as_target
|
189
|
-
end
|
190
|
-
```
|
191
|
-
|
192
|
-
##### Target as a Mongoid model
|
193
|
-
|
194
|
-
```ruby
|
195
|
-
require 'mongoid'
|
196
|
-
class User
|
197
|
-
include Mongoid::Document
|
198
|
-
include Mongoid::Timestamps
|
199
|
-
include GlobalID::Identification
|
200
|
-
|
201
|
-
# You need include ActivityNotification::Models except models which extend ActiveRecord::Base
|
202
|
-
include ActivityNotification::Models
|
203
204
|
acts_as_target
|
204
205
|
end
|
205
206
|
```
|
206
207
|
|
207
|
-
|
208
|
-
|
209
|
-
#### Configuring notifiable models
|
210
|
-
|
211
|
-
Configure your notifiable model (e.g. *app/models/comment.rb*).
|
208
|
+
Then, configure your notifiable model (e.g. *app/models/comment.rb*).
|
212
209
|
Add **acts_as_notifiable** configuration to your notifiable model representing activity to notify for each of your target model.
|
213
|
-
You have to define notification targets for all notifications from this notifiable model by *:targets* option. Other configurations are
|
214
|
-
|
215
|
-
##### Notifiable as an ActiveRecord model
|
210
|
+
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.
|
216
211
|
|
217
212
|
```ruby
|
218
213
|
class Article < ActiveRecord::Base
|
@@ -225,52 +220,9 @@ class Comment < ActiveRecord::Base
|
|
225
220
|
belongs_to :article
|
226
221
|
belongs_to :user
|
227
222
|
|
228
|
-
# acts_as_notifiable configures your model as ActivityNotification::Notifiable
|
229
|
-
# with parameters as value or custom methods defined in your model as lambda or symbol.
|
230
|
-
# The first argument is the plural symbol name of your target model.
|
231
|
-
acts_as_notifiable :users,
|
232
|
-
# Notification targets as :targets is a necessary option
|
233
|
-
# Set to notify to author and users commented to the article, except comment owner self
|
234
|
-
targets: ->(comment, key) {
|
235
|
-
([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
|
236
|
-
},
|
237
|
-
# Path to move when the notification is opened by the target user
|
238
|
-
# This is an optional configuration since activity_notification uses polymorphic_path as default
|
239
|
-
notifiable_path: :article_notifiable_path
|
240
|
-
|
241
|
-
def article_notifiable_path
|
242
|
-
article_path(article)
|
243
|
-
end
|
244
|
-
end
|
245
|
-
```
|
246
|
-
|
247
|
-
##### Notifiable as a Mongoid model
|
248
|
-
|
249
|
-
```ruby
|
250
|
-
require 'mongoid'
|
251
|
-
class Article
|
252
|
-
include Mongoid::Document
|
253
|
-
include Mongoid::Timestamps
|
254
|
-
|
255
|
-
belongs_to :user
|
256
|
-
has_many :comments, dependent: :destroy
|
257
|
-
|
258
|
-
def commented_users
|
259
|
-
User.where(:id.in => comments.pluck(:user_id))
|
260
|
-
end
|
261
|
-
end
|
262
|
-
|
263
|
-
require 'mongoid'
|
264
|
-
class Comment
|
265
|
-
include Mongoid::Document
|
266
|
-
include Mongoid::Timestamps
|
267
|
-
include GlobalID::Identification
|
268
|
-
|
269
|
-
# You need include ActivityNotification::Models except models which extend ActiveRecord::Base
|
270
|
-
include ActivityNotification::Models
|
271
223
|
acts_as_notifiable :users,
|
272
224
|
targets: ->(comment, key) {
|
273
|
-
([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
|
225
|
+
([comment.article.user] + comment.article.reload.commented_users.to_a - [comment.user]).uniq
|
274
226
|
},
|
275
227
|
notifiable_path: :article_notifiable_path
|
276
228
|
|
@@ -280,40 +232,12 @@ class Comment
|
|
280
232
|
end
|
281
233
|
```
|
282
234
|
|
283
|
-
|
284
|
-
|
285
|
-
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:
|
286
|
-
|
287
|
-
```ruby
|
288
|
-
acts_as_notifiable :users,
|
289
|
-
targets: ->(comment, key) {
|
290
|
-
([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
|
291
|
-
},
|
292
|
-
notifiable_path: ->(comment, key) { "#{comment.article_notifiable_path}##{key}" }
|
293
|
-
```
|
294
|
-
|
295
|
-
This will attach the key of the notification to the notifiable path.
|
235
|
+
See [Configuring models](/docs/Setup.md#configuring-models) for more details.
|
296
236
|
|
297
237
|
### Configuring views
|
298
238
|
|
299
|
-
*activity_notification* provides view templates to customize your notification views.
|
300
|
-
|
301
|
-
```console
|
302
|
-
$ bin/rails generate activity_notification:views
|
303
|
-
```
|
304
|
-
|
305
|
-
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:
|
306
|
-
|
307
|
-
```console
|
308
|
-
$ bin/rails generate activity_notification:views users
|
309
|
-
```
|
310
|
-
|
311
|
-
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),
|
312
|
-
you can pass a list of modules to the generator with the *-v* flag.
|
313
|
-
|
314
|
-
```console
|
315
|
-
$ bin/rails generate activity_notification:views -v notifications
|
316
|
-
```
|
239
|
+
*activity_notification* provides view templates to customize your notification views.
|
240
|
+
See [Configuring views](/docs/Setup.md#configuring-views) for more details.
|
317
241
|
|
318
242
|
### Configuring routes
|
319
243
|
|
@@ -325,1054 +249,107 @@ Rails.application.routes.draw do
|
|
325
249
|
end
|
326
250
|
```
|
327
251
|
|
328
|
-
|
329
|
-
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).
|
252
|
+
See [Configuring routes](/docs/Setup.md#configuring-routes) for more details.
|
330
253
|
|
331
|
-
|
332
|
-
|
333
|
-
You can also configure *activity_notification* routes with scope like this:
|
254
|
+
You can also configure *activity_notification* routes as REST API backend with *api_mode* option like this:
|
334
255
|
|
335
256
|
```ruby
|
336
257
|
Rails.application.routes.draw do
|
337
|
-
scope :
|
338
|
-
|
258
|
+
scope :api do
|
259
|
+
scope :"v2" do
|
260
|
+
notify_to :users, api_mode: true
|
261
|
+
end
|
339
262
|
end
|
340
263
|
end
|
341
264
|
```
|
342
265
|
|
343
|
-
|
266
|
+
See [Routes as REST API backend](/docs/Setup.md#configuring-routes) and [REST API backend](/docs/Functions.md#rest-api-backend) for more details.
|
344
267
|
|
345
268
|
### Creating notifications
|
346
269
|
|
347
|
-
#### Notification API
|
348
|
-
|
349
270
|
You can trigger notifications by setting all your required parameters and triggering **notify** on the notifiable model, like this:
|
350
271
|
|
351
272
|
```ruby
|
352
273
|
@comment.notify :users, key: "comment.reply"
|
353
274
|
```
|
354
275
|
|
355
|
-
Or, you can call public API as **ActivityNotification::Notification.notify**
|
356
|
-
|
357
|
-
```ruby
|
358
|
-
ActivityNotification::Notification.notify :users, @comment, key: "comment.reply"
|
359
|
-
```
|
360
|
-
|
361
276
|
The first argument is the plural symbol name of your target model, which is configured in notifiable model by *acts_as_notifiable*.
|
362
277
|
The new instances of **ActivityNotification::Notification** model will be generated for the specified targets.
|
363
278
|
|
364
|
-
|
365
|
-
You can override it by *Notifiable#default_notification_key*.
|
366
|
-
|
367
|
-
#### Asynchronous notification API with ActiveJob
|
368
|
-
|
369
|
-
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:
|
370
|
-
|
371
|
-
```ruby
|
372
|
-
@comment.notify_later :users, key: "comment.reply"
|
373
|
-
```
|
374
|
-
|
375
|
-
You can also use *:notify_later* option in *notify* method. This is the same operation as calling *notify_later* method.
|
376
|
-
|
377
|
-
```ruby
|
378
|
-
@comment.notify :users, key: "comment.reply", notify_later: true
|
379
|
-
```
|
380
|
-
|
381
|
-
*Note*: *notify_now* is an alias for *notify* and does the same.
|
382
|
-
|
383
|
-
When you use asynchronous notification API, you should setup ActiveJob with background queuing service such as Sidekiq.
|
384
|
-
You can set *config.active_job_queue* in your initializer to specify a queue name *activity_notification* will use.
|
385
|
-
The default queue name is *:activity_notification*.
|
386
|
-
|
387
|
-
```ruby
|
388
|
-
# Configure ActiveJob queue name for delayed notifications.
|
389
|
-
config.active_job_queue = :my_notification_queue
|
390
|
-
```
|
391
|
-
|
392
|
-
#### Automatic tracked notifications
|
393
|
-
|
394
|
-
You can also generate automatic tracked notifications by **:tracked** option in *acts_as_notifiable*.
|
395
|
-
*:tracked* option adds required callbacks to generate notifications for creation and update of the notifiable model.
|
396
|
-
Set true to *:tracked* option to generate all tracked notifications, like this:
|
397
|
-
|
398
|
-
```ruby
|
399
|
-
class Comment < ActiveRecord::Base
|
400
|
-
acts_as_notifiable :users,
|
401
|
-
targets: ->(comment, key) {
|
402
|
-
([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
|
403
|
-
},
|
404
|
-
# Set true to :tracked option to generate automatic tracked notifications.
|
405
|
-
# It adds required callbacks to generate notifications for creation and update of the notifiable model.
|
406
|
-
tracked: true
|
407
|
-
end
|
408
|
-
```
|
409
|
-
|
410
|
-
Or, set *:only* or *:except* option to generate specified tracked notifications, like this:
|
411
|
-
|
412
|
-
```ruby
|
413
|
-
class Comment < ActiveRecord::Base
|
414
|
-
acts_as_notifiable :users,
|
415
|
-
targets: ->(comment, key) {
|
416
|
-
([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
|
417
|
-
},
|
418
|
-
# Set { only: [:create] } to :tracked option to generate tracked notifications for creation only.
|
419
|
-
# It adds required callbacks to generate notifications for creation of the notifiable model.
|
420
|
-
tracked: { only: [:create] }
|
421
|
-
end
|
422
|
-
```
|
423
|
-
|
424
|
-
```ruby
|
425
|
-
class Comment < ActiveRecord::Base
|
426
|
-
acts_as_notifiable :users,
|
427
|
-
targets: ->(comment, key) {
|
428
|
-
([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
|
429
|
-
},
|
430
|
-
# Set { except: [:update] } to :tracked option to generate tracked notifications except update (creation only).
|
431
|
-
# It adds required callbacks to generate notifications for creation of the notifiable model.
|
432
|
-
tracked: { except: [:update], key: 'comment.create.now', send_later: false }
|
433
|
-
end
|
434
|
-
```
|
435
|
-
|
436
|
-
*Hint*: `#{notifiable_type}.create` and `#{notifiable_type}.update` will be used as the key of tracked notifications.
|
437
|
-
You can override them by *Notifiable#notification_key_for_tracked_creation* and *Notifiable#notification_key_for_tracked_update*.
|
438
|
-
You can also specify key option in the *:tracked* statement.
|
439
|
-
|
440
|
-
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:
|
441
|
-
|
442
|
-
```ruby
|
443
|
-
class Comment < ActiveRecord::Base
|
444
|
-
acts_as_notifiable :users,
|
445
|
-
targets: ->(comment, key) {
|
446
|
-
([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
|
447
|
-
},
|
448
|
-
# It adds required callbacks to generate notifications asynchronously for creation of the notifiable model.
|
449
|
-
tracked: { only: [:create], key: 'comment.create.later', notify_later: true }
|
450
|
-
end
|
451
|
-
```
|
279
|
+
See [Creating notifications](/docs/Setup.md#creating-notifications) for more details.
|
452
280
|
|
453
281
|
### Displaying notifications
|
454
282
|
|
455
|
-
|
456
|
-
|
457
|
-
To display notifications, you can use **notifications** association of the target model:
|
458
|
-
|
459
|
-
```ruby
|
460
|
-
# custom_notifications_controller.rb
|
461
|
-
def index
|
462
|
-
@notifications = @target.notifications
|
463
|
-
end
|
464
|
-
```
|
465
|
-
|
466
|
-
You can also use several scope to filter notifications. For example, **unopened_only** to filter them unopened notifications only.
|
467
|
-
|
468
|
-
```ruby
|
469
|
-
# custom_notifications_controller.rb
|
470
|
-
def index
|
471
|
-
@notifications = @target.notifications.unopened_only
|
472
|
-
end
|
473
|
-
```
|
474
|
-
|
475
|
-
Moreover, you can use **notification_index** or **notification_index_with_attributes** methods to automatically prepare notification index for the target.
|
476
|
-
|
477
|
-
```ruby
|
478
|
-
# custom_notifications_controller.rb
|
479
|
-
def index
|
480
|
-
@notifications = @target.notification_index_with_attributes
|
481
|
-
end
|
482
|
-
```
|
483
|
-
|
484
|
-
#### Rendering notifications
|
485
|
-
|
486
|
-
You can use **render_notifications** helper in your views to show the notification index:
|
487
|
-
|
488
|
-
```erb
|
489
|
-
<%= render_notifications(@notifications) %>
|
490
|
-
```
|
491
|
-
|
492
|
-
We can set *:target* option to specify the target type of notifications:
|
283
|
+
*activity_notification* also provides notification views. You can prepare target notifications, render them in your controller, and show them provided or custom notification views.
|
493
284
|
|
494
|
-
|
495
|
-
<%= render_notifications(@notifications, target: :users) %>
|
496
|
-
```
|
497
|
-
|
498
|
-
*Note*: *render_notifications* is an alias for *render_notification* and does the same.
|
499
|
-
|
500
|
-
If you want to set notification index in the common layout, such as common header, you can use **render_notifications_of** helper like this:
|
501
|
-
|
502
|
-
```shared/_header.html.erb
|
503
|
-
<%= render_notifications_of current_user, index_content: :with_attributes %>
|
504
|
-
```
|
505
|
-
|
506
|
-
Then, content named **:notification_index** will be prepared and you can use it in your partial template.
|
507
|
-
|
508
|
-
```activity_notifications/notifications/users/_index.html.erb
|
509
|
-
...
|
510
|
-
<%= yield :notification_index %>
|
511
|
-
...
|
512
|
-
```
|
513
|
-
|
514
|
-
Sometimes, it's desirable to pass additional local variables to partials. It can be done this way:
|
515
|
-
|
516
|
-
```erb
|
517
|
-
<%= render_notification(@notification, locals: { friends: current_user.friends }) %>
|
518
|
-
```
|
519
|
-
|
520
|
-
#### Notification views
|
521
|
-
|
522
|
-
*activity_notification* looks for views in *app/views/activity_notification/notifications/:target* with **:key** of the notifications.
|
523
|
-
|
524
|
-
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)*.
|
525
|
-
|
526
|
-
*Hint*: the *"notification."* prefix in *:key* is completely optional, you can skip it in your projects or use this prefix only to make namespace.
|
527
|
-
|
528
|
-
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:
|
285
|
+
See [Displaying notifications](/docs/Setup.md#displaying-notifications) for more details.
|
529
286
|
|
530
|
-
|
531
|
-
<%= render_notification(@notification, target: :users, fallback: :default) %>
|
532
|
-
```
|
533
|
-
|
534
|
-
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)*.
|
535
|
-
|
536
|
-
If you do not specify *:target* option like this,
|
537
|
-
|
538
|
-
```erb
|
539
|
-
<%= render_notification(@notification, fallback: :default) %>
|
540
|
-
```
|
541
|
-
|
542
|
-
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)*.
|
543
|
-
|
544
|
-
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:
|
545
|
-
|
546
|
-
```erb
|
547
|
-
<%= render_notification(@notification, fallback: :text) %>
|
548
|
-
```
|
549
|
-
|
550
|
-
Finally, default views of *activity_notification* depends on jQuery and you have to add requirements to *application.js* in your apps:
|
287
|
+
### Run example Rails application
|
551
288
|
|
552
|
-
|
553
|
-
|
554
|
-
//= require jquery_ujs
|
555
|
-
```
|
289
|
+
Test module includes example Rails application in *[spec/rails_app](/spec/rails_app)*.
|
290
|
+
Pull git repository and you can run the example application as common Rails application.
|
556
291
|
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
user:
|
566
|
-
article:
|
567
|
-
create:
|
568
|
-
text: 'Article has been created'
|
569
|
-
update:
|
570
|
-
text: 'Article %{article_title} has been updated'
|
571
|
-
destroy:
|
572
|
-
text: 'Some user removed an article!'
|
573
|
-
comment:
|
574
|
-
post:
|
575
|
-
text:
|
576
|
-
one: "<p>%{notifier_name} posted a comment on your article %{article_title}</p>"
|
577
|
-
other: "<p>%{notifier_name} posted %{count} comments on your article %{article_title}</p>"
|
578
|
-
reply:
|
579
|
-
text: "<p>%{notifier_name} and %{group_member_count} other people replied %{group_notification_count} times to your comment</p>"
|
580
|
-
mail_subject: 'New comment on your article'
|
581
|
-
admin:
|
582
|
-
article:
|
583
|
-
post:
|
584
|
-
text: '[Admin] Article has been created'
|
292
|
+
```console
|
293
|
+
$ git pull https://github.com/simukappu/activity_notification.git
|
294
|
+
$ cd activity_notification
|
295
|
+
$ bundle install —path vendor/bundle
|
296
|
+
$ cd spec/rails_app
|
297
|
+
$ bin/rake db:migrate
|
298
|
+
$ bin/rake db:seed
|
299
|
+
$ bin/rails server
|
585
300
|
```
|
301
|
+
Then, you can access <http://localhost:3000> for the example application.
|
586
302
|
|
587
|
-
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.
|
588
|
-
|
589
|
-
### Customizing controllers (optional)
|
590
|
-
|
591
|
-
If the customization at the views level is not enough, you can customize each controller by following these steps:
|
592
|
-
|
593
|
-
1. Create your custom controllers using the generator with a target:
|
594
|
-
|
595
|
-
```console
|
596
|
-
$ bin/rails generate activity_notification:controllers users
|
597
|
-
```
|
598
|
-
|
599
|
-
If you specify *users* as the target, controllers will be created in *app/controllers/users*.
|
600
|
-
And the notifications controller will look like this:
|
601
|
-
|
602
|
-
```ruby
|
603
|
-
class Users::NotificationsController < ActivityNotification::NotificationsController
|
604
|
-
# GET /:target_type/:target_id/notifications
|
605
|
-
# def index
|
606
|
-
# super
|
607
|
-
# end
|
608
|
-
|
609
|
-
# ...
|
610
|
-
|
611
|
-
# POST /:target_type/:target_id/notifications/:id/open
|
612
|
-
# def open
|
613
|
-
# super
|
614
|
-
# end
|
615
|
-
|
616
|
-
# ...
|
617
|
-
end
|
618
|
-
```
|
619
|
-
|
620
|
-
2. Tell the router to use this controller:
|
621
|
-
|
622
|
-
```ruby
|
623
|
-
notify_to :users, controller: 'users/notifications'
|
624
|
-
```
|
625
|
-
|
626
|
-
3. Finally, change or extend the desired controller actions.
|
627
303
|
|
628
|
-
|
629
|
-
```ruby
|
630
|
-
class Users::NotificationsController < ActivityNotification::NotificationsController
|
631
|
-
# ...
|
632
|
-
|
633
|
-
# POST /:target_type/:target_id/notifications/:id/open
|
634
|
-
def open
|
635
|
-
# Custom code to open notification here
|
636
|
-
|
637
|
-
# super
|
638
|
-
end
|
304
|
+
## Setup
|
639
305
|
|
640
|
-
|
641
|
-
end
|
642
|
-
```
|
306
|
+
See [Setup](/docs/Setup.md#Setup).
|
643
307
|
|
644
308
|
|
645
309
|
## Functions
|
646
310
|
|
647
|
-
|
648
|
-
|
649
|
-
*activity_notification* provides email notification to the notification targets.
|
650
|
-
|
651
|
-
#### Mailer setup
|
652
|
-
|
653
|
-
Set up SMTP server configuration for *ActionMailer*. Then, you need to set up the default URL options for the *activity_notification* mailer in each environment. Here is a possible configuration for *config/environments/development.rb*:
|
654
|
-
|
655
|
-
```ruby
|
656
|
-
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
|
657
|
-
```
|
311
|
+
See [Functions](/docs/Functions.md#Functions).
|
658
312
|
|
659
|
-
Email notification is disabled as default. You can configure to enable email notification in initializer *activity_notification.rb*.
|
660
313
|
|
661
|
-
|
662
|
-
config.email_enabled = true
|
663
|
-
```
|
314
|
+
## Testing
|
664
315
|
|
665
|
-
|
316
|
+
See [Testing](/docs/Testing.md#Testing).
|
666
317
|
|
667
|
-
```ruby
|
668
|
-
class User < ActiveRecord::Base
|
669
|
-
# Example using confirmed_at of devise field
|
670
|
-
# to decide whether activity_notification sends notification email to this user
|
671
|
-
acts_as_target email: :email, email_allowed: :confirmed_at
|
672
|
-
end
|
673
|
-
```
|
674
318
|
|
675
|
-
|
676
|
-
class Comment < ActiveRecord::Base
|
677
|
-
belongs_to :article
|
678
|
-
belongs_to :user
|
319
|
+
## Documentation
|
679
320
|
|
680
|
-
|
681
|
-
targets: ->(comment, key) {
|
682
|
-
([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
|
683
|
-
},
|
684
|
-
# Allow notification email
|
685
|
-
email_allowed: true,
|
686
|
-
notifiable_path: :article_notifiable_path
|
321
|
+
See [API Reference](http://www.rubydoc.info/github/simukappu/activity_notification/index) for more details.
|
687
322
|
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
323
|
+
RubyDoc.info does not support parsing methods in *included* and *class_methods* of *ActiveSupport::Concern* currently.
|
324
|
+
To read complete documents, please generate YARD documents on your local environment:
|
325
|
+
```console
|
326
|
+
$ git pull https://github.com/simukappu/activity_notification.git
|
327
|
+
$ cd activity_notification
|
328
|
+
$ bundle install —path vendor/bundle
|
329
|
+
$ bundle exec yard doc
|
330
|
+
$ bundle exec yard server
|
692
331
|
```
|
332
|
+
Then you can see the documents at <http://localhost:8808/docs/index>.
|
693
333
|
|
694
|
-
#### Sender configuration
|
695
|
-
|
696
|
-
You can configure the notification *"from"* address inside of *activity_notification.rb* in two ways.
|
697
|
-
|
698
|
-
Using a simple email address as *String*:
|
699
|
-
|
700
|
-
```ruby
|
701
|
-
config.mailer_sender = 'your_notification_sender@example.com'
|
702
|
-
```
|
703
334
|
|
704
|
-
|
335
|
+
## Common Examples
|
705
336
|
|
706
|
-
|
707
|
-
config.mailer_sender = ->(key){ key == 'inquiry.post' ? 'support@example.com' : 'noreply@example.com' }
|
708
|
-
```
|
337
|
+
See example Rails application in *[/spec/rails_app](/spec/rails_app)*.
|
709
338
|
|
710
|
-
|
339
|
+
You can also try this example Rails application as Online Demo here:
|
340
|
+
* **https://activity-notification-example.herokuapp.com/**
|
711
341
|
|
712
|
-
|
342
|
+
You can login as test users to experience user activity notifications. For more details, see [Online Demo](#online-demo).
|
713
343
|
|
714
|
-
If this template is missing, the gem will look for a partial in *default* as the target type which means *activity_notification/mailer/default/_default.html.(|erb|haml|slim|something_else)*.
|
715
344
|
|
716
|
-
|
345
|
+
## Contributing
|
717
346
|
|
718
|
-
|
347
|
+
We encourage you to contribute to *activity_notification*!
|
348
|
+
Please check out the [Contributing to *activity_notification* guide](/docs/CONTRIBUTING.md#how-to-contribute-to-activity_notification) for guidelines about how to proceed.
|
719
349
|
|
720
|
-
|
721
|
-
class Comment < ActiveRecord::Base
|
722
|
-
belongs_to :article
|
723
|
-
belongs_to :user
|
724
|
-
|
725
|
-
acts_as_notifiable :users,
|
726
|
-
targets: ->(comment, key) {
|
727
|
-
([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
|
728
|
-
},
|
729
|
-
notifiable_path: :article_notifiable_path
|
730
|
-
|
731
|
-
def overriding_notification_email_subject(target, key)
|
732
|
-
if key == "comment.create"
|
733
|
-
"New comment to your article!"
|
734
|
-
else
|
735
|
-
"Notification for new comments!"
|
736
|
-
end
|
737
|
-
end
|
738
|
-
end
|
739
|
-
|
740
|
-
```
|
741
|
-
|
742
|
-
If you use i18n for email, you can configure email subject in your locale files. See [i18n for email](#i18n-for-email).
|
743
|
-
|
744
|
-
#### i18n for email
|
745
|
-
|
746
|
-
The subject of notification email can be put in your locale *.yml* files as **mail_subject** field:
|
747
|
-
|
748
|
-
```yaml
|
749
|
-
notification:
|
750
|
-
user:
|
751
|
-
comment:
|
752
|
-
post:
|
753
|
-
text: "<p>Someone posted comments to your article</p>"
|
754
|
-
mail_subject: 'New comment to your article'
|
755
|
-
```
|
756
|
-
|
757
|
-
### Batch email notification
|
758
|
-
|
759
|
-
*activity_notification* provides batch email notification to the notification targets. You can send notification email daily, hourly or weekly and so on with a scheduler like *whenever*.
|
760
|
-
|
761
|
-
#### Batch mailer setup
|
762
|
-
|
763
|
-
Set up SMTP server configuration for *ActionMailer* and the default URL options for the *activity_notification* mailer in each environment.
|
764
|
-
|
765
|
-
Batch email notification is disabled as default. You can configure to enable email notification in initializer *activity_notification.rb* like single email notification.
|
766
|
-
|
767
|
-
```ruby
|
768
|
-
config.email_enabled = true
|
769
|
-
```
|
770
|
-
|
771
|
-
You can also configure them for each target model by *acts_as_target* role like this.
|
772
|
-
|
773
|
-
```ruby
|
774
|
-
class User < ActiveRecord::Base
|
775
|
-
# Example using confirmed_at of devise field
|
776
|
-
# to decide whether activity_notification sends batch notification email to this user
|
777
|
-
acts_as_target email: :email, batch_email_allowed: :confirmed_at
|
778
|
-
end
|
779
|
-
```
|
780
|
-
|
781
|
-
Then, you can send batch notification email for unopened notifications only to the all specified targets with *batch_key*.
|
782
|
-
|
783
|
-
```ruby
|
784
|
-
# Send batch notification email to the users with unopened notifications
|
785
|
-
User.send_batch_unopened_notification_email(batch_key: 'batch.comment.post')
|
786
|
-
```
|
787
|
-
|
788
|
-
You can also add conditions to filter notifications, like this:
|
789
|
-
|
790
|
-
```ruby
|
791
|
-
# Send batch notification email to the users with unopened notifications of specified key in 1 hour
|
792
|
-
User.send_batch_unopened_notification_email(batch_key: 'batch.comment.post', filtered_by_key: 'comment.post', custom_filter: ["created_at >= ?", time.hour.ago])
|
793
|
-
```
|
794
|
-
|
795
|
-
#### Batch sender configuration
|
796
|
-
|
797
|
-
*activity_notification* uses same sender configuration of real-time email notification as batch email sender.
|
798
|
-
You can configure *config.mailer_sender* as simply *String* or *Proc* based on the *batch_key*:
|
799
|
-
|
800
|
-
```ruby
|
801
|
-
config.mailer_sender = ->(batch_key){ batch_key == 'batch.inquiry.post' ? 'support@example.com' : 'noreply@example.com' }
|
802
|
-
```
|
803
|
-
|
804
|
-
*batch_key* is specified by **:batch_key** option. If this option is not specified, the key of the first notification will be used as *batch_key*.
|
805
|
-
|
806
|
-
#### Batch email templates
|
807
|
-
|
808
|
-
*activity_notification* will look for batch email template in the same way as email notification using *batch_key*.
|
809
|
-
|
810
|
-
#### Batch email subject
|
811
|
-
|
812
|
-
*activity_notification* will resolve batch email subject as the same way as [email subject](#email-subject) with *batch_key*.
|
813
|
-
|
814
|
-
If you use i18n for batch email, you can configure batch email subject in your locale files. See [i18n for batch email](#i18n-for-batch-email).
|
815
|
-
|
816
|
-
#### i18n for batch email
|
817
|
-
|
818
|
-
The subject of batch notification email also can be put in your locale *.yml* files as **mail_subject** field for *batch_key*.
|
819
|
-
|
820
|
-
```yaml
|
821
|
-
notification:
|
822
|
-
user:
|
823
|
-
batch:
|
824
|
-
comment:
|
825
|
-
post:
|
826
|
-
mail_subject: 'New comments to your article'
|
827
|
-
```
|
828
|
-
|
829
|
-
### Grouping notifications
|
830
|
-
|
831
|
-
*activity_notification* provides the function for automatically grouping notifications. When you created a notification like this, all *unopened* notifications to the same target will be grouped by *article* set as **:group** options:
|
832
|
-
|
833
|
-
```ruby
|
834
|
-
@comment.notify :users key: 'comment.post', group: @comment.article
|
835
|
-
```
|
836
|
-
|
837
|
-
When you use default notification view, it is helpful to configure **acts_as_notification_group** (or *acts_as_group*) with *:printable_name* option to render group instance.
|
838
|
-
|
839
|
-
```ruby
|
840
|
-
class Article < ActiveRecord::Base
|
841
|
-
belongs_to :user
|
842
|
-
acts_as_notification_group printable_name: ->(article) { "article \"#{article.title}\"" }
|
843
|
-
end
|
844
|
-
```
|
845
|
-
|
846
|
-
You can use **group_owners_only** scope to filter owner notifications representing each group:
|
847
|
-
|
848
|
-
```ruby
|
849
|
-
# custom_notifications_controller.rb
|
850
|
-
def index
|
851
|
-
@notifications = @target.notifications.group_owners_only
|
852
|
-
end
|
853
|
-
```
|
854
|
-
*notification_index* and *notification_index_with_attributes* methods also use *group_owners_only* scope internally.
|
855
|
-
|
856
|
-
And you can render them in a view like this:
|
857
|
-
```erb
|
858
|
-
<% if notification.group_member_exists? %>
|
859
|
-
<%= "#{notification.notifier.name} and #{notification.group_member_count} other users" %>
|
860
|
-
<% else %>
|
861
|
-
<%= "#{notification.notifier.name}" %>
|
862
|
-
<% end %>
|
863
|
-
<%= "posted comments to your article \"#{notification.group.title}\"" %>
|
864
|
-
```
|
865
|
-
|
866
|
-
This presentation will be shown to target users as *Kevin and 7 other users posted comments to your article "Let's use Ruby"*.
|
867
|
-
|
868
|
-
You can also use `%{group_member_count}`, `%{group_notification_count}`, `%{group_member_notifier_count}` and `%{group_notifier_count}` in i18n text as a field:
|
869
|
-
|
870
|
-
```yaml
|
871
|
-
notification:
|
872
|
-
user:
|
873
|
-
comment:
|
874
|
-
post:
|
875
|
-
text: "<p>%{notifier_name} and %{group_member_notifier_count} other users posted %{group_notification_count} comments to your article</p>"
|
876
|
-
mail_subject: 'New comment to your article'
|
877
|
-
```
|
878
|
-
|
879
|
-
Then, you will see *"Kevin and 7 other users posted 10 comments to your article"*.
|
880
|
-
|
881
|
-
|
882
|
-
### Subscription management
|
883
|
-
|
884
|
-
*activity_notification* provides the function for subscription management of notifications and notification email.
|
885
|
-
|
886
|
-
#### Configuring subscriptions
|
887
|
-
|
888
|
-
Subscription management is disabled as default. You can configure to enable subscription management in initializer *activity_notification.rb*.
|
889
|
-
|
890
|
-
```ruby
|
891
|
-
config.subscription_enabled = true
|
892
|
-
```
|
893
|
-
|
894
|
-
This makes all target model subscribers. You can also configure them for each target model by *acts_as_target* role like this.
|
895
|
-
|
896
|
-
```ruby
|
897
|
-
class User < ActiveRecord::Base
|
898
|
-
# Example using confirmed_at of devise field
|
899
|
-
# to decide whether activity_notification manages subscriptions of this user
|
900
|
-
acts_as_target email: :email, email_allowed: :confirmed_at, subscription_allowed: :confirmed_at
|
901
|
-
end
|
902
|
-
```
|
903
|
-
|
904
|
-
If you do not have a subscriptions table in you database, create a migration for subscriptions and migrate the database in your Rails project:
|
905
|
-
|
906
|
-
```console
|
907
|
-
$ bin/rails generate activity_notification:migration CreateSubscriptions -t subscriptions
|
908
|
-
$ bin/rake db:migrate
|
909
|
-
```
|
910
|
-
If you are using a different table name than the default "subscriptions", change the settings in your config/initializers/activity_notification.rb file, e.g, if you use the table name "notifications_subscription" instead:
|
911
|
-
|
912
|
-
```
|
913
|
-
config.subscription_table_name = "notifications_subscriptions"
|
914
|
-
```
|
915
|
-
|
916
|
-
#### Managing subscriptions
|
917
|
-
|
918
|
-
Subscriptions are managed by instances of **ActivityNotification::Subscription** model which belongs to *target* and *key* of the notification.
|
919
|
-
*Subscription#subscribing* manages subscription of notifications.
|
920
|
-
*true* means the target will receive the notifications with this key.
|
921
|
-
*false* means the target will not receive these notifications.
|
922
|
-
*Subscription#subscribing_to_email* manages subscription of notification email.
|
923
|
-
*true* means the target will receive the notification email with this key including batch notification email with this *batch_key*.
|
924
|
-
*false* means the target will not receive these notification email.
|
925
|
-
|
926
|
-
As default, all target subscribes to notification and notification email when subscription record does not exist in your database.
|
927
|
-
You can change this **subscribe_as_default** parameter in initializer *activity_notification.rb*.
|
928
|
-
|
929
|
-
```ruby
|
930
|
-
config.subscribe_as_default = false
|
931
|
-
```
|
932
|
-
|
933
|
-
Then, all target does not subscribe to notification and notification email and will not receive any notifications as default.
|
934
|
-
|
935
|
-
You can create subscription record from subscription API in your target model like this:
|
936
|
-
|
937
|
-
```ruby
|
938
|
-
# Subscribe 'comment.reply' notifications and notification email
|
939
|
-
user.create_subscription(key: 'comment.reply')
|
940
|
-
|
941
|
-
# Subscribe 'comment.reply' notifications but does not subscribe notification email
|
942
|
-
user.create_subscription(key: 'comment.reply', subscribing_to_email: false)
|
943
|
-
|
944
|
-
# Unsubscribe 'comment.reply' notifications and notification email
|
945
|
-
user.create_subscription(key: 'comment.reply', subscribing: false)
|
946
|
-
```
|
947
|
-
|
948
|
-
You can also update subscriptions like this:
|
949
|
-
|
950
|
-
```ruby
|
951
|
-
# Subscribe 'comment.reply' notifications and notification email
|
952
|
-
user.find_or_create_subscription('comment.reply').subscribe
|
953
|
-
|
954
|
-
# Unsubscribe 'comment.reply' notifications and notification email
|
955
|
-
user.find_or_create_subscription('comment.reply').unsubscribe
|
956
|
-
|
957
|
-
# Unsubscribe 'comment.reply' notification email
|
958
|
-
user.find_or_create_subscription('comment.reply').unsubscribe_to_email
|
959
|
-
```
|
960
|
-
|
961
|
-
#### Customizing subscriptions
|
962
|
-
|
963
|
-
*activity_notification* provides basic controllers and views to manage the subscriptions.
|
964
|
-
|
965
|
-
Add subscription routing to *config/routes.rb* for the target (e.g. *:users*):
|
966
|
-
|
967
|
-
```ruby
|
968
|
-
Rails.application.routes.draw do
|
969
|
-
subscribed_by :users
|
970
|
-
end
|
971
|
-
```
|
972
|
-
|
973
|
-
or, you can also configure it with notifications like this:
|
974
|
-
|
975
|
-
```ruby
|
976
|
-
Rails.application.routes.draw do
|
977
|
-
notify_to :users, with_subscription: true
|
978
|
-
end
|
979
|
-
```
|
980
|
-
|
981
|
-
Then, you can access *users/1/subscriptions* and use **subscriptions_controller** or **subscriptions_with_devise_controller** to manage the subscriptions.
|
982
|
-
|
983
|
-
If you would like to customize subscription controllers or views, you can use generators like notifications:
|
984
|
-
|
985
|
-
* Customize subscription controllers
|
986
|
-
|
987
|
-
1. Create your custom controllers using controller generator with a target:
|
988
|
-
|
989
|
-
```console
|
990
|
-
$ bin/rails generate activity_notification:controllers users -c subscriptions subscriptions_with_devise
|
991
|
-
```
|
992
|
-
|
993
|
-
2. Tell the router to use this controller:
|
994
|
-
|
995
|
-
```ruby
|
996
|
-
notify_to :users, with_subscription: { controller: 'users/subscriptions' }
|
997
|
-
```
|
998
|
-
|
999
|
-
* Customize subscription views
|
1000
|
-
|
1001
|
-
```console
|
1002
|
-
$ bin/rails generate activity_notification:views users -v subscriptions
|
1003
|
-
```
|
1004
|
-
|
1005
|
-
|
1006
|
-
### Integration with Devise
|
1007
|
-
|
1008
|
-
*activity_notification* supports to integrate with devise authentication.
|
1009
|
-
|
1010
|
-
#### Configuring integration with Devise
|
1011
|
-
|
1012
|
-
Add **:with_devise** option in notification routing to *config/routes.rb* for the target:
|
1013
|
-
|
1014
|
-
```ruby
|
1015
|
-
Rails.application.routes.draw do
|
1016
|
-
devise_for :users
|
1017
|
-
# Integrated with devise
|
1018
|
-
notify_to :users, with_devise: :users
|
1019
|
-
end
|
1020
|
-
```
|
1021
|
-
|
1022
|
-
Then *activity_notification* will use **notifications_with_devise_controller** as a notification controller. The controller actions automatically call *authenticate_user!* and the user will be restricted to access and operate own notifications only, not others'.
|
1023
|
-
|
1024
|
-
*Hint*: HTTP 403 Forbidden will be returned for unauthorized notifications.
|
1025
|
-
|
1026
|
-
#### Using different model as target
|
1027
|
-
|
1028
|
-
You can also use different model from Devise resource as a target. When you will add this to *config/routes.rb*:
|
1029
|
-
|
1030
|
-
```ruby
|
1031
|
-
Rails.application.routes.draw do
|
1032
|
-
devise_for :users
|
1033
|
-
# Integrated with devise for different model
|
1034
|
-
notify_to :admins, with_devise: :users
|
1035
|
-
end
|
1036
|
-
```
|
1037
|
-
|
1038
|
-
and add **:devise_resource** option to *acts_as_target* in the target model:
|
1039
|
-
|
1040
|
-
```ruby
|
1041
|
-
class Admin < ActiveRecord::Base
|
1042
|
-
belongs_to :user
|
1043
|
-
acts_as_target devise_resource: :user
|
1044
|
-
end
|
1045
|
-
```
|
1046
|
-
|
1047
|
-
*activity_notification* will authenticate *:admins* notifications with devise authentication for *:users*.
|
1048
|
-
In this example, *activity_notification* will confirm *admin* belonging to authenticated *user* by Devise.
|
1049
|
-
|
1050
|
-
#### Configuring simple default routes
|
1051
|
-
|
1052
|
-
You can configure simple default routes for authenticated users, like */notifications* instead of */users/1/notifications*. Use *:devise_default_routes* option like this:
|
1053
|
-
|
1054
|
-
```ruby
|
1055
|
-
Rails.application.routes.draw do
|
1056
|
-
devise_for :users
|
1057
|
-
notify_to :users, with_devise: :users, devise_default_routes: true
|
1058
|
-
end
|
1059
|
-
```
|
1060
|
-
|
1061
|
-
If you use multiple notification targets with Devise, you can also use this option with scope like this:
|
1062
|
-
|
1063
|
-
```ruby
|
1064
|
-
Rails.application.routes.draw do
|
1065
|
-
devise_for :users
|
1066
|
-
# Integrated with devise for different model, and use with scope
|
1067
|
-
scope :admins, as: :admins do
|
1068
|
-
notify_to :admins, with_devise: :users, devise_default_routes: true, routing_scope: :admins
|
1069
|
-
end
|
1070
|
-
end
|
1071
|
-
```
|
1072
|
-
|
1073
|
-
Then, you can access */admins/notifications* instead of */admins/1/notifications*.
|
1074
|
-
|
1075
|
-
|
1076
|
-
### Optional notification targets
|
1077
|
-
|
1078
|
-
*activity_notification* supports configurable optional notification targets like Amazon SNS, Slack, SMS and so on.
|
1079
|
-
|
1080
|
-
#### Configuring optional targets
|
1081
|
-
|
1082
|
-
*activity_notification* provides default optional target implementation for Amazon SNS and Slack.
|
1083
|
-
You can develop any optional target classes which extends *ActivityNotification::OptionalTarget::Base*, and configure them to notifiable model by *acts_as_notifiable* like this.
|
1084
|
-
|
1085
|
-
```ruby
|
1086
|
-
class Comment < ActiveRecord::Base
|
1087
|
-
belongs_to :article
|
1088
|
-
belongs_to :user
|
1089
|
-
|
1090
|
-
require 'activity_notification/optional_targets/amazon_sns'
|
1091
|
-
require 'activity_notification/optional_targets/slack'
|
1092
|
-
require 'custom_optional_targets/console_output'
|
1093
|
-
acts_as_notifiable :admins, targets: [Admin.first].compact,
|
1094
|
-
notifiable_path: :article_notifiable_path,
|
1095
|
-
# Set optional target implementation class and initializing parameters
|
1096
|
-
optional_targets: {
|
1097
|
-
ActivityNotification::OptionalTarget::AmazonSNS => { topic_arn: 'arn:aws:sns:XXXXX:XXXXXXXXXXXX:XXXXX' },
|
1098
|
-
ActivityNotification::OptionalTarget::Slack => {
|
1099
|
-
webhook_url: 'https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX',
|
1100
|
-
slack_name: :slack_name, channel: 'activity_notification', username: 'ActivityNotification', icon_emoji: ":ghost:"
|
1101
|
-
},
|
1102
|
-
CustomOptionalTarget::ConsoleOutput => {}
|
1103
|
-
}
|
1104
|
-
|
1105
|
-
def article_notifiable_path
|
1106
|
-
article_path(article)
|
1107
|
-
end
|
1108
|
-
end
|
1109
|
-
```
|
1110
|
-
|
1111
|
-
Write *require* statement for optional target implementation classes and set them with initializing parameters to *acts_as_notifiable*.
|
1112
|
-
*activity_notification* will publish all notifications of those targets and notifiables to optional targets.
|
1113
|
-
|
1114
|
-
#### Customizing message format
|
1115
|
-
|
1116
|
-
Optional targets prepare publishing messages from notification instance using view template like rendering notifications.
|
1117
|
-
As default, all optional targets use *app/views/activity_notification/optional_targets/default/base/_default.text.erb*.
|
1118
|
-
You can customize this template by creating *app/views/activity_notification/optional_targets/<target_class_name>/<optional_target_class_name>/<notification_key>.text.(|erb|haml|slim|something_else)*.
|
1119
|
-
For example, if you have a notification for *:users* target with *:key* set to *"notification.comment.reply"* and *ActivityNotification::OptionalTarget::AmazonSNS* optional target is configured, the gem will look for a partial in *app/views/activity_notification/optional_targets/users/amazon_sns/comment/_reply.text.erb*.
|
1120
|
-
The gem will also look for templates whose *<target_class_name>* is *default*, *<optional_target_class_name>* is *base* and *<notification_key>* is *default*, which means *app/views/activity_notification/optional_targets/users/amazon_sns/_default.text.erb*, *app/views/activity_notification/optional_targets/users/base/_default.text.erb*, *app/views/activity_notification/optional_targets/default/amazon_sns/_default.text.erb* and *app/views/activity_notification/optional_targets/default/base/_default.text.erb*.
|
1121
|
-
|
1122
|
-
#### Amazon SNS as optional target
|
1123
|
-
|
1124
|
-
*activity_notification* provides **ActivityNotification::OptionalTarget::AmazonSNS** as default optional target implementation for Amazon SNS.
|
1125
|
-
|
1126
|
-
First, add **aws-sdk** or **aws-sdk-sns** (>= AWS SDK for Ruby v3) gem to your Gemfile and set AWS Credentials for SDK (See [Configuring the AWS SDK for Ruby](https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/setup-config.html)).
|
1127
|
-
|
1128
|
-
```ruby
|
1129
|
-
gem 'aws-sdk', '~> 2'
|
1130
|
-
# --- or ---
|
1131
|
-
gem 'aws-sdk-sns', '~> 1'
|
1132
|
-
```
|
1133
|
-
|
1134
|
-
```ruby
|
1135
|
-
require 'aws-sdk'
|
1136
|
-
# --- or ---
|
1137
|
-
require 'aws-sdk-sns'
|
1138
|
-
|
1139
|
-
Aws.config.update(
|
1140
|
-
region: 'your_region',
|
1141
|
-
credentials: Aws::Credentials.new('your_access_key_id', 'your_secret_access_key')
|
1142
|
-
)
|
1143
|
-
```
|
1144
|
-
|
1145
|
-
Then, write `require 'activity_notification/optional_targets/amazon_sns'` statement in your notifiable model and set *ActivityNotification::OptionalTarget::AmazonSNS* to *acts_as_notifiable* with *:topic_arn*, *:target_arn* or *:phone_number* initializing parameters.
|
1146
|
-
Any other options for `Aws::SNS::Client.new` are available as initializing parameters. See [API Reference of Class: Aws::SNS::Client](http://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/SNS/Client.html) for more details.
|
1147
|
-
|
1148
|
-
```ruby
|
1149
|
-
class Comment < ActiveRecord::Base
|
1150
|
-
require 'activity_notification/optional_targets/amazon_sns'
|
1151
|
-
acts_as_notifiable :admins, targets: [Admin.first].compact,
|
1152
|
-
optional_targets: {
|
1153
|
-
ActivityNotification::OptionalTarget::AmazonSNS => { topic_arn: 'arn:aws:sns:XXXXX:XXXXXXXXXXXX:XXXXX' }
|
1154
|
-
}
|
1155
|
-
end
|
1156
|
-
```
|
1157
|
-
|
1158
|
-
#### Slack as optional target
|
1159
|
-
|
1160
|
-
*activity_notification* provides **ActivityNotification::OptionalTarget::Slack** as default optional target implementation for Slack.
|
1161
|
-
|
1162
|
-
First, add **slack-notifier** gem to your Gemfile and create Incoming WebHooks in Slack (See [Incoming WebHooks](https://wemakejp.slack.com/apps/A0F7XDUAZ-incoming-webhooks)).
|
1163
|
-
|
1164
|
-
```ruby
|
1165
|
-
gem 'slack-notifier'
|
1166
|
-
```
|
1167
|
-
|
1168
|
-
Then, write `require 'activity_notification/optional_targets/slack'` statement in your notifiable model and set *ActivityNotification::OptionalTarget::Slack* to *acts_as_notifiable* with *:webhook_url* and *:target_username* initializing parameters. *:webhook_url* is created WebHook URL and required, *:target_username* is target's slack user name as String value, symbol method name or lambda function and is optional.
|
1169
|
-
Any other options for `Slack::Notifier.new` are available as initializing parameters. See [Github slack-notifier](https://github.com/stevenosloan/slack-notifier) and [API Reference of Class: Slack::Notifier](http://www.rubydoc.info/gems/slack-notifier/1.5.1/Slack/Notifier) for more details.
|
1170
|
-
|
1171
|
-
```ruby
|
1172
|
-
class Comment < ActiveRecord::Base
|
1173
|
-
require 'activity_notification/optional_targets/slack'
|
1174
|
-
acts_as_notifiable :admins, targets: [Admin.first].compact,
|
1175
|
-
optional_targets: {
|
1176
|
-
ActivityNotification::OptionalTarget::Slack => {
|
1177
|
-
webhook_url: 'https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX',
|
1178
|
-
target_username: :slack_username, channel: 'activity_notification', username: 'ActivityNotification', icon_emoji: ":ghost:"
|
1179
|
-
}
|
1180
|
-
}
|
1181
|
-
end
|
1182
|
-
```
|
1183
|
-
|
1184
|
-
#### Developing custom optional targets
|
1185
|
-
|
1186
|
-
You can develop any custom optional targets.
|
1187
|
-
Custom optional target class must extend **ActivityNotification::OptionalTarget::Base** and override **initialize_target** and **notify** method.
|
1188
|
-
You can use **render_notification_message** method to prepare message from notification instance using view template.
|
1189
|
-
|
1190
|
-
For example, create *lib/custom_optional_targets/amazon_sns.rb* as follows:
|
1191
|
-
|
1192
|
-
```ruby
|
1193
|
-
module CustomOptionalTarget
|
1194
|
-
# Custom optional target implementation for mobile push notification or SMS using Amazon SNS.
|
1195
|
-
class AmazonSNS < ActivityNotification::OptionalTarget::Base
|
1196
|
-
require 'aws-sdk'
|
1197
|
-
|
1198
|
-
# Initialize method to prepare Aws::SNS::Client
|
1199
|
-
def initialize_target(options = {})
|
1200
|
-
@topic_arn = options.delete(:topic_arn)
|
1201
|
-
@target_arn = options.delete(:target_arn)
|
1202
|
-
@phone_number = options.delete(:phone_number)
|
1203
|
-
@sns_client = Aws::SNS::Client.new(options)
|
1204
|
-
end
|
1205
|
-
|
1206
|
-
# Publishes notification message to Amazon SNS
|
1207
|
-
def notify(notification, options = {})
|
1208
|
-
@sns_client.publish(
|
1209
|
-
topic_arn: notification.target.resolve_value(options.delete(:topic_arn) || @topic_arn),
|
1210
|
-
target_arn: notification.target.resolve_value(options.delete(:target_arn) || @target_arn),
|
1211
|
-
phone_number: notification.target.resolve_value(options.delete(:phone_number) || @phone_number),
|
1212
|
-
message: render_notification_message(notification, options)
|
1213
|
-
)
|
1214
|
-
end
|
1215
|
-
end
|
1216
|
-
end
|
1217
|
-
```
|
1218
|
-
|
1219
|
-
Then, you can configure them to notifiable model by *acts_as_notifiable* like this.
|
1220
|
-
|
1221
|
-
```ruby
|
1222
|
-
class Comment < ActiveRecord::Base
|
1223
|
-
require 'custom_optional_targets/amazon_sns'
|
1224
|
-
acts_as_notifiable :admins, targets: [Admin.first].compact,
|
1225
|
-
optional_targets: {
|
1226
|
-
CustomOptionalTarget::AmazonSNS => { topic_arn: 'arn:aws:sns:XXXXX:XXXXXXXXXXXX:XXXXX' }
|
1227
|
-
}
|
1228
|
-
end
|
1229
|
-
```
|
1230
|
-
|
1231
|
-
*acts_as_notifiable* creates optional target instances and calls *initialize_target* method with initializing parameters.
|
1232
|
-
|
1233
|
-
|
1234
|
-
## Testing
|
1235
|
-
|
1236
|
-
### Testing your application
|
1237
|
-
|
1238
|
-
First, you need to configure ActivityNotification as described above.
|
1239
|
-
|
1240
|
-
#### Testing notifications with RSpec
|
1241
|
-
Prepare target and notifiable model instances to test generating notifications (e.g. `@user` and `@comment`).
|
1242
|
-
Then, you can call notify API and test if notifications of the target are generated.
|
1243
|
-
```ruby
|
1244
|
-
# Prepare
|
1245
|
-
@article_author = create(:user)
|
1246
|
-
@comment = @article_author.articles.create.comments.create
|
1247
|
-
expect(@article_author.notifications.unopened_only.count).to eq(0)
|
1248
|
-
|
1249
|
-
# Call notify API
|
1250
|
-
@comment.notify :users
|
1251
|
-
|
1252
|
-
# Test generated notifications
|
1253
|
-
expect(@article_author_user.notifications.unopened_only.count).to eq(1)
|
1254
|
-
expect(@article_author_user.notifications.unopened_only.latest.notifiable).to eq(@comment)
|
1255
|
-
```
|
1256
|
-
|
1257
|
-
#### Testing email notifications with RSpec
|
1258
|
-
Prepare target and notifiable model instances to test sending notification email.
|
1259
|
-
Then, you can call notify API and test if notification email is sent.
|
1260
|
-
```ruby
|
1261
|
-
# Prepare
|
1262
|
-
@article_author = create(:user)
|
1263
|
-
@comment = @article_author.articles.create.comments.create
|
1264
|
-
expect(ActivityNotification::Mailer.deliveries.size).to eq(0)
|
1265
|
-
|
1266
|
-
# Call notify API and send email now
|
1267
|
-
@comment.notify :users, send_later: false
|
1268
|
-
|
1269
|
-
# Test sent notification email
|
1270
|
-
expect(ActivityNotification::Mailer.deliveries.size).to eq(1)
|
1271
|
-
expect(ActivityNotification::Mailer.deliveries.first.to[0]).to eq(@article_author.email)
|
1272
|
-
```
|
1273
|
-
Note that notification email will be sent asynchronously without false as *:send_later* option.
|
1274
|
-
```ruby
|
1275
|
-
# Prepare
|
1276
|
-
include ActiveJob::TestHelper
|
1277
|
-
@article_author = create(:user)
|
1278
|
-
@comment = @article_author.articles.create.comments.create
|
1279
|
-
expect(ActivityNotification::Mailer.deliveries.size).to eq(0)
|
1280
|
-
|
1281
|
-
# Call notify API and send email asynchronously as default
|
1282
|
-
# Test sent notification email with ActiveJob queue
|
1283
|
-
expect {
|
1284
|
-
perform_enqueued_jobs do
|
1285
|
-
@comment.notify :users
|
1286
|
-
end
|
1287
|
-
}.to change { ActivityNotification::Mailer.deliveries.size }.by(1)
|
1288
|
-
expect(ActivityNotification::Mailer.deliveries.first.to[0]).to eq(@article_author.email)
|
1289
|
-
```
|
1290
|
-
|
1291
|
-
### Testing gem alone
|
1292
|
-
|
1293
|
-
#### Testing with RSpec
|
1294
|
-
Pull git repository and execute RSpec.
|
1295
|
-
```console
|
1296
|
-
$ git pull https://github.com/simukappu/activity_notification.git
|
1297
|
-
$ cd activity_notification
|
1298
|
-
$ bundle install —path vendor/bundle
|
1299
|
-
$ bundle exec rspec
|
1300
|
-
- or -
|
1301
|
-
$ bundle exec rake
|
1302
|
-
```
|
1303
|
-
|
1304
|
-
#### Dummy Rails application
|
1305
|
-
Test module includes dummy Rails application in **spec/rails_app**. You can run the dummy application as common Rails application.
|
1306
|
-
```console
|
1307
|
-
$ cd spec/rails_app
|
1308
|
-
$ bin/rake db:migrate
|
1309
|
-
$ bin/rake db:seed
|
1310
|
-
$ bin/rails server
|
1311
|
-
```
|
1312
|
-
Then, you can access <http://localhost:3000> for the dummy application.
|
1313
|
-
|
1314
|
-
##### Run with your local database
|
1315
|
-
As default, dummy Rails application runs with local SQLite database in *spec/rails_app/db/development.sqlite3*.
|
1316
|
-
This application supports to run with your local MySQL, PostgreSQL and MongoDB.
|
1317
|
-
Set **AN_TEST_DB** environment variable like:
|
1318
|
-
```console
|
1319
|
-
$ export AN_TEST_DB=mysql
|
1320
|
-
```
|
1321
|
-
for MySQL,
|
1322
|
-
```console
|
1323
|
-
$ export AN_TEST_DB=postgresql
|
1324
|
-
```
|
1325
|
-
for PostgreSQL, and
|
1326
|
-
```console
|
1327
|
-
$ export AN_TEST_DB=mongodb
|
1328
|
-
```
|
1329
|
-
for MongoDB. When you set **mongodb** as *AN_TEST_DB*, you have to use *activity_notification* with MongoDB. Also set **AN_ORM** like:
|
1330
|
-
```console
|
1331
|
-
$ export AN_ORM=mongoid
|
1332
|
-
```
|
1333
|
-
|
1334
|
-
Then, configure *spec/rails_app/config/database.yml* or *spec/rails_app/config/mongoid.yml* as your local database.
|
1335
|
-
Finally, run database migration, seed data script and the dummy appliation.
|
1336
|
-
```console
|
1337
|
-
$ cd spec/rails_app
|
1338
|
-
$ # You don't need migration when you use MongoDB only (AN_ORM=mongoid and AN_TEST_DB=mongodb)
|
1339
|
-
$ bin/rake db:migrate
|
1340
|
-
$ bin/rake db:seed
|
1341
|
-
$ bin/rails server
|
1342
|
-
```
|
1343
|
-
|
1344
|
-
|
1345
|
-
## Documentation
|
1346
|
-
|
1347
|
-
See [API Reference](http://www.rubydoc.info/github/simukappu/activity_notification/index) for more details.
|
1348
|
-
|
1349
|
-
RubyDoc.info does not support parsing methods in *included* and *class_methods* of *ActiveSupport::Concern* currently.
|
1350
|
-
To read complete documents, please generate YARD documents on your local environment:
|
1351
|
-
```console
|
1352
|
-
$ git pull https://github.com/simukappu/activity_notification.git
|
1353
|
-
$ cd activity_notification
|
1354
|
-
$ bundle install —path vendor/bundle
|
1355
|
-
$ bundle exec yard doc
|
1356
|
-
$ bundle exec yard server
|
1357
|
-
```
|
1358
|
-
Then you can see the documents at <http://localhost:8808/docs/index>.
|
1359
|
-
|
1360
|
-
## Common examples
|
1361
|
-
|
1362
|
-
To be prepared. See dummy Rails application in *spec/rails_app*.
|
1363
|
-
|
1364
|
-
|
1365
|
-
## Help
|
1366
|
-
|
1367
|
-
We are welcome your Github issues for gem problems or enhancement requests. If you have any questions or problems for your own application environment, contact us by email of this repository.
|
1368
|
-
|
1369
|
-
|
1370
|
-
## Contributing
|
350
|
+
Everyone interacting in *activity_notification* codebases, issue trackers, and pull requests is expected to follow the *activity_notification* [Code of Conduct](/docs/CODE_OF_CONDUCT.md#contributor-covenant-code-of-conduct).
|
1371
351
|
|
1372
|
-
We
|
1373
|
-
* Write tests with RSpec
|
1374
|
-
* Write code docs and README if necessary
|
1375
|
-
* Send your pull request to *development* branch (Do NOT send to *master* branch)
|
352
|
+
We appreciate any of your contribution!
|
1376
353
|
|
1377
354
|
|
1378
355
|
## License
|