acts_as_notifiable 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/acts_as_notifiable/notifiable.rb +31 -21
- data/lib/app/models/notification.rb +1 -1
- metadata +1 -1
@@ -1,5 +1,12 @@
|
|
1
1
|
module ActsAsNotifiable
|
2
2
|
def acts_as_notifiable(options={})
|
3
|
+
if respond_to?(:class_attribute)
|
4
|
+
class_attribute :readable_options
|
5
|
+
else
|
6
|
+
class_inheritable_accessor :readable_options
|
7
|
+
end
|
8
|
+
self.readable_options = options
|
9
|
+
|
3
10
|
has_many :notifications, as: :notifiable, dependent: :destroy
|
4
11
|
|
5
12
|
after_create :create_notification
|
@@ -9,26 +16,27 @@ module ActsAsNotifiable
|
|
9
16
|
# Find all notifications by type
|
10
17
|
def self.find_all_unnoticed_by(user, type=nil, type_id=nil)
|
11
18
|
@notifications = Notification.where(notifiable_type: self.to_s, user_id: user.id)
|
12
|
-
self.polymorphic_queries(type, type_id)
|
19
|
+
@notifications = self.polymorphic_queries(@notifications, type, type_id)
|
13
20
|
@notifications
|
14
21
|
end
|
15
22
|
|
16
23
|
# Mark all notifications noticed by type
|
17
24
|
def self.mark_all_noticed_by(user, type=nil, type_id=nil)
|
18
25
|
@notifications = Notification.where(user_id: user.id, notifiable_type: self.to_s)
|
19
|
-
self.polymorphic_queries(type, type_id)
|
26
|
+
@notifications = self.polymorphic_queries(@notifications, type, type_id)
|
20
27
|
@notifications.destroy_all
|
21
28
|
end
|
22
29
|
|
23
30
|
private
|
24
31
|
|
25
|
-
def self.polymorphic_queries(type, type_id)
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
+
def self.polymorphic_queries(notifications, type, type_id)
|
33
|
+
notifications = notifications.where(polymorphic_type: type)
|
34
|
+
notifications = notifications.where(polymorphic_id: type_id) if type_id
|
35
|
+
notifications
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.polymorphic?
|
39
|
+
self.readable_options[:polymorphic] == true
|
32
40
|
end
|
33
41
|
|
34
42
|
include NotifiableInstanceMethods
|
@@ -38,32 +46,34 @@ module ActsAsNotifiable
|
|
38
46
|
# Check if the notifiable object has been read by the user (no Notification will exist)
|
39
47
|
def unnoticed_by?(user, type=nil, type_id=nil)
|
40
48
|
@notifications = Notification.where(notifiable_id: self.id, notifiable_type: self.class.to_s, user_id: user.id)
|
41
|
-
self.class.polymorphic_queries(type, type_id)
|
49
|
+
@notifications = self.class.polymorphic_queries(@notifications, type, type_id)
|
42
50
|
!@notifications.empty?
|
43
51
|
end
|
44
52
|
|
45
53
|
# mark the notifiable object as read (remove the Notification)
|
46
54
|
def mark_as_noticed_by(user, type=nil, type_id=nil)
|
47
55
|
@notifications = Notification.where(notifiable_id: self.id, notifiable_type: self.class.to_s, user_id: user.id)
|
48
|
-
self.class.polymorphic_queries(type, type_id)
|
56
|
+
@notifications = self.class.polymorphic_queries(@notifications, type, type_id)
|
49
57
|
@notifications.first.destroy if self.unnoticed_by?(user, type, type_id)
|
50
58
|
end
|
51
59
|
|
52
60
|
# Callback method to create the notification
|
53
61
|
def create_notification
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
62
|
+
if self.class.polymorphic?
|
63
|
+
polymorphic_array = polymorphic_sender
|
64
|
+
Notification.find_or_create_by_user_id_and_notifiable_id_and_notifiable_type_and_polymorphic_type_and_polymorphic_id(self.user_id, self.id, self.class.to_s, polymorphic_array[0], polymorphic_array[1])
|
65
|
+
else
|
58
66
|
Notification.find_or_create_by_user_id_and_notifiable_id_and_notifiable_type(self.user_id, self.id, self.class.to_s)
|
59
|
-
|
67
|
+
end
|
60
68
|
end
|
61
69
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
70
|
+
# Returns class of polymorphic association and id of the association in a array
|
71
|
+
def polymorphic_sender
|
72
|
+
attribute_type = ""
|
73
|
+
self.attributes.each{|a| attribute_type = a[0] if a[0] =~ /ble_type/ }
|
74
|
+
attribute_type = attribute_type.gsub('_type', '')
|
75
|
+
polymorphic_parent = self.send(attribute_type)
|
76
|
+
[polymorphic_parent.class.to_s, polymorphic_parent.id]
|
67
77
|
end
|
68
78
|
end
|
69
79
|
end
|