notifications 0.4.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 55c8e18fe2e682eaf7a45ef6fb104d0a8d7446d6
4
- data.tar.gz: 74a390763224f92a43807c2dbd31f86c2e04c75f
2
+ SHA256:
3
+ metadata.gz: 9cf008411221f8643f6272a4f492d6567f7e4c2e0391ee64aab731a3895c7e74
4
+ data.tar.gz: 75455d8f4aafd04a1d2ec0b57fd4e50e61518b54a8e27667d8e694be5210747d
5
5
  SHA512:
6
- metadata.gz: 314ca88887751450405a13e24f6ed4c8a093e670e24efd688c4bb040fb08a0c2394f339487497cdc28d77ccfe4fcc88138dc409dcb1949e4a96b9777499c9f62
7
- data.tar.gz: cc94c073af15fdb5bc721d99e9a057d331ec9654980422ec6de19d0ad2f64eccfc0585b07d10ab4705717f53256d7522b70b3b3a0caff00362aa7dd1da2584f5
6
+ metadata.gz: 1dbee55f530440d4c1ed3000857ffdc4abcbb25825c06164b658fa67b7f19e29a872fc851cfa64b7477db7da491a4f25ab6e5cda6bf461942251220f7c1c687b
7
+ data.tar.gz: 1feea3486d23c7e3e6598cdf012cd7f947cc017189b2258503c07236b8d1f71854cff20645d0e9df94bd6c548df8b6d4955d7e012407baba3eeafb6c6a842f7e
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # Notifications
2
2
 
3
- Rails mountable Notification for any applications.
3
+ Mountable notifications for any Rails applications.
4
4
 
5
- [![Gem Version](https://badge.fury.io/rb/notifications.svg)](https://badge.fury.io/rb/notifications) [![Build Status](https://travis-ci.org/rails-engine/notifications.svg)](https://travis-ci.org/rails-engine/notifications) [![Code Climate](https://codeclimate.com/github/rails-engine/notifications/badges/gpa.svg)](https://codeclimate.com/github/rails-engine/notifications) [![codecov.io](https://codecov.io/github/rails-engine/notifications/coverage.svg?branch=master)](https://codecov.io/github/rails-engine/notifications?branch=master) [![](http://inch-ci.org/github/rails-engine/notifications.svg?branch=master)](http://inch-ci.org/github/rails-engine/notifications?branch=master)
5
+ [![Gem Version](https://badge.fury.io/rb/notifications.svg)](https://badge.fury.io/rb/notifications) [![Build Status](https://travis-ci.org/rails-engine/notifications.svg)](https://travis-ci.org/rails-engine/notifications) [![codecov.io](https://codecov.io/github/rails-engine/notifications/coverage.svg?branch=master)](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
- Now you have notifications generator in Rails application:
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
- And, you can generate views, controller if you need custom them:
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
- belongs_user :user
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 user unread count:
61
+ Get unread notifications count for a user:
60
62
 
61
63
  ```rb
62
- count = Notification.unread_count(current_user)
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
- You app must be have:
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 mention you in
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
- Suggest you to use [second_level_cache](https://github.com/hooopo/second_level_cache) for solve N+1 performance issue.
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
- Contribution directions go here.
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("../test/dummy/Rakefile", __FILE__)
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 "You need reqiure user login for /notifications page." unless current_user
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
- class CreateNotifications < ActiveRecord::Migration[4.2]
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.integer :user_id, null: false
5
- t.integer :actor_id
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.integer :target_id
10
+ t.bigint :target_id
9
11
  t.string :second_target_type
10
- t.integer :second_target_id
12
+ t.bigint :second_target_id
11
13
  t.string :third_target_type
12
- t.integer :third_target_id
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, [:user_id, :notify_type]
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('../../../../app/controllers', __FILE__)
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(notifications).each do |fname|
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('../../../../', __FILE__)
6
+ source_root File.expand_path('../../..', __dir__)
7
7
 
8
8
  def add_locales
9
- %w(en.yml zh-CN.yml).each do |fname|
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('../../../../', __FILE__)
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('../../../../', __FILE__)
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
@@ -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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Notifications
2
- VERSION = '0.4.3'
4
+ VERSION = '1.0.0'
3
5
  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.3
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: 2017-07-16 00:00:00.000000000 Z
11
+ date: 2020-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rails
14
+ name: kaminari
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 4.2.0
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: 4.2.0
26
+ version: '0.15'
27
27
  - !ruby/object:Gem::Dependency
28
- name: kaminari
28
+ name: rails
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '0.15'
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: '0.15'
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
- rubyforge_project:
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.