notifications 0.4.4 → 1.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 79db559c211b2011d5615a2ac349e6d36abf5ec76c61bb2fcb08db013d5ced50
4
- data.tar.gz: cffba71597d27b35f00357c9a743645a9c276b673acc618214084110f0f46c75
3
+ metadata.gz: 60fefd52121d61d6beb17f2ddc27a829b78147ded2c816e643d691c89ceb583d
4
+ data.tar.gz: 67eb1c905055995f9f7c15563e0e45c83a90654765ba1201296f1f3426590182
5
5
  SHA512:
6
- metadata.gz: '0998a62dd596d90afcdc6289d4a54195f00ad39c782ef31e503f4263cd94309c5ce1660651f48a6686bf50749449f71cda01b6902bd1a8d0a2c2224f211385eb'
7
- data.tar.gz: 754e36beeb1b439e4acaa89f7c651e36f21f884cac0a628ba3b081691abe92e9c23bac0955b787e2d60be463efa586f454162c44a14bb346af5b854988b617da
6
+ metadata.gz: db8c63d042a9f10e5e30931dd710ff98d1fe46cd2885b918a2cedc5c868a0accf021ae4fea485d98436b9f384aa1d43965b97e88285ffd3f1fddfd59c3e0e711
7
+ data.tar.gz: d198158df36b375edd8e474d132be772666d24e3a3afaaeeb074cfcae31eff3a810c86ab91fd96ef86e8b46efb4e8440bcb5de28f262aeecbbe81af30d956d8c
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
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
 
@@ -10,13 +10,10 @@ Mountable notifications for any Rails applications.
10
10
 
11
11
  ## Installation
12
12
 
13
- ```ruby
14
- # Gemfile
15
- gem 'notifications'
13
+ ```bash
14
+ $ bundle add notifications
16
15
  ```
17
16
 
18
- And then run `bundle install`.
19
-
20
17
  You now have a notifications generator in your Rails application:
21
18
 
22
19
  ```bash
@@ -43,7 +40,7 @@ end
43
40
 
44
41
  class Comment
45
42
  belongs_to :post
46
- belongs_user :user
43
+  belongs_to :user
47
44
 
48
45
  after_commit :create_notifications, on: [:create]
49
46
  def create_notifications
@@ -59,7 +56,43 @@ end
59
56
  Get unread notifications count for a user:
60
57
 
61
58
  ```rb
62
- count = Notification.unread_count(current_user)
59
+ # unread count
60
+ unread_count = Notification.unread_count(current_user)
61
+
62
+ # read count
63
+ read_count = Notification.read_count(current_user)
64
+
65
+ ```
66
+
67
+ ```rb initialize/**.rb
68
+ # for non-user class
69
+ Notifications.config.user_class = 'Member'
70
+
71
+ #or change
72
+
73
+ Notifications.configure do
74
+ # Class name of you User model, default: 'User'
75
+ self.user_class = 'User'
76
+
77
+ # Method of user name in User model, default: 'name'
78
+ # self.user_name_method = 'name'
79
+
80
+ # Method of user avatar in User model, default: nil
81
+ # self.user_avatar_url_method = nil
82
+
83
+ # Method name of user profile page path, in User model, default: nil
84
+ # self.user_profile_url_method = 'profile_url'
85
+
86
+ # authenticate_user method in your Controller, default: nil
87
+ # If you use Devise, authenticate_user! is correct
88
+ # self.authenticate_user_method = 'authenticate_user!'
89
+
90
+ # current_user method name in your Controller, default: 'current_user'
91
+ # If you use Devise, current_user is correct
92
+ # self.current_user_method = 'current_user'
93
+ end
94
+
95
+
63
96
  ```
64
97
 
65
98
  ### Write your custom Notification partial view for notify_types:
@@ -72,7 +105,7 @@ Notification.create(notify_type: 'follow' ....)
72
105
  Notification.create(notify_type: 'mention', target: @reply, second_target: @topic, ....)
73
106
  ```
74
107
 
75
- You app must have:
108
+ Your app must have:
76
109
 
77
110
  - app/views/notifications/_follow.html.erb
78
111
  - app/views/notifications/_mention.html.erb
@@ -80,28 +113,36 @@ You app must have:
80
113
  ```erb
81
114
  # app/views/notifications/_follow.html.erb
82
115
  <div class="media-heading">
83
- <%= link_to notification.actor.title, notification.actor %> just followed you.
116
+ <%= link_to notification.actor.title, main_app.user_path(notification.actor) %> just followed you.
84
117
  </div>
85
118
  ```
86
119
 
87
120
  ```erb
88
121
  # app/views/notifications/_mention.html.erb
89
122
  <div class="media-heading">
90
- <%= link_to notification.actor.title, notification.actor %> has mentioned you in
91
- <%= link_to notification.second_target.title, topic_path(notification.second_target) %>
123
+ <%= link_to notification.actor.title, main_app.user_path(notification.actor) %> has mentioned you in
124
+ <%= link_to notification.second_target.title, main_app.topic_path(notification.second_target) %>
92
125
  </div>
93
126
  <div class="media-content">
94
127
  <%= notification.target.body %>
95
128
  </div>
96
129
  ```
97
130
 
131
+ > 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)`
132
+
98
133
  ### About Notification template N+1 performance
99
134
 
100
135
  It is recommended that you use [second_level_cache](https://github.com/hooopo/second_level_cache) for solving N+1 performance issues.
101
136
 
102
137
  ## Contributing
103
138
 
104
- Contribution directions go here.
139
+ Testing for multiple Rails versions:
140
+
141
+ ```bash
142
+ make test_51
143
+ # or test all
144
+ make test
145
+ ```
105
146
 
106
147
  ## Site Used
107
148
 
data/Rakefile CHANGED
@@ -1,24 +1,24 @@
1
1
  begin
2
- require 'bundler/setup'
2
+ require "bundler/setup"
3
3
  rescue LoadError
4
- puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
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
- load 'rails/tasks/engine.rake'
10
- load 'rails/tasks/statistics.rake'
9
+ load "rails/tasks/engine.rake"
10
+ load "rails/tasks/statistics.rake"
11
11
 
12
12
  Bundler::GemHelper.install_tasks
13
13
 
14
- require 'rake/testtask'
14
+ require "rake/testtask"
15
15
 
16
16
  Rake::TestTask.new(:test) do |t|
17
- t.libs << 'lib'
18
- t.libs << 'test'
19
- t.pattern = 'test/**/*_test.rb'
17
+ t.libs << "lib"
18
+ t.libs << "test"
19
+ t.pattern = "test/**/*_test.rb"
20
20
  t.verbose = false
21
21
  end
22
22
 
23
- task 'assets:precompile' => 'app:assets:precompile'
23
+ task "assets:precompile" => "app:assets:precompile"
24
24
  task default: :test
@@ -1,24 +1,28 @@
1
1
  module Notifications
2
2
  class NotificationsController < Notifications::ApplicationController
3
3
  def index
4
- @notifications = notifications.includes(:actor).order('id desc').page(params[:page])
4
+ @notifications = notifications.includes(:actor).order("id desc").page(params[:page])
5
5
 
6
6
  unread_ids = @notifications.reject(&:read?).select(&:id)
7
- Notification.read!(unread_ids)
7
+ Notification.read!(current_user, unread_ids)
8
8
 
9
9
  @notification_groups = @notifications.group_by { |note| note.created_at.to_date }
10
10
  end
11
11
 
12
+ def read
13
+ Notification.read!(current_user, params[:ids])
14
+ render json: { ok: 1 }
15
+ end
16
+
12
17
  def clean
13
18
  notifications.delete_all
14
19
  redirect_to notifications_path
15
20
  end
16
21
 
17
22
  private
18
-
19
- def notifications
20
- raise "You need reqiure user login for /notifications page." unless current_user
21
- Notification.where(user_id: current_user.id)
22
- end
23
+ def notifications
24
+ raise "You need reqiure user login for /notifications page." unless current_user
25
+ Notification.where(user_id: current_user.id)
26
+ end
23
27
  end
24
28
  end
@@ -1,7 +1,8 @@
1
1
  Notifications::Engine.routes.draw do
2
- resources :notifications, path: '' do
2
+ resources :notifications, path: "" do
3
3
  collection do
4
4
  delete :clean
5
+ post :read
5
6
  end
6
7
  end
7
8
  end
@@ -1,21 +1,23 @@
1
- class CreateNotifications < ActiveRecord::Migration[5.0]
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
@@ -1,12 +1,12 @@
1
- require 'rails/generators'
1
+ 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!"
@@ -1,12 +1,12 @@
1
- require 'rails/generators'
1
+ require "rails/generators"
2
2
  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!"
@@ -1,27 +1,27 @@
1
- require 'rails/generators'
1
+ require "rails/generators"
2
2
  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"
10
10
  if File.exist?(path)
11
- puts 'Skipping config/initializers/notifications.rb creation, as file already exists!'
11
+ puts "Skipping config/initializers/notifications.rb creation, as file already exists!"
12
12
  else
13
- puts 'Adding Notifications initializer (config/initializers/notifications.rb)...'
14
- template 'config/initializers/notifications.rb', path
13
+ puts "Adding Notifications initializer (config/initializers/notifications.rb)..."
14
+ template "config/initializers/notifications.rb", path
15
15
  end
16
16
  end
17
17
 
18
18
  def add_models
19
19
  path = "#{Rails.root}/app/models/notification.rb"
20
20
  if File.exist?(path)
21
- puts 'Skipping notification.rb creation, as file already exists!'
21
+ puts "Skipping notification.rb creation, as file already exists!"
22
22
  else
23
- puts 'Adding model (notification.rb)...'
24
- template 'app/models/notification.rb', path
23
+ puts "Adding model (notification.rb)..."
24
+ template "app/models/notification.rb", path
25
25
  end
26
26
  end
27
27
 
@@ -30,7 +30,7 @@ module Notifications
30
30
  end
31
31
 
32
32
  def add_migrations
33
- exec('rake notifications:install:migrations')
33
+ exec("rake notifications:install:migrations")
34
34
  end
35
35
  end
36
36
  end
@@ -1,13 +1,13 @@
1
- require 'rails/generators'
1
+ 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
9
- directory 'app/views/notifications', 'app/views/notifications'
10
- template 'app/assets/stylesheets/notifications.scss', 'app/assets/stylesheets/notifications.scss'
9
+ directory "app/views/notifications", "app/views/notifications"
10
+ template "app/assets/stylesheets/notifications.scss", "app/assets/stylesheets/notifications.scss"
11
11
  end
12
12
  end
13
13
  end
@@ -1,20 +1,20 @@
1
- require 'notifications/model'
2
- require 'notifications/engine'
3
- require 'notifications/configuration'
4
- require 'notifications/version'
5
- require 'kaminari'
1
+ require "notifications/model"
2
+ require "notifications/engine"
3
+ require "notifications/configuration"
4
+ require "notifications/version"
5
+ require "kaminari"
6
6
 
7
7
  module Notifications
8
8
  class << self
9
9
  def config
10
10
  return @config if defined?(@config)
11
11
  @config = Configuration.new
12
- @config.user_class = 'User'
13
- @config.user_name_method = 'name'
12
+ @config.user_class = "User"
13
+ @config.user_name_method = "name"
14
14
  @config.user_avatar_url_method = nil
15
15
  @config.user_profile_url_method = nil
16
16
  @config.authenticate_user_method = nil
17
- @config.current_user_method = 'current_user'
17
+ @config.current_user_method = "current_user"
18
18
  @config
19
19
  end
20
20
 
@@ -0,0 +1,12 @@
1
+ module Notifications
2
+ class Base
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ end
7
+
8
+ class_methods do
9
+ def
10
+ end
11
+ end
12
+ end
@@ -2,7 +2,7 @@ module Notifications
2
2
  module Model
3
3
  extend ActiveSupport::Concern
4
4
 
5
- DEFAULT_AVATAR = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPAAAADwCAMAAAAJixmgAAAAFVBMVEWkpKSnp6eqqqq3t7fS0tLV1dXZ2dmshcKEAAAAtklEQVR4Ae3XsRGAAAjAQFRk/5HtqaTz5H+DlInvAQAAAAAAAAAAAAAAAAAAAACymiveO6o7BQsWLFiwYMGCBS8PFixYsGDBggULFixYsGDBggULFixYsGDBggULFixYsGDBc4IFCxYsWLBgwYIFC14ZfOeAPRQ8IliwYMGCBQsWLFiwYMGCBQsWLFiwYMGCBQsWLFiwYMGCBQsWLFiwYMGCBQv+JQAAAAAAAAAAAAAAAAAAAOAB4KJfdHmj+kwAAAAASUVORK5CYII='
5
+ DEFAULT_AVATAR = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPAAAADwCAMAAAAJixmgAAAAFVBMVEWkpKSnp6eqqqq3t7fS0tLV1dXZ2dmshcKEAAAAtklEQVR4Ae3XsRGAAAjAQFRk/5HtqaTz5H+DlInvAQAAAAAAAAAAAAAAAAAAAACymiveO6o7BQsWLFiwYMGCBS8PFixYsGDBggULFixYsGDBggULFixYsGDBggULFixYsGDBc4IFCxYsWLBgwYIFC14ZfOeAPRQ8IliwYMGCBQsWLFiwYMGCBQsWLFiwYMGCBQsWLFiwYMGCBQsWLFiwYMGCBQv+JQAAAAAAAAAAAAAAAAAAAOAB4KJfdHmj+kwAAAAASUVORK5CYII="
6
6
 
7
7
  included do
8
8
  belongs_to :actor, class_name: Notifications.config.user_class, optional: true
@@ -13,6 +13,7 @@ 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) }
16
17
  end
17
18
 
18
19
  def read?
@@ -20,7 +21,7 @@ module Notifications
20
21
  end
21
22
 
22
23
  def actor_name
23
- return '' if self.actor.blank?
24
+ return "" if self.actor.blank?
24
25
  self.actor.send(Notifications.config.user_name_method)
25
26
  end
26
27
 
@@ -37,14 +38,18 @@ module Notifications
37
38
  end
38
39
 
39
40
  module ClassMethods
40
- def read!(ids = [])
41
+ def read!(user, ids = [])
41
42
  return if ids.blank?
42
- Notification.where(id: ids).update_all(read_at: Time.now)
43
+ Notification.where(user: user, id: ids).update_all(read_at: Time.now)
43
44
  end
44
45
 
45
46
  def unread_count(user)
46
47
  Notification.where(user: user).unread.count
47
48
  end
49
+
50
+ def read_count(user)
51
+ Notification.where(user: user).read.count
52
+ end
48
53
  end
49
54
  end
50
55
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Notifications
2
- VERSION = '0.4.4'
4
+ VERSION = "1.1.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.4
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Lee
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-23 00:00:00.000000000 Z
11
+ date: 2020-10-09 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
@@ -64,6 +64,7 @@ files:
64
64
  - lib/generators/notifications/install_generator.rb
65
65
  - lib/generators/notifications/views_generator.rb
66
66
  - lib/notifications.rb
67
+ - lib/notifications/base.rb
67
68
  - lib/notifications/configuration.rb
68
69
  - lib/notifications/engine.rb
69
70
  - lib/notifications/model.rb
@@ -73,7 +74,7 @@ homepage: https://github.com/rails-engine/notifications
73
74
  licenses:
74
75
  - MIT
75
76
  metadata: {}
76
- post_install_message:
77
+ post_install_message:
77
78
  rdoc_options: []
78
79
  require_paths:
79
80
  - lib
@@ -88,9 +89,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
89
  - !ruby/object:Gem::Version
89
90
  version: '0'
90
91
  requirements: []
91
- rubyforge_project:
92
- rubygems_version: 2.7.6
93
- signing_key:
92
+ rubygems_version: 3.1.2
93
+ signing_key:
94
94
  specification_version: 4
95
95
  summary: Rails mountable Notification for any applications.
96
96
  test_files: []