notifications 0.4.3 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/README.md +62 -16
- data/Rakefile +1 -1
- data/app/controllers/notifications/notifications_controller.rb +1 -1
- data/db/migrate/20160328045436_create_notifications.rb +9 -7
- data/lib/generators/notifications/controllers_generator.rb +2 -2
- data/lib/generators/notifications/i18n_generator.rb +2 -2
- data/lib/generators/notifications/install_generator.rb +1 -1
- data/lib/generators/notifications/views_generator.rb +1 -1
- data/lib/notifications/model.rb +7 -1
- data/lib/notifications/version.rb +3 -1
- metadata +9 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9cf008411221f8643f6272a4f492d6567f7e4c2e0391ee64aab731a3895c7e74
|
4
|
+
data.tar.gz: 75455d8f4aafd04a1d2ec0b57fd4e50e61518b54a8e27667d8e694be5210747d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1dbee55f530440d4c1ed3000857ffdc4abcbb25825c06164b658fa67b7f19e29a872fc851cfa64b7477db7da491a4f25ab6e5cda6bf461942251220f7c1c687b
|
7
|
+
data.tar.gz: 1feea3486d23c7e3e6598cdf012cd7f947cc017189b2258503c07236b8d1f71854cff20645d0e9df94bd6c548df8b6d4955d7e012407baba3eeafb6c6a842f7e
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# Notifications
|
2
2
|
|
3
|
-
|
3
|
+
Mountable notifications for any Rails applications.
|
4
4
|
|
5
|
-
[](https://badge.fury.io/rb/notifications) [](https://travis-ci.org/rails-engine/notifications) [](https://badge.fury.io/rb/notifications) [](https://travis-ci.org/rails-engine/notifications) [](https://codecov.io/github/rails-engine/notifications?branch=master)
|
6
6
|
|
7
7
|
## Example:
|
8
8
|
|
@@ -11,19 +11,21 @@ Rails mountable Notification for any applications.
|
|
11
11
|
## Installation
|
12
12
|
|
13
13
|
```ruby
|
14
|
-
# Gemfile
|
15
|
-
gem 'notifications'
|
14
|
+
# Gemfile Rails ~> 5
|
15
|
+
gem 'notifications', '~> 0.6.0'
|
16
|
+
# Gemfile for Rails ~> 4.2
|
17
|
+
gem 'notifications', '~> 0.5.0'
|
16
18
|
```
|
17
19
|
|
18
20
|
And then run `bundle install`.
|
19
21
|
|
20
|
-
|
22
|
+
You now have a notifications generator in your Rails application:
|
21
23
|
|
22
24
|
```bash
|
23
25
|
$ rails g notifications:install
|
24
26
|
```
|
25
27
|
|
26
|
-
|
28
|
+
You can generate views, controllers if you need to customize them:
|
27
29
|
|
28
30
|
```bash
|
29
31
|
$ rails g notifications:views
|
@@ -43,7 +45,7 @@ end
|
|
43
45
|
|
44
46
|
class Comment
|
45
47
|
belongs_to :post
|
46
|
-
|
48
|
+
belongs_to :user
|
47
49
|
|
48
50
|
after_commit :create_notifications, on: [:create]
|
49
51
|
def create_notifications
|
@@ -56,15 +58,51 @@ class Comment
|
|
56
58
|
end
|
57
59
|
```
|
58
60
|
|
59
|
-
Get
|
61
|
+
Get unread notifications count for a user:
|
60
62
|
|
61
63
|
```rb
|
62
|
-
|
64
|
+
# unread count
|
65
|
+
unread_count = Notification.unread_count(current_user)
|
66
|
+
|
67
|
+
# read count
|
68
|
+
read_count = Notification.read_count(current_user)
|
69
|
+
|
70
|
+
```
|
71
|
+
|
72
|
+
```rb initialize/**.rb
|
73
|
+
# for non-user class
|
74
|
+
Notifications.config.user_class = 'Member'
|
75
|
+
|
76
|
+
#or change
|
77
|
+
|
78
|
+
Notifications.configure do
|
79
|
+
# Class name of you User model, default: 'User'
|
80
|
+
self.user_class = 'User'
|
81
|
+
|
82
|
+
# Method of user name in User model, default: 'name'
|
83
|
+
# self.user_name_method = 'name'
|
84
|
+
|
85
|
+
# Method of user avatar in User model, default: nil
|
86
|
+
# self.user_avatar_url_method = nil
|
87
|
+
|
88
|
+
# Method name of user profile page path, in User model, default: nil
|
89
|
+
# self.user_profile_url_method = 'profile_url'
|
90
|
+
|
91
|
+
# authenticate_user method in your Controller, default: nil
|
92
|
+
# If you use Devise, authenticate_user! is correct
|
93
|
+
# self.authenticate_user_method = 'authenticate_user!'
|
94
|
+
|
95
|
+
# current_user method name in your Controller, default: 'current_user'
|
96
|
+
# If you use Devise, current_user is correct
|
97
|
+
# self.current_user_method = 'current_user'
|
98
|
+
end
|
99
|
+
|
100
|
+
|
63
101
|
```
|
64
102
|
|
65
103
|
### Write your custom Notification partial view for notify_types:
|
66
104
|
|
67
|
-
If you create a notify_type, you need add a partial view in `app/views/notifications/` path, for example:
|
105
|
+
If you create a notify_type, you need to add a partial view in `app/views/notifications/` path, for example:
|
68
106
|
|
69
107
|
```rb
|
70
108
|
# There have two notify_type
|
@@ -72,7 +110,7 @@ Notification.create(notify_type: 'follow' ....)
|
|
72
110
|
Notification.create(notify_type: 'mention', target: @reply, second_target: @topic, ....)
|
73
111
|
```
|
74
112
|
|
75
|
-
|
113
|
+
Your app must have:
|
76
114
|
|
77
115
|
- app/views/notifications/_follow.html.erb
|
78
116
|
- app/views/notifications/_mention.html.erb
|
@@ -80,28 +118,36 @@ You app must be have:
|
|
80
118
|
```erb
|
81
119
|
# app/views/notifications/_follow.html.erb
|
82
120
|
<div class="media-heading">
|
83
|
-
<%= link_to notification.actor.title, notification.actor %> just followed you.
|
121
|
+
<%= link_to notification.actor.title, main_app.user_path(notification.actor) %> just followed you.
|
84
122
|
</div>
|
85
123
|
```
|
86
124
|
|
87
125
|
```erb
|
88
126
|
# app/views/notifications/_mention.html.erb
|
89
127
|
<div class="media-heading">
|
90
|
-
<%= link_to notification.actor.title, notification.actor %> has
|
91
|
-
<%= link_to notification.second_target.title, topic_path(notification.second_target) %>
|
128
|
+
<%= link_to notification.actor.title, main_app.user_path(notification.actor) %> has mentioned you in
|
129
|
+
<%= link_to notification.second_target.title, main_app.topic_path(notification.second_target) %>
|
92
130
|
</div>
|
93
131
|
<div class="media-content">
|
94
132
|
<%= notification.target.body %>
|
95
133
|
</div>
|
96
134
|
```
|
97
135
|
|
136
|
+
> NOTE: When you want use Rails route path name in notification views, you must use [main_app](http://api.rubyonrails.org/classes/Rails/Engine.html#class-Rails::Engine-label-Using+Engine-27s+routes+outside+Engine) prefix. etc: `main_app.user_path(user)`
|
137
|
+
|
98
138
|
### About Notification template N+1 performance
|
99
139
|
|
100
|
-
|
140
|
+
It is recommended that you use [second_level_cache](https://github.com/hooopo/second_level_cache) for solving N+1 performance issues.
|
101
141
|
|
102
142
|
## Contributing
|
103
143
|
|
104
|
-
|
144
|
+
Testing for multiple Rails versions:
|
145
|
+
|
146
|
+
```bash
|
147
|
+
make test_51
|
148
|
+
# or test all
|
149
|
+
make test
|
150
|
+
```
|
105
151
|
|
106
152
|
## Site Used
|
107
153
|
|
data/Rakefile
CHANGED
@@ -4,7 +4,7 @@ rescue LoadError
|
|
4
4
|
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
5
|
end
|
6
6
|
|
7
|
-
APP_RAKEFILE = File.expand_path(
|
7
|
+
APP_RAKEFILE = File.expand_path('test/dummy/Rakefile', __dir__)
|
8
8
|
|
9
9
|
load 'rails/tasks/engine.rake'
|
10
10
|
load 'rails/tasks/statistics.rake'
|
@@ -17,7 +17,7 @@ module Notifications
|
|
17
17
|
private
|
18
18
|
|
19
19
|
def notifications
|
20
|
-
raise
|
20
|
+
raise 'You need reqiure user login for /notifications page.' unless current_user
|
21
21
|
Notification.where(user_id: current_user.id)
|
22
22
|
end
|
23
23
|
end
|
@@ -1,21 +1,23 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class CreateNotifications < ActiveRecord::Migration[5.2]
|
2
4
|
def change
|
3
5
|
create_table :notifications do |t|
|
4
|
-
t.
|
5
|
-
t.
|
6
|
+
t.bigint :user_id, null: false
|
7
|
+
t.bigint :actor_id
|
6
8
|
t.string :notify_type, null: false
|
7
9
|
t.string :target_type
|
8
|
-
t.
|
10
|
+
t.bigint :target_id
|
9
11
|
t.string :second_target_type
|
10
|
-
t.
|
12
|
+
t.bigint :second_target_id
|
11
13
|
t.string :third_target_type
|
12
|
-
t.
|
14
|
+
t.bigint :third_target_id
|
13
15
|
t.datetime :read_at
|
14
16
|
|
15
17
|
t.timestamps null: false
|
16
18
|
end
|
17
19
|
|
18
|
-
add_index :notifications, [
|
20
|
+
add_index :notifications, %i[user_id notify_type]
|
19
21
|
add_index :notifications, [:user_id]
|
20
22
|
end
|
21
23
|
end
|
@@ -2,11 +2,11 @@ require 'rails/generators'
|
|
2
2
|
module Notifications
|
3
3
|
module Generators
|
4
4
|
class ControllersGenerator < Rails::Generators::Base #:nodoc:
|
5
|
-
source_root File.expand_path('
|
5
|
+
source_root File.expand_path('../../../app/controllers', __dir__)
|
6
6
|
desc "Used to copy Notifications's controllers to your application's controllers."
|
7
7
|
|
8
8
|
def copy_controllers
|
9
|
-
%w
|
9
|
+
%w[notifications].each do |fname|
|
10
10
|
path = "#{Rails.root}/app/controllers/notifications/#{fname}_controller.rb"
|
11
11
|
if File.exist?(path)
|
12
12
|
puts "Skipping notifications/#{fname}_controller.rb creation, as file already exists!"
|
@@ -3,10 +3,10 @@ module Notifications
|
|
3
3
|
module Generators
|
4
4
|
class I18nGenerator < Rails::Generators::Base
|
5
5
|
desc "Create Notifications's default I18n files"
|
6
|
-
source_root File.expand_path('
|
6
|
+
source_root File.expand_path('../../..', __dir__)
|
7
7
|
|
8
8
|
def add_locales
|
9
|
-
%w
|
9
|
+
%w[en.yml zh-CN.yml].each do |fname|
|
10
10
|
path = "#{Rails.root}/config/locales/notifications.#{fname}"
|
11
11
|
if File.exist?(path)
|
12
12
|
puts "Skipping config/locales/notifications.#{fname} creation, as file already exists!"
|
@@ -3,7 +3,7 @@ module Notifications
|
|
3
3
|
module Generators
|
4
4
|
class InstallGenerator < Rails::Generators::Base
|
5
5
|
desc "Create Notifications's base files"
|
6
|
-
source_root File.expand_path('
|
6
|
+
source_root File.expand_path('../../..', __dir__)
|
7
7
|
|
8
8
|
def add_initializer
|
9
9
|
path = "#{Rails.root}/config/initializers/notifications.rb"
|
@@ -2,7 +2,7 @@ require 'rails/generators'
|
|
2
2
|
module Notifications
|
3
3
|
module Generators
|
4
4
|
class ViewsGenerator < Rails::Generators::Base #:nodoc:
|
5
|
-
source_root File.expand_path('
|
5
|
+
source_root File.expand_path('../../..', __dir__)
|
6
6
|
desc "Used to copy Notifications's views to your application's views."
|
7
7
|
|
8
8
|
def copy_views
|
data/lib/notifications/model.rb
CHANGED
@@ -5,7 +5,7 @@ module Notifications
|
|
5
5
|
DEFAULT_AVATAR = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPAAAADwCAMAAAAJixmgAAAAFVBMVEWkpKSnp6eqqqq3t7fS0tLV1dXZ2dmshcKEAAAAtklEQVR4Ae3XsRGAAAjAQFRk/5HtqaTz5H+DlInvAQAAAAAAAAAAAAAAAAAAAACymiveO6o7BQsWLFiwYMGCBS8PFixYsGDBggULFixYsGDBggULFixYsGDBggULFixYsGDBc4IFCxYsWLBgwYIFC14ZfOeAPRQ8IliwYMGCBQsWLFiwYMGCBQsWLFiwYMGCBQsWLFiwYMGCBQsWLFiwYMGCBQv+JQAAAAAAAAAAAAAAAAAAAOAB4KJfdHmj+kwAAAAASUVORK5CYII='
|
6
6
|
|
7
7
|
included do
|
8
|
-
belongs_to :actor, class_name: Notifications.config.user_class
|
8
|
+
belongs_to :actor, class_name: Notifications.config.user_class, optional: true
|
9
9
|
belongs_to :user, class_name: Notifications.config.user_class
|
10
10
|
|
11
11
|
belongs_to :target, polymorphic: true, optional: true
|
@@ -13,6 +13,8 @@ module Notifications
|
|
13
13
|
belongs_to :third_target, polymorphic: true, optional: true
|
14
14
|
|
15
15
|
scope :unread, -> { where(read_at: nil) }
|
16
|
+
scope :read, -> { where.not(read_at: nil) }
|
17
|
+
|
16
18
|
end
|
17
19
|
|
18
20
|
def read?
|
@@ -45,6 +47,10 @@ module Notifications
|
|
45
47
|
def unread_count(user)
|
46
48
|
Notification.where(user: user).unread.count
|
47
49
|
end
|
50
|
+
|
51
|
+
def read_count(user)
|
52
|
+
Notification.where(user: user).read.count
|
53
|
+
end
|
48
54
|
end
|
49
55
|
end
|
50
56
|
end
|
metadata
CHANGED
@@ -1,43 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: notifications
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Lee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: kaminari
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: '0.15'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: '0.15'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: rails
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '5.2'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '5.2'
|
41
41
|
description: Rails mountable Notification for any applications.
|
42
42
|
email:
|
43
43
|
- huacnlee@gmail.com
|
@@ -88,8 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: '0'
|
90
90
|
requirements: []
|
91
|
-
|
92
|
-
rubygems_version: 2.6.11
|
91
|
+
rubygems_version: 3.0.3
|
93
92
|
signing_key:
|
94
93
|
specification_version: 4
|
95
94
|
summary: Rails mountable Notification for any applications.
|