simple_notifications 1.1.7 → 1.1.8
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac34511b2c3da57df59fcf6a5d31d4a3e108d45a93b56d78acae05b032303311
|
4
|
+
data.tar.gz: 3daf3195e073ed5117b8bda4f5cc14fa00b404b68934fbb7ff7348bea357c308
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4571f8e0080aa3592c5c30a22b09fe6a0f3d4336144268616bd3b922e6ecec64ffa3b71049628ac5faa6ee35c286876e7295aebe0f732a467d72f42c006a8360
|
7
|
+
data.tar.gz: eef1b1bbe353c76239379b739c73763b794c455e0388a10d233961a081a8a69b68870eede03f0bbdc39bd6995ee7ad2c349e6e26e92dcd5cd46a8944c2be09c8
|
@@ -1,33 +1,42 @@
|
|
1
1
|
module SimpleNotifications
|
2
2
|
module Base
|
3
|
-
mattr_accessor :options, :
|
4
|
-
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
3
|
+
mattr_accessor :options, :active
|
4
|
+
|
5
|
+
# notify sender: :product_class,
|
6
|
+
# receivers: :variants,
|
7
|
+
# action: [:follow, :update, :create, :destroy]
|
8
|
+
def acts_as_notifier(options = {})
|
9
|
+
Base.options = options
|
10
|
+
raise 'SimpleNotifications::ActionsNotAvailable' if Base.options[:actions].nil? || Base.options[:actions].empty?
|
11
|
+
|
12
|
+
# Default Assumptions
|
13
|
+
Base.options[:sender] ||= :user # Default Assumption: [:user] association is available.
|
14
|
+
Base.options[:entity_class] = self
|
15
|
+
|
16
|
+
# Filter system defined callback actions
|
17
|
+
# After this
|
18
|
+
# Base.options[:callbacks] -> will have all system defined actions only like (create/update/destroy)
|
19
|
+
# Base.options[:actions] -> will have user defined actions only.
|
20
|
+
Base.options[:callbacks] = []
|
21
|
+
%i[create update destroy].each {|method| Base.options[:callbacks] << Base.options[:actions].delete(method)}
|
22
|
+
Base.options[:callbacks].compact!
|
23
|
+
|
16
24
|
open_sender_class
|
17
25
|
open_receiver_class
|
18
26
|
open_notified_class
|
19
|
-
|
27
|
+
|
28
|
+
Base.active = true
|
20
29
|
end
|
21
30
|
|
22
|
-
#Getter to check if model is enabled with notifications
|
23
|
-
def
|
24
|
-
|
31
|
+
# Getter to check if model is enabled with notifications
|
32
|
+
def notifications_active?
|
33
|
+
!!Base.active
|
25
34
|
end
|
26
35
|
|
27
36
|
# Opening the class which is defined as sender.
|
28
37
|
def open_sender_class
|
29
38
|
# Define association for sender model
|
30
|
-
sender_class(
|
39
|
+
sender_class(Base.options[:sender]).class_eval do
|
31
40
|
has_many :sent_notifications, class_name: 'SimpleNotifications::Record', as: :sender
|
32
41
|
end
|
33
42
|
end
|
@@ -35,7 +44,7 @@ module SimpleNotifications
|
|
35
44
|
# Opening the classes which are defined as receivers.
|
36
45
|
def open_receiver_class
|
37
46
|
# Define association for receiver model
|
38
|
-
[receivers_class(
|
47
|
+
[receivers_class(Base.options[:receivers])].flatten.each do |base|
|
39
48
|
base.class_eval do
|
40
49
|
has_many :deliveries, class_name: 'SimpleNotifications::Delivery', as: :receiver
|
41
50
|
has_many :received_notifications, through: :deliveries, source: :simple_notification
|
@@ -46,22 +55,54 @@ module SimpleNotifications
|
|
46
55
|
# Opening the class on which the notify functionality is applied.
|
47
56
|
def open_notified_class
|
48
57
|
class_eval do
|
49
|
-
prepend NotificationActions
|
58
|
+
prepend NotificationActions # For adding all user defined methods at the last level of present class
|
50
59
|
attr_accessor :message, :notify_flag
|
51
60
|
|
61
|
+
# -------------------------------------------------- #
|
52
62
|
# Define association for the notified model
|
63
|
+
# -------------------------------------------------- #
|
53
64
|
has_many :notifications, class_name: 'SimpleNotifications::Record', as: :entity
|
54
|
-
has_many :notifiers, through: :notifications, source: :sender,
|
65
|
+
has_many :notifiers, through: :notifications, source: :sender,
|
66
|
+
source_type: sender_class(Base.options[:sender]).to_s
|
55
67
|
has_many :read_deliveries, through: :notifications, source: :read_deliveries
|
56
68
|
has_many :unread_deliveries, through: :notifications, source: :unread_deliveries
|
57
69
|
|
70
|
+
# -------------------------------------------------- #
|
71
|
+
# For defining the System defined callbacks
|
72
|
+
# -------------------------------------------------- #
|
73
|
+
|
58
74
|
# Callbacks
|
59
|
-
after_create_commit :create_notification
|
60
|
-
|
61
|
-
|
75
|
+
# after_create_commit :create_notification
|
76
|
+
Base.options[:callbacks].each do |callback|
|
77
|
+
send("after_#{callback}_commit", "#{callback}_notification")
|
78
|
+
end
|
79
|
+
|
80
|
+
def create_notification
|
81
|
+
notify(sender: get_obj(Base.options[:sender]),
|
82
|
+
receivers: get_obj(Base.options[:receivers]),
|
83
|
+
action: 'create',
|
84
|
+
message: default_message(self, get_obj(Base.options[:sender]), 'created'))
|
85
|
+
end
|
86
|
+
|
87
|
+
def update_notification
|
88
|
+
notify(sender: get_obj(Base.options[:sender]),
|
89
|
+
receivers: get_obj(Base.options[:receivers]),
|
90
|
+
action: 'update',
|
91
|
+
message: default_message(self, get_obj(Base.options[:sender]), 'updated'))
|
92
|
+
end
|
93
|
+
|
94
|
+
def destroy_notification
|
95
|
+
notify(sender: get_obj(Base.options[:sender]),
|
96
|
+
receivers: get_obj(Base.options[:receivers]),
|
97
|
+
action: 'destroy',
|
98
|
+
message: default_message(self, get_obj(Base.options[:sender]), 'destroyed'))
|
99
|
+
end
|
62
100
|
|
101
|
+
# -------------------------------------------------- #
|
102
|
+
# For defining the callbacks for user defined actions
|
103
|
+
# -------------------------------------------------- #
|
63
104
|
NotificationActions.module_eval do
|
64
|
-
|
105
|
+
Base.options[:actions].each do |action|
|
65
106
|
define_method(action) do
|
66
107
|
run_callbacks action do
|
67
108
|
super()
|
@@ -72,27 +113,35 @@ module SimpleNotifications
|
|
72
113
|
end
|
73
114
|
|
74
115
|
define_method("after_#{action}".to_sym) do
|
75
|
-
|
116
|
+
notify(sender: Base.options[:sender],
|
117
|
+
receivers: Base.options[:receivers],
|
118
|
+
action: action,
|
119
|
+
message: default_message(self, Base.options[:sender], action.to_s))
|
76
120
|
end
|
77
121
|
end
|
78
122
|
end
|
79
123
|
|
80
|
-
|
124
|
+
Base.options[:actions].each do |action|
|
81
125
|
define_model_callbacks action
|
82
126
|
send("before_#{action}", "before_#{action}".to_sym)
|
83
127
|
send("after_#{action}", "after_#{action}".to_sym)
|
84
128
|
end
|
85
129
|
|
86
|
-
#
|
87
|
-
#
|
88
|
-
#post.
|
130
|
+
# -------------------------------------------------- #
|
131
|
+
# Example
|
132
|
+
# post.notify(sender: :author, receivers: :followers, message: 'My Custom logic message')
|
133
|
+
# post.create(content: '', notify: false) -> It does not create the notification.
|
89
134
|
def notify(options = {})
|
90
|
-
options[:sender] ||=
|
91
|
-
options[:receivers] ||=
|
135
|
+
options[:sender] ||= Base.options[:sender]
|
136
|
+
options[:receivers] ||= Base.options[:receivers]
|
92
137
|
if notify_flag.nil? || (!notify_flag.nil? && !!notify_flag)
|
93
|
-
raise 'SimpleNotification::SenderReceiverError' unless
|
138
|
+
raise 'SimpleNotification::SenderReceiverError' unless Base.options[:sender] && Base.options[:receivers]
|
139
|
+
|
94
140
|
@message = options[:message] if options[:message]
|
95
|
-
notification = notifications.build(entity: self,
|
141
|
+
notification = notifications.build(entity: self,
|
142
|
+
sender: get_obj(options[:sender]),
|
143
|
+
action: '',
|
144
|
+
message: default_message(self, get_obj(options[:sender]), 'notified'))
|
96
145
|
[get_obj(options[:receivers])].flatten.each {|receiver| notification.deliveries.build(receiver: receiver)}
|
97
146
|
notification.save
|
98
147
|
end
|
@@ -109,21 +158,22 @@ module SimpleNotifications
|
|
109
158
|
|
110
159
|
def notificants
|
111
160
|
SimpleNotifications::Record.includes(deliveries: :receiver)
|
112
|
-
|
113
|
-
|
161
|
+
.collect(&:deliveries).flatten
|
162
|
+
.collect(&:receiver)
|
114
163
|
end
|
115
164
|
|
116
165
|
def read_marked_notificants
|
117
166
|
SimpleNotifications::Record.includes(deliveries: :receiver)
|
118
|
-
|
119
|
-
|
120
|
-
|
167
|
+
.collect(&:deliveries).flatten
|
168
|
+
.select(&:is_read)
|
169
|
+
.collect(&:receiver)
|
121
170
|
end
|
122
171
|
|
123
172
|
def unread_marked_notificants
|
124
|
-
SimpleNotifications::Record
|
125
|
-
|
126
|
-
|
173
|
+
SimpleNotifications::Record
|
174
|
+
.includes(deliveries: :receiver)
|
175
|
+
.collect(&:deliveries).flatten
|
176
|
+
.reject {|record| record.is_read}.collect(&:receiver)
|
127
177
|
end
|
128
178
|
|
129
179
|
# Mark notifications in read mode.
|
@@ -141,7 +191,9 @@ module SimpleNotifications
|
|
141
191
|
end
|
142
192
|
|
143
193
|
def default_message(entity, sender, action)
|
144
|
-
@message ||
|
194
|
+
@message ||
|
195
|
+
(method(Base.options[:notify_message]).call if !!Base.options[:notify_message]) ||
|
196
|
+
"#{get_obj(sender).class.name} #{action} #{entity.class.name} #{entity.name}"
|
145
197
|
end
|
146
198
|
|
147
199
|
private
|
@@ -149,18 +201,6 @@ module SimpleNotifications
|
|
149
201
|
def get_obj(sender_or_receivers)
|
150
202
|
sender_or_receivers.kind_of?(Symbol) ? send(sender_or_receivers) : sender_or_receivers
|
151
203
|
end
|
152
|
-
|
153
|
-
def create_notification
|
154
|
-
notify({sender: get_obj(@@options[:sender]), receivers: get_obj(@@options[:receivers]), message: default_message(self, get_obj(@@options[:sender]), 'created')})
|
155
|
-
end
|
156
|
-
|
157
|
-
def update_notification
|
158
|
-
notify({sender: get_obj(@@options[:sender]), receivers: get_obj(@@options[:receivers]), message: default_message(self, get_obj(@@options[:sender]), 'updated')})
|
159
|
-
end
|
160
|
-
|
161
|
-
def destroy_notification
|
162
|
-
notify({sender: get_obj(@@options[:sender]), receivers: get_obj(@@options[:receivers]), message: default_message(self, get_obj(@@options[:sender]), 'deleted')})
|
163
|
-
end
|
164
204
|
end
|
165
205
|
end
|
166
206
|
|