action_cable_notifications 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -2
- data/lib/action_cable_notifications/callbacks.rb +13 -6
- data/lib/action_cable_notifications/streams.rb +18 -16
- data/lib/action_cable_notifications/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 79722760dc7087e962c3dae93d08d9680d472472
|
4
|
+
data.tar.gz: 39dfd3ec02a502ec8005fe3eb723275bde0ccd46
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e3edd413a94cd4104ebf0499877b7c66cdf2be8d191b3cd66a08de8282ecaf0d3e47bc60ac7ddd16399137f596c4024ffc04cb4eb5ac23cfb57670f4eaa9014
|
7
|
+
data.tar.gz: c975d1f2af7e6b0f869f6252468fc8204cf1f61be7ae05c247d50a3452ee7969dabee84b2d721ab82c80c324e51b44cc247d3fc671a4cc8ed5526eae832a87e7
|
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# ActionCableNotifications
|
2
|
-
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/action_cable_notifications.svg)](https://badge.fury.io/rb/action_cable_notifications)
|
3
|
+
|
4
|
+
This gem is under develoment and is not ready for production usage.
|
3
5
|
|
4
6
|
## Usage
|
5
7
|
How to use my plugin.
|
@@ -30,7 +32,7 @@ class TestChannel < ApplicationCable::Channel
|
|
30
32
|
include ActionCableNotifications::Streams
|
31
33
|
|
32
34
|
def subscribed
|
33
|
-
|
35
|
+
stream_notifications_for model, include_initial: true
|
34
36
|
end
|
35
37
|
|
36
38
|
def unsubscribed
|
@@ -3,24 +3,31 @@ module ActionCableNotifications
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
included do
|
6
|
+
# Options
|
7
|
+
class_attribute :ActionCableNotificationsOptions
|
8
|
+
self.ActionCableNotificationsOptions = {}
|
9
|
+
|
6
10
|
after_update :notify_update
|
7
11
|
after_create :notify_create
|
8
12
|
after_destroy :notify_destroy
|
9
13
|
end
|
10
14
|
|
11
15
|
module ClassMethods
|
12
|
-
def
|
13
|
-
|
16
|
+
def set_action_cable_notification_options( options = {} )
|
17
|
+
self.ActionCableNotificationsOptions = options
|
18
|
+
end
|
19
|
+
|
20
|
+
def notify_initial
|
14
21
|
data = {
|
15
22
|
collection: self.model_name.collection,
|
16
23
|
msg: 'add',
|
17
|
-
data:
|
24
|
+
data: self.all
|
18
25
|
}
|
19
26
|
end
|
20
27
|
end
|
21
28
|
|
22
29
|
def notify_create
|
23
|
-
ActionCable.server.broadcast self.
|
30
|
+
ActionCable.server.broadcast self.ActionCableNotificationsOptions[:broadcast_name],
|
24
31
|
collection: self.model_name.collection,
|
25
32
|
msg: 'create',
|
26
33
|
id: self.id,
|
@@ -33,7 +40,7 @@ module ActionCableNotifications
|
|
33
40
|
changes[k] = v[1]
|
34
41
|
end
|
35
42
|
|
36
|
-
ActionCable.server.broadcast self.
|
43
|
+
ActionCable.server.broadcast self.ActionCableNotificationsOptions[:broadcast_name],
|
37
44
|
collection: self.model_name.collection,
|
38
45
|
msg: 'update',
|
39
46
|
id: self.id,
|
@@ -41,7 +48,7 @@ module ActionCableNotifications
|
|
41
48
|
end
|
42
49
|
|
43
50
|
def notify_destroy
|
44
|
-
ActionCable.server.broadcast self.
|
51
|
+
ActionCable.server.broadcast self.ActionCableNotificationsOptions[:broadcast_name],
|
45
52
|
collection: self.model_name.collection,
|
46
53
|
msg: 'destroy',
|
47
54
|
id: self.id
|
@@ -3,31 +3,33 @@ module ActionCableNotifications
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
included do
|
6
|
-
|
7
|
-
self.ActionCableNotificationsOptions = {}
|
6
|
+
|
8
7
|
end
|
9
8
|
|
10
9
|
private
|
11
10
|
|
12
|
-
def
|
11
|
+
def stream_notifications_for(model, options = {}, callback = nil)
|
12
|
+
# Default options
|
13
|
+
options = {
|
14
|
+
include_initial: false, # Send all records to the subscriber on connection
|
15
|
+
broadcast_name: model.model_name.collection
|
16
|
+
}.merge(options)
|
13
17
|
|
14
|
-
#
|
15
|
-
if
|
16
|
-
|
17
|
-
|
18
|
+
# Checks if model already includes notification callbacks
|
19
|
+
if !model.respond_to? :ActionCableNotificationsOptions
|
20
|
+
model.send('include', ActionCableNotifications::Callbacks)
|
21
|
+
end
|
18
22
|
|
19
|
-
|
20
|
-
|
21
|
-
model.send('include', ActionCableNotifications::Callbacks)
|
22
|
-
end
|
23
|
+
# Set specified options on model
|
24
|
+
model.send('set_action_cable_notification_options', options)
|
23
25
|
|
24
|
-
|
26
|
+
stream_from(options[:broadcast_name], callback)
|
25
27
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
end
|
28
|
+
# Transmit initial state if required
|
29
|
+
if options[:include_initial]
|
30
|
+
transmit model.notify_initial
|
30
31
|
end
|
32
|
+
|
31
33
|
end
|
32
34
|
|
33
35
|
end
|