activity_notification 0.0.9 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.travis.yml +5 -0
- data/.yardopts +3 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +50 -44
- data/README.md +242 -81
- data/Rakefile +13 -13
- data/activity_notification.gemspec +6 -8
- data/app/controllers/activity_notification/notifications_controller.rb +89 -11
- data/app/controllers/activity_notification/notifications_with_devise_controller.rb +12 -3
- data/app/mailers/activity_notification/mailer.rb +3 -0
- data/gemfiles/Gemfile.rails-4.2 +13 -0
- data/gemfiles/Gemfile.rails-4.2.lock +190 -0
- data/gemfiles/Gemfile.rails-5.0 +14 -0
- data/gemfiles/Gemfile.rails-5.0.lock +201 -0
- data/lib/activity_notification.rb +10 -6
- data/lib/activity_notification/apis/notification_api.rb +137 -27
- data/lib/activity_notification/common.rb +48 -24
- data/lib/activity_notification/config.rb +68 -10
- data/lib/activity_notification/controllers/store_controller.rb +13 -5
- data/lib/activity_notification/helpers/polymorphic_helpers.rb +17 -3
- data/lib/activity_notification/helpers/view_helpers.rb +161 -45
- data/lib/activity_notification/mailers/helpers.rb +121 -83
- data/lib/activity_notification/models/concerns/notifiable.rb +162 -69
- data/lib/activity_notification/models/concerns/notifier.rb +2 -0
- data/lib/activity_notification/models/concerns/target.rb +124 -25
- data/lib/activity_notification/models/notification.rb +168 -4
- data/lib/activity_notification/rails/routes.rb +50 -48
- data/lib/activity_notification/renderable.rb +106 -26
- data/lib/activity_notification/roles/acts_as_notifiable.rb +99 -26
- data/lib/activity_notification/roles/acts_as_notifier.rb +3 -0
- data/lib/activity_notification/roles/acts_as_target.rb +70 -0
- data/lib/activity_notification/version.rb +1 -1
- data/lib/generators/activity_notification/active_record/migration_generator.rb +3 -1
- data/lib/generators/activity_notification/controllers_generator.rb +5 -0
- data/lib/generators/activity_notification/install_generator.rb +7 -3
- data/lib/generators/activity_notification/models/notification_generator.rb +4 -2
- data/lib/generators/activity_notification/views_generator.rb +20 -0
- data/spec/concerns/apis/notification_api_spec.rb +105 -36
- data/spec/concerns/common_spec.rb +1 -1
- data/spec/concerns/models/notifiable_spec.rb +2 -2
- data/spec/concerns/models/notifier_spec.rb +1 -1
- data/spec/concerns/models/target_spec.rb +9 -8
- data/spec/controllers/notifications_controller_shared_examples.rb +101 -28
- data/spec/controllers/notifications_with_devise_controller_spec.rb +14 -4
- data/spec/helpers/view_helpers_spec.rb +3 -3
- data/spec/mailers/mailer_spec.rb +1 -1
- data/spec/models/notification_spec.rb +57 -3
- data/spec/rails_app/app/models/article.rb +1 -2
- data/spec/rails_app/app/models/comment.rb +8 -6
- data/spec/rails_app/app/models/user.rb +1 -1
- data/spec/rails_app/app/views/layouts/_header.html.erb +2 -0
- data/spec/rails_app/config/application.rb +3 -1
- data/spec/rails_app/config/environment.rb +12 -2
- data/spec/rails_app/config/environments/test.rb +11 -2
- data/spec/roles/acts_as_notifiable_spec.rb +2 -2
- data/spec/roles/acts_as_notifier_spec.rb +1 -1
- data/spec/roles/acts_as_target_spec.rb +3 -3
- data/spec/spec_helper.rb +6 -0
- metadata +35 -40
- data/spec/rails_app/app/models/concerns/.keep +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 081985f450efdee936e92d5ad47423335fb56e64
|
4
|
+
data.tar.gz: 38ed7ad55624f099aabf907bebc63f32b9c46791
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dbe7d56c0527df73da45b7dc006c3efd3d2871ffe0df7e50b0729ff82a6498f439ee643e24547aa7079e8ec53cc4bce8f9d48cd60478dfc9f476c65200fe0d74
|
7
|
+
data.tar.gz: f945b80426ee27de55a182382f36319f69bff5ba72b2c44d1df34e5ad2f05d98143303e3a9ed282013121ec08c88daac54f951497278059414bf388b04c09c02
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/.yardopts
ADDED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,44 +1,44 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
activity_notification (0.0.
|
5
|
-
activerecord (>=
|
4
|
+
activity_notification (0.0.10)
|
5
|
+
activerecord (>= 4.2.0)
|
6
6
|
i18n (>= 0.5.0)
|
7
|
-
|
7
|
+
railties (>= 4.2.0, < 5.1)
|
8
8
|
|
9
9
|
GEM
|
10
10
|
remote: https://rubygems.org/
|
11
11
|
specs:
|
12
|
-
actionmailer (4.2.7)
|
13
|
-
actionpack (= 4.2.7)
|
14
|
-
actionview (= 4.2.7)
|
15
|
-
activejob (= 4.2.7)
|
12
|
+
actionmailer (4.2.7.1)
|
13
|
+
actionpack (= 4.2.7.1)
|
14
|
+
actionview (= 4.2.7.1)
|
15
|
+
activejob (= 4.2.7.1)
|
16
16
|
mail (~> 2.5, >= 2.5.4)
|
17
17
|
rails-dom-testing (~> 1.0, >= 1.0.5)
|
18
|
-
actionpack (4.2.7)
|
19
|
-
actionview (= 4.2.7)
|
20
|
-
activesupport (= 4.2.7)
|
18
|
+
actionpack (4.2.7.1)
|
19
|
+
actionview (= 4.2.7.1)
|
20
|
+
activesupport (= 4.2.7.1)
|
21
21
|
rack (~> 1.6)
|
22
22
|
rack-test (~> 0.6.2)
|
23
23
|
rails-dom-testing (~> 1.0, >= 1.0.5)
|
24
24
|
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
25
|
-
actionview (4.2.7)
|
26
|
-
activesupport (= 4.2.7)
|
25
|
+
actionview (4.2.7.1)
|
26
|
+
activesupport (= 4.2.7.1)
|
27
27
|
builder (~> 3.1)
|
28
28
|
erubis (~> 2.7.0)
|
29
29
|
rails-dom-testing (~> 1.0, >= 1.0.5)
|
30
30
|
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
31
|
-
activejob (4.2.7)
|
32
|
-
activesupport (= 4.2.7)
|
31
|
+
activejob (4.2.7.1)
|
32
|
+
activesupport (= 4.2.7.1)
|
33
33
|
globalid (>= 0.3.0)
|
34
|
-
activemodel (4.2.7)
|
35
|
-
activesupport (= 4.2.7)
|
34
|
+
activemodel (4.2.7.1)
|
35
|
+
activesupport (= 4.2.7.1)
|
36
36
|
builder (~> 3.1)
|
37
|
-
activerecord (4.2.7)
|
38
|
-
activemodel (= 4.2.7)
|
39
|
-
activesupport (= 4.2.7)
|
37
|
+
activerecord (4.2.7.1)
|
38
|
+
activemodel (= 4.2.7.1)
|
39
|
+
activesupport (= 4.2.7.1)
|
40
40
|
arel (~> 6.0)
|
41
|
-
activesupport (4.2.7)
|
41
|
+
activesupport (4.2.7.1)
|
42
42
|
i18n (~> 0.7)
|
43
43
|
json (~> 1.7, >= 1.7.7)
|
44
44
|
minitest (~> 5.1)
|
@@ -52,12 +52,12 @@ GEM
|
|
52
52
|
bcrypt (3.1.11)
|
53
53
|
builder (3.2.2)
|
54
54
|
concurrent-ruby (1.0.2)
|
55
|
-
coveralls (0.8.
|
55
|
+
coveralls (0.8.15)
|
56
56
|
json (>= 1.8, < 3)
|
57
57
|
simplecov (~> 0.12.0)
|
58
58
|
term-ansicolor (~> 1.3)
|
59
59
|
thor (~> 0.19.1)
|
60
|
-
tins (
|
60
|
+
tins (>= 1.6.0, < 2)
|
61
61
|
devise (4.2.0)
|
62
62
|
bcrypt (~> 3.0)
|
63
63
|
orm_adapter (~> 0.1)
|
@@ -72,10 +72,10 @@ GEM
|
|
72
72
|
factory_girl_rails (4.7.0)
|
73
73
|
factory_girl (~> 4.7.0)
|
74
74
|
railties (>= 3.0.0)
|
75
|
-
globalid (0.3.
|
75
|
+
globalid (0.3.7)
|
76
76
|
activesupport (>= 4.1.0)
|
77
77
|
i18n (0.7.0)
|
78
|
-
jquery-rails (4.
|
78
|
+
jquery-rails (4.2.1)
|
79
79
|
rails-dom-testing (>= 1, < 3)
|
80
80
|
railties (>= 4.2.0)
|
81
81
|
thor (>= 0.14, < 2.0)
|
@@ -97,16 +97,16 @@ GEM
|
|
97
97
|
rack (1.6.4)
|
98
98
|
rack-test (0.6.3)
|
99
99
|
rack (>= 1.0)
|
100
|
-
rails (4.2.7)
|
101
|
-
actionmailer (= 4.2.7)
|
102
|
-
actionpack (= 4.2.7)
|
103
|
-
actionview (= 4.2.7)
|
104
|
-
activejob (= 4.2.7)
|
105
|
-
activemodel (= 4.2.7)
|
106
|
-
activerecord (= 4.2.7)
|
107
|
-
activesupport (= 4.2.7)
|
100
|
+
rails (4.2.7.1)
|
101
|
+
actionmailer (= 4.2.7.1)
|
102
|
+
actionpack (= 4.2.7.1)
|
103
|
+
actionview (= 4.2.7.1)
|
104
|
+
activejob (= 4.2.7.1)
|
105
|
+
activemodel (= 4.2.7.1)
|
106
|
+
activerecord (= 4.2.7.1)
|
107
|
+
activesupport (= 4.2.7.1)
|
108
108
|
bundler (>= 1.3.0, < 2.0)
|
109
|
-
railties (= 4.2.7)
|
109
|
+
railties (= 4.2.7.1)
|
110
110
|
sprockets-rails
|
111
111
|
rails-deprecated_sanitizer (1.0.3)
|
112
112
|
activesupport (>= 4.2.0.alpha)
|
@@ -116,15 +116,15 @@ GEM
|
|
116
116
|
rails-deprecated_sanitizer (>= 1.0.1)
|
117
117
|
rails-html-sanitizer (1.0.3)
|
118
118
|
loofah (~> 2.0)
|
119
|
-
railties (4.2.7)
|
120
|
-
actionpack (= 4.2.7)
|
121
|
-
activesupport (= 4.2.7)
|
119
|
+
railties (4.2.7.1)
|
120
|
+
actionpack (= 4.2.7.1)
|
121
|
+
activesupport (= 4.2.7.1)
|
122
122
|
rake (>= 0.8.7)
|
123
123
|
thor (>= 0.18.1, < 2.0)
|
124
124
|
rake (11.2.2)
|
125
|
-
responders (2.
|
125
|
+
responders (2.3.0)
|
126
126
|
railties (>= 4.2.0, < 5.1)
|
127
|
-
rspec-core (3.5.
|
127
|
+
rspec-core (3.5.2)
|
128
128
|
rspec-support (~> 3.5.0)
|
129
129
|
rspec-expectations (3.5.0)
|
130
130
|
diff-lcs (>= 1.2.0, < 2.0)
|
@@ -132,7 +132,7 @@ GEM
|
|
132
132
|
rspec-mocks (3.5.0)
|
133
133
|
diff-lcs (>= 1.2.0, < 2.0)
|
134
134
|
rspec-support (~> 3.5.0)
|
135
|
-
rspec-rails (3.5.
|
135
|
+
rspec-rails (3.5.2)
|
136
136
|
actionpack (>= 3.0)
|
137
137
|
activesupport (>= 3.0)
|
138
138
|
railties (>= 3.0)
|
@@ -146,7 +146,7 @@ GEM
|
|
146
146
|
json (>= 1.8, < 3)
|
147
147
|
simplecov-html (~> 0.10.0)
|
148
148
|
simplecov-html (0.10.0)
|
149
|
-
sprockets (3.
|
149
|
+
sprockets (3.7.0)
|
150
150
|
concurrent-ruby (~> 1.0)
|
151
151
|
rack (> 1, < 3)
|
152
152
|
sprockets-rails (3.1.1)
|
@@ -159,26 +159,32 @@ GEM
|
|
159
159
|
thor (0.19.1)
|
160
160
|
thread_safe (0.3.5)
|
161
161
|
timecop (0.8.1)
|
162
|
-
tins (1.
|
162
|
+
tins (1.12.0)
|
163
163
|
tzinfo (1.2.2)
|
164
164
|
thread_safe (~> 0.1)
|
165
165
|
warden (1.2.6)
|
166
166
|
rack (>= 1.0)
|
167
|
+
yard (0.9.5)
|
168
|
+
yard-activesupport-concern (0.0.1)
|
169
|
+
yard (>= 0.8)
|
167
170
|
|
168
171
|
PLATFORMS
|
169
172
|
ruby
|
170
173
|
|
171
174
|
DEPENDENCIES
|
172
175
|
activity_notification!
|
173
|
-
ammeter
|
176
|
+
ammeter
|
174
177
|
coveralls
|
175
178
|
devise (~> 4.2.0)
|
176
179
|
factory_girl_rails (~> 4.7.0)
|
177
|
-
jquery-rails
|
180
|
+
jquery-rails
|
181
|
+
rails (~> 4.2.7)
|
178
182
|
rspec-rails (~> 3.5.1)
|
179
183
|
simplecov (~> 0.12.0)
|
180
|
-
sqlite3
|
184
|
+
sqlite3
|
181
185
|
timecop
|
186
|
+
yard (~> 0.9.5)
|
187
|
+
yard-activesupport-concern (~> 0.0.1)
|
182
188
|
|
183
189
|
BUNDLED WITH
|
184
190
|
1.12.5
|
data/README.md
CHANGED
@@ -4,11 +4,12 @@
|
|
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
5
|
[![Code Climate](https://codeclimate.com/github/simukappu/activity_notification/badges/gpa.svg)](https://codeclimate.com/github/simukappu/activity_notification)
|
6
6
|
[![Gem Version](https://badge.fury.io/rb/activity_notification.svg)](https://badge.fury.io/rb/activity_notification)
|
7
|
+
[![Inline docs](http://inch-ci.org/github/simukappu/activity_notification.svg?branch=master)](http://inch-ci.org/github/simukappu/activity_notification)
|
7
8
|
[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](MIT-LICENSE)
|
8
9
|
|
9
10
|
`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.
|
10
11
|
|
11
|
-
|
12
|
+
`activity_notification` supports Rails 5.0 and 4.2+. Currently, it is only supported with ActiveRecord ORM.
|
12
13
|
|
13
14
|
|
14
15
|
## Table of contents
|
@@ -29,11 +30,13 @@ Currently, `activity_notification` is only supported with ActiveRecord ORM in Ra
|
|
29
30
|
2. [Rendering notifications](#rendering-notifications)
|
30
31
|
3. [Notification views](#notification-views)
|
31
32
|
4. [i18n for notifications](#i18n-for-notifications)
|
32
|
-
|
33
|
-
|
33
|
+
3. [Functions](#functions)
|
34
|
+
1. [Email notification](#email-notification)
|
34
35
|
1. [Setup mailer](#setup-mailer)
|
35
36
|
2. [Email templates](#email-templates)
|
36
37
|
3. [i18n for email](#i18n-for-email)
|
38
|
+
2. [Grouping notifications](#grouping-notifications)
|
39
|
+
3. [Integration with Devise](#integration-with-devise)
|
37
40
|
4. [Testing](#testing)
|
38
41
|
5. [Documentation](#documentation)
|
39
42
|
6. **[Common examples](#common-examples)**
|
@@ -44,7 +47,7 @@ Currently, `activity_notification` is only supported with ActiveRecord ORM in Ra
|
|
44
47
|
* Notification API (creating notifications, query for notifications and managing notification parameters)
|
45
48
|
* Notification controllers (managing open/unopen of notifications, link to notifiable activity page)
|
46
49
|
* Notification views (presentation of notifications)
|
47
|
-
*
|
50
|
+
* Grouping notifications (grouping like `"Tom and other 7 people posted comments to this article"`)
|
48
51
|
* Email notification
|
49
52
|
* Integration with [Devise](https://github.com/plataformatec/devise) authentication
|
50
53
|
|
@@ -89,25 +92,25 @@ $ rake db:migrate
|
|
89
92
|
#### Configuring target model
|
90
93
|
|
91
94
|
Configure your target model (e.g. app/models/user.rb).
|
92
|
-
Add `
|
95
|
+
Add `acts_as_target` configuration to your target model to get notifications.
|
93
96
|
|
94
97
|
```ruby
|
95
98
|
class User < ActiveRecord::Base
|
96
|
-
#
|
97
|
-
#
|
98
|
-
|
99
|
+
# acts_as_target configures your model as ActivityNotification::Target
|
100
|
+
# with parameters as value or custom methods defined in your model as lambda or symbol
|
101
|
+
|
102
|
+
# This is an example without any options (default configuration) as the target
|
103
|
+
acts_as_target
|
99
104
|
end
|
100
105
|
```
|
101
106
|
|
102
|
-
*Note*: `
|
103
|
-
|
104
|
-
You can override several methods in your target model (e.g. `notification_index` or `notification_email_allowed?`).
|
107
|
+
*Note*: `acts_as_notification_target` is an alias for `acts_as_target` and does the same.
|
105
108
|
|
106
109
|
#### Configuring notifiable model
|
107
110
|
|
108
111
|
Configure your notifiable model (e.g. app/models/comment.rb).
|
109
112
|
Add `acts_as_notifiable` configuration to your notifiable model representing activity to notify.
|
110
|
-
You have to define notification targets for all notifications from this notifiable model by `:targets` option. Other configurations are options.
|
113
|
+
You have to define notification targets for all notifications from this notifiable model by `:targets` option. Other configurations are options. `:notifiable_path` option is a path to move when the notification will be opened by the target user.
|
111
114
|
|
112
115
|
```ruby
|
113
116
|
class Article < ActiveRecord::Base
|
@@ -120,26 +123,27 @@ class Comment < ActiveRecord::Base
|
|
120
123
|
belongs_to :article
|
121
124
|
belongs_to :user
|
122
125
|
|
123
|
-
#
|
126
|
+
# acts_as_notifiable configures your model as ActivityNotification::Notifiable
|
127
|
+
# with parameters as value or custom methods defined in your model as lambda or symbol
|
124
128
|
acts_as_notifiable :users,
|
125
|
-
#
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
129
|
+
# Notification targets as :targets is a necessary option
|
130
|
+
# Set to notify to author and users commented to the article, except comment owner self
|
131
|
+
targets: ->(comment, key) {
|
132
|
+
([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
|
133
|
+
},
|
134
|
+
# Path to move when the notification will be opened by the target user
|
135
|
+
# This is a optional since activity_notification uses polymorphic_path as default
|
136
|
+
notifiable_path: :article_notifiable_path
|
137
|
+
|
138
|
+
def article_notifiable_path
|
133
139
|
article_path(article)
|
134
140
|
end
|
135
141
|
end
|
136
142
|
```
|
137
143
|
|
138
|
-
You can override several methods in your notifiable model (e.g. `notifiable_path` or `notification_email_allowed?`).
|
139
|
-
|
140
144
|
### Configuring views
|
141
145
|
|
142
|
-
`activity_notification` provides view templates to customize your notification views. The view
|
146
|
+
`activity_notification` provides view templates to customize your notification views. The view generator can generate default views for all targets.
|
143
147
|
|
144
148
|
```console
|
145
149
|
$ rails generate activity_notification:views
|
@@ -155,7 +159,7 @@ If you would like to generate only a few sets of views, like the ones for the `n
|
|
155
159
|
you can pass a list of modules to the generator with the `-v` flag.
|
156
160
|
|
157
161
|
```console
|
158
|
-
$ rails generate activity_notification:views -v
|
162
|
+
$ rails generate activity_notification:views -v notifications
|
159
163
|
```
|
160
164
|
|
161
165
|
### Configuring controllers
|
@@ -177,7 +181,15 @@ If the customization at the views level is not enough, you can customize each co
|
|
177
181
|
# def index
|
178
182
|
# super
|
179
183
|
# end
|
180
|
-
|
184
|
+
|
185
|
+
# ...
|
186
|
+
|
187
|
+
# POST /:target_type/:target_id/notifcations/:id/open
|
188
|
+
# def open
|
189
|
+
# super
|
190
|
+
# end
|
191
|
+
|
192
|
+
# ...
|
181
193
|
end
|
182
194
|
```
|
183
195
|
|
@@ -194,11 +206,16 @@ If the customization at the views level is not enough, you can customize each co
|
|
194
206
|
You can completely override a controller action
|
195
207
|
```ruby
|
196
208
|
class Users::NotificationsController < ActivityNotification::NotificationsController
|
209
|
+
# ...
|
210
|
+
|
197
211
|
# POST /:target_type/:target_id/notifcations/:id/open
|
198
212
|
def open
|
199
|
-
#
|
213
|
+
# Custom code to open notification here
|
214
|
+
|
215
|
+
# super
|
200
216
|
end
|
201
|
-
|
217
|
+
|
218
|
+
# ...
|
202
219
|
end
|
203
220
|
```
|
204
221
|
|
@@ -207,15 +224,9 @@ If the customization at the views level is not enough, you can customize each co
|
|
207
224
|
`activity_notification` also provides routing helper. Add notification routing to `config/routes.rb` for the target (e.g. `:users`):
|
208
225
|
|
209
226
|
```ruby
|
210
|
-
# Simply
|
211
227
|
Rails.application.routes.draw do
|
212
228
|
notify_to :users
|
213
229
|
end
|
214
|
-
|
215
|
-
# Or integrated with devise
|
216
|
-
Rails.application.routes.draw do
|
217
|
-
notify_to :users, with_devise: :users
|
218
|
-
end
|
219
230
|
```
|
220
231
|
|
221
232
|
### Creating notifications
|
@@ -224,15 +235,17 @@ You can trigger notifications by setting all your required parameters and trigge
|
|
224
235
|
on the notifiable model, like this:
|
225
236
|
|
226
237
|
```ruby
|
227
|
-
@comment.notify :users key:
|
238
|
+
@comment.notify :users, key: "comment.reply"
|
228
239
|
```
|
229
240
|
|
230
241
|
Or, you can call public API as `ActivityNotification::Notification.notify`
|
231
242
|
|
232
243
|
```ruby
|
233
|
-
ActivityNotification::Notification.notify :users, @comment,
|
244
|
+
ActivityNotification::Notification.notify :users, @comment, key: "comment.reply"
|
234
245
|
```
|
235
246
|
|
247
|
+
*Hint*: `:key` is a option. Default key `#{notifiable_type}.default` which means `comment.default` will be used without specified key.
|
248
|
+
|
236
249
|
### Displaying notifications
|
237
250
|
|
238
251
|
#### Preparing target notifications
|
@@ -294,12 +307,6 @@ Then, content named :notification_index will be prepared and you can use it in y
|
|
294
307
|
...
|
295
308
|
```
|
296
309
|
|
297
|
-
##### Layouts
|
298
|
-
|
299
|
-
Under construction
|
300
|
-
|
301
|
-
##### Locals
|
302
|
-
|
303
310
|
Sometimes, it's desirable to pass additional local variables to partials. It can be done this way:
|
304
311
|
|
305
312
|
```erb
|
@@ -310,7 +317,7 @@ Sometimes, it's desirable to pass additional local variables to partials. It can
|
|
310
317
|
|
311
318
|
`activity_notification` looks for views in `app/views/activity_notification/notifications/:target`.
|
312
319
|
|
313
|
-
For example, if you have an notification with `:key` set to `"notification.
|
320
|
+
For example, if you have an 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)`.
|
314
321
|
|
315
322
|
*Hint*: the `"notification."` prefix in `:key` is completely optional and kept for backwards compatibility, you can skip it in new projects.
|
316
323
|
|
@@ -361,9 +368,79 @@ notification:
|
|
361
368
|
text: '[Admin] Article has been created'
|
362
369
|
```
|
363
370
|
|
364
|
-
This structure is valid for notifications with keys `"notification.
|
371
|
+
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.
|
372
|
+
|
373
|
+
|
374
|
+
## Functions
|
375
|
+
|
376
|
+
### Email notification
|
377
|
+
|
378
|
+
`activity_notification` provides email notification to the notification targets.
|
379
|
+
|
380
|
+
#### Setup mailer
|
381
|
+
|
382
|
+
First, 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`:
|
383
|
+
|
384
|
+
```ruby
|
385
|
+
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
|
386
|
+
```
|
387
|
+
|
388
|
+
Email notification is disabled as default. You can configure to enable email notification in initializer `activity_notification.rb`.
|
389
|
+
|
390
|
+
```ruby
|
391
|
+
config.email_enabled = true
|
392
|
+
config.mailer_sender = 'your_notification_sender@example.com'
|
393
|
+
```
|
394
|
+
|
395
|
+
You can also configure them for each model by acts_as roles like these.
|
396
|
+
|
397
|
+
```ruby
|
398
|
+
class User < ActiveRecord::Base
|
399
|
+
# Example using confirmed_at of devise field
|
400
|
+
# to decide whether activity_notification sends notification email to this user
|
401
|
+
acts_as_notification_target email: :email, email_allowed: :confirmed_at
|
402
|
+
end
|
403
|
+
```
|
404
|
+
|
405
|
+
```ruby
|
406
|
+
class Comment < ActiveRecord::Base
|
407
|
+
belongs_to :article
|
408
|
+
belongs_to :user
|
409
|
+
|
410
|
+
acts_as_notifiable :users,
|
411
|
+
targets: ->(comment, key) {
|
412
|
+
([comment.article.user] + comment.article.commented_users.to_a - [comment.user]).uniq
|
413
|
+
},
|
414
|
+
# Allow notification email
|
415
|
+
email_allowed: true,
|
416
|
+
notifiable_path: :article_notifiable_path
|
417
|
+
|
418
|
+
def article_notifiable_path
|
419
|
+
article_path(article)
|
420
|
+
end
|
421
|
+
end
|
422
|
+
```
|
423
|
+
|
424
|
+
#### Email templates
|
425
|
+
|
426
|
+
`activity_notification` will look for email template in the same way as notification views. For example, if you have an 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)`.
|
427
|
+
|
428
|
+
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)`.
|
429
|
+
|
430
|
+
#### i18n for email
|
431
|
+
|
432
|
+
The subject of notification email can be put in your locale `.yml` files as `mail_subject` field:
|
365
433
|
|
366
|
-
|
434
|
+
```yaml
|
435
|
+
notification:
|
436
|
+
user:
|
437
|
+
comment:
|
438
|
+
reply:
|
439
|
+
text: "<p>%{notifier_name} and %{group_member_count} people replied for your comments</p>"
|
440
|
+
mail_subject: 'New comment to your article'
|
441
|
+
```
|
442
|
+
|
443
|
+
### Grouping notifications
|
367
444
|
|
368
445
|
`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:
|
369
446
|
|
@@ -393,80 +470,164 @@ And you can render them in a view like this:
|
|
393
470
|
|
394
471
|
This presentation will be shown to target users as `Tom and other 7 people posted comments to your article "Let's use Ruby"`.
|
395
472
|
|
396
|
-
|
473
|
+
You can also use `%{group_member_count}` in i18n text as a field:
|
397
474
|
|
398
|
-
|
475
|
+
```yaml
|
476
|
+
notification:
|
477
|
+
user:
|
478
|
+
comment:
|
479
|
+
reply:
|
480
|
+
text: "<p>%{notifier_name} and %{group_member_count} people replied for your comments</p>"
|
481
|
+
mail_subject: 'New comment to your article'
|
482
|
+
```
|
399
483
|
|
400
|
-
|
484
|
+
Then, you will see `Tom and 7 people replied for your comments"`.
|
485
|
+
|
486
|
+
### Integration with Devise
|
487
|
+
|
488
|
+
`activity_notification` supports to integrate with devise authentication.
|
489
|
+
|
490
|
+
First, add notification routing as integrated with devise to `config/routes.rb` for the target:
|
401
491
|
|
402
492
|
```ruby
|
403
|
-
|
493
|
+
Rails.application.routes.draw do
|
494
|
+
devise_for :users
|
495
|
+
# Integrated with devise
|
496
|
+
notify_to :users, with_devise: :users
|
497
|
+
end
|
404
498
|
```
|
405
499
|
|
406
|
-
|
500
|
+
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'.
|
501
|
+
|
502
|
+
*Hint*: HTTP 403 Forbidden will be returned for unauthorized notifications.
|
503
|
+
|
504
|
+
You can also use different model from Devise resource as a target. When you will add this to `config/routes.rb`:
|
407
505
|
|
408
506
|
```ruby
|
409
|
-
|
410
|
-
|
507
|
+
Rails.application.routes.draw do
|
508
|
+
devise_for :users
|
509
|
+
# Integrated with devise for different model
|
510
|
+
notify_to :admins, with_devise: :users
|
511
|
+
end
|
411
512
|
```
|
412
513
|
|
413
|
-
|
514
|
+
and add `devise_resource` option to `acts_as_target` in the target model:
|
414
515
|
|
415
516
|
```ruby
|
416
|
-
class
|
417
|
-
|
517
|
+
class Admin < ActiveRecord::Base
|
518
|
+
belongs_to :user
|
519
|
+
acts_as_target devise_resource: :user
|
418
520
|
end
|
419
521
|
```
|
420
522
|
|
523
|
+
`activity_notification` will authenticate `:admins` notifications with devise authentication for `:users`.
|
524
|
+
In this example `activity_notification` will confirm the `user` who `admin` belongs to with authenticated user by devise.
|
525
|
+
|
526
|
+
|
527
|
+
## Testing
|
528
|
+
|
529
|
+
### Testing your application
|
530
|
+
|
531
|
+
First, you need to configure ActivityNotification as described above.
|
532
|
+
|
533
|
+
#### Testing notifications with RSpec
|
534
|
+
Prepare target and notifiable model instances to test generating notifications (e.g. `@user` and `@comment`).
|
535
|
+
Then, you can call notify API and test if notifications of the target are generated.
|
421
536
|
```ruby
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
537
|
+
# Prepare
|
538
|
+
@article_author = create(:user)
|
539
|
+
@comment = @article_author.articles.create.comments.create
|
540
|
+
expect(@article_author.notifications.unopened_only.count).to eq(0)
|
426
541
|
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
542
|
+
# Call notify API
|
543
|
+
@comment.notify :users
|
544
|
+
|
545
|
+
# Test generated notifications
|
546
|
+
expect(@article_author_user.notifications.unopened_only.count).to eq(1)
|
547
|
+
expect(@article_author_user.notifications.unopened_only.latest.notifiable).to eq(@comment)
|
432
548
|
```
|
433
549
|
|
434
|
-
####
|
550
|
+
#### Testing email notifications with RSpec
|
551
|
+
Prepare target and notifiable model instances to test sending notification email.
|
552
|
+
Then, you can call notify API and test if notification email is sent.
|
553
|
+
```ruby
|
554
|
+
# Prepare
|
555
|
+
@article_author = create(:user)
|
556
|
+
@comment = @article_author.articles.create.comments.create
|
557
|
+
expect(ActivityNotification::Mailer.deliveries.size).to eq(0)
|
435
558
|
|
436
|
-
|
559
|
+
# Call notify API and send email now
|
560
|
+
@comment.notify :users, send_later: false
|
437
561
|
|
438
|
-
|
439
|
-
|
440
|
-
|
562
|
+
# Test sent notification email
|
563
|
+
expect(ActivityNotification::Mailer.deliveries.size).to eq(1)
|
564
|
+
expect(ActivityNotification::Mailer.deliveries.first.to[0]).to eq(@article_author.email)
|
565
|
+
```
|
566
|
+
Note that notification email will be sent asynchronously without false as `send_later` option.
|
567
|
+
```ruby
|
568
|
+
# Prepare
|
569
|
+
include ActiveJob::TestHelper
|
570
|
+
@article_author = create(:user)
|
571
|
+
@comment = @article_author.articles.create.comments.create
|
572
|
+
expect(ActivityNotification::Mailer.deliveries.size).to eq(0)
|
573
|
+
|
574
|
+
# Call notify API and send email asynchronously as default
|
575
|
+
# Test sent notification email with ActiveJob queue
|
576
|
+
expect {
|
577
|
+
perform_enqueued_jobs do
|
578
|
+
@comment.notify :users
|
579
|
+
end
|
580
|
+
}.to change { ActivityNotification::Mailer.deliveries.size }.by(1)
|
581
|
+
expect(ActivityNotification::Mailer.deliveries.first.to[0]).to eq(@article_author.email)
|
582
|
+
```
|
441
583
|
|
442
|
-
|
584
|
+
### Testing gem alone
|
443
585
|
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
mail_subject: 'New comment to your article'
|
586
|
+
#### Testing with RSpec
|
587
|
+
Pull git repository and execute RSpec.
|
588
|
+
```console
|
589
|
+
$ git pull https://github.com/simukappu/activity_notification.git
|
590
|
+
$ cd activity_notification
|
591
|
+
$ bundle install —path vendor/bundle
|
592
|
+
$ bundle exec rspec
|
452
593
|
```
|
453
594
|
|
454
|
-
|
595
|
+
#### Dummy Rails application
|
596
|
+
Test module includes dummy Rails application. You can run the dummy application as common Rails application.
|
597
|
+
```console
|
598
|
+
$ cd spec/rails_app
|
599
|
+
$ bin/rake db:migrate
|
600
|
+
$ bin/rake db:seed
|
601
|
+
$ bin/rails server
|
602
|
+
```
|
603
|
+
Then, you can access <http://localhost:3000> for the dummy application.
|
455
604
|
|
456
|
-
Under construction
|
457
605
|
|
458
606
|
## Documentation
|
459
607
|
|
460
|
-
|
608
|
+
See [API Reference](http://www.rubydoc.info/github/simukappu/activity_notification/index) for more details.
|
609
|
+
|
610
|
+
RubyDoc.info does not support parsing methods in `included` and `class_methods` of `ActiveSupport::Concern` currently.
|
611
|
+
To read complete documents, please generate YARD documents on your local environment:
|
612
|
+
```console
|
613
|
+
$ git pull https://github.com/simukappu/activity_notification.git
|
614
|
+
$ cd activity_notification
|
615
|
+
$ bundle install —path vendor/bundle
|
616
|
+
$ bundle exec yard doc
|
617
|
+
$ bundle exec yard server
|
618
|
+
```
|
619
|
+
Then you can see the documents at <http://localhost:8808/docs/index>.
|
461
620
|
|
462
621
|
## Common examples
|
463
622
|
|
464
623
|
Under construction
|
465
624
|
|
625
|
+
|
466
626
|
## Help
|
467
627
|
|
468
628
|
Under construction
|
469
629
|
|
630
|
+
|
470
631
|
## License
|
471
632
|
|
472
633
|
`activity_notification` project rocks and uses [MIT License](MIT-LICENSE).
|