notification-settings 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 +271 -271
- data/app/models/notification_settings/setting.rb +15 -13
- data/app/models/notification_settings/subscription.rb +15 -13
- data/lib/generators/notification_settings/install_generator.rb +40 -43
- data/lib/generators/templates/install/initializer.rb +28 -24
- data/lib/generators/templates/install/notifications_migration.rb.erb +27 -29
- data/lib/notification-settings.rb +15 -15
- data/lib/notification_settings/configuration.rb +30 -32
- data/lib/notification_settings/engine.rb +9 -7
- data/lib/notification_settings/notification_library.rb +144 -72
- data/lib/notification_settings/notification_scopes.rb +28 -30
- data/lib/notification_settings/setting_library.rb +60 -28
- data/lib/notification_settings/subscribable.rb +50 -42
- data/lib/notification_settings/subscriber.rb +35 -30
- data/lib/notification_settings/subscription_library.rb +23 -25
- data/lib/notification_settings/target.rb +27 -26
- metadata +46 -34
- data/CHANGELOG.md +0 -59
- data/lib/generators/templates/install/README.md +0 -1
@@ -1,13 +1,15 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NotificationSettings
|
4
|
+
class Setting < ActiveRecord::Base
|
5
|
+
self.table_name = 'notification_settings_settings'
|
6
|
+
|
7
|
+
include NotificationSettings::SettingLibrary
|
8
|
+
|
9
|
+
serialize :settings, Hash
|
10
|
+
serialize :category_settings, Hash
|
11
|
+
|
12
|
+
belongs_to :object, polymorphic: true, optional: true
|
13
|
+
belongs_to :subscription, optional: true
|
14
|
+
end
|
15
|
+
end
|
@@ -1,13 +1,15 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NotificationSettings
|
4
|
+
class Subscription < ActiveRecord::Base
|
5
|
+
self.table_name = 'notification_settings_subscriptions'
|
6
|
+
|
7
|
+
include NotificationSettings::SubscriptionLibrary
|
8
|
+
|
9
|
+
belongs_to :subscriber, polymorphic: true
|
10
|
+
belongs_to :subscribable, polymorphic: true
|
11
|
+
|
12
|
+
has_many :notifications, class_name: '::Notification'
|
13
|
+
has_one :notification_setting, class_name: 'Setting'
|
14
|
+
end
|
15
|
+
end
|
@@ -1,43 +1,40 @@
|
|
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
|
-
end
|
42
|
-
|
43
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails/generators'
|
4
|
+
require 'rails/generators/migration'
|
5
|
+
|
6
|
+
module NotificationSettings
|
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 NotificationSettings'
|
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
|
20
|
+
|
21
|
+
def create_initializer
|
22
|
+
template 'initializer.rb', 'config/initializers/notification-settings.rb'
|
23
|
+
end
|
24
|
+
|
25
|
+
def create_notifications_migration_file
|
26
|
+
migration_template(
|
27
|
+
'notifications_migration.rb.erb',
|
28
|
+
'db/migrate/notification_settings_migration.rb',
|
29
|
+
migration_version: migration_version
|
30
|
+
)
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def migration_version
|
36
|
+
return unless Rails.version >= '5.0.0'
|
37
|
+
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -1,24 +1,28 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
NotificationSettings.configure do |config|
|
4
|
+
# Choose your default notification category. Takes a string.
|
5
|
+
# config.default_category = 'notification'
|
6
|
+
|
7
|
+
### STATUS ###
|
8
|
+
|
9
|
+
# Time duration without activity after which the status defaults to `'idle'`.
|
10
|
+
# Takes a time.
|
11
|
+
# config.idle_after = 10.minutes
|
12
|
+
|
13
|
+
# Time duration without activity after which the status defaults to
|
14
|
+
# `'offline'`. Takes a time.
|
15
|
+
# config.offline_after = 3.hours
|
16
|
+
|
17
|
+
# Stringified datetime attribute name of `object` that defines the time of the
|
18
|
+
# last activity. Takes a string.
|
19
|
+
# config.last_seen = 'last_seen'
|
20
|
+
|
21
|
+
# Array of possible statuses that will prevent creating notifications for a
|
22
|
+
# target. Takes an array of strings.
|
23
|
+
# config.do_not_notify_statuses = []
|
24
|
+
|
25
|
+
# Array of possible statuses that will prevent pushing notifications of a
|
26
|
+
# target. Takes an array of strings.
|
27
|
+
# config.do_not_push_statuses = ['do not disturb']
|
28
|
+
end
|
@@ -1,29 +1,27 @@
|
|
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
|
29
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class NotificationSettingsMigration < ActiveRecord::Migration<%= migration_version %>
|
4
|
+
def change
|
5
|
+
add_column :notifications, :subscription_id, :bigint, index: true
|
6
|
+
add_column :notifications, :category, :string, index: true
|
7
|
+
|
8
|
+
create_table :notification_settings_settings do |t|
|
9
|
+
t.references :object, polymorphic: true, index: { name: 'idx_settings_object_type_object_id' }
|
10
|
+
t.references :subscription, index: true
|
11
|
+
|
12
|
+
t.string :status
|
13
|
+
|
14
|
+
t.text :settings
|
15
|
+
t.text :category_settings
|
16
|
+
|
17
|
+
t.timestamps
|
18
|
+
end
|
19
|
+
|
20
|
+
create_table :notification_settings_subscriptions do |t|
|
21
|
+
t.references :subscriber, polymorphic: true, index: { name: 'idx_subscriptions_subscriber_type_subscriber_id' }
|
22
|
+
t.references :subscribable, polymorphic: true, index: { name: 'idx_subscriptions_subscribable_type_subscribable_id' }
|
23
|
+
|
24
|
+
t.timestamps
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -1,15 +1,15 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NotificationSettings
|
4
|
+
require 'notification_settings/configuration'
|
5
|
+
|
6
|
+
require 'notification_settings/engine'
|
7
|
+
|
8
|
+
autoload :Target, 'notification_settings/target'
|
9
|
+
autoload :Subscriber, 'notification_settings/subscriber'
|
10
|
+
autoload :Subscribable, 'notification_settings/subscribable'
|
11
|
+
autoload :SettingLibrary, 'notification_settings/setting_library'
|
12
|
+
autoload :SubscriptionLibrary, 'notification_settings/subscription_library'
|
13
|
+
autoload :NotificationLibrary, 'notification_settings/notification_library'
|
14
|
+
autoload :NotificationScopes, 'notification_settings/notification_scopes'
|
15
|
+
end
|
@@ -1,32 +1,30 @@
|
|
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
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NotificationSettings
|
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 :default_category
|
15
|
+
attr_accessor :last_seen
|
16
|
+
attr_accessor :idle_after
|
17
|
+
attr_accessor :offline_after
|
18
|
+
attr_accessor :do_not_notify_statuses
|
19
|
+
attr_accessor :do_not_push_statuses
|
20
|
+
|
21
|
+
def initialize
|
22
|
+
@default_category = 'notification'
|
23
|
+
@last_seen = 'last_seen'
|
24
|
+
@idle_after = 10.minutes
|
25
|
+
@offline_after = 3.hours
|
26
|
+
@do_not_notify_statuses = []
|
27
|
+
@do_not_push_statuses = ['do not disturb']
|
28
|
+
end
|
29
|
+
end
|
30
|
+
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 NotificationSettings
|
7
|
+
class Engine < ::Rails::Engine
|
8
|
+
end
|
9
|
+
end
|
@@ -1,72 +1,144 @@
|
|
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
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'notification-handler'
|
4
|
+
require 'active_support'
|
5
|
+
|
6
|
+
module NotificationSettings
|
7
|
+
module NotificationLibrary
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
included do
|
11
|
+
before_create :validate_create
|
12
|
+
|
13
|
+
belongs_to :subscription,
|
14
|
+
class_name: 'NotificationSettings::Subscription',
|
15
|
+
optional: true
|
16
|
+
|
17
|
+
include NotificationSettings::NotificationLibrary::InstanceMethods
|
18
|
+
end
|
19
|
+
|
20
|
+
module InstanceMethods
|
21
|
+
def category
|
22
|
+
self[:category] || NotificationSettings.configuration.default_category
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def validate_create
|
28
|
+
throw(:abort) unless can_create?
|
29
|
+
end
|
30
|
+
|
31
|
+
def can_create?
|
32
|
+
if target.notification_setting.present?
|
33
|
+
unless status_allows_create? &&
|
34
|
+
settings_allow_create? &&
|
35
|
+
category_settings_allow_create?
|
36
|
+
return false
|
37
|
+
end
|
38
|
+
end
|
39
|
+
true
|
40
|
+
end
|
41
|
+
|
42
|
+
def status_allows_create?
|
43
|
+
!do_not_notify_statuses.include?(target.notification_setting.status)
|
44
|
+
end
|
45
|
+
|
46
|
+
def settings_allow_create?
|
47
|
+
target.notification_setting.settings.dig(:enabled)
|
48
|
+
end
|
49
|
+
|
50
|
+
def category_settings_allow_create?
|
51
|
+
target.notification_setting.category_settings.dig(
|
52
|
+
category.to_sym,
|
53
|
+
:enabled
|
54
|
+
)
|
55
|
+
end
|
56
|
+
|
57
|
+
def initialize_pusher
|
58
|
+
return unless can_push?
|
59
|
+
super
|
60
|
+
end
|
61
|
+
|
62
|
+
def can_push?
|
63
|
+
if target.notification_setting.present?
|
64
|
+
return false unless status_allows_push?
|
65
|
+
|
66
|
+
if push.is_a?(Array)
|
67
|
+
return false unless can_use_pushers?(push)
|
68
|
+
else
|
69
|
+
return false unless can_use_pusher?(push)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
true
|
73
|
+
end
|
74
|
+
|
75
|
+
def can_use_pushers?(pushers)
|
76
|
+
pushers.each do |pusher|
|
77
|
+
return false unless can_use_pusher?(pusher)
|
78
|
+
end
|
79
|
+
true
|
80
|
+
end
|
81
|
+
|
82
|
+
def can_use_pusher?(pusher)
|
83
|
+
unless settings_allow_push?(pusher) &&
|
84
|
+
category_settings_allow_push?(pusher)
|
85
|
+
return false
|
86
|
+
end
|
87
|
+
true
|
88
|
+
end
|
89
|
+
|
90
|
+
def status_allows_push?
|
91
|
+
!do_not_push_statuses.include?(target.notification_setting.status)
|
92
|
+
end
|
93
|
+
|
94
|
+
def settings_allow_push?(pusher)
|
95
|
+
return global_settings_allow_push? unless local_pusher_settings?(pusher)
|
96
|
+
local_settings_allow_push?(pusher)
|
97
|
+
end
|
98
|
+
|
99
|
+
def local_pusher_settings?(pusher)
|
100
|
+
local_settings_allow_push?(pusher).present?
|
101
|
+
end
|
102
|
+
|
103
|
+
def local_settings_allow_push?(pusher)
|
104
|
+
target.notification_setting.settings.dig(pusher.to_sym)
|
105
|
+
end
|
106
|
+
|
107
|
+
def global_settings_allow_push?
|
108
|
+
target.notification_setting.settings.dig(:index)
|
109
|
+
end
|
110
|
+
|
111
|
+
def category_settings_allow_push?(pusher)
|
112
|
+
if local_pusher_category_settings?(pusher)
|
113
|
+
local_category_settings_allow_push?(pusher)
|
114
|
+
else
|
115
|
+
global_category_settings_allow_push?
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def local_pusher_category_settings?(pusher)
|
120
|
+
local_category_settings_allow_push?(pusher).present?
|
121
|
+
end
|
122
|
+
|
123
|
+
def local_category_settings_allow_push?(pusher)
|
124
|
+
target.notification_setting.category_settings.dig(
|
125
|
+
category.to_sym, pusher.to_sym
|
126
|
+
)
|
127
|
+
end
|
128
|
+
|
129
|
+
def global_category_settings_allow_push?
|
130
|
+
target.notification_setting.category_settings.dig(
|
131
|
+
category.to_sym, :index
|
132
|
+
)
|
133
|
+
end
|
134
|
+
|
135
|
+
def do_not_notify_statuses
|
136
|
+
NotificationSettings.configuration.do_not_notify_statuses
|
137
|
+
end
|
138
|
+
|
139
|
+
def do_not_push_statuses
|
140
|
+
NotificationSettings.configuration.do_not_push_statuses
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|