simple_notifications 1.0.4 → 1.1.0
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.
- checksums.yaml +4 -4
- data/README.md +3 -1
- data/lib/simple_notifications.rb +1 -0
- data/lib/simple_notifications/app/models/delivery.rb +10 -1
- data/lib/simple_notifications/base.rb +48 -22
- data/lib/simple_notifications/notification_actions.rb +7 -0
- data/lib/simple_notifications/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 901a82711bccfaa4b7eb9294b84eddb4e9cc4a439660b3bdbd9103aace8c919a
|
4
|
+
data.tar.gz: 89b3175be328a334f75ebb585ec61019f16dab74f366072f3d1003c005d0a00b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c12be42028d7096e859733f290cdf12b5050b5103c70d7d8e7b94af2e338f69e2e1f1deccd05405dacf675c05d1f435561307263ed55768a60f5850f3d14bee
|
7
|
+
data.tar.gz: 14f6988d25a32dc032e7c40a5b8a131127ebd42741719aaa1f9ac07cb6ea04d47bcfc313c525e35758a7b56c7c56b5a87944e0f545645a017def7f27e3f38b9c
|
data/README.md
CHANGED
@@ -42,7 +42,9 @@ Add following line to the model for which notifications functionality is require
|
|
42
42
|
notify sender: :author,
|
43
43
|
receivers: :followers,
|
44
44
|
before_notify: proc {puts 'Before Notifications actions'},
|
45
|
-
after_notify: proc {puts 'Deliver Push notifications'}
|
45
|
+
after_notify: proc {puts 'Deliver Push notifications'},
|
46
|
+
after_delivered: proc {puts 'Notification delivered'},
|
47
|
+
after_read: proc { puts 'Notification marked as read'}
|
46
48
|
```
|
47
49
|
Or you can provide ActiveRecord::Base object or ActiveRecord::Relation objects as
|
48
50
|
|
data/lib/simple_notifications.rb
CHANGED
@@ -2,6 +2,9 @@ module SimpleNotifications
|
|
2
2
|
class Delivery < ActiveRecord::Base
|
3
3
|
self.table_name = 'deliveries'
|
4
4
|
|
5
|
+
# Class Attribute Accessors
|
6
|
+
cattr_accessor :after_delivered, :after_read
|
7
|
+
|
5
8
|
# Associations
|
6
9
|
belongs_to :simple_notification,
|
7
10
|
class_name: 'SimpleNotifications::Record',
|
@@ -9,12 +12,18 @@ module SimpleNotifications
|
|
9
12
|
belongs_to :receiver, polymorphic: true
|
10
13
|
|
11
14
|
# Callbacks
|
12
|
-
|
15
|
+
after_update_commit :after_create_actions
|
13
16
|
|
14
17
|
private
|
15
18
|
|
16
19
|
def after_create_actions
|
20
|
+
if !!SimpleNotifications::Delivery.after_delivered && previous_changes['is_delivered'] == [false, true]
|
21
|
+
SimpleNotifications::Delivery.after_delivered.call
|
22
|
+
end
|
17
23
|
|
24
|
+
if !!SimpleNotifications::Delivery.after_read && previous_changes['is_read'] == [false, true]
|
25
|
+
SimpleNotifications::Delivery.after_read.call
|
26
|
+
end
|
18
27
|
end
|
19
28
|
end
|
20
29
|
end
|
@@ -1,28 +1,34 @@
|
|
1
1
|
module SimpleNotifications
|
2
2
|
module Base
|
3
|
-
mattr_accessor :notified_flag, :entity_class, :sender, :receivers
|
4
|
-
|
5
|
-
#Getter to check if model is enabled with notifications
|
6
|
-
def notified?
|
7
|
-
!!@@notified_flag
|
8
|
-
end
|
3
|
+
mattr_accessor :notified_flag, :entity_class, :sender, :receivers, :actions, :notify_message
|
9
4
|
|
10
5
|
#Example
|
11
6
|
#notify(sender: :author, receivers: :followers)
|
7
|
+
#notify sender: :product_class,
|
8
|
+
# receivers: :variants,
|
9
|
+
# action: [:follow, :update, :create, :destroy]
|
12
10
|
def notify(options = {})
|
13
11
|
@@entity_class = self
|
14
12
|
@@sender = options[:sender]
|
15
13
|
@@receivers = options[:receivers]
|
14
|
+
@@actions = options[:actions]
|
15
|
+
@@notify_message = options[:notify_message]
|
16
|
+
|
16
17
|
SimpleNotifications::Record.before_notify = options[:before_notify] if !!options[:before_notify]
|
17
18
|
SimpleNotifications::Record.after_notify = options[:after_notify] if !!options[:after_notify]
|
18
|
-
|
19
|
+
SimpleNotifications::Delivery.after_delivered = options[:after_delivered] if !!options[:after_delivered]
|
20
|
+
SimpleNotifications::Delivery.after_read = options[:after_read] if !!options[:after_read]
|
19
21
|
open_sender_class
|
20
22
|
open_receiver_class
|
21
23
|
open_notified_class
|
22
|
-
|
23
24
|
@@notified_flag = true
|
24
25
|
end
|
25
26
|
|
27
|
+
#Getter to check if model is enabled with notifications
|
28
|
+
def notified?
|
29
|
+
!!@@notified_flag
|
30
|
+
end
|
31
|
+
|
26
32
|
# Opening the class which is defined as sender.
|
27
33
|
def open_sender_class
|
28
34
|
# Define association for sender model
|
@@ -45,12 +51,12 @@ module SimpleNotifications
|
|
45
51
|
# Opening the class on which the notify functionality is applied.
|
46
52
|
def open_notified_class
|
47
53
|
class_eval do
|
54
|
+
prepend NotificationActions
|
48
55
|
attr_accessor :message, :notify
|
56
|
+
|
49
57
|
# Define association for the notified model
|
50
58
|
has_many :notifications, class_name: 'SimpleNotifications::Record', as: :entity
|
51
59
|
has_many :notifiers, through: :notifications, source: :sender, source_type: sender_class(@@sender).to_s
|
52
|
-
|
53
|
-
# Opening the Notification class.
|
54
60
|
SimpleNotifications::Record.class_eval do
|
55
61
|
[@@entity_class.receivers_class(@@receivers)].flatten.each do |receiver_class|
|
56
62
|
has_many "#{receiver_class.name.downcase}_receivers".to_sym,
|
@@ -59,13 +65,11 @@ module SimpleNotifications
|
|
59
65
|
source_type: receiver_class.name
|
60
66
|
end
|
61
67
|
end
|
62
|
-
|
63
68
|
[receivers_class(@@receivers)].flatten.each do |receiver_class|
|
64
69
|
has_many "#{receiver_class.name.downcase}_notificants".to_sym,
|
65
70
|
through: :notifications,
|
66
71
|
source: "#{receiver_class.name.downcase}_receivers".to_sym
|
67
72
|
end
|
68
|
-
|
69
73
|
has_many :read_deliveries, through: :notifications, source: :read_deliveries
|
70
74
|
has_many :unread_deliveries, through: :notifications, source: :unread_deliveries
|
71
75
|
# has_many :notificants, through: :notifications, source: :receivers
|
@@ -76,9 +80,27 @@ module SimpleNotifications
|
|
76
80
|
after_create_commit :create_notification, if: proc {@notify.nil? || !!@notify}
|
77
81
|
after_update_commit :update_notification, if: proc {@notify.nil? || !!@notify}
|
78
82
|
|
79
|
-
|
80
|
-
|
81
|
-
|
83
|
+
NotificationActions.module_eval do
|
84
|
+
@@actions.each do |action|
|
85
|
+
define_method(action) do
|
86
|
+
run_callbacks action do
|
87
|
+
super()
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
define_method("before_#{action}") do
|
92
|
+
end
|
93
|
+
|
94
|
+
define_method("after_#{action}") do
|
95
|
+
self.notify(sender: @@sender, receivers: @@receivers, message: default_message(self, @@sender, action.to_s))
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
@@actions.each do |action|
|
101
|
+
define_model_callbacks action
|
102
|
+
send("before_#{action}", "before_#{action}".to_sym)
|
103
|
+
send("after_#{action}", "after_#{action}".to_sym)
|
82
104
|
end
|
83
105
|
|
84
106
|
#Example
|
@@ -87,12 +109,16 @@ module SimpleNotifications
|
|
87
109
|
def notify(options = {})
|
88
110
|
raise 'SimpleNotification::SenderReceiverError' unless options[:sender] && options[:receivers]
|
89
111
|
@message = options[:message] if options[:message]
|
90
|
-
notification = notifications.build(entity: self, sender: options[:sender],
|
91
|
-
|
92
|
-
options[:receivers].each {|receiver| notification.deliveries.build(receiver: receiver)}
|
112
|
+
notification = notifications.build(entity: self, sender: get_obj(options[:sender]), message: default_message(self, get_obj(options[:sender]), 'created'))
|
113
|
+
get_obj(options[:receivers]).each {|receiver| notification.deliveries.build(receiver: receiver)}
|
93
114
|
notification.save
|
94
115
|
end
|
95
116
|
|
117
|
+
# Check if notifications has already been delivered.
|
118
|
+
def notified?
|
119
|
+
!notifications.blank?
|
120
|
+
end
|
121
|
+
|
96
122
|
def notificants
|
97
123
|
#TODO : Need to eager load
|
98
124
|
SimpleNotifications::Record.where(entity: self).collect {|notification| notification.deliveries.collect(&:receiver)}.flatten
|
@@ -122,16 +148,16 @@ module SimpleNotifications
|
|
122
148
|
(notificants ? read_deliveries.where(receiver: notificants) : read_deliveries).update_all(is_read: false)
|
123
149
|
end
|
124
150
|
|
151
|
+
def default_message(entity, sender, action)
|
152
|
+
@message || (method(@@notify_message).call if !!@@notify_message) || "#{get_obj(sender).class.name} #{action} #{entity.class.name} #{entity.name}."
|
153
|
+
end
|
154
|
+
|
125
155
|
private
|
126
156
|
|
127
157
|
def get_obj(sender_or_receivers)
|
128
158
|
sender_or_receivers.kind_of?(Symbol) ? send(sender_or_receivers) : sender_or_receivers
|
129
159
|
end
|
130
160
|
|
131
|
-
def default_message(entity, sender, action)
|
132
|
-
@message || "#{entity.class.name} #{entity.name} #{action}."
|
133
|
-
end
|
134
|
-
|
135
161
|
def create_notification
|
136
162
|
notify({sender: get_obj(@@sender), receivers: get_obj(@@receivers), message: default_message(self, get_obj(@@sender), 'created')})
|
137
163
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_notifications
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ashish Garg
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-12-
|
11
|
+
date: 2018-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -82,6 +82,7 @@ files:
|
|
82
82
|
- lib/simple_notifications/app/models/delivery.rb
|
83
83
|
- lib/simple_notifications/app/models/simple_notification.rb
|
84
84
|
- lib/simple_notifications/base.rb
|
85
|
+
- lib/simple_notifications/notification_actions.rb
|
85
86
|
- lib/simple_notifications/version.rb
|
86
87
|
- simple_notifications.gemspec
|
87
88
|
homepage: https://github.com/aashishgarg/simple_notifications
|