activity_notification 1.4.4 → 2.2.4
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/.github/workflows/build.yml +116 -0
- data/.gitignore +15 -3
- data/CHANGELOG.md +200 -1
- data/Gemfile +17 -2
- data/Procfile +2 -0
- data/README.md +168 -1033
- data/Rakefile +19 -10
- data/activity_notification.gemspec +14 -9
- 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 +60 -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 +83 -73
- data/app/jobs/activity_notification/notify_all_job.rb +25 -0
- data/app/jobs/activity_notification/notify_job.rb +26 -0
- data/app/jobs/activity_notification/notify_to_job.rb +25 -0
- data/app/views/activity_notification/notifications/default/_default.html.erb +23 -23
- data/app/views/activity_notification/notifications/default/_default_without_grouping.html.erb +19 -19
- data/app/views/activity_notification/notifications/default/_index.html.erb +3 -3
- data/app/views/activity_notification/notifications/default/index.html.erb +60 -7
- data/app/views/activity_notification/notifications/default/open.js.erb +2 -2
- data/app/views/activity_notification/notifications/default/open_all.js.erb +2 -2
- data/app/views/activity_notification/notifications/default/show.html.erb +2 -2
- data/app/views/activity_notification/optional_targets/default/action_cable_channel/_default.html.erb +176 -0
- data/app/views/activity_notification/optional_targets/default/base/_default.text.erb +1 -1
- data/app/views/activity_notification/optional_targets/default/slack/_default.text.erb +1 -1
- data/app/views/activity_notification/subscriptions/default/_form.html.erb +2 -2
- data/app/views/activity_notification/subscriptions/default/_notification_keys.html.erb +5 -33
- data/app/views/activity_notification/subscriptions/default/_subscription.html.erb +8 -8
- data/app/views/activity_notification/subscriptions/default/index.html.erb +13 -9
- data/app/views/activity_notification/subscriptions/default/show.html.erb +3 -3
- data/app/views/activity_notification/subscriptions/default/subscribe.js.erb +1 -1
- data/app/views/activity_notification/subscriptions/default/subscribe_to_email.js.erb +1 -1
- data/app/views/activity_notification/subscriptions/default/subscribe_to_optional_target.js.erb +1 -1
- data/app/views/activity_notification/subscriptions/default/unsubscribe.js.erb +1 -1
- data/app/views/activity_notification/subscriptions/default/unsubscribe_to_email.js.erb +1 -1
- data/app/views/activity_notification/subscriptions/default/unsubscribe_to_optional_target.js.erb +1 -1
- data/bin/_dynamodblocal +4 -0
- data/bin/bundle_update.sh +7 -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 +1146 -0
- data/docs/Setup.md +817 -0
- data/docs/Testing.md +148 -0
- data/gemfiles/Gemfile.rails-5.0 +8 -1
- data/gemfiles/Gemfile.rails-5.1 +7 -1
- data/gemfiles/Gemfile.rails-5.2 +24 -0
- data/gemfiles/Gemfile.rails-6.0 +23 -0
- data/gemfiles/Gemfile.rails-6.1 +22 -0
- data/gemfiles/Gemfile.rails-7.0 +25 -0
- data/lib/activity_notification/apis/notification_api.rb +356 -159
- data/lib/activity_notification/apis/subscription_api.rb +98 -59
- data/lib/activity_notification/apis/swagger.rb +6 -0
- data/lib/activity_notification/common.rb +18 -7
- data/lib/activity_notification/config.rb +176 -30
- data/lib/activity_notification/controllers/common_api_controller.rb +30 -0
- data/lib/activity_notification/controllers/common_controller.rb +47 -27
- 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 +22 -5
- 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 +118 -28
- data/lib/activity_notification/mailers/helpers.rb +19 -12
- data/lib/activity_notification/models/concerns/notifiable.rb +142 -55
- data/lib/activity_notification/models/concerns/subscriber.rb +28 -13
- 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 +131 -32
- data/lib/activity_notification/models/notification.rb +1 -0
- data/lib/activity_notification/models/subscription.rb +1 -0
- data/lib/activity_notification/models.rb +23 -1
- 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 +23 -34
- data/lib/activity_notification/orm/active_record/subscription.rb +1 -1
- data/lib/activity_notification/orm/active_record.rb +1 -1
- data/lib/activity_notification/orm/dynamoid/extension.rb +262 -0
- data/lib/activity_notification/orm/dynamoid/notification.rb +224 -0
- data/lib/activity_notification/orm/dynamoid/subscription.rb +82 -0
- data/lib/activity_notification/orm/dynamoid.rb +530 -0
- data/lib/activity_notification/orm/mongoid/notification.rb +29 -28
- data/lib/activity_notification/orm/mongoid/subscription.rb +3 -3
- data/lib/activity_notification/orm/mongoid.rb +33 -1
- data/lib/activity_notification/rails/routes.rb +273 -60
- data/lib/activity_notification/renderable.rb +22 -7
- data/lib/activity_notification/roles/acts_as_notifiable.rb +64 -1
- data/lib/activity_notification/roles/acts_as_target.rb +99 -9
- data/lib/activity_notification/version.rb +1 -1
- data/lib/activity_notification.rb +14 -0
- data/lib/generators/activity_notification/controllers_generator.rb +2 -1
- data/lib/generators/templates/activity_notification.rb +61 -7
- data/lib/generators/templates/controllers/README +2 -2
- 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/migrations/migration.rb +5 -5
- 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 +49 -0
- data/spec/channels/notification_api_with_devise_channel_spec.rb +76 -0
- data/spec/channels/notification_channel_shared_examples.rb +59 -0
- data/spec/channels/notification_channel_spec.rb +48 -0
- data/spec/channels/notification_with_devise_channel_spec.rb +97 -0
- data/spec/concerns/apis/notification_api_spec.rb +177 -12
- data/spec/concerns/apis/subscription_api_spec.rb +146 -4
- data/spec/concerns/common_spec.rb +25 -3
- data/spec/concerns/models/notifiable_spec.rb +161 -11
- data/spec/concerns/models/subscriber_spec.rb +253 -79
- data/spec/concerns/models/target_spec.rb +180 -47
- data/spec/concerns/renderable_spec.rb +35 -16
- data/spec/config_spec.rb +52 -1
- data/spec/controllers/controller_spec_utility.rb +100 -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 +14 -8
- 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 +99 -121
- data/spec/controllers/subscriptions_controller_spec.rb +1 -2
- data/spec/controllers/subscriptions_with_devise_controller_spec.rb +14 -8
- data/spec/factories/notifications.rb +1 -1
- data/spec/factories/subscriptions.rb +3 -3
- data/spec/factories/users.rb +3 -3
- data/spec/generators/migration/migration_generator_spec.rb +29 -4
- data/spec/helpers/view_helpers_spec.rb +31 -21
- data/spec/jobs/notify_all_job_spec.rb +23 -0
- data/spec/jobs/notify_job_spec.rb +23 -0
- data/spec/jobs/notify_to_job_spec.rb +23 -0
- data/spec/mailers/mailer_spec.rb +42 -1
- 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 +34 -0
- data/spec/optional_targets/action_cable_channel_spec.rb +41 -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/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 -1
- data/spec/rails_app/app/controllers/comments_controller.rb +3 -1
- 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 +26 -0
- data/spec/rails_app/app/javascript/App.vue +40 -0
- data/spec/rails_app/app/javascript/components/DeviseTokenAuth.vue +82 -0
- data/spec/rails_app/app/javascript/components/Top.vue +98 -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 +14 -0
- data/spec/rails_app/app/javascript/router/index.js +73 -0
- data/spec/rails_app/app/javascript/store/index.js +37 -0
- data/spec/rails_app/app/models/admin.rb +15 -10
- 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/dummy/dummy_base.rb +1 -0
- data/spec/rails_app/app/models/dummy/dummy_group.rb +9 -0
- data/spec/rails_app/app/models/dummy/dummy_notifiable.rb +1 -0
- data/spec/rails_app/app/models/dummy/dummy_notifiable_target.rb +27 -0
- data/spec/rails_app/app/models/dummy/dummy_notifier.rb +1 -0
- data/spec/rails_app/app/models/dummy/dummy_subscriber.rb +1 -0
- data/spec/rails_app/app/models/dummy/dummy_target.rb +1 -0
- data/spec/rails_app/app/models/user.rb +44 -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/activity_notification/optional_targets/admins/amazon_sns/comment/_default.text.erb +1 -1
- data/spec/rails_app/app/views/articles/index.html.erb +68 -20
- data/spec/rails_app/app/views/articles/show.html.erb +1 -1
- data/spec/rails_app/app/views/layouts/_header.html.erb +9 -3
- 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 +26 -6
- data/spec/rails_app/config/cable.yml +8 -0
- data/spec/rails_app/config/database.yml +1 -1
- data/spec/rails_app/config/dynamoid.rb +13 -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 +7 -1
- data/spec/rails_app/config/environments/test.rb +7 -11
- data/spec/rails_app/config/initializers/activity_notification.rb +63 -9
- 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 +10 -4
- data/spec/rails_app/config/routes.rb +42 -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/{20160715050433_create_test_tables.rb → 20160716000000_create_test_tables.rb} +1 -1
- data/spec/rails_app/db/migrate/{20160715050420_create_activity_notification_tables.rb → 20181209000000_create_activity_notification_tables.rb} +3 -3
- data/spec/rails_app/db/migrate/20191201000000_add_tokens_to_users.rb +10 -0
- data/spec/rails_app/db/schema.rb +46 -43
- data/spec/rails_app/db/seeds.rb +28 -4
- data/spec/rails_app/lib/custom_optional_targets/raise_error.rb +14 -0
- data/spec/rails_app/lib/mailer_previews/mailer_preview.rb +14 -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 +80 -20
- 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 +13 -11
- data/spec/version_spec.rb +31 -0
- metadata +306 -53
- data/.travis.yml +0 -85
- data/Gemfile.lock +0 -234
- data/gemfiles/Gemfile.rails-4.2 +0 -17
- data/gemfiles/Gemfile.rails-4.2.lock +0 -225
- data/gemfiles/Gemfile.rails-5.0.lock +0 -234
- data/gemfiles/Gemfile.rails-5.1.lock +0 -234
- data/spec/rails_app/app/views/activity_notification/notifications/users/overriden/custom/_test.html.erb +0 -1
- /data/spec/rails_app/app/{models → assets/images}/.keep +0 -0
data/README.md
CHANGED
@@ -1,103 +1,165 @@
|
|
1
1
|
# ActivityNotification
|
2
2
|
|
3
|
-
[![Build Status](https://
|
3
|
+
[![Build Status](https://github.com/simukappu/activity_notification/actions/workflows/build.yml/badge.svg)](https://github.com/simukappu/activity_notification/actions/workflows/build.yml)
|
4
4
|
[![Coverage Status](https://coveralls.io/repos/github/simukappu/activity_notification/badge.svg?branch=master)](https://coveralls.io/github/simukappu/activity_notification?branch=master)
|
5
|
-
[![
|
6
|
-
[![Dependency Status](https://gemnasium.com/badges/github.com/simukappu/activity_notification.svg)](https://gemnasium.com/github.com/simukappu/activity_notification)
|
5
|
+
[![Dependency](https://img.shields.io/depfu/simukappu/activity_notification.svg)](https://depfu.com/repos/simukappu/activity_notification)
|
7
6
|
[![Inline docs](http://inch-ci.org/github/simukappu/activity_notification.svg?branch=master)](http://inch-ci.org/github/simukappu/activity_notification)
|
8
|
-
[![Gem Version](https://badge.fury.io/rb/activity_notification.svg)](https://
|
7
|
+
[![Gem Version](https://badge.fury.io/rb/activity_notification.svg)](https://rubygems.org/gems/activity_notification)
|
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
|
13
|
+
*activity_notification* supports Rails 5.0+ 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. If you are using Rails 4.2, use [v2.1.4](https://rubygems.org/gems/activity_notification/versions/2.1.4) or older version of *activity_notification*.
|
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
|
-
|
69
|
-
- [
|
70
|
-
|
71
|
-
|
72
|
-
- [
|
73
|
-
- [
|
74
|
-
|
75
|
-
- [
|
76
|
-
|
77
|
-
|
78
|
-
- [
|
79
|
-
|
80
|
-
- [
|
81
|
-
- [
|
82
|
-
- [
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
- [
|
87
|
-
- [
|
88
|
-
- [
|
89
|
-
- [
|
90
|
-
- [
|
91
|
-
- [
|
92
|
-
- [
|
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)
|
93
154
|
- [Documentation](#documentation)
|
94
|
-
-
|
95
|
-
- [Help](#help)
|
155
|
+
- [Common Examples](#common-examples)
|
96
156
|
- [Contributing](#contributing)
|
97
157
|
- [License](#license)
|
98
158
|
|
99
159
|
|
100
|
-
##
|
160
|
+
## Getting Started
|
161
|
+
|
162
|
+
This getting started shows easy setup description of *activity_notification*. See [Setup](/docs/Setup.md#Setup) for more details.
|
101
163
|
|
102
164
|
### Gem installation
|
103
165
|
|
@@ -119,12 +181,9 @@ $ bin/rails generate activity_notification:install
|
|
119
181
|
```
|
120
182
|
|
121
183
|
The generator will install an initializer which describes all configuration options of *activity_notification*.
|
122
|
-
It also generates a i18n based translation file which we can configure the presentation of notifications.
|
123
184
|
|
124
185
|
### Database setup
|
125
186
|
|
126
|
-
#### Using ActiveRecord ORM
|
127
|
-
|
128
187
|
When you use *activity_notification* with ActiveRecord ORM as default configuration,
|
129
188
|
create migration for notifications and migrate the database in your Rails project:
|
130
189
|
|
@@ -133,77 +192,22 @@ $ bin/rails generate activity_notification:migration
|
|
133
192
|
$ bin/rake db:migrate
|
134
193
|
```
|
135
194
|
|
136
|
-
|
137
|
-
|
138
|
-
```ruby
|
139
|
-
config.notification_table_name = "activity_notifications"
|
140
|
-
```
|
141
|
-
|
142
|
-
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"*:
|
143
|
-
|
144
|
-
```ruby
|
145
|
-
config.subscription_table_name = "notifications_subscriptions"
|
146
|
-
```
|
147
|
-
|
148
|
-
|
149
|
-
#### Using Mongoid ORM
|
150
|
-
|
151
|
-
When you use *activity_notification* with [Mongoid](http://mongoid.org) ORM, set **AN_ORM** environment variable to **mongoid**:
|
152
|
-
|
153
|
-
```console
|
154
|
-
$ export AN_ORM=mongoid
|
155
|
-
```
|
156
|
-
|
157
|
-
You can also configure ORM in initializer **activity_notification.rb**:
|
158
|
-
|
159
|
-
```ruby
|
160
|
-
config.orm = :mongoid
|
161
|
-
```
|
162
|
-
|
163
|
-
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.
|
164
196
|
|
165
197
|
### Configuring models
|
166
198
|
|
167
|
-
#### Configuring target models
|
168
|
-
|
169
199
|
Configure your target model (e.g. *app/models/user.rb*).
|
170
200
|
Add **acts_as_target** configuration to your target model to get notifications.
|
171
201
|
|
172
|
-
##### Target as an ActiveRecord model
|
173
|
-
|
174
202
|
```ruby
|
175
203
|
class User < ActiveRecord::Base
|
176
|
-
# acts_as_target configures your model as ActivityNotification::Target
|
177
|
-
# with parameters as value or custom methods defined in your model as lambda or symbol.
|
178
|
-
# This is an example without any options (default configuration) as the target.
|
179
204
|
acts_as_target
|
180
205
|
end
|
181
206
|
```
|
182
207
|
|
183
|
-
|
184
|
-
|
185
|
-
```ruby
|
186
|
-
require 'mongoid'
|
187
|
-
class User
|
188
|
-
include Mongoid::Document
|
189
|
-
include Mongoid::Timestamps
|
190
|
-
include GlobalID::Identification
|
191
|
-
|
192
|
-
# You need include ActivityNotification::Models except models which extend ActiveRecord::Base
|
193
|
-
include ActivityNotification::Models
|
194
|
-
acts_as_target
|
195
|
-
end
|
196
|
-
```
|
197
|
-
|
198
|
-
*Note*: *acts_as_notification_target* is an alias for *acts_as_target* and does the same.
|
199
|
-
|
200
|
-
#### Configuring notifiable models
|
201
|
-
|
202
|
-
Configure your notifiable model (e.g. *app/models/comment.rb*).
|
208
|
+
Then, configure your notifiable model (e.g. *app/models/comment.rb*).
|
203
209
|
Add **acts_as_notifiable** configuration to your notifiable model representing activity to notify for each of your target model.
|
204
|
-
You have to define notification targets for all notifications from this notifiable model by *:targets* option. Other configurations are
|
205
|
-
|
206
|
-
##### 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.
|
207
211
|
|
208
212
|
```ruby
|
209
213
|
class Article < ActiveRecord::Base
|
@@ -216,52 +220,9 @@ class Comment < ActiveRecord::Base
|
|
216
220
|
belongs_to :article
|
217
221
|
belongs_to :user
|
218
222
|
|
219
|
-
# acts_as_notifiable configures your model as ActivityNotification::Notifiable
|
220
|
-
# with parameters as value or custom methods defined in your model as lambda or symbol.
|
221
|
-
# The first argument is the plural symbol name of your target model.
|
222
|
-
acts_as_notifiable :users,
|
223
|
-
# Notification targets as :targets is a necessary option
|
224
|
-
# Set to notify to author and users commented to the article, except comment owner self
|
225
|
-
targets: ->(comment, key) {
|
226
|
-
([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
|
227
|
-
},
|
228
|
-
# Path to move when the notification is opened by the target user
|
229
|
-
# This is an optional configuration since activity_notification uses polymorphic_path as default
|
230
|
-
notifiable_path: :article_notifiable_path
|
231
|
-
|
232
|
-
def article_notifiable_path
|
233
|
-
article_path(article)
|
234
|
-
end
|
235
|
-
end
|
236
|
-
```
|
237
|
-
|
238
|
-
##### Notifiable as a Mongoid model
|
239
|
-
|
240
|
-
```ruby
|
241
|
-
require 'mongoid'
|
242
|
-
class Article
|
243
|
-
include Mongoid::Document
|
244
|
-
include Mongoid::Timestamps
|
245
|
-
|
246
|
-
belongs_to :user
|
247
|
-
has_many :comments, dependent: :destroy
|
248
|
-
|
249
|
-
def commented_users
|
250
|
-
User.where(:id.in => comments.pluck(:user_id))
|
251
|
-
end
|
252
|
-
end
|
253
|
-
|
254
|
-
require 'mongoid'
|
255
|
-
class Comment
|
256
|
-
include Mongoid::Document
|
257
|
-
include Mongoid::Timestamps
|
258
|
-
include GlobalID::Identification
|
259
|
-
|
260
|
-
# You need include ActivityNotification::Models except models which extend ActiveRecord::Base
|
261
|
-
include ActivityNotification::Models
|
262
223
|
acts_as_notifiable :users,
|
263
224
|
targets: ->(comment, key) {
|
264
|
-
([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
|
265
226
|
},
|
266
227
|
notifiable_path: :article_notifiable_path
|
267
228
|
|
@@ -271,40 +232,12 @@ class Comment
|
|
271
232
|
end
|
272
233
|
```
|
273
234
|
|
274
|
-
|
275
|
-
|
276
|
-
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:
|
277
|
-
|
278
|
-
```ruby
|
279
|
-
acts_as_notifiable :users,
|
280
|
-
targets: ->(comment, key) {
|
281
|
-
([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
|
282
|
-
},
|
283
|
-
notifiable_path: ->(comment, key) { "#{comment.article_notifiable_path}##{key}" }
|
284
|
-
```
|
285
|
-
|
286
|
-
This will attach the key of the notification to the notifiable path.
|
235
|
+
See [Configuring models](/docs/Setup.md#configuring-models) for more details.
|
287
236
|
|
288
237
|
### Configuring views
|
289
238
|
|
290
|
-
*activity_notification* provides view templates to customize your notification views.
|
291
|
-
|
292
|
-
```console
|
293
|
-
$ bin/rails generate activity_notification:views
|
294
|
-
```
|
295
|
-
|
296
|
-
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:
|
297
|
-
|
298
|
-
```console
|
299
|
-
$ bin/rails generate activity_notification:views users
|
300
|
-
```
|
301
|
-
|
302
|
-
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),
|
303
|
-
you can pass a list of modules to the generator with the *-v* flag.
|
304
|
-
|
305
|
-
```console
|
306
|
-
$ bin/rails generate activity_notification:views -v notifications
|
307
|
-
```
|
239
|
+
*activity_notification* provides view templates to customize your notification views.
|
240
|
+
See [Configuring views](/docs/Setup.md#configuring-views) for more details.
|
308
241
|
|
309
242
|
### Configuring routes
|
310
243
|
|
@@ -316,872 +249,71 @@ Rails.application.routes.draw do
|
|
316
249
|
end
|
317
250
|
```
|
318
251
|
|
319
|
-
|
320
|
-
|
321
|
-
### Creating notifications
|
322
|
-
|
323
|
-
#### Notification API
|
324
|
-
|
325
|
-
You can trigger notifications by setting all your required parameters and triggering **notify**
|
326
|
-
on the notifiable model, like this:
|
327
|
-
|
328
|
-
```ruby
|
329
|
-
@comment.notify :users, key: "comment.reply"
|
330
|
-
```
|
331
|
-
|
332
|
-
Or, you can call public API as **ActivityNotification::Notification.notify**
|
333
|
-
|
334
|
-
```ruby
|
335
|
-
ActivityNotification::Notification.notify :users, @comment, key: "comment.reply"
|
336
|
-
```
|
337
|
-
|
338
|
-
The first argument is the plural symbol name of your target model, which is configured in notifiable model by *acts_as_notifiable*.
|
339
|
-
The new instances of **ActivityNotification::Notification** model will be generated for the specified targets.
|
340
|
-
|
341
|
-
*Hint*: *:key* is a option. Default key `#{notifiable_type}.default` which means *comment.default* will be used without specified key.
|
342
|
-
You can override it by *Notifiable#default_notification_key*.
|
343
|
-
|
344
|
-
#### Automatic tracked notifications
|
345
|
-
|
346
|
-
You can also generate automatic tracked notifications by **:tracked** option in *acts_as_notifiable*.
|
347
|
-
*:tracked* option adds required callbacks to generate notifications for creation and update of the notifiable model.
|
348
|
-
Set true to *:tracked* option to generate all tracked notifications, like this:
|
349
|
-
|
350
|
-
```ruby
|
351
|
-
class Comment < ActiveRecord::Base
|
352
|
-
acts_as_notifiable :users,
|
353
|
-
targets: ->(comment, key) {
|
354
|
-
([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
|
355
|
-
},
|
356
|
-
# Set true to :tracked option to generate automatic tracked notifications.
|
357
|
-
# It adds required callbacks to generate notifications for creation and update of the notifiable model.
|
358
|
-
tracked: true
|
359
|
-
end
|
360
|
-
```
|
361
|
-
|
362
|
-
Or, set *:only* or *:except* option to generate specified tracked notifications, like this:
|
252
|
+
See [Configuring routes](/docs/Setup.md#configuring-routes) for more details.
|
363
253
|
|
364
|
-
|
365
|
-
class Comment < ActiveRecord::Base
|
366
|
-
acts_as_notifiable :users,
|
367
|
-
targets: ->(comment, key) {
|
368
|
-
([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
|
369
|
-
},
|
370
|
-
# Set { only: [:create] } to :tracked option to generate tracked notifications for creation only.
|
371
|
-
# It adds required callbacks to generate notifications for creation of the notifiable model.
|
372
|
-
tracked: { only: [:create] }
|
373
|
-
end
|
374
|
-
```
|
254
|
+
You can also configure *activity_notification* routes as REST API backend with *api_mode* option like this:
|
375
255
|
|
376
256
|
```ruby
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
},
|
382
|
-
# Set { except: [:update] } to :tracked option to generate tracked notifications except update (creation only).
|
383
|
-
# It adds required callbacks to generate notifications for creation of the notifiable model.
|
384
|
-
tracked: { except: [:update], key: 'comment.edit', send_later: false }
|
385
|
-
end
|
386
|
-
```
|
387
|
-
|
388
|
-
*Hint*: `#{notifiable_type}.create` and `#{notifiable_type}.update` will be used as the key of tracked notifications.
|
389
|
-
You can override them by *Notifiable#notification_key_for_tracked_creation* and *Notifiable#notification_key_for_tracked_update*.
|
390
|
-
You can also specify key option in the *:tracked* statement.
|
391
|
-
|
392
|
-
### Displaying notifications
|
393
|
-
|
394
|
-
#### Preparing target notifications
|
395
|
-
|
396
|
-
To display notifications, you can use **notifications** association of the target model:
|
397
|
-
|
398
|
-
```ruby
|
399
|
-
# custom_notifications_controller.rb
|
400
|
-
def index
|
401
|
-
@notifications = @target.notifications
|
402
|
-
end
|
403
|
-
```
|
404
|
-
|
405
|
-
You can also use several scope to filter notifications. For example, **unopened_only** to filter them unopened notifications only.
|
406
|
-
|
407
|
-
```ruby
|
408
|
-
# custom_notifications_controller.rb
|
409
|
-
def index
|
410
|
-
@notifications = @target.notifications.unopened_only
|
411
|
-
end
|
412
|
-
```
|
413
|
-
|
414
|
-
Moreover, you can use **notification_index** or **notification_index_with_attributes** methods to automatically prepare notification index for the target.
|
415
|
-
|
416
|
-
```ruby
|
417
|
-
# custom_notifications_controller.rb
|
418
|
-
def index
|
419
|
-
@notifications = @target.notification_index_with_attributes
|
420
|
-
end
|
421
|
-
```
|
422
|
-
|
423
|
-
#### Rendering notifications
|
424
|
-
|
425
|
-
You can use **render_notifications** helper in your views to show the notification index:
|
426
|
-
|
427
|
-
```erb
|
428
|
-
<%= render_notifications(@notifications) %>
|
429
|
-
```
|
430
|
-
|
431
|
-
We can set *:target* option to specify the target type of notifications:
|
432
|
-
|
433
|
-
```erb
|
434
|
-
<%= render_notifications(@notifications, target: :users) %>
|
435
|
-
```
|
436
|
-
|
437
|
-
*Note*: *render_notifications* is an alias for *render_notification* and does the same.
|
438
|
-
|
439
|
-
If you want to set notification index in the common layout, such as common header, you can use **render_notifications_of** helper like this:
|
440
|
-
|
441
|
-
```shared/_header.html.erb
|
442
|
-
<%= render_notifications_of current_user, index_content: :with_attributes %>
|
443
|
-
```
|
444
|
-
|
445
|
-
Then, content named **:notification_index** will be prepared and you can use it in your partial template.
|
446
|
-
|
447
|
-
```activity_notifications/notifications/users/_index.html.erb
|
448
|
-
...
|
449
|
-
<%= yield :notification_index %>
|
450
|
-
...
|
451
|
-
```
|
452
|
-
|
453
|
-
Sometimes, it's desirable to pass additional local variables to partials. It can be done this way:
|
454
|
-
|
455
|
-
```erb
|
456
|
-
<%= render_notification(@notification, locals: { friends: current_user.friends }) %>
|
457
|
-
```
|
458
|
-
|
459
|
-
#### Notification views
|
460
|
-
|
461
|
-
*activity_notification* looks for views in *app/views/activity_notification/notifications/:target* with **:key** of the notifications.
|
462
|
-
|
463
|
-
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)*.
|
464
|
-
|
465
|
-
*Hint*: the *"notification."* prefix in *:key* is completely optional, you can skip it in your projects or use this prefix only to make namespace.
|
466
|
-
|
467
|
-
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:
|
468
|
-
|
469
|
-
```erb
|
470
|
-
<%= render_notification(@notification, target: :users, fallback: :default) %>
|
471
|
-
```
|
472
|
-
|
473
|
-
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)*.
|
474
|
-
|
475
|
-
If you do not specify *:target* option like this,
|
476
|
-
|
477
|
-
```erb
|
478
|
-
<%= render_notification(@notification, fallback: :default) %>
|
479
|
-
```
|
480
|
-
|
481
|
-
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)*.
|
482
|
-
|
483
|
-
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:
|
484
|
-
|
485
|
-
```erb
|
486
|
-
<%= render_notification(@notification, fallback: :text) %>
|
487
|
-
```
|
488
|
-
|
489
|
-
Finally, default views of *activity_notification* depends on jQuery and you have to add requirements to *application.js* in your apps:
|
490
|
-
|
491
|
-
```app/assets/javascripts/application.js
|
492
|
-
//= require jquery
|
493
|
-
//= require jquery_ujs
|
494
|
-
```
|
495
|
-
|
496
|
-
#### i18n for notifications
|
497
|
-
|
498
|
-
Translations are used by the *#text* method, to which you can pass additional options in form of a hash. *#render* method uses translations when view templates have not been provided. You can render pure i18n strings by passing `{ i18n: true }` to *#render_notification* or *#render*.
|
499
|
-
|
500
|
-
Translations should be put in your locale *.yml* files as **text** field. To render pure strings from I18n example structure:
|
501
|
-
|
502
|
-
```yaml
|
503
|
-
notification:
|
504
|
-
user:
|
505
|
-
article:
|
506
|
-
create:
|
507
|
-
text: 'Article has been created'
|
508
|
-
destroy:
|
509
|
-
text: 'Some user removed an article!'
|
510
|
-
comment:
|
511
|
-
post:
|
512
|
-
text: "<p>%{notifier_name} posted comments to your article %{article_title}</p>"
|
513
|
-
reply:
|
514
|
-
text: "<p>%{notifier_name} and %{group_member_count} other users replied for your comments</p>"
|
515
|
-
admin:
|
516
|
-
article:
|
517
|
-
post:
|
518
|
-
text: '[Admin] Article has been created'
|
519
|
-
```
|
520
|
-
|
521
|
-
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.
|
522
|
-
|
523
|
-
### Customizing controllers (optional)
|
524
|
-
|
525
|
-
If the customization at the views level is not enough, you can customize each controller by following these steps:
|
526
|
-
|
527
|
-
1. Create your custom controllers using the generator with a target:
|
528
|
-
|
529
|
-
```console
|
530
|
-
$ bin/rails generate activity_notification:controllers users
|
531
|
-
```
|
532
|
-
|
533
|
-
If you specify *users* as the target, controllers will be created in *app/controllers/users*.
|
534
|
-
And the notifications controller will look like this:
|
535
|
-
|
536
|
-
```ruby
|
537
|
-
class Users::NotificationsController < ActivityNotification::NotificationsController
|
538
|
-
# GET /:target_type/:target_id/notifications
|
539
|
-
# def index
|
540
|
-
# super
|
541
|
-
# end
|
542
|
-
|
543
|
-
# ...
|
544
|
-
|
545
|
-
# POST /:target_type/:target_id/notifications/:id/open
|
546
|
-
# def open
|
547
|
-
# super
|
548
|
-
# end
|
549
|
-
|
550
|
-
# ...
|
551
|
-
end
|
552
|
-
```
|
553
|
-
|
554
|
-
2. Tell the router to use this controller:
|
555
|
-
|
556
|
-
```ruby
|
557
|
-
notify_to :users, controller: 'users/notifications'
|
558
|
-
```
|
559
|
-
|
560
|
-
3. Finally, change or extend the desired controller actions.
|
561
|
-
|
562
|
-
You can completely override a controller action
|
563
|
-
```ruby
|
564
|
-
class Users::NotificationsController < ActivityNotification::NotificationsController
|
565
|
-
# ...
|
566
|
-
|
567
|
-
# POST /:target_type/:target_id/notifications/:id/open
|
568
|
-
def open
|
569
|
-
# Custom code to open notification here
|
570
|
-
|
571
|
-
# super
|
572
|
-
end
|
573
|
-
|
574
|
-
# ...
|
257
|
+
Rails.application.routes.draw do
|
258
|
+
scope :api do
|
259
|
+
scope :"v2" do
|
260
|
+
notify_to :users, api_mode: true
|
575
261
|
end
|
576
|
-
```
|
577
|
-
|
578
|
-
|
579
|
-
## Functions
|
580
|
-
|
581
|
-
### Email notification
|
582
|
-
|
583
|
-
*activity_notification* provides email notification to the notification targets.
|
584
|
-
|
585
|
-
#### Mailer setup
|
586
|
-
|
587
|
-
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*:
|
588
|
-
|
589
|
-
```ruby
|
590
|
-
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
|
591
|
-
```
|
592
|
-
|
593
|
-
Email notification is disabled as default. You can configure to enable email notification in initializer *activity_notification.rb*.
|
594
|
-
|
595
|
-
```ruby
|
596
|
-
config.email_enabled = true
|
597
|
-
config.mailer_sender = 'your_notification_sender@example.com'
|
598
|
-
```
|
599
|
-
|
600
|
-
You can also configure them for each model by *acts_as roles* like these.
|
601
|
-
|
602
|
-
```ruby
|
603
|
-
class User < ActiveRecord::Base
|
604
|
-
# Example using confirmed_at of devise field
|
605
|
-
# to decide whether activity_notification sends notification email to this user
|
606
|
-
acts_as_target email: :email, email_allowed: :confirmed_at
|
607
|
-
end
|
608
|
-
```
|
609
|
-
|
610
|
-
```ruby
|
611
|
-
class Comment < ActiveRecord::Base
|
612
|
-
belongs_to :article
|
613
|
-
belongs_to :user
|
614
|
-
|
615
|
-
acts_as_notifiable :users,
|
616
|
-
targets: ->(comment, key) {
|
617
|
-
([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
|
618
|
-
},
|
619
|
-
# Allow notification email
|
620
|
-
email_allowed: true,
|
621
|
-
notifiable_path: :article_notifiable_path
|
622
|
-
|
623
|
-
def article_notifiable_path
|
624
|
-
article_path(article)
|
625
262
|
end
|
626
263
|
end
|
627
264
|
```
|
628
265
|
|
629
|
-
|
630
|
-
|
631
|
-
*activity_notification* will look for email template in the same way as notification views. For example, if you have a notification with *:key* set to *"notification.comment.reply"* and target_type *users*, the gem will look for a partial in *app/views/activity_notification/mailer/users/comment/_reply.html.(|erb|haml|slim|something_else)*.
|
632
|
-
|
633
|
-
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)*.
|
634
|
-
|
635
|
-
#### i18n for email
|
636
|
-
|
637
|
-
The subject of notification email can be put in your locale *.yml* files as **mail_subject** field:
|
638
|
-
|
639
|
-
```yaml
|
640
|
-
notification:
|
641
|
-
user:
|
642
|
-
comment:
|
643
|
-
post:
|
644
|
-
text: "<p>Someone posted comments to your article</p>"
|
645
|
-
mail_subject: 'New comment to your article'
|
646
|
-
```
|
647
|
-
|
648
|
-
### Batch email notification
|
649
|
-
|
650
|
-
*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*.
|
651
|
-
|
652
|
-
#### Batch mailer setup
|
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.
|
653
267
|
|
654
|
-
|
655
|
-
|
656
|
-
Batch email notification is disabled as default. You can configure to enable email notification in initializer *activity_notification.rb* like single email notification.
|
657
|
-
|
658
|
-
```ruby
|
659
|
-
config.email_enabled = true
|
660
|
-
config.mailer_sender = 'your_notification_sender@example.com'
|
661
|
-
```
|
662
|
-
|
663
|
-
You can also configure them for each target model by *acts_as_target* role like this.
|
664
|
-
|
665
|
-
```ruby
|
666
|
-
class User < ActiveRecord::Base
|
667
|
-
# Example using confirmed_at of devise field
|
668
|
-
# to decide whether activity_notification sends batch notification email to this user
|
669
|
-
acts_as_target email: :email, batch_email_allowed: :confirmed_at
|
670
|
-
end
|
671
|
-
```
|
672
|
-
|
673
|
-
Then, you can send batch notification email for unopened notifications only to the all specified targets with *batch_key*.
|
674
|
-
|
675
|
-
```ruby
|
676
|
-
# Send batch notification email to the users with unopened notifications
|
677
|
-
User.send_batch_unopened_notification_email(batch_key: 'batch.comment.post')
|
678
|
-
```
|
679
|
-
|
680
|
-
You can also add conditions to filter notifications, like this:
|
681
|
-
|
682
|
-
```ruby
|
683
|
-
# Send batch notification email to the users with unopened notifications of specified key in 1 hour
|
684
|
-
User.send_batch_unopened_notification_email(batch_key: 'batch.comment.post', filtered_by_key: 'comment.post', custom_filter: ["created_at >= ?", time.hour.ago])
|
685
|
-
```
|
686
|
-
|
687
|
-
#### Batch email templates
|
688
|
-
|
689
|
-
*activity_notification* will look for batch email template in the same way as email notification using *batch_key*.
|
690
|
-
*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*.
|
691
|
-
|
692
|
-
#### i18n for batch email
|
693
|
-
|
694
|
-
The subject of batch notification email also can be put in your locale *.yml* files as **mail_subject** field for *batch_key*.
|
695
|
-
|
696
|
-
```yaml
|
697
|
-
notification:
|
698
|
-
user:
|
699
|
-
batch:
|
700
|
-
comment:
|
701
|
-
post:
|
702
|
-
mail_subject: 'New comments to your article'
|
703
|
-
```
|
704
|
-
|
705
|
-
### Grouping notifications
|
706
|
-
|
707
|
-
*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:
|
708
|
-
|
709
|
-
```ruby
|
710
|
-
@comment.notify :users key: 'comment.post', group: @comment.article
|
711
|
-
```
|
712
|
-
|
713
|
-
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.
|
714
|
-
|
715
|
-
```ruby
|
716
|
-
class Article < ActiveRecord::Base
|
717
|
-
belongs_to :user
|
718
|
-
acts_as_notification_group printable_name: ->(article) { "article \"#{article.title}\"" }
|
719
|
-
end
|
720
|
-
```
|
268
|
+
### Creating notifications
|
721
269
|
|
722
|
-
You can
|
270
|
+
You can trigger notifications by setting all your required parameters and triggering **notify** on the notifiable model, like this:
|
723
271
|
|
724
272
|
```ruby
|
725
|
-
|
726
|
-
def index
|
727
|
-
@notifications = @target.notifications.group_owners_only
|
728
|
-
end
|
729
|
-
```
|
730
|
-
*notification_index* and *notification_index_with_attributes* methods also use *group_owners_only* scope internally.
|
731
|
-
|
732
|
-
And you can render them in a view like this:
|
733
|
-
```erb
|
734
|
-
<% if notification.group_member_exists? %>
|
735
|
-
<%= "#{notification.notifier.name} and #{notification.group_member_count} other users" %>
|
736
|
-
<% else %>
|
737
|
-
<%= "#{notification.notifier.name}" %>
|
738
|
-
<% end %>
|
739
|
-
<%= "posted comments to your article \"#{notification.group.title}\"" %>
|
740
|
-
```
|
741
|
-
|
742
|
-
This presentation will be shown to target users as *Kevin and 7 other users posted comments to your article "Let's use Ruby"*.
|
743
|
-
|
744
|
-
You can also use `%{group_member_count}`, `%{group_notification_count}`, `%{group_member_notifier_count}` and `%{group_notifier_count}` in i18n text as a field:
|
745
|
-
|
746
|
-
```yaml
|
747
|
-
notification:
|
748
|
-
user:
|
749
|
-
comment:
|
750
|
-
post:
|
751
|
-
text: "<p>%{notifier_name} and %{group_member_notifier_count} other users posted %{group_notification_count} comments to your article</p>"
|
752
|
-
mail_subject: 'New comment to your article'
|
273
|
+
@comment.notify :users, key: "comment.reply"
|
753
274
|
```
|
754
275
|
|
755
|
-
|
756
|
-
|
276
|
+
The first argument is the plural symbol name of your target model, which is configured in notifiable model by *acts_as_notifiable*.
|
277
|
+
The new instances of **ActivityNotification::Notification** model will be generated for the specified targets.
|
757
278
|
|
758
|
-
|
279
|
+
See [Creating notifications](/docs/Setup.md#creating-notifications) for more details.
|
759
280
|
|
760
|
-
|
281
|
+
### Displaying notifications
|
761
282
|
|
762
|
-
|
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.
|
763
284
|
|
764
|
-
|
285
|
+
See [Displaying notifications](/docs/Setup.md#displaying-notifications) for more details.
|
765
286
|
|
766
|
-
|
767
|
-
config.subscription_enabled = true
|
768
|
-
```
|
287
|
+
### Run example Rails application
|
769
288
|
|
770
|
-
|
771
|
-
|
772
|
-
```ruby
|
773
|
-
class User < ActiveRecord::Base
|
774
|
-
# Example using confirmed_at of devise field
|
775
|
-
# to decide whether activity_notification manages subscriptions of this user
|
776
|
-
acts_as_target email: :email, email_allowed: :confirmed_at, subscription_allowed: :confirmed_at
|
777
|
-
end
|
778
|
-
```
|
779
|
-
|
780
|
-
If you do not have a subscriptions table in you database, create a migration for subscriptions and migrate the database in your Rails project:
|
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.
|
781
291
|
|
782
292
|
```console
|
783
|
-
$
|
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
|
784
297
|
$ bin/rake db:migrate
|
298
|
+
$ bin/rake db:seed
|
299
|
+
$ bin/rails server
|
785
300
|
```
|
786
|
-
|
787
|
-
|
788
|
-
```
|
789
|
-
config.subscription_table_name = "notifications_subscriptions"
|
790
|
-
```
|
791
|
-
|
792
|
-
#### Managing subscriptions
|
793
|
-
|
794
|
-
Subscriptions are managed by instances of **ActivityNotification::Subscription** model which belongs to *target* and *key* of the notification.
|
795
|
-
*Subscription#subscribing* manages subscription of notifications.
|
796
|
-
*true* means the target will receive the notifications with this key.
|
797
|
-
*false* means the target will not receive these notifications.
|
798
|
-
*Subscription#subscribing_to_email* manages subscription of notification email.
|
799
|
-
*true* means the target will receive the notification email with this key including batch notification email with this *batch_key*.
|
800
|
-
*false* means the target will not receive these notification email.
|
801
|
-
|
802
|
-
As default, all target subscribes to notification and notification email when subscription record does not exist in your database.
|
803
|
-
You can change this **subscribe_as_default** parameter in initializer *activity_notification.rb*.
|
804
|
-
|
805
|
-
```ruby
|
806
|
-
config.subscribe_as_default = false
|
807
|
-
```
|
808
|
-
|
809
|
-
Then, all target does not subscribe to notification and notification email and will not receive any notifications as default.
|
810
|
-
|
811
|
-
You can create subscription record from subscription API in your target model like this:
|
812
|
-
|
813
|
-
```ruby
|
814
|
-
# Subscribe 'comment.reply' notifications and notification email
|
815
|
-
user.create_subscription(key: 'comment.reply')
|
816
|
-
|
817
|
-
# Subscribe 'comment.reply' notifications but does not subscribe notification email
|
818
|
-
user.create_subscription(key: 'comment.reply', subscribing_to_email: false)
|
819
|
-
|
820
|
-
# Unsubscribe 'comment.reply' notifications and notification email
|
821
|
-
user.create_subscription(key: 'comment.reply', subscribing: false)
|
822
|
-
```
|
823
|
-
|
824
|
-
You can also update subscriptions like this:
|
825
|
-
|
826
|
-
```ruby
|
827
|
-
# Subscribe 'comment.reply' notifications and notification email
|
828
|
-
user.find_or_create_subscription('comment.reply').subscribe
|
829
|
-
|
830
|
-
# Unsubscribe 'comment.reply' notifications and notification email
|
831
|
-
user.find_or_create_subscription('comment.reply').unsubscribe
|
832
|
-
|
833
|
-
# Unsubscribe 'comment.reply' notification email
|
834
|
-
user.find_or_create_subscription('comment.reply').unsubscribe_to_email
|
835
|
-
```
|
836
|
-
|
837
|
-
#### Customizing subscriptions
|
838
|
-
|
839
|
-
*activity_notification* provides basic controllers and views to manage the subscriptions.
|
840
|
-
|
841
|
-
Add subscription routing to *config/routes.rb* for the target (e.g. *:users*):
|
842
|
-
|
843
|
-
```ruby
|
844
|
-
Rails.application.routes.draw do
|
845
|
-
subscribed_by :users
|
846
|
-
end
|
847
|
-
```
|
848
|
-
|
849
|
-
or, you can also configure it with notifications like this:
|
850
|
-
|
851
|
-
```ruby
|
852
|
-
Rails.application.routes.draw do
|
853
|
-
notify_to :users, with_subscription: true
|
854
|
-
end
|
855
|
-
```
|
856
|
-
|
857
|
-
Then, you can access *users/1/subscriptions* and use **subscriptions_controller** or **subscriptions_with_devise_controller** to manage the subscriptions.
|
858
|
-
|
859
|
-
If you would like to customize subscription controllers or views, you can use generators like notifications:
|
860
|
-
|
861
|
-
* Customize subscription controllers
|
862
|
-
|
863
|
-
1. Create your custom controllers using controller generator with a target:
|
864
|
-
|
865
|
-
```console
|
866
|
-
$ bin/rails generate activity_notification:controllers users -c subscriptions subscriptions_with_devise
|
867
|
-
```
|
868
|
-
|
869
|
-
2. Tell the router to use this controller:
|
870
|
-
|
871
|
-
```ruby
|
872
|
-
notify_to :users, with_subscription: { controller: 'users/subscriptions' }
|
873
|
-
```
|
874
|
-
|
875
|
-
* Customize subscription views
|
876
|
-
|
877
|
-
```console
|
878
|
-
$ bin/rails generate activity_notification:views users -v subscriptions
|
879
|
-
```
|
880
|
-
|
881
|
-
|
882
|
-
### Integration with Devise
|
883
|
-
|
884
|
-
*activity_notification* supports to integrate with devise authentication.
|
885
|
-
|
886
|
-
First, add **:with_devise** option in notification routing to *config/routes.rb* for the target:
|
887
|
-
|
888
|
-
```ruby
|
889
|
-
Rails.application.routes.draw do
|
890
|
-
devise_for :users
|
891
|
-
# Integrated with devise
|
892
|
-
notify_to :users, with_devise: :users
|
893
|
-
end
|
894
|
-
```
|
895
|
-
|
896
|
-
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'.
|
897
|
-
|
898
|
-
*Hint*: HTTP 403 Forbidden will be returned for unauthorized notifications.
|
899
|
-
|
900
|
-
You can also use different model from Devise resource as a target. When you will add this to *config/routes.rb*:
|
901
|
-
|
902
|
-
```ruby
|
903
|
-
Rails.application.routes.draw do
|
904
|
-
devise_for :users
|
905
|
-
# Integrated with devise for different model
|
906
|
-
notify_to :admins, with_devise: :users
|
907
|
-
end
|
908
|
-
```
|
909
|
-
|
910
|
-
and add **:devise_resource** option to *acts_as_target* in the target model:
|
911
|
-
|
912
|
-
```ruby
|
913
|
-
class Admin < ActiveRecord::Base
|
914
|
-
belongs_to :user
|
915
|
-
acts_as_target devise_resource: :user
|
916
|
-
end
|
917
|
-
```
|
918
|
-
|
919
|
-
*activity_notification* will authenticate *:admins* notifications with devise authentication for *:users*.
|
920
|
-
In this example *activity_notification* will confirm the *user* who *admin* belongs to with authenticated user by devise.
|
921
|
-
|
922
|
-
|
923
|
-
### Optional notification targets
|
924
|
-
|
925
|
-
*activity_notification* supports configurable optional notification targets like Amazon SNS, Slack, SMS and so on.
|
926
|
-
|
927
|
-
#### Configuring optional targets
|
928
|
-
|
929
|
-
*activity_notification* provides default optional target implementation for Amazon SNS and Slack.
|
930
|
-
You can develop any optional target classes which extends *ActivityNotification::OptionalTarget::Base*, and configure them to notifiable model by *acts_as_notifiable* like this.
|
931
|
-
|
932
|
-
```ruby
|
933
|
-
class Comment < ActiveRecord::Base
|
934
|
-
belongs_to :article
|
935
|
-
belongs_to :user
|
936
|
-
|
937
|
-
require 'activity_notification/optional_targets/amazon_sns'
|
938
|
-
require 'activity_notification/optional_targets/slack'
|
939
|
-
require 'custom_optional_targets/console_output'
|
940
|
-
acts_as_notifiable :admins, targets: [Admin.first].compact,
|
941
|
-
notifiable_path: :article_notifiable_path,
|
942
|
-
# Set optional target implementation class and initializing parameters
|
943
|
-
optional_targets: {
|
944
|
-
ActivityNotification::OptionalTarget::AmazonSNS => { topic_arn: 'arn:aws:sns:XXXXX:XXXXXXXXXXXX:XXXXX' },
|
945
|
-
ActivityNotification::OptionalTarget::Slack => {
|
946
|
-
webhook_url: 'https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX',
|
947
|
-
slack_name: :slack_name, channel: 'activity_notification', username: 'ActivityNotification', icon_emoji: ":ghost:"
|
948
|
-
},
|
949
|
-
CustomOptionalTarget::ConsoleOutput => {}
|
950
|
-
}
|
951
|
-
|
952
|
-
def article_notifiable_path
|
953
|
-
article_path(article)
|
954
|
-
end
|
955
|
-
end
|
956
|
-
```
|
957
|
-
|
958
|
-
Write *require* statement for optional target implementation classes and set them with initializing parameters to *acts_as_notifiable*.
|
959
|
-
*activity_notification* will publish all notifications of those targets and notifiables to optional targets.
|
960
|
-
|
961
|
-
#### Customizing message format
|
962
|
-
|
963
|
-
Optional targets prepare publishing messages from notification instance using view template like rendering notifications.
|
964
|
-
As default, all optional targets use *app/views/activity_notification/optional_targets/default/base/_default.text.erb*.
|
965
|
-
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)*.
|
966
|
-
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*.
|
967
|
-
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*.
|
968
|
-
|
969
|
-
#### Amazon SNS as optional target
|
970
|
-
|
971
|
-
*activity_notification* provides **ActivityNotification::OptionalTarget::AmazonSNS** as default optional target implementation for Amazon SNS.
|
972
|
-
|
973
|
-
First, add **aws-sdk** 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/v2/developer-guide/setup-config.html)).
|
974
|
-
|
975
|
-
```ruby
|
976
|
-
gem 'aws-sdk', '~> 2'
|
977
|
-
```
|
978
|
-
|
979
|
-
```ruby
|
980
|
-
require 'aws-sdk'
|
981
|
-
Aws.config.update(
|
982
|
-
region: 'your_region',
|
983
|
-
credentials: Aws::Credentials.new('your_access_key_id', 'your_secret_access_key')
|
984
|
-
)
|
985
|
-
```
|
986
|
-
|
987
|
-
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.
|
988
|
-
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/sdkforruby/api/Aws/SNS/Client.html) for more details.
|
989
|
-
|
990
|
-
```ruby
|
991
|
-
class Comment < ActiveRecord::Base
|
992
|
-
require 'activity_notification/optional_targets/amazon_sns'
|
993
|
-
acts_as_notifiable :admins, targets: [Admin.first].compact,
|
994
|
-
optional_targets: {
|
995
|
-
ActivityNotification::OptionalTarget::AmazonSNS => { topic_arn: 'arn:aws:sns:XXXXX:XXXXXXXXXXXX:XXXXX' }
|
996
|
-
}
|
997
|
-
end
|
998
|
-
```
|
999
|
-
|
1000
|
-
#### Slack as optional target
|
1001
|
-
|
1002
|
-
*activity_notification* provides **ActivityNotification::OptionalTarget::Slack** as default optional target implementation for Slack.
|
1003
|
-
|
1004
|
-
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)).
|
1005
|
-
|
1006
|
-
```ruby
|
1007
|
-
gem 'slack-notifier'
|
1008
|
-
```
|
1009
|
-
|
1010
|
-
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.
|
1011
|
-
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.
|
1012
|
-
|
1013
|
-
```ruby
|
1014
|
-
class Comment < ActiveRecord::Base
|
1015
|
-
require 'activity_notification/optional_targets/slack'
|
1016
|
-
acts_as_notifiable :admins, targets: [Admin.first].compact,
|
1017
|
-
optional_targets: {
|
1018
|
-
ActivityNotification::OptionalTarget::Slack => {
|
1019
|
-
webhook_url: 'https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX',
|
1020
|
-
target_username: :slack_username, channel: 'activity_notification', username: 'ActivityNotification', icon_emoji: ":ghost:"
|
1021
|
-
}
|
1022
|
-
}
|
1023
|
-
end
|
1024
|
-
```
|
1025
|
-
|
1026
|
-
#### Developing custom optional targets
|
301
|
+
Then, you can access <http://localhost:3000> for the example application.
|
1027
302
|
|
1028
|
-
You can develop any custom optional targets.
|
1029
|
-
Custom optional target class must extend **ActivityNotification::OptionalTarget::Base** and override **initialize_target** and **notify** method.
|
1030
|
-
You can use **render_notification_message** method to prepare message from notification instance using view template.
|
1031
303
|
|
1032
|
-
|
1033
|
-
|
1034
|
-
```ruby
|
1035
|
-
module CustomOptionalTarget
|
1036
|
-
# Custom optional target implementation for mobile push notification or SMS using Amazon SNS.
|
1037
|
-
class AmazonSNS < ActivityNotification::OptionalTarget::Base
|
1038
|
-
require 'aws-sdk'
|
1039
|
-
|
1040
|
-
# Initialize method to prepare Aws::SNS::Client
|
1041
|
-
def initialize_target(options = {})
|
1042
|
-
@topic_arn = options.delete(:topic_arn)
|
1043
|
-
@target_arn = options.delete(:target_arn)
|
1044
|
-
@phone_number = options.delete(:phone_number)
|
1045
|
-
@sns_client = Aws::SNS::Client.new(options)
|
1046
|
-
end
|
304
|
+
## Setup
|
1047
305
|
|
1048
|
-
|
1049
|
-
def notify(notification, options = {})
|
1050
|
-
@sns_client.publish(
|
1051
|
-
topic_arn: notification.target.resolve_value(options.delete(:topic_arn) || @topic_arn),
|
1052
|
-
target_arn: notification.target.resolve_value(options.delete(:target_arn) || @target_arn),
|
1053
|
-
phone_number: notification.target.resolve_value(options.delete(:phone_number) || @phone_number),
|
1054
|
-
message: render_notification_message(notification, options)
|
1055
|
-
)
|
1056
|
-
end
|
1057
|
-
end
|
1058
|
-
end
|
1059
|
-
```
|
306
|
+
See [Setup](/docs/Setup.md#Setup).
|
1060
307
|
|
1061
|
-
Then, you can configure them to notifiable model by *acts_as_notifiable* like this.
|
1062
308
|
|
1063
|
-
|
1064
|
-
class Comment < ActiveRecord::Base
|
1065
|
-
require 'custom_optional_targets/amazon_sns'
|
1066
|
-
acts_as_notifiable :admins, targets: [Admin.first].compact,
|
1067
|
-
optional_targets: {
|
1068
|
-
CustomOptionalTarget::AmazonSNS => { topic_arn: 'arn:aws:sns:XXXXX:XXXXXXXXXXXX:XXXXX' }
|
1069
|
-
}
|
1070
|
-
end
|
1071
|
-
```
|
309
|
+
## Functions
|
1072
310
|
|
1073
|
-
|
311
|
+
See [Functions](/docs/Functions.md#Functions).
|
1074
312
|
|
1075
313
|
|
1076
314
|
## Testing
|
1077
315
|
|
1078
|
-
|
1079
|
-
|
1080
|
-
First, you need to configure ActivityNotification as described above.
|
1081
|
-
|
1082
|
-
#### Testing notifications with RSpec
|
1083
|
-
Prepare target and notifiable model instances to test generating notifications (e.g. `@user` and `@comment`).
|
1084
|
-
Then, you can call notify API and test if notifications of the target are generated.
|
1085
|
-
```ruby
|
1086
|
-
# Prepare
|
1087
|
-
@article_author = create(:user)
|
1088
|
-
@comment = @article_author.articles.create.comments.create
|
1089
|
-
expect(@article_author.notifications.unopened_only.count).to eq(0)
|
1090
|
-
|
1091
|
-
# Call notify API
|
1092
|
-
@comment.notify :users
|
1093
|
-
|
1094
|
-
# Test generated notifications
|
1095
|
-
expect(@article_author_user.notifications.unopened_only.count).to eq(1)
|
1096
|
-
expect(@article_author_user.notifications.unopened_only.latest.notifiable).to eq(@comment)
|
1097
|
-
```
|
1098
|
-
|
1099
|
-
#### Testing email notifications with RSpec
|
1100
|
-
Prepare target and notifiable model instances to test sending notification email.
|
1101
|
-
Then, you can call notify API and test if notification email is sent.
|
1102
|
-
```ruby
|
1103
|
-
# Prepare
|
1104
|
-
@article_author = create(:user)
|
1105
|
-
@comment = @article_author.articles.create.comments.create
|
1106
|
-
expect(ActivityNotification::Mailer.deliveries.size).to eq(0)
|
1107
|
-
|
1108
|
-
# Call notify API and send email now
|
1109
|
-
@comment.notify :users, send_later: false
|
1110
|
-
|
1111
|
-
# Test sent notification email
|
1112
|
-
expect(ActivityNotification::Mailer.deliveries.size).to eq(1)
|
1113
|
-
expect(ActivityNotification::Mailer.deliveries.first.to[0]).to eq(@article_author.email)
|
1114
|
-
```
|
1115
|
-
Note that notification email will be sent asynchronously without false as *:send_later* option.
|
1116
|
-
```ruby
|
1117
|
-
# Prepare
|
1118
|
-
include ActiveJob::TestHelper
|
1119
|
-
@article_author = create(:user)
|
1120
|
-
@comment = @article_author.articles.create.comments.create
|
1121
|
-
expect(ActivityNotification::Mailer.deliveries.size).to eq(0)
|
1122
|
-
|
1123
|
-
# Call notify API and send email asynchronously as default
|
1124
|
-
# Test sent notification email with ActiveJob queue
|
1125
|
-
expect {
|
1126
|
-
perform_enqueued_jobs do
|
1127
|
-
@comment.notify :users
|
1128
|
-
end
|
1129
|
-
}.to change { ActivityNotification::Mailer.deliveries.size }.by(1)
|
1130
|
-
expect(ActivityNotification::Mailer.deliveries.first.to[0]).to eq(@article_author.email)
|
1131
|
-
```
|
1132
|
-
|
1133
|
-
### Testing gem alone
|
1134
|
-
|
1135
|
-
#### Testing with RSpec
|
1136
|
-
Pull git repository and execute RSpec.
|
1137
|
-
```console
|
1138
|
-
$ git pull https://github.com/simukappu/activity_notification.git
|
1139
|
-
$ cd activity_notification
|
1140
|
-
$ bundle install —path vendor/bundle
|
1141
|
-
$ bundle exec rspec
|
1142
|
-
- or -
|
1143
|
-
$ bundle exec rake
|
1144
|
-
```
|
1145
|
-
|
1146
|
-
#### Dummy Rails application
|
1147
|
-
Test module includes dummy Rails application in **spec/rails_app**. You can run the dummy application as common Rails application.
|
1148
|
-
```console
|
1149
|
-
$ cd spec/rails_app
|
1150
|
-
$ bin/rake db:migrate
|
1151
|
-
$ bin/rake db:seed
|
1152
|
-
$ bin/rails server
|
1153
|
-
```
|
1154
|
-
Then, you can access <http://localhost:3000> for the dummy application.
|
1155
|
-
|
1156
|
-
##### Run with your local database
|
1157
|
-
As default, dummy Rails application runs with local SQLite database in *spec/rails_app/db/development.sqlite3*.
|
1158
|
-
This application supports to run with your local MySQL, PostgreSQL and MongoDB.
|
1159
|
-
Set **AN_TEST_DB** environment variable like:
|
1160
|
-
```console
|
1161
|
-
$ export AN_TEST_DB=mysql
|
1162
|
-
```
|
1163
|
-
for MySQL,
|
1164
|
-
```console
|
1165
|
-
$ export AN_TEST_DB=postgresql
|
1166
|
-
```
|
1167
|
-
for PostgreSQL, and
|
1168
|
-
```console
|
1169
|
-
$ export AN_TEST_DB=mongodb
|
1170
|
-
```
|
1171
|
-
for MongoDB. When you set **mongodb** as *AN_TEST_DB*, you have to use *activity_notification* with MongoDB. Also set **AN_ORM** like:
|
1172
|
-
```console
|
1173
|
-
$ export AN_ORM=mongoid
|
1174
|
-
```
|
1175
|
-
|
1176
|
-
Then, configure *spec/rails_app/config/database.yml* or *spec/rails_app/config/mongoid.yml* as your local database.
|
1177
|
-
Finally, run database migration, seed data script and the dummy appliation.
|
1178
|
-
```console
|
1179
|
-
$ cd spec/rails_app
|
1180
|
-
$ # You don't need migration when you use MongoDB only (AN_ORM=mongoid and AN_TEST_DB=mongodb)
|
1181
|
-
$ bin/rake db:migrate
|
1182
|
-
$ bin/rake db:seed
|
1183
|
-
$ bin/rails server
|
1184
|
-
```
|
316
|
+
See [Testing](/docs/Testing.md#Testing).
|
1185
317
|
|
1186
318
|
|
1187
319
|
## Documentation
|
@@ -1199,22 +331,25 @@ $ bundle exec yard server
|
|
1199
331
|
```
|
1200
332
|
Then you can see the documents at <http://localhost:8808/docs/index>.
|
1201
333
|
|
1202
|
-
## Common examples
|
1203
334
|
|
1204
|
-
|
335
|
+
## Common Examples
|
1205
336
|
|
337
|
+
See example Rails application in *[/spec/rails_app](/spec/rails_app)*.
|
1206
338
|
|
1207
|
-
|
339
|
+
You can also try this example Rails application as Online Demo here:
|
340
|
+
* **https://activity-notification-example.herokuapp.com/**
|
1208
341
|
|
1209
|
-
|
342
|
+
You can login as test users to experience user activity notifications. For more details, see [Online Demo](#online-demo).
|
1210
343
|
|
1211
344
|
|
1212
345
|
## Contributing
|
1213
346
|
|
1214
|
-
We
|
1215
|
-
*
|
1216
|
-
|
1217
|
-
|
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.
|
349
|
+
|
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).
|
351
|
+
|
352
|
+
We appreciate any of your contribution!
|
1218
353
|
|
1219
354
|
|
1220
355
|
## License
|