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.
@@ -1,13 +1,15 @@
1
- class NotificationSettings::Setting < ActiveRecord::Base
2
-
3
- self.table_name = 'notification_settings_settings'
4
-
5
- include NotificationSettings::SettingLibrary
6
-
7
- serialize :settings, Hash
8
- serialize :category_settings, Hash
9
-
10
- belongs_to :object, polymorphic: true, optional: true
11
- belongs_to :subscription, optional: true
12
-
13
- end
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
- class NotificationSettings::Subscription < ActiveRecord::Base
2
-
3
- self.table_name = 'notification_settings_subscriptions'
4
-
5
- include NotificationSettings::SubscriptionLibrary
6
-
7
- belongs_to :subscriber, polymorphic: true
8
- belongs_to :subscribable, polymorphic: true
9
-
10
- has_many :notifications, class_name: '::Notification'
11
- has_one :notification_setting, class_name: 'Setting'
12
-
13
- end
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
- require 'rails/generators'
2
- require 'rails/generators/migration'
3
-
4
- module NotificationSettings
5
-
6
- class InstallGenerator < Rails::Generators::Base
7
-
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
- "%.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 'notifications_migration.rb.erb', 'db/migrate/notification_settings_migration.rb', migration_version: migration_version
27
- end
28
-
29
- def show_readme
30
- readme 'README.md'
31
- end
32
-
33
- private
34
-
35
- def migration_version
36
- if Rails.version >= '5.0.0'
37
- "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
38
- end
39
- end
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
- NotificationSettings.configure do |config|
2
-
3
- # Choose your default notification category. Takes a string.
4
- # config.default_category = 'notification'
5
-
6
-
7
- ### STATUS ###
8
-
9
- # Time duration without activity after which the status defaults to `'idle'`. Takes a time.
10
- # config.idle_after = 10.minutes
11
-
12
- # Time duration without activity after which the status defaults to `'offline'`. Takes a time.
13
- # config.offline_after = 3.hours
14
-
15
- # Stringified datetime attribute name of `object` that defines the time of the last activity. Takes a string.
16
- # config.last_seen = 'last_seen'
17
-
18
- # Array of possible statuses that will prevent creating notifications for a target. Takes an array of strings.
19
- # config.do_not_notify_statuses = []
20
-
21
- # Array of possible statuses that will prevent pushing notifications of a target. Takes an array of strings.
22
- # config.do_not_push_statuses = ['do not disturb']
23
-
24
- end
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
- class NotificationSettingsMigration < ActiveRecord::Migration<%= migration_version %>
2
- def change
3
- add_column :notifications, :subscription_id, :bigint, index: true
4
- add_column :notifications, :category, :string, index: true
5
-
6
- create_table :notification_settings_settings do |t|
7
-
8
- t.references :object, polymorphic: true, index: { name: 'idx_settings_object_type_object_id' }
9
- t.references :subscription, index: true
10
-
11
- t.string :status
12
-
13
- t.text :settings
14
- t.text :category_settings
15
-
16
- t.timestamps
17
-
18
- end
19
-
20
- create_table :notification_settings_subscriptions do |t|
21
-
22
- t.references :subscriber, polymorphic: true, index: { name: 'idx_subscriptions_subscriber_type_subscriber_id' }
23
- t.references :subscribable, polymorphic: true, index: { name: 'idx_subscriptions_subscribable_type_subscribable_id' }
24
-
25
- t.timestamps
26
-
27
- end
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
- module NotificationSettings
2
-
3
- require 'notification_settings/configuration'
4
-
5
- require 'notification_settings/engine'
6
-
7
- autoload :Target, 'notification_settings/target'
8
- autoload :Subscriber, 'notification_settings/subscriber'
9
- autoload :Subscribable, 'notification_settings/subscribable'
10
- autoload :SettingLibrary, 'notification_settings/setting_library'
11
- autoload :SubscriptionLibrary, 'notification_settings/subscription_library'
12
- autoload :NotificationLibrary, 'notification_settings/notification_library'
13
- autoload :NotificationScopes, 'notification_settings/notification_scopes'
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
- module NotificationSettings
2
-
3
- class << self
4
- attr_accessor :configuration
5
- end
6
-
7
- def self.configure
8
- self.configuration ||= Configuration.new
9
- yield configuration
10
- end
11
-
12
- class Configuration
13
-
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
-
30
- end
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
- require 'rails/railtie'
2
- require 'active_record'
3
-
4
- module NotificationSettings
5
- class Engine < ::Rails::Engine
6
- end
7
- end
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
- require 'notification-handler'
2
- require 'active_support'
3
-
4
- module NotificationSettings
5
- module NotificationLibrary
6
-
7
- extend ActiveSupport::Concern
8
-
9
- included do
10
- before_create :validate_create
11
- belongs_to :subscription, class_name: 'NotificationSettings::Subscription', optional: true
12
-
13
- include NotificationSettings::NotificationLibrary::InstanceMethods
14
- end
15
-
16
- module InstanceMethods
17
-
18
- def category
19
- self[:category] || NotificationRenderer.configuration.default_category
20
- end
21
-
22
- private
23
-
24
- def validate_create
25
- valid = true
26
-
27
- if self.target.notification_setting.present?
28
- # Status
29
- valid = false if NotificationSettings.configuration.do_not_notify_statuses.include?(self.target.notification_setting.status)
30
-
31
- # Settings
32
- valid = false if !self.target.notification_setting.settings.dig(:enabled)
33
- ## Category
34
- valid = false if !self.target.notification_setting.category_settings.dig(self.category.to_sym, :enabled)
35
- end
36
-
37
- valid
38
- end
39
-
40
- def validate_push
41
- valid = true
42
-
43
- if self.target.notification_setting.present?
44
- # Status
45
- valid = false if NotificationSettings.configuration.do_not_push_statuses.include?(self.target.notification_setting.status)
46
-
47
- # Settings
48
- if self.push.kind_of?(Array)
49
- self.push.each do |pusher|
50
- valid = false if !self.target.notification_setting.settings.dig(pusher.to_sym) || ( !self.target.notification_setting.settings.dig(:index) && self.target.notification_setting.settings.dig(pusher.to_sym).nil? )
51
- ## Category
52
- valid = false if !self.target.notification_setting.category_settings.dig(self.category.to_sym, pusher.to_sym) || ( !self.target.notification_setting.category_settings.dig(self.category.to_sym, :index) && self.target.notification_setting.category_settings.dig(self.category.to_sym, pusher.to_sym).nil? )
53
- end
54
- else
55
- valid = false if !self.target.notification_setting.settings.dig(self.push.to_sym) || ( !self.target.notification_setting.settings.dig(:index) && self.target.notification_setting.settings.dig(self.push.to_sym).nil? )
56
- ## Category
57
- valid = false if !self.target.notification_setting.category_settings.dig(self.category.to_sym, self.push.to_sym) || ( !self.target.notification_setting.category_settings.dig(self.category.to_sym, :index) && self.target.notification_setting.category_settings.dig(self.category.to_sym, self.push.to_sym).nil? )
58
- end
59
- end
60
-
61
- valid
62
- end
63
-
64
- def initialize_pusher
65
- return unless validate_push
66
- super
67
- end
68
-
69
- end
70
-
71
- end
72
- end
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