notification-handler 2.0.0 → 3.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f2cdb58ba8f4da880b60d9443747199aac809a9fc881c52ea6d6521b02ebb1c6
4
- data.tar.gz: 2b22c562259da40608261bd742224fc6302ba678e2536ece9c01a01a01c51d82
3
+ metadata.gz: 1dc02802065493c5e297334667b3331b8b0c8757e80989b052d6426b154d450b
4
+ data.tar.gz: 4c3f0782b4a05e9df8960cfcfcd18cbb27bc14d25855fa0f63ce0b973324b55b
5
5
  SHA512:
6
- metadata.gz: a6eb0d2c4cc82ca45ca54d08bc4bf75aaa531d793a31109c0644345e2e00d392f84bf145b414863b835225e52a2249d64ff12df872b2698657ba6ce0b7401693
7
- data.tar.gz: cac446fcf48614af0346f947a181be5bfbb5a2fe9f54dd3aebd7e0b231d2550d73f851fad439cf3a07565e8daa9f7278ccc9c5ddd59395b8ee82a1d8e9d50aea
6
+ metadata.gz: 9a49f5df7d39be5495797718c4909b35c831bcd59dfcde06b14ad7b63a8866d4d9e666cb9de868b30fc18008db0fc9090f8a36bcce98e8fe339625c7b4775ccd
7
+ data.tar.gz: fc6f3e388e7cb39e44fcda525b4b6adde35dd7d1adb05733010d4aa1a7c8d4006efbcd04f23f0b15ae24d2257aa05b12bf7ea1891d9d513c7e54f4aa2c86cb5d
data/README.md CHANGED
@@ -99,7 +99,7 @@ Notification.read
99
99
  Notification.unread
100
100
 
101
101
  # Number of unread notifications
102
- Notification.unread.count
102
+ Notification.unread.size
103
103
  ```
104
104
 
105
105
  ### `notification_target`
@@ -124,8 +124,6 @@ You can create a notification from a `target`:
124
124
  User.first.notify(object: Recipe.first)
125
125
  ```
126
126
 
127
- ...
128
-
129
127
  ### `notification_object`
130
128
 
131
129
  When using records of an ActiveRecord class as notification objects, add this to your class:
@@ -142,8 +140,6 @@ Now associated notifications are easy to access:
142
140
  notifications = Recipe.first.belonging_notifications
143
141
  ```
144
142
 
145
- ...
146
-
147
143
  ### Groups
148
144
 
149
145
  Groups are a powerful way to bulk-create notifications for multiple objects that don't necessarily have a common class.
@@ -176,10 +172,12 @@ The only requirement is that the result of evaluating the proc be Enumerable.
176
172
  Bulk-creation of notifications for a certain group is fairly simple:
177
173
 
178
174
  ```ruby
179
- notification = Notification.create(object: Recipe.first, group: :subscribers)
180
- notification = Notification.create(object: Recipe.first, group: :company_members, group_args: 4)
175
+ notifications = Notification.for_group(:subscribers, attrs: { object: Recipe.first })
176
+ notifications = Notification.for_group(:company_members, args: [4], attrs: { object: Recipe.first })
181
177
  ```
182
178
 
179
+ `Notification.for_group` returns an array of `Notification` objects.
180
+
183
181
  **Note:** You are not able to set the `target` attribute when a `group` has been specified.
184
182
 
185
183
  ### Caching
@@ -14,7 +14,8 @@ module NotificationHandler
14
14
  if ActiveRecord::Base.timestamped_migrations
15
15
  Time.now.utc.strftime('%Y%m%d%H%M%S')
16
16
  else
17
- format('%.3d', current_migration_number(dirname) + 1)
17
+ format('%<migration_number>.3d',
18
+ migration_number: current_migration_number(dirname) + 1)
18
19
  end
19
20
  end
20
21
 
@@ -9,12 +9,9 @@ module NotificationHandler
9
9
  included do
10
10
  self.inheritance_column = :_type_disabled
11
11
 
12
- before_validation :create_for_group
13
12
  after_commit :cache
14
13
 
15
14
  serialize :metadata, Hash
16
- attr_accessor :group
17
- attr_accessor :group_args
18
15
 
19
16
  belongs_to :target, polymorphic: true
20
17
  belongs_to :object, polymorphic: true, optional: true
@@ -32,6 +29,18 @@ module NotificationHandler
32
29
  end
33
30
  end
34
31
 
32
+ module ClassMethods
33
+ def for_group(group, args: [], attrs: {})
34
+ return if group.nil?
35
+
36
+ target_scope = NotificationHandler::Group.find_by_name!(group)
37
+ .target_scope
38
+ target_scope.call(*args)&.map do |target|
39
+ Notification.create(attrs.merge(target: target))
40
+ end
41
+ end
42
+ end
43
+
35
44
  module InstanceMethods
36
45
  def read?
37
46
  read
@@ -43,24 +52,11 @@ module NotificationHandler
43
52
 
44
53
  private
45
54
 
46
- def create_for_group
47
- return if group.nil?
48
-
49
- target_scope = NotificationHandler::Group.find_by_name!(group)
50
- .target_scope
51
- target_scope.call(*group_args)&.each_with_index do |target, index|
52
- notification = index.zero? ? self : dup
53
- notification.target = target
54
- notification.group = nil
55
- notification.save!
56
- end
57
- end
58
-
59
55
  def cache
60
56
  return unless read_changed?
61
57
 
62
- target.read_notification_count = target.notifications.read.count
63
- target.unread_notification_count = target.notifications.unread.count
58
+ target.read_notification_count = target.notifications.read.size
59
+ target.unread_notification_count = target.notifications.unread.size
64
60
  target.save!
65
61
  end
66
62
  end
@@ -1,25 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'active_support'
4
+
3
5
  module NotificationHandler
4
6
  module Object
5
- def self.included(base)
6
- base.extend(ClassMethods)
7
- end
7
+ extend ActiveSupport::Concern
8
8
 
9
- module ClassMethods
10
- def notification_object
9
+ included do
10
+ def self.notification_object
11
11
  has_many :belonging_notifications,
12
12
  as: :object, class_name: 'Notification', dependent: :destroy
13
- include NotificationHandler::Object::InstanceMethods
14
-
15
- return unless defined?(NotificationSettings)
16
13
 
17
- include NotificationSettings::Subscribable
14
+ # rubocop:disable Style/GuardClause
15
+ if defined?(NotificationSettings)
16
+ include NotificationSettings::Subscribable
17
+ end
18
+ # rubocop:enable Style/GuardClause
18
19
  end
19
20
  end
20
-
21
- module InstanceMethods
22
- # ...
23
- end
24
21
  end
25
22
  end
@@ -1,20 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'active_support'
4
+
3
5
  module NotificationHandler
4
6
  module Target
5
- def self.included(base)
6
- base.extend(ClassMethods)
7
- end
7
+ extend ActiveSupport::Concern
8
8
 
9
- module ClassMethods
10
- def notification_target
9
+ included do
10
+ def self.notification_target
11
11
  has_many :notifications, as: :target, dependent: :destroy
12
12
  include NotificationHandler::Target::InstanceMethods
13
13
 
14
14
  include NotificationSettings::Target if defined?(NotificationSettings)
15
- return unless defined?(NotificationSettings)
16
-
17
- include NotificationSettings::Subscriber
15
+ # rubocop:disable Style/GuardClause
16
+ if defined?(NotificationSettings)
17
+ include NotificationSettings::Subscriber
18
+ end
19
+ # rubocop:enable Style/GuardClause
18
20
  end
19
21
  end
20
22
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: notification-handler
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 3.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Hübotter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-04 00:00:00.000000000 Z
11
+ date: 2019-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -174,7 +174,8 @@ files:
174
174
  homepage: https://github.com/jonhue/notifications-rails/tree/master/notification-handler
175
175
  licenses:
176
176
  - MIT
177
- metadata: {}
177
+ metadata:
178
+ github_repo: ssh://github.com/jonhue/notifications-rails
178
179
  post_install_message:
179
180
  rdoc_options: []
180
181
  require_paths: