notification-handler 1.2.5 → 1.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +21 -21
- data/README.md +260 -260
- data/app/models/notification_handler/notification.rb +8 -6
- data/lib/generators/notification_handler/install_generator.rb +33 -34
- data/lib/generators/templates/install/initializer.rb +12 -11
- data/lib/generators/templates/install/notification_model.rb +4 -2
- data/lib/generators/templates/install/notifications_migration.rb.erb +16 -16
- data/lib/notification-handler.rb +10 -10
- data/lib/notification_handler/configuration.rb +26 -28
- data/lib/notification_handler/engine.rb +9 -7
- data/lib/notification_handler/group.rb +19 -17
- data/lib/notification_handler/notification_library.rb +68 -62
- data/lib/notification_handler/notification_scopes.rb +21 -17
- data/lib/notification_handler/object.rb +24 -24
- data/lib/notification_handler/railtie.rb +16 -16
- data/lib/notification_handler/target.rb +26 -27
- metadata +41 -29
- data/CHANGELOG.md +0 -51
- data/lib/generators/templates/install/README.md +0 -1
@@ -1,6 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NotificationHandler
|
4
|
+
class Notification < ApplicationRecord
|
5
|
+
include NotificationHandler::NotificationLibrary
|
6
|
+
include NotificationHandler::NotificationScopes
|
7
|
+
end
|
8
|
+
end
|
@@ -1,46 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'rails/generators'
|
2
4
|
require 'rails/generators/migration'
|
3
5
|
|
4
6
|
module NotificationHandler
|
7
|
+
class InstallGenerator < Rails::Generators::Base
|
8
|
+
include Rails::Generators::Migration
|
9
|
+
|
10
|
+
source_root(File.join(File.dirname(__FILE__), '../templates/install'))
|
11
|
+
desc 'Install NotificationHandler'
|
12
|
+
|
13
|
+
def self.next_migration_number(dirname)
|
14
|
+
if ActiveRecord::Base.timestamped_migrations
|
15
|
+
Time.now.utc.strftime('%Y%m%d%H%M%S')
|
16
|
+
else
|
17
|
+
format('%.3d', current_migration_number(dirname) + 1)
|
18
|
+
end
|
19
|
+
end
|
5
20
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
source_root File.join File.dirname(__FILE__), '../templates/install'
|
11
|
-
desc 'Install NotificationHandler'
|
12
|
-
|
13
|
-
def self.next_migration_number dirname
|
14
|
-
if ActiveRecord::Base.timestamped_migrations
|
15
|
-
Time.now.utc.strftime '%Y%m%d%H%M%S'
|
16
|
-
else
|
17
|
-
"%.3d" % ( current_migration_number(dirname) + 1 )
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def create_initializer
|
22
|
-
template 'initializer.rb', 'config/initializers/notification-handler.rb'
|
23
|
-
end
|
21
|
+
def create_initializer
|
22
|
+
template 'initializer.rb', 'config/initializers/notification-handler.rb'
|
23
|
+
end
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
25
|
+
def create_notifications_migration_file
|
26
|
+
migration_template(
|
27
|
+
'notifications_migration.rb.erb',
|
28
|
+
'db/migrate/notification_handler_migration.rb',
|
29
|
+
migration_version: migration_version
|
30
|
+
)
|
31
|
+
end
|
31
32
|
|
32
|
-
|
33
|
-
|
34
|
-
|
33
|
+
def create_notification_model
|
34
|
+
template 'notification_model.rb', 'app/models/notification.rb'
|
35
|
+
end
|
35
36
|
|
36
|
-
|
37
|
+
private
|
37
38
|
|
38
|
-
|
39
|
-
|
40
|
-
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
|
41
|
-
end
|
42
|
-
end
|
39
|
+
def migration_version
|
40
|
+
return unless Rails.version >= '5.0.0'
|
43
41
|
|
42
|
+
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
|
44
43
|
end
|
45
|
-
|
44
|
+
end
|
46
45
|
end
|
@@ -1,11 +1,12 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
NotificationHandler.configure do |config|
|
4
|
+
# Cache amount of unread and read notifications for notification targets.
|
5
|
+
# Learn more: https://github.com/jonhue/notifications-rails/tree/master/notification-handler#caching
|
6
|
+
# config.cache = false
|
7
|
+
|
8
|
+
# Groups are a powerful way to bulk-create notifications for multiple objects
|
9
|
+
# that don't necessarily have a common class.
|
10
|
+
# Learn more: https://github.com/jonhue/notifications-rails/tree/master/notification-handler#groups
|
11
|
+
# config.define_group :subscribers, User.where(subscriber: true)
|
12
|
+
end
|
@@ -1,16 +1,16 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class NotificationHandlerMigration < ActiveRecord::Migration<%= migration_version %>
|
4
|
+
def change
|
5
|
+
create_table :notifications do |t|
|
6
|
+
t.references :target, polymorphic: true, index: true
|
7
|
+
t.references :object, polymorphic: true, index: true
|
8
|
+
|
9
|
+
t.boolean :read, default: false, null: false, index: true
|
10
|
+
|
11
|
+
t.text :metadata
|
12
|
+
|
13
|
+
t.timestamps
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/notification-handler.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require 'notification_handler/configuration'
|
1
|
+
# frozen_string_literal: true
|
4
2
|
|
5
|
-
|
3
|
+
module NotificationHandler
|
4
|
+
require 'notification_handler/configuration'
|
6
5
|
|
7
|
-
|
6
|
+
require 'notification_handler/engine'
|
8
7
|
|
9
|
-
|
10
|
-
autoload :Object, 'notification_handler/object'
|
11
|
-
autoload :NotificationLibrary, 'notification_handler/notification_library'
|
12
|
-
autoload :NotificationScopes, 'notification_handler/notification_scopes'
|
8
|
+
autoload :Group, 'notification_handler/group'
|
13
9
|
|
14
|
-
|
10
|
+
autoload :Target, 'notification_handler/target'
|
11
|
+
autoload :Object, 'notification_handler/object'
|
12
|
+
autoload :NotificationLibrary, 'notification_handler/notification_library'
|
13
|
+
autoload :NotificationScopes, 'notification_handler/notification_scopes'
|
15
14
|
|
15
|
+
require 'notification_handler/railtie'
|
16
16
|
end
|
@@ -1,28 +1,26 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NotificationHandler
|
4
|
+
class << self
|
5
|
+
attr_accessor :configuration
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.configure
|
9
|
+
self.configuration ||= Configuration.new
|
10
|
+
yield configuration
|
11
|
+
end
|
12
|
+
|
13
|
+
class Configuration
|
14
|
+
attr_accessor :groups
|
15
|
+
attr_accessor :cache
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
@groups = []
|
19
|
+
@cache = false
|
20
|
+
end
|
21
|
+
|
22
|
+
def define_group(name, target_scope)
|
23
|
+
groups << ::NotificationHandler::Group.new(name.to_sym, target_scope)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -1,7 +1,9 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails/railtie'
|
4
|
+
require 'active_record'
|
5
|
+
|
6
|
+
module NotificationHandler
|
7
|
+
class Engine < ::Rails::Engine
|
8
|
+
end
|
9
|
+
end
|
@@ -1,17 +1,19 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NotificationHandler
|
4
|
+
class Group
|
5
|
+
attr_accessor :name
|
6
|
+
attr_accessor :target_scope
|
7
|
+
|
8
|
+
def initialize(name, target_scope)
|
9
|
+
@name = name
|
10
|
+
@target_scope = target_scope
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.find_by_name(name)
|
14
|
+
NotificationHandler.configuration.groups.select do |group|
|
15
|
+
group.name == name.to_sym
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -1,62 +1,68 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support'
|
4
|
+
|
5
|
+
module NotificationHandler
|
6
|
+
module NotificationLibrary
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
self.inheritance_column = :_type_disabled
|
11
|
+
|
12
|
+
before_validation :create_for_group
|
13
|
+
after_commit :cache
|
14
|
+
|
15
|
+
serialize :metadata, Hash
|
16
|
+
attr_accessor :group
|
17
|
+
|
18
|
+
belongs_to :target, polymorphic: true
|
19
|
+
belongs_to :object, polymorphic: true, optional: true
|
20
|
+
|
21
|
+
include NotificationHandler::NotificationLibrary::InstanceMethods
|
22
|
+
|
23
|
+
if defined?(NotificationRenderer)
|
24
|
+
include NotificationRenderer::NotificationLibrary
|
25
|
+
end
|
26
|
+
if defined?(NotificationPusher)
|
27
|
+
include NotificationPusher::NotificationLibrary
|
28
|
+
end
|
29
|
+
if defined?(NotificationSettings)
|
30
|
+
include NotificationSettings::NotificationLibrary
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
module InstanceMethods
|
35
|
+
def read?
|
36
|
+
read
|
37
|
+
end
|
38
|
+
|
39
|
+
def unread?
|
40
|
+
!read
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def create_for_group
|
46
|
+
return if group.nil?
|
47
|
+
|
48
|
+
target_scope = NotificationHandler::Group.find_by_name(group)
|
49
|
+
.last.target_scope
|
50
|
+
target_scope&.each do |target|
|
51
|
+
notification = dup
|
52
|
+
notification.target = target
|
53
|
+
notification.group = nil
|
54
|
+
notification.save
|
55
|
+
end
|
56
|
+
false
|
57
|
+
end
|
58
|
+
|
59
|
+
def cache
|
60
|
+
return unless read_changed?
|
61
|
+
|
62
|
+
target.read_notification_count = target.notifications.read.count
|
63
|
+
target.unread_notification_count = target.notifications.unread.count
|
64
|
+
target.save!
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -1,17 +1,21 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support'
|
4
|
+
|
5
|
+
module NotificationHandler
|
6
|
+
module NotificationScopes
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
scope :read, -> { where(read: true) }
|
11
|
+
scope :unread, -> { where(read: false) }
|
12
|
+
|
13
|
+
if defined?(NotificationRenderer)
|
14
|
+
include NotificationRenderer::NotificationScopes
|
15
|
+
end
|
16
|
+
if defined?(NotificationSettings)
|
17
|
+
include NotificationSettings::NotificationScopes
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|