activity_notification 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +11 -1
- data/README.md +63 -28
- data/activity_notification.gemspec +4 -2
- data/app/controllers/activity_notification/notifications_controller.rb +1 -1
- data/app/controllers/activity_notification/notifications_with_devise_controller.rb +10 -10
- data/app/views/activity_notification/notifications/default/_index.html.erb +9 -4
- data/lib/activity_notification.rb +7 -6
- data/lib/activity_notification/apis/notification_api.rb +14 -15
- data/lib/activity_notification/common.rb +15 -7
- data/lib/activity_notification/config.rb +2 -0
- data/lib/activity_notification/helpers/view_helpers.rb +5 -4
- data/lib/activity_notification/mailers/helpers.rb +9 -9
- data/lib/activity_notification/models.rb +16 -0
- data/lib/activity_notification/models/{notifiable.rb → concerns/notifiable.rb} +15 -10
- data/lib/activity_notification/models/{notifier.rb → concerns/notifier.rb} +6 -0
- data/lib/activity_notification/models/{target.rb → concerns/target.rb} +34 -17
- data/lib/activity_notification/renderable.rb +2 -1
- data/lib/activity_notification/roles/acts_as_notifiable.rb +38 -23
- data/lib/activity_notification/roles/acts_as_notifier.rb +11 -0
- data/lib/activity_notification/roles/acts_as_target.rb +9 -18
- data/lib/activity_notification/version.rb +1 -1
- data/lib/generators/activity_notification/{migration → active_record}/migration_generator.rb +5 -4
- data/lib/generators/activity_notification/controllers_generator.rb +1 -1
- data/lib/generators/activity_notification/install_generator.rb +3 -6
- data/lib/generators/activity_notification/{notification → models}/notification_generator.rb +5 -4
- data/lib/generators/activity_notification/views_generator.rb +20 -22
- data/lib/generators/templates/active_record/migration.rb +1 -1
- data/lib/generators/templates/activity_notification.rb +13 -3
- data/{config → lib/generators/templates}/locales/en.yml +0 -0
- data/lib/generators/templates/notification/notification.rb +4 -1
- data/spec/concerns/{notification_api_spec.rb → apis/notification_api_spec.rb} +169 -45
- data/spec/concerns/common_spec.rb +150 -0
- data/spec/concerns/models/notifiable_spec.rb +435 -0
- data/spec/concerns/models/notifier_spec.rb +23 -0
- data/spec/concerns/models/target_spec.rb +579 -0
- data/spec/concerns/renderable_spec.rb +110 -0
- data/spec/controllers/notifications_controller_shared_examples.rb +457 -0
- data/spec/controllers/notifications_controller_spec.rb +12 -0
- data/spec/controllers/notifications_with_devise_controller_spec.rb +81 -0
- data/spec/factories/admins.rb +5 -0
- data/spec/factories/articles.rb +1 -1
- data/spec/factories/comments.rb +1 -1
- data/spec/factories/dummy/dummy_notifiable.rb +4 -0
- data/spec/factories/dummy/dummy_notifier.rb +4 -0
- data/spec/factories/dummy/dummy_target.rb +4 -0
- data/spec/factories/notifications.rb +1 -1
- data/spec/factories/users.rb +7 -1
- data/spec/generators/active_record/migration_generator_spec.rb +41 -0
- data/spec/generators/controllers_generator_spec.rb +62 -0
- data/spec/generators/install_generator_spec.rb +43 -0
- data/spec/generators/models/notification_generator_spec.rb +41 -0
- data/spec/generators/views_generator_spec.rb +111 -0
- data/spec/helpers/polymorphic_helpers_spec.rb +89 -0
- data/spec/helpers/view_helpers_spec.rb +258 -0
- data/spec/mailers/mailer_spec.rb +98 -0
- data/spec/models/dummy/dummy_notifiable_spec.rb +6 -0
- data/spec/models/dummy/dummy_notifier_spec.rb +6 -0
- data/spec/models/dummy/dummy_target_spec.rb +6 -0
- data/spec/models/notification_spec.rb +5 -4
- data/spec/rails_app/app/assets/javascripts/application.js +2 -0
- data/spec/rails_app/app/controllers/articles_controller.rb +62 -0
- data/spec/rails_app/app/controllers/comments_controller.rb +34 -0
- data/spec/rails_app/app/models/admin.rb +8 -0
- data/spec/rails_app/app/models/article.rb +6 -6
- data/spec/rails_app/app/models/comment.rb +2 -2
- data/spec/rails_app/app/models/dummy/dummy_base.rb +2 -0
- data/spec/rails_app/app/models/dummy/dummy_notifiable.rb +4 -0
- data/spec/rails_app/app/models/dummy/dummy_notifier.rb +4 -0
- data/spec/rails_app/app/models/dummy/dummy_target.rb +4 -0
- data/spec/rails_app/app/models/user.rb +5 -5
- data/spec/rails_app/app/views/activity_notification/notifications/default/custom/_path_test.html.erb +1 -0
- data/spec/rails_app/app/views/activity_notification/notifications/default/custom/_test.html.erb +1 -0
- data/spec/rails_app/app/views/activity_notification/notifications/users/_custom_index.html.erb +1 -0
- data/spec/rails_app/app/views/activity_notification/notifications/users/custom/_test.html.erb +1 -0
- data/spec/rails_app/app/views/articles/_form.html.erb +20 -0
- data/spec/rails_app/app/views/articles/edit.html.erb +6 -0
- data/spec/rails_app/app/views/articles/index.html.erb +67 -0
- data/spec/rails_app/app/views/articles/new.html.erb +5 -0
- data/spec/rails_app/app/views/articles/show.html.erb +38 -0
- data/spec/rails_app/app/views/layouts/_header.html.erb +8 -0
- data/spec/rails_app/app/views/layouts/application.html.erb +3 -4
- data/spec/rails_app/config/initializers/activity_notification.rb +13 -3
- data/spec/rails_app/config/initializers/devise.rb +274 -274
- data/spec/rails_app/config/locales/activity_notification.en.yml +20 -0
- data/spec/rails_app/config/locales/devise.en.yml +62 -0
- data/spec/rails_app/config/routes.rb +6 -2
- data/spec/rails_app/db/migrate/20160715050433_create_test_tables.rb +6 -2
- data/spec/rails_app/db/schema.rb +8 -0
- data/spec/rails_app/db/seeds.rb +43 -0
- data/spec/roles/acts_as_notifiable_spec.rb +32 -0
- data/spec/roles/acts_as_notifier_spec.rb +17 -0
- data/spec/roles/acts_as_target_spec.rb +40 -0
- data/spec/spec_helper.rb +18 -14
- metadata +136 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a5bab367f956293fdbc2bab7e9b2ad767d2c582
|
4
|
+
data.tar.gz: bd432e9c7d6800f63d79f681584db7ca0bf619af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7d70c2d58b4ff931e361db55bbd34194cd365e28e7fed65b4bab413de5cae2670f8cc56e8beb24a89af88afbaec34f3143884e9717e77ec019404a63f54b332b
|
7
|
+
data.tar.gz: b4b5f443a364f3d0e90d3f67cae2b5d0befe222bca969e3085ebcc5ec91d8840ef4df6b6b94b6638c31c5d282b38f9f8228aa2729ddd8bf18bfbbe15cd9ae4cc
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
activity_notification (0.0.
|
4
|
+
activity_notification (0.0.9)
|
5
5
|
activerecord (>= 3.0)
|
6
6
|
i18n (>= 0.5.0)
|
7
7
|
rails (~> 4.2)
|
@@ -44,6 +44,10 @@ GEM
|
|
44
44
|
minitest (~> 5.1)
|
45
45
|
thread_safe (~> 0.3, >= 0.3.4)
|
46
46
|
tzinfo (~> 1.1)
|
47
|
+
ammeter (1.1.3)
|
48
|
+
activesupport (>= 3.0)
|
49
|
+
railties (>= 3.0)
|
50
|
+
rspec-rails (>= 2.2)
|
47
51
|
arel (6.0.3)
|
48
52
|
bcrypt (3.1.11)
|
49
53
|
builder (3.2.2)
|
@@ -71,6 +75,10 @@ GEM
|
|
71
75
|
globalid (0.3.6)
|
72
76
|
activesupport (>= 4.1.0)
|
73
77
|
i18n (0.7.0)
|
78
|
+
jquery-rails (4.1.1)
|
79
|
+
rails-dom-testing (>= 1, < 3)
|
80
|
+
railties (>= 4.2.0)
|
81
|
+
thor (>= 0.14, < 2.0)
|
74
82
|
json (1.8.3)
|
75
83
|
loofah (2.0.3)
|
76
84
|
nokogiri (>= 1.5.9)
|
@@ -162,9 +170,11 @@ PLATFORMS
|
|
162
170
|
|
163
171
|
DEPENDENCIES
|
164
172
|
activity_notification!
|
173
|
+
ammeter (~> 1.1.3)
|
165
174
|
coveralls
|
166
175
|
devise (~> 4.2.0)
|
167
176
|
factory_girl_rails (~> 4.7.0)
|
177
|
+
jquery-rails (~> 4.1.1)
|
168
178
|
rspec-rails (~> 3.5.1)
|
169
179
|
simplecov (~> 0.12.0)
|
170
180
|
sqlite3 (~> 1.3.11)
|
data/README.md
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
[![Gem Version](https://badge.fury.io/rb/activity_notification.svg)](https://badge.fury.io/rb/activity_notification)
|
7
7
|
[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](MIT-LICENSE)
|
8
8
|
|
9
|
-
`activity_notification` provides integrated user activity notifications for Rails. You can easily use it to configure multiple notification targets and make activity notifications with notifiable models, like adding comments, responding etc.
|
9
|
+
`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
10
|
|
11
11
|
Currently, `activity_notification` is only supported with ActiveRecord ORM in Rails 4.
|
12
12
|
|
@@ -89,51 +89,49 @@ $ rake db:migrate
|
|
89
89
|
#### Configuring target model
|
90
90
|
|
91
91
|
Configure your target model (e.g. app/models/user.rb).
|
92
|
-
Add
|
92
|
+
Add `acts_as_notification_target` configuration to your target model to get notifications.
|
93
93
|
|
94
94
|
```ruby
|
95
95
|
class User < ActiveRecord::Base
|
96
|
-
include ActivityNotification::Target
|
97
96
|
# Example using confirmed_at of Device field
|
98
97
|
# to decide whether activity_notification sends notification email to this user
|
99
|
-
|
98
|
+
acts_as_notification_target email: :email, email_allowed: :confirmed_at
|
100
99
|
end
|
101
100
|
```
|
102
101
|
|
102
|
+
*Note*: `acts_as_target` is an alias for `acts_as_notification_target` and does the same.
|
103
|
+
|
103
104
|
You can override several methods in your target model (e.g. `notification_index` or `notification_email_allowed?`).
|
104
105
|
|
105
106
|
#### Configuring notifiable model
|
106
107
|
|
107
108
|
Configure your notifiable model (e.g. app/models/comment.rb).
|
108
|
-
Add
|
109
|
+
Add `acts_as_notifiable` configuration to your notifiable model representing activity to notify.
|
109
110
|
You have to define notification targets for all notifications from this notifiable model by `:targets` option. Other configurations are options.
|
110
111
|
|
111
112
|
```ruby
|
113
|
+
class Article < ActiveRecord::Base
|
114
|
+
belongs_to :user
|
115
|
+
has_many :comments, dependent: :delete_all
|
116
|
+
has_many :commented_users, through: :comments, source: :user
|
117
|
+
end
|
118
|
+
|
112
119
|
class Comment < ActiveRecord::Base
|
113
120
|
belongs_to :article
|
114
121
|
belongs_to :user
|
115
122
|
|
116
|
-
|
117
|
-
# Example that ActivityNotification::Notifiable is configured with custom methods in your model as symbol
|
123
|
+
# Example that ActivityNotification::Notifiable is configured with custom methods in your model as lambda or symbol
|
118
124
|
acts_as_notifiable :users,
|
119
|
-
|
125
|
+
# Notify to users who commented to the same article and article auther, except comment owner self
|
126
|
+
targets: ->(comment, key) { (comment.article.commented_users.to_a - [comment.user] + [comment.article.user]).uniq },
|
120
127
|
group: :article,
|
121
128
|
notifier: :user,
|
122
|
-
email_allowed:
|
129
|
+
email_allowed: true,
|
123
130
|
notifiable_path: :custom_notifiable_path
|
124
131
|
|
125
|
-
def custom_notification_users(key)
|
126
|
-
User.where(id: self.article.comments.pluck(:user_id))
|
127
|
-
end
|
128
|
-
|
129
|
-
def custom_notification_email_to_users_allowed?(user, key)
|
130
|
-
true
|
131
|
-
end
|
132
|
-
|
133
132
|
def custom_notifiable_path
|
134
133
|
article_path(article)
|
135
134
|
end
|
136
|
-
|
137
135
|
end
|
138
136
|
```
|
139
137
|
|
@@ -226,13 +224,13 @@ You can trigger notifications by setting all your required parameters and trigge
|
|
226
224
|
on the notifiable model, like this:
|
227
225
|
|
228
226
|
```ruby
|
229
|
-
@comment.notify
|
227
|
+
@comment.notify :users key: 'comment.reply', group: @comment.article
|
230
228
|
```
|
231
229
|
|
232
230
|
Or, you can call public API as `ActivityNotification::Notification.notify`
|
233
231
|
|
234
232
|
```ruby
|
235
|
-
ActivityNotification::Notification.notify
|
233
|
+
ActivityNotification::Notification.notify :users, @comment, group: @comment.article
|
236
234
|
```
|
237
235
|
|
238
236
|
### Displaying notifications
|
@@ -342,17 +340,25 @@ If a view file does not exist then ActionView::MisingTemplate will be raised. If
|
|
342
340
|
|
343
341
|
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`.
|
344
342
|
|
345
|
-
Translations should be put in your locale `.yml` files. To render pure strings from I18n example structure:
|
343
|
+
Translations should be put in your locale `.yml` files as `text` field. To render pure strings from I18n example structure:
|
346
344
|
|
347
345
|
```yaml
|
348
346
|
notification:
|
349
347
|
user:
|
350
348
|
article:
|
351
|
-
create:
|
352
|
-
|
353
|
-
destroy:
|
354
|
-
|
355
|
-
|
349
|
+
create:
|
350
|
+
text: 'Article has been created'
|
351
|
+
destroy:
|
352
|
+
text: 'Some user removed an article!'
|
353
|
+
comment:
|
354
|
+
post:
|
355
|
+
text: "<p>%{notifier_name} posted comments to your article %{article_title}</p>"
|
356
|
+
reply:
|
357
|
+
text: "<p>%{notifier_name} and %{group_member_count} people replied for your comments</p>"
|
358
|
+
admin:
|
359
|
+
article:
|
360
|
+
post:
|
361
|
+
text: '[Admin] Article has been created'
|
356
362
|
```
|
357
363
|
|
358
364
|
This structure is valid for notifications with keys `"notification.article.comment.replied"` or `"article.comment.replied"`. 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.
|
@@ -362,7 +368,7 @@ This structure is valid for notifications with keys `"notification.article.comme
|
|
362
368
|
`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:
|
363
369
|
|
364
370
|
```ruby
|
365
|
-
@comment.notify
|
371
|
+
@comment.notify :users key: 'comment.reply', group: @comment.article
|
366
372
|
```
|
367
373
|
|
368
374
|
You can use `group_owners_only` scope to filter owner notifications representing each group:
|
@@ -397,6 +403,34 @@ First, you need to set up the default URL options for the `activity_notification
|
|
397
403
|
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
|
398
404
|
```
|
399
405
|
|
406
|
+
Email notification is disabled as default. You can configure to enable email notification in initializer `activity_notification.rb`.
|
407
|
+
|
408
|
+
```ruby
|
409
|
+
config.email_enabled = true
|
410
|
+
config.mailer_sender = 'your_notification_sender@example.com'
|
411
|
+
```
|
412
|
+
|
413
|
+
You can also configure them for each model by acts_as roles like these.
|
414
|
+
|
415
|
+
```ruby
|
416
|
+
class User < ActiveRecord::Base
|
417
|
+
acts_as_notification_target email: :email, email_allowed: true
|
418
|
+
end
|
419
|
+
```
|
420
|
+
|
421
|
+
```ruby
|
422
|
+
class Comment < ActiveRecord::Base
|
423
|
+
acts_as_notifiable :users,
|
424
|
+
targets: :custom_notification_users,
|
425
|
+
email_allowed: :custom_notification_email_allowed?
|
426
|
+
|
427
|
+
def custom_notification_email_allowed?(target, key)
|
428
|
+
# You can add enabled conditions from target and key
|
429
|
+
true
|
430
|
+
end
|
431
|
+
end
|
432
|
+
```
|
433
|
+
|
400
434
|
#### Email templates
|
401
435
|
|
402
436
|
`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.article.comment.replied"` and target_type `users`, the gem will look for a partial in `app/views/activity_notification/mailer/users/article/comment/_replied.html.(|erb|haml|slim|something_else)`.
|
@@ -412,7 +446,8 @@ notification:
|
|
412
446
|
user:
|
413
447
|
article:
|
414
448
|
comment:
|
415
|
-
|
449
|
+
reply:
|
450
|
+
text "<p>%{notifier_name} and %{group_member_count} people replied for your comments</p>"
|
416
451
|
mail_subject: 'New comment to your article'
|
417
452
|
```
|
418
453
|
|
@@ -11,8 +11,8 @@ Gem::Specification.new do |s|
|
|
11
11
|
s.authors = ["Shota Yamazaki"]
|
12
12
|
s.email = ["shota.yamazaki.8@gmail.com"]
|
13
13
|
s.homepage = "https://github.com/simukappu/activity_notification"
|
14
|
-
s.summary = "Integrated user activity notifications for Rails"
|
15
|
-
s.description = "Integrated user activity notifications for Rails. Provides functions to configure multiple notification targets and make activity notifications with notifiable models, like adding comments, responding etc."
|
14
|
+
s.summary = "Integrated user activity notifications for Ruby on Rails"
|
15
|
+
s.description = "Integrated user activity notifications for Ruby on Rails. Provides functions to configure multiple notification targets and make activity notifications with notifiable models, like adding comments, responding etc."
|
16
16
|
s.license = "MIT"
|
17
17
|
|
18
18
|
s.files = `git ls-files`.split("\n")
|
@@ -26,8 +26,10 @@ Gem::Specification.new do |s|
|
|
26
26
|
s.add_dependency 'activerecord', '>= 3.0'
|
27
27
|
|
28
28
|
s.add_development_dependency 'sqlite3', '~> 1.3.11'
|
29
|
+
s.add_development_dependency "jquery-rails", '~> 4.1.1'
|
29
30
|
s.add_development_dependency "rspec-rails", '~> 3.5.1'
|
30
31
|
s.add_development_dependency "factory_girl_rails", '~> 4.7.0'
|
31
32
|
s.add_development_dependency 'simplecov', '~> 0.12.0'
|
33
|
+
s.add_development_dependency 'ammeter', '~> 1.1.3'
|
32
34
|
s.add_development_dependency "devise", '~> 4.2.0'
|
33
35
|
end
|
@@ -47,7 +47,7 @@ module ActivityNotification
|
|
47
47
|
# GET /:target_type/:target_id/notifcations/:id/move
|
48
48
|
def move
|
49
49
|
@notification.open! if params[:open].to_s.to_boolean(false)
|
50
|
-
redirect_to @notification.
|
50
|
+
redirect_to @notification.notifiable_path
|
51
51
|
end
|
52
52
|
|
53
53
|
# No action routing
|
@@ -6,22 +6,22 @@ module ActivityNotification
|
|
6
6
|
protected
|
7
7
|
|
8
8
|
def authenticate_devise_resource!
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
if params[:devise_type].present?
|
10
|
+
authenticate_method_name = "authenticate_#{params[:devise_type].to_resource_name}!"
|
11
|
+
if respond_to?(authenticate_method_name)
|
12
|
+
send(authenticate_method_name)
|
13
|
+
else
|
14
|
+
render text: "403 Forbidden: Unauthenticated", status: 403
|
15
|
+
end
|
12
16
|
else
|
13
|
-
render text: "
|
17
|
+
render text: "400 Bad Request: Missing parameter", status: 400
|
14
18
|
end
|
15
19
|
end
|
16
20
|
|
17
21
|
def authenticate_target!
|
18
22
|
current_resource_method_name = "current_#{params[:devise_type].to_resource_name}"
|
19
|
-
|
20
|
-
|
21
|
-
render text: "403 Forbidden: Unauthorized target", status: 403
|
22
|
-
end
|
23
|
-
else
|
24
|
-
render text: "403 Forbidden: Unauthenticated", status: 403
|
23
|
+
unless @target.authenticated_with_devise?(send(current_resource_method_name))
|
24
|
+
render text: "403 Forbidden: Unauthorized target", status: 403
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
@@ -1,9 +1,14 @@
|
|
1
1
|
<div>
|
2
|
-
<%= link_to open_all_notifications_path_for(@target), method: :post, remote: true
|
2
|
+
<%= link_to open_all_notifications_path_for(@target), method: :post, remote: true,
|
3
|
+
class: "dropdown-toggle", data: {toggle: "dropdown"} do %>
|
3
4
|
<div id="notification_count"><%= @target.unopened_notification_count %></div>
|
4
5
|
<% end %>
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
7
|
+
<!-- Make this dropdown -->
|
8
|
+
<ul class="dropdown-menu">
|
9
|
+
<div id="notification_index">
|
10
|
+
<p>Notifications</p>
|
11
|
+
<%= yield :notification_index %>
|
12
|
+
</div>
|
13
|
+
</ul>
|
9
14
|
</div>
|
@@ -6,13 +6,12 @@ module ActivityNotification
|
|
6
6
|
extend ActiveSupport::Concern
|
7
7
|
extend ActiveSupport::Autoload
|
8
8
|
|
9
|
+
autoload :NotificationApi, 'activity_notification/apis/notification_api'
|
9
10
|
autoload :Notification, 'activity_notification/models/notification'
|
10
|
-
autoload :Target, 'activity_notification/models/target'
|
11
|
-
autoload :Notifiable, 'activity_notification/models/notifiable'
|
12
|
-
autoload :
|
13
|
-
autoload :ActsAsNotifiable, 'activity_notification/roles/acts_as_notifiable'
|
11
|
+
autoload :Target, 'activity_notification/models/concerns/target'
|
12
|
+
autoload :Notifiable, 'activity_notification/models/concerns/notifiable'
|
13
|
+
autoload :Notifier, 'activity_notification/models/concerns/notifier'
|
14
14
|
autoload :StoreController, 'activity_notification/controllers/store_controller'
|
15
|
-
autoload :NotificationApi, 'activity_notification/apis/notification_api'
|
16
15
|
autoload :Common
|
17
16
|
autoload :Config
|
18
17
|
autoload :Renderable
|
@@ -47,6 +46,8 @@ end
|
|
47
46
|
require 'activity_notification/helpers/polymorphic_helpers'
|
48
47
|
require 'activity_notification/helpers/view_helpers'
|
49
48
|
|
49
|
+
# Load role for models
|
50
|
+
require 'activity_notification/models'
|
51
|
+
|
50
52
|
# Define Rails::Engine
|
51
53
|
require 'activity_notification/rails'
|
52
|
-
|
@@ -17,9 +17,7 @@ module ActivityNotification
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def notify_all(targets, notifiable, options = {})
|
20
|
-
targets.
|
21
|
-
notify_to(target, notifiable, options)
|
22
|
-
end
|
20
|
+
Array(targets).map { |target| notify_to(target, notifiable, options) }
|
23
21
|
end
|
24
22
|
|
25
23
|
def notify_to(target, notifiable, options = {})
|
@@ -29,19 +27,20 @@ module ActivityNotification
|
|
29
27
|
notification = store_notification(target, notifiable, options)
|
30
28
|
# Send notification email
|
31
29
|
notification.send_notification_email(send_later) if send_email
|
30
|
+
# Return created notification
|
32
31
|
notification
|
33
32
|
end
|
34
33
|
|
35
34
|
# Open all notifications of specified target
|
36
35
|
def open_all_of(target, opened_at = nil)
|
37
36
|
opened_at = DateTime.now if opened_at.blank?
|
38
|
-
|
37
|
+
where(target: target, opened_at: nil).update_all(opened_at: opened_at)
|
39
38
|
end
|
40
39
|
|
41
40
|
#TODO description
|
42
41
|
# Call from controllers or views to avoid N+1
|
43
42
|
def group_member_exists?(notifications)
|
44
|
-
|
43
|
+
notifications.present? && where(group_owner_id: notifications.pluck(:id)).exists?
|
45
44
|
end
|
46
45
|
|
47
46
|
def available_options
|
@@ -61,8 +60,8 @@ module ActivityNotification
|
|
61
60
|
|
62
61
|
# Bundle notification group by target, notifiable_type, group and key
|
63
62
|
# Defferent notifiable.id can be made in a same group
|
64
|
-
group_owner =
|
65
|
-
|
63
|
+
group_owner = where(target: target, notifiable_type: notifiable.to_class_name, key: key, group: group)
|
64
|
+
.where(group_owner_id: nil, opened_at: nil).earliest
|
66
65
|
if group.present? and group_owner.present?
|
67
66
|
create(target: target, notifiable: notifiable, key: key, group: group, group_owner: group_owner, parameters: parameters, notifier: notifier)
|
68
67
|
else
|
@@ -85,7 +84,7 @@ module ActivityNotification
|
|
85
84
|
def open!(opened_at = nil)
|
86
85
|
opened_at = DateTime.now if opened_at.blank?
|
87
86
|
update(opened_at: opened_at)
|
88
|
-
group_members.update_all(opened_at: opened_at)
|
87
|
+
group_members.update_all(opened_at: opened_at) + 1
|
89
88
|
end
|
90
89
|
|
91
90
|
def unopened?
|
@@ -117,7 +116,7 @@ module ActivityNotification
|
|
117
116
|
notification.unopened_group_member_count
|
118
117
|
end
|
119
118
|
|
120
|
-
def
|
119
|
+
def notifiable_path
|
121
120
|
notifiable.notifiable_path(target_type)
|
122
121
|
end
|
123
122
|
|
@@ -128,18 +127,18 @@ module ActivityNotification
|
|
128
127
|
def unopened_group_member_count
|
129
128
|
# Cache group-by query result to avoid N+1 call
|
130
129
|
unopened_group_member_counts = target.notifications
|
131
|
-
|
132
|
-
|
133
|
-
|
130
|
+
.unopened_index_group_members_only
|
131
|
+
.group(:group_owner_id)
|
132
|
+
.count
|
134
133
|
unopened_group_member_counts[id] || 0
|
135
134
|
end
|
136
135
|
|
137
136
|
def opened_group_member_count(limit = ActivityNotification.config.opened_limit)
|
138
137
|
# Cache group-by query result to avoid N+1 call
|
139
138
|
opened_group_member_counts = target.notifications
|
140
|
-
|
141
|
-
|
142
|
-
|
139
|
+
.opened_index_group_members_only(limit)
|
140
|
+
.group(:group_owner_id)
|
141
|
+
.count
|
143
142
|
opened_group_member_counts[id] || 0
|
144
143
|
end
|
145
144
|
|
@@ -8,15 +8,23 @@ module ActivityNotification
|
|
8
8
|
case thing
|
9
9
|
when Symbol
|
10
10
|
begin
|
11
|
-
context.__send__(thing, *args)
|
12
|
-
rescue ArgumentError
|
13
|
-
|
11
|
+
context.__send__(thing, ActivityNotification.get_controller, *args)
|
12
|
+
rescue ArgumentError
|
13
|
+
begin
|
14
|
+
context.__send__(thing, ActivityNotification.get_controller)
|
15
|
+
rescue ArgumentError
|
16
|
+
context.__send__(thing)
|
17
|
+
end
|
14
18
|
end
|
15
19
|
when Proc
|
16
20
|
begin
|
17
21
|
thing.call(ActivityNotification.get_controller, context, *args)
|
18
|
-
rescue ArgumentError
|
19
|
-
|
22
|
+
rescue ArgumentError
|
23
|
+
begin
|
24
|
+
thing.call(ActivityNotification.get_controller, context)
|
25
|
+
rescue ArgumentError
|
26
|
+
thing.call(context)
|
27
|
+
end
|
20
28
|
end
|
21
29
|
when Hash
|
22
30
|
thing.dup.tap do |hash|
|
@@ -40,13 +48,13 @@ module ActivityNotification
|
|
40
48
|
when Symbol
|
41
49
|
begin
|
42
50
|
__send__(thing, *args)
|
43
|
-
rescue ArgumentError
|
51
|
+
rescue ArgumentError
|
44
52
|
__send__(thing)
|
45
53
|
end
|
46
54
|
when Proc
|
47
55
|
begin
|
48
56
|
thing.call(self, *args)
|
49
|
-
rescue ArgumentError
|
57
|
+
rescue ArgumentError
|
50
58
|
thing.call(self)
|
51
59
|
end
|
52
60
|
when Hash
|