notification-handler 1.2.5 → 1.2.6

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.
@@ -1,6 +1,8 @@
1
- class NotificationHandler::Notification < ApplicationRecord
2
-
3
- include NotificationHandler::NotificationLibrary
4
- include NotificationHandler::NotificationScopes
5
-
6
- end
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
- 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 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
- def create_notifications_migration_file
26
- migration_template 'notifications_migration.rb.erb', 'db/migrate/notification_handler_migration.rb', migration_version: migration_version
27
- end
28
- def create_notification_model
29
- template 'notification_model.rb', 'app/models/notification.rb'
30
- end
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
- def show_readme
33
- readme 'README.md'
34
- end
33
+ def create_notification_model
34
+ template 'notification_model.rb', 'app/models/notification.rb'
35
+ end
35
36
 
36
- private
37
+ private
37
38
 
38
- def migration_version
39
- if Rails.version >= '5.0.0'
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
- NotificationHandler.configure do |config|
2
-
3
- # Cache amount of unread and read notifications for notification targets
4
- # Learn more: https://github.com/jonhue/notifications-rails/tree/master/notification-handler#caching
5
- # config.cache = false
6
-
7
- # Groups are a powerful way to bulk-create notifications for multiple objects that don't necessarily have a common class.
8
- # Learn more: https://github.com/jonhue/notifications-rails/tree/master/notification-handler#groups
9
- # config.define_group :subscribers, User.where(subscriber: true)
10
-
11
- end
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,2 +1,4 @@
1
- class Notification < NotificationHandler::Notification
2
- end
1
+ # frozen_string_literal: true
2
+
3
+ class Notification < NotificationHandler::Notification
4
+ end
@@ -1,16 +1,16 @@
1
- class NotificationHandlerMigration < ActiveRecord::Migration<%= migration_version %>
2
- def change
3
- create_table :notifications do |t|
4
-
5
- t.references :target, polymorphic: true, index: true
6
- t.references :object, polymorphic: true, index: true
7
-
8
- t.boolean :read, default: false, null: false, index: true
9
-
10
- t.text :metadata
11
-
12
- t.timestamps
13
-
14
- end
15
- end
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
@@ -1,16 +1,16 @@
1
- module NotificationHandler
2
-
3
- require 'notification_handler/configuration'
1
+ # frozen_string_literal: true
4
2
 
5
- require 'notification_handler/engine'
3
+ module NotificationHandler
4
+ require 'notification_handler/configuration'
6
5
 
7
- autoload :Group, 'notification_handler/group'
6
+ require 'notification_handler/engine'
8
7
 
9
- autoload :Target, 'notification_handler/target'
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
- require 'notification_handler/railtie'
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
- module NotificationHandler
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 :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
- self.groups << ::NotificationHandler::Group.new(name.to_sym, target_scope)
24
- end
25
-
26
- end
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
- require 'rails/railtie'
2
- require 'active_record'
3
-
4
- module NotificationHandler
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 NotificationHandler
7
+ class Engine < ::Rails::Engine
8
+ end
9
+ end
@@ -1,17 +1,19 @@
1
- module NotificationHandler
2
- class Group
3
-
4
- attr_accessor :name
5
- attr_accessor :target_scope
6
-
7
- def initialize name, target_scope
8
- @name = name
9
- @target_scope = target_scope
10
- end
11
-
12
- def self.find_by_name name
13
- NotificationHandler.configuration.groups.select { |group| group.name == name.to_sym }
14
- end
15
-
16
- end
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
- require 'active_support'
2
-
3
- module NotificationHandler
4
- module NotificationLibrary
5
-
6
- extend ActiveSupport::Concern
7
-
8
- included do
9
- self.inheritance_column = :_type_disabled
10
-
11
- before_validation :create_for_group
12
- after_commit :cache
13
-
14
- serialize :metadata, Hash
15
- attr_accessor :group
16
-
17
- belongs_to :target, polymorphic: true
18
- belongs_to :object, polymorphic: true, optional: true
19
-
20
- include NotificationHandler::NotificationLibrary::InstanceMethods
21
-
22
- include NotificationRenderer::NotificationLibrary if defined?(NotificationRenderer)
23
- include NotificationPusher::NotificationLibrary if defined?(NotificationPusher)
24
- include NotificationSettings::NotificationLibrary if defined?(NotificationSettings)
25
- end
26
-
27
- module InstanceMethods
28
-
29
- def read?
30
- self.read
31
- end
32
- def unread?
33
- !self.read
34
- end
35
-
36
- private
37
-
38
- def create_for_group
39
- unless self.group.nil?
40
- target_scope = NotificationHandler::Group.find_by_name(self.group).last.target_scope
41
- target_scope&.each do |target|
42
- notification = self.dup
43
- notification.target = target
44
- notification.group = nil
45
- notification.save
46
- end
47
- return false
48
- end
49
- end
50
-
51
- def cache
52
- if self.read_changed?
53
- self.target.read_notification_count = self.target.notifications.read.count
54
- self.target.unread_notification_count = self.target.notifications.unread.count
55
- self.target.save!
56
- end
57
- end
58
-
59
- end
60
-
61
- end
62
- end
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
- require 'active_support'
2
-
3
- module NotificationHandler
4
- module NotificationScopes
5
-
6
- extend ActiveSupport::Concern
7
-
8
- included do
9
- scope :read, -> { where(read: true) }
10
- scope :unread, -> { where(read: false) }
11
-
12
- include NotificationRenderer::NotificationScopes if defined?(NotificationRenderer)
13
- include NotificationSettings::NotificationScopes if defined?(NotificationSettings)
14
- end
15
-
16
- end
17
- end
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