acts_as_notifiable 0.0.2 → 0.0.3
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.
- data/lib/acts_as_notifiable/notifiable.rb +60 -0
- metadata +2 -1
@@ -0,0 +1,60 @@
|
|
1
|
+
module ActsAsNotifiable
|
2
|
+
def acts_as_notifiable
|
3
|
+
has_many :notifications, as: :notifiable, dependent: :destroy
|
4
|
+
|
5
|
+
after_create :create_notification
|
6
|
+
|
7
|
+
# CLASS METHODS
|
8
|
+
|
9
|
+
# Find all notifications by type
|
10
|
+
def self.find_all_unnoticed_by(user, type=nil, type_id=nil)
|
11
|
+
@notifications = Notification.where(notifiable_type: self.to_s, user_id: user.id)
|
12
|
+
self.polymorphic_queries(type, type_id)
|
13
|
+
@notifications
|
14
|
+
end
|
15
|
+
|
16
|
+
# Mark all notifications noticed by type
|
17
|
+
def self.mark_all_noticed_by(user, type=nil, type_id=nil)
|
18
|
+
@notifications = Notification.where(user_id: user.id, notifiable_type: self.to_s)
|
19
|
+
self.polymorphic_queries(type, type_id)
|
20
|
+
@notifications.delete_all
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def self.polymorphic_queries(type, type_id)
|
26
|
+
if type
|
27
|
+
@notifications = @notifications.where(polymorphic_type: type)
|
28
|
+
@notifications = @notifications.where(polymorphic_id: type_id) if type_id
|
29
|
+
else
|
30
|
+
@notifications = @notifications.where(polymorphic_type: nil)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
include NotifiableInstanceMethods
|
35
|
+
end
|
36
|
+
|
37
|
+
module NotifiableInstanceMethods
|
38
|
+
# Check if the notifiable object has been read by the user (no Notification will exist)
|
39
|
+
def unnoticed_by?(user, type=nil, type_id=nil)
|
40
|
+
@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)
|
42
|
+
!@notifications.empty?
|
43
|
+
end
|
44
|
+
|
45
|
+
# mark the notifiable object as read (remove the Notification)
|
46
|
+
def mark_as_noticed_by(user, type=nil, type_id=nil)
|
47
|
+
@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)
|
49
|
+
@notifications.first.destroy if self.unnoticed_by?(user, type, type_id)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Callback method to create the notification
|
53
|
+
def create_notification
|
54
|
+
# Notification.create(user_id: self.user_id, notifiable_id: self.id, notifiable_type: self.class.to_s)
|
55
|
+
Notification.find_or_create_by_user_id_and_notifiable_id_and_notifiable_type(self.user_id, self.id, self.class.to_s)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
ActiveRecord::Base.extend ActsAsNotifiable
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_notifiable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -38,6 +38,7 @@ files:
|
|
38
38
|
- lib/acts_as_notifiable.rb
|
39
39
|
- lib/app/models/notification.rb
|
40
40
|
- lib/app/models/notification_object.rb
|
41
|
+
- lib/acts_as_notifiable/notifiable.rb
|
41
42
|
homepage:
|
42
43
|
licenses: []
|
43
44
|
post_install_message:
|