simple_notifications 1.1.0 → 1.1.1
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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f1709f0238eda36afb2285dd4e57a53ec62fcf1e80a9f3268d573294ea9fb4e1
|
4
|
+
data.tar.gz: d75e8e7a639e530bc92907050f365013b7c1901a3a7a6ffd019092d9e95130c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad26e5c56c0678bbe5f6805707783e4d489604d686212fa11c05010689d76fd245563d68026e38a22def0e624d533f185fb5832481274dfb67b88f0ebe7c2431
|
7
|
+
data.tar.gz: 298446be7bf1586b81636f1c179ca7224d7d64d423459b263f7f64334ccc1cd2e7062ca152ebb9fb406612b2364622cc4dbea3c5fccd3bde179fbe6ab2643ef5
|
data/README.md
CHANGED
@@ -39,12 +39,16 @@ rails db:migrate
|
|
39
39
|
Add following line to the model for which notifications functionality is required
|
40
40
|
|
41
41
|
```ruby
|
42
|
-
notify sender: :
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
42
|
+
notify sender: :product_class,
|
43
|
+
receivers: :variants,
|
44
|
+
actions: [:follow, :unfollow, :update, :create],
|
45
|
+
notify_message: :dd_message,
|
46
|
+
before_notify: :before_notify_method,
|
47
|
+
after_notify: :after_notify_method,
|
48
|
+
before_delivered: :before_delivered_method,
|
49
|
+
after_delivered: :after_delivered_method,
|
50
|
+
before_read: :before_read_method,
|
51
|
+
after_read: :after_read_method
|
48
52
|
```
|
49
53
|
Or you can provide ActiveRecord::Base object or ActiveRecord::Relation objects as
|
50
54
|
|
@@ -12,17 +12,32 @@ module SimpleNotifications
|
|
12
12
|
belongs_to :receiver, polymorphic: true
|
13
13
|
|
14
14
|
# Callbacks
|
15
|
-
|
15
|
+
before_update :before_read, if: proc {!!SimpleNotifications::Base.options[:before_read] && changes['is_read'] == [false, true]}
|
16
|
+
before_update :before_delivered, if: proc {!!SimpleNotifications::Base.options[:before_delivered] && changes['is_delivered'] == [false, true]}
|
17
|
+
after_update_commit :after_read, if: proc {!!SimpleNotifications::Base.options[:after_read] && previous_changes['is_read'] == [false, true]}
|
18
|
+
after_update_commit :after_delivered, if: proc {!!SimpleNotifications::Base.options[:after_delivered] && previous_changes['is_delivered'] == [false, true]}
|
19
|
+
|
20
|
+
def entity
|
21
|
+
simple_notification.entity
|
22
|
+
end
|
16
23
|
|
17
24
|
private
|
18
25
|
|
19
|
-
|
20
|
-
|
21
|
-
SimpleNotifications::
|
26
|
+
%w(read delivered).each do |update_type|
|
27
|
+
define_method("before_#{update_type}") do
|
28
|
+
call_method(SimpleNotifications::Base.options["before_#{update_type}".to_sym])
|
22
29
|
end
|
23
30
|
|
24
|
-
|
25
|
-
SimpleNotifications::
|
31
|
+
define_method("after_#{update_type}") do
|
32
|
+
call_method(SimpleNotifications::Base.options["after_#{update_type}".to_sym])
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def call_method(_method)
|
37
|
+
if _method.class == Symbol
|
38
|
+
entity.method(_method).call if entity.class.instance_methods(false).include?(_method)
|
39
|
+
elsif _method.class == Proc
|
40
|
+
_method.call
|
26
41
|
end
|
27
42
|
end
|
28
43
|
end
|
@@ -36,12 +36,17 @@ module SimpleNotifications
|
|
36
36
|
|
37
37
|
private
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
39
|
+
%w(before after).each do |call_type|
|
40
|
+
define_method("#{call_type}_actions".to_sym) do
|
41
|
+
_method = SimpleNotifications::Base.send(:options)["#{call_type}_notify".to_sym]
|
42
|
+
if _method.present?
|
43
|
+
if _method.class == Symbol
|
44
|
+
entity.method(_method).call if entity.class.instance_methods(false).include?(_method)
|
45
|
+
elsif _method.class == Proc
|
46
|
+
_method.call
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
45
50
|
end
|
46
51
|
end
|
47
52
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module SimpleNotifications
|
2
2
|
module Base
|
3
|
-
mattr_accessor :
|
3
|
+
mattr_accessor :options, :notified_flag
|
4
4
|
|
5
5
|
#Example
|
6
6
|
#notify(sender: :author, receivers: :followers)
|
@@ -8,16 +8,11 @@ module SimpleNotifications
|
|
8
8
|
# receivers: :variants,
|
9
9
|
# action: [:follow, :update, :create, :destroy]
|
10
10
|
def notify(options = {})
|
11
|
-
@@
|
12
|
-
@@
|
13
|
-
@@
|
14
|
-
@@
|
15
|
-
@@
|
16
|
-
|
17
|
-
SimpleNotifications::Record.before_notify = options[:before_notify] if !!options[:before_notify]
|
18
|
-
SimpleNotifications::Record.after_notify = options[:after_notify] if !!options[:after_notify]
|
19
|
-
SimpleNotifications::Delivery.after_delivered = options[:after_delivered] if !!options[:after_delivered]
|
20
|
-
SimpleNotifications::Delivery.after_read = options[:after_read] if !!options[:after_read]
|
11
|
+
@@options = options
|
12
|
+
@@options[:entity_class] = self
|
13
|
+
@@options[:callbacks] = []
|
14
|
+
[:create, :update, :destroy].each {|_method| @@options[:callbacks] << @@options[:actions].delete(_method)}
|
15
|
+
@@options[:callbacks].compact!
|
21
16
|
open_sender_class
|
22
17
|
open_receiver_class
|
23
18
|
open_notified_class
|
@@ -32,7 +27,7 @@ module SimpleNotifications
|
|
32
27
|
# Opening the class which is defined as sender.
|
33
28
|
def open_sender_class
|
34
29
|
# Define association for sender model
|
35
|
-
sender_class(@@sender).class_eval do
|
30
|
+
sender_class(@@options[:sender]).class_eval do
|
36
31
|
has_many :sent_notifications, class_name: 'SimpleNotifications::Record', as: :sender
|
37
32
|
end
|
38
33
|
end
|
@@ -40,7 +35,7 @@ module SimpleNotifications
|
|
40
35
|
# Opening the classes which are defined as receivers.
|
41
36
|
def open_receiver_class
|
42
37
|
# Define association for receiver model
|
43
|
-
[receivers_class(@@receivers)].flatten.each do |base|
|
38
|
+
[receivers_class(@@options[:receivers])].flatten.each do |base|
|
44
39
|
base.class_eval do
|
45
40
|
has_many :deliveries, class_name: 'SimpleNotifications::Delivery', as: :receiver
|
46
41
|
has_many :received_notifications, through: :deliveries, source: :simple_notification
|
@@ -56,16 +51,16 @@ module SimpleNotifications
|
|
56
51
|
|
57
52
|
# Define association for the notified model
|
58
53
|
has_many :notifications, class_name: 'SimpleNotifications::Record', as: :entity
|
59
|
-
has_many :notifiers, through: :notifications, source: :sender, source_type: sender_class(@@sender).to_s
|
54
|
+
has_many :notifiers, through: :notifications, source: :sender, source_type: sender_class(@@options[:sender]).to_s
|
60
55
|
SimpleNotifications::Record.class_eval do
|
61
|
-
[@@entity_class.receivers_class(@@receivers)].flatten.each do |receiver_class|
|
56
|
+
[@@options[:entity_class].receivers_class(@@options[:receivers])].flatten.each do |receiver_class|
|
62
57
|
has_many "#{receiver_class.name.downcase}_receivers".to_sym,
|
63
58
|
through: :deliveries,
|
64
59
|
source: :receiver,
|
65
60
|
source_type: receiver_class.name
|
66
61
|
end
|
67
62
|
end
|
68
|
-
[receivers_class(@@receivers)].flatten.each do |receiver_class|
|
63
|
+
[receivers_class(@@options[:receivers])].flatten.each do |receiver_class|
|
69
64
|
has_many "#{receiver_class.name.downcase}_notificants".to_sym,
|
70
65
|
through: :notifications,
|
71
66
|
source: "#{receiver_class.name.downcase}_receivers".to_sym
|
@@ -77,27 +72,26 @@ module SimpleNotifications
|
|
77
72
|
# has_many :unread_notificants, through: :unread_deliveries, source: :receiver, source_type: 'User'
|
78
73
|
|
79
74
|
# Callbacks
|
80
|
-
|
81
|
-
after_update_commit :update_notification, if: proc {@notify.nil? || !!@notify}
|
75
|
+
@@options[:callbacks].each {|_method| send("after_#{_method}_commit", "#{_method}_notification".to_sym)}
|
82
76
|
|
83
77
|
NotificationActions.module_eval do
|
84
|
-
@@actions.each do |action|
|
78
|
+
@@options[:actions].each do |action|
|
85
79
|
define_method(action) do
|
86
80
|
run_callbacks action do
|
87
81
|
super()
|
88
82
|
end
|
89
83
|
end
|
90
84
|
|
91
|
-
define_method("before_#{action}") do
|
85
|
+
define_method("before_#{action}".to_sym) do
|
92
86
|
end
|
93
87
|
|
94
|
-
define_method("after_#{action}") do
|
95
|
-
self.notify(sender: @@sender, receivers: @@receivers, message: default_message(self, @@sender, action.to_s))
|
88
|
+
define_method("after_#{action}".to_sym) do
|
89
|
+
self.notify(sender: @@options[:sender], receivers: @@options[:receivers], message: default_message(self, @@options[:sender], action.to_s))
|
96
90
|
end
|
97
91
|
end
|
98
92
|
end
|
99
93
|
|
100
|
-
@@actions.each do |action|
|
94
|
+
@@options[:actions].each do |action|
|
101
95
|
define_model_callbacks action
|
102
96
|
send("before_#{action}", "before_#{action}".to_sym)
|
103
97
|
send("after_#{action}", "after_#{action}".to_sym)
|
@@ -107,7 +101,7 @@ module SimpleNotifications
|
|
107
101
|
#post.notify(sender: :author, receivers: :followers, message: 'My Custom logic message')
|
108
102
|
#post.create(content: '', notify: false) -> It does not create the notification.
|
109
103
|
def notify(options = {})
|
110
|
-
raise 'SimpleNotification::SenderReceiverError' unless options[:sender] && options[:receivers]
|
104
|
+
raise 'SimpleNotification::SenderReceiverError' unless @@options[:sender] && @@options[:receivers]
|
111
105
|
@message = options[:message] if options[:message]
|
112
106
|
notification = notifications.build(entity: self, sender: get_obj(options[:sender]), message: default_message(self, get_obj(options[:sender]), 'created'))
|
113
107
|
get_obj(options[:receivers]).each {|receiver| notification.deliveries.build(receiver: receiver)}
|
@@ -149,7 +143,7 @@ module SimpleNotifications
|
|
149
143
|
end
|
150
144
|
|
151
145
|
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}."
|
146
|
+
@message || (method(@@options[:notify_message]).call if !!@@options[:notify_message]) || "#{get_obj(sender).class.name} #{action} #{entity.class.name} #{entity.name}."
|
153
147
|
end
|
154
148
|
|
155
149
|
private
|
@@ -159,11 +153,15 @@ module SimpleNotifications
|
|
159
153
|
end
|
160
154
|
|
161
155
|
def create_notification
|
162
|
-
notify({sender: get_obj(@@sender), receivers: get_obj(@@receivers), message: default_message(self, get_obj(@@sender), 'created')})
|
156
|
+
notify({sender: get_obj(@@options[:sender]), receivers: get_obj(@@options[:receivers]), message: default_message(self, get_obj(@@options[:sender]), 'created')})
|
163
157
|
end
|
164
158
|
|
165
159
|
def update_notification
|
166
|
-
notify({sender: get_obj(@@sender), receivers: get_obj(@@receivers), message: default_message(self, get_obj(@@sender), 'updated')})
|
160
|
+
notify({sender: get_obj(@@options[:sender]), receivers: get_obj(@@options[:receivers]), message: default_message(self, get_obj(@@options[:sender]), 'updated')})
|
161
|
+
end
|
162
|
+
|
163
|
+
def destroy_notification
|
164
|
+
notify({sender: get_obj(@@options[:sender]), receivers: get_obj(@@options[:receivers]), message: default_message(self, get_obj(@@options[:sender]), 'deleted')})
|
167
165
|
end
|
168
166
|
end
|
169
167
|
end
|