action_cable_notifications 0.1.3 → 0.1.4
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
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 10872c8c2efdc7d4bd67ea4301331116774bdff2
|
4
|
+
data.tar.gz: e9b661dff6fafdf5076fedd5161020e11a1c82f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2eabf026860c2ce9baf5232b99f512aed3e61160aa251cbf0c3ab4137f200f66de8242cd9e5ebd3013a8c0dde50b67913ac1f9eae42e5bea10f939e516dfd60c
|
7
|
+
data.tar.gz: d4a121bd883694dcf6a6a39b14737769e4095490d01ead576237d1721b2417289b6efb20738e1a1dd3f5735395f711df884cf8e28351a56ba0447d5668ee35a2
|
@@ -13,25 +13,51 @@ module ActionCableNotifications
|
|
13
13
|
end
|
14
14
|
|
15
15
|
module ClassMethods
|
16
|
-
|
17
|
-
|
16
|
+
# Options setter
|
17
|
+
def action_cable_notification_options= ( broadcasting, options = nil )
|
18
|
+
if options.present?
|
19
|
+
self.ActionCableNotificationsOptions[broadcasting.to_sym] = options
|
20
|
+
else
|
21
|
+
self.ActionCableNotificationsOptions.except! broadcasting.to_sym
|
22
|
+
end
|
18
23
|
end
|
19
24
|
|
20
|
-
|
21
|
-
|
25
|
+
# Options getter
|
26
|
+
def action_cable_notification_options ( broadcasting )
|
27
|
+
if broadcasting.present?
|
28
|
+
self.ActionCableNotificationsOptions[broadcasting.to_sym]
|
29
|
+
else
|
30
|
+
self.ActionCableNotificationsOptions
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def scoped_collection ( scope )
|
35
|
+
Array(Array(scope).inject(self) {|o, a| o.try(*a)})
|
36
|
+
end
|
37
|
+
|
38
|
+
def notify_initial ( broadcasting )
|
39
|
+
options = self.action_cable_notification_options( broadcasting )
|
40
|
+
{
|
22
41
|
collection: self.model_name.collection,
|
23
|
-
msg: '
|
24
|
-
data: self.
|
42
|
+
msg: 'add_collection',
|
43
|
+
data: self.scoped_collection(options[:scope])
|
25
44
|
}
|
26
45
|
end
|
27
46
|
end
|
28
47
|
|
48
|
+
private
|
49
|
+
|
29
50
|
def notify_create
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
51
|
+
self.ActionCableNotificationsOptions.each do |broadcasting, options|
|
52
|
+
# Checks if record is within scope before broadcasting
|
53
|
+
if self.class.scoped_collection(options[:scope]).include? self
|
54
|
+
ActionCable.server.broadcast broadcasting,
|
55
|
+
collection: self.model_name.collection,
|
56
|
+
msg: 'create',
|
57
|
+
id: self.id,
|
58
|
+
data: self
|
59
|
+
end
|
60
|
+
end
|
35
61
|
end
|
36
62
|
|
37
63
|
def notify_update
|
@@ -40,18 +66,28 @@ module ActionCableNotifications
|
|
40
66
|
changes[k] = v[1]
|
41
67
|
end
|
42
68
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
69
|
+
self.ActionCableNotificationsOptions.each do |broadcasting, options|
|
70
|
+
# Checks if record is within scope before broadcasting
|
71
|
+
if self.class.scoped_collection(options[:scope]).include? self
|
72
|
+
ActionCable.server.broadcast broadcasting,
|
73
|
+
collection: self.model_name.collection,
|
74
|
+
msg: 'update',
|
75
|
+
id: self.id,
|
76
|
+
data: changes
|
77
|
+
end
|
78
|
+
end
|
48
79
|
end
|
49
80
|
|
50
81
|
def notify_destroy
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
82
|
+
self.ActionCableNotificationsOptions.each do |broadcasting, options|
|
83
|
+
# Checks if record is within scope before broadcasting
|
84
|
+
if self.class.scoped_collection(options[:scope]).include? self
|
85
|
+
ActionCable.server.broadcast broadcasting,
|
86
|
+
collection: self.model_name.collection,
|
87
|
+
msg: 'destroy',
|
88
|
+
id: self.id
|
89
|
+
end
|
90
|
+
end
|
55
91
|
end
|
56
92
|
end
|
57
93
|
end
|
@@ -3,34 +3,50 @@ module ActionCableNotifications
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
included do
|
6
|
-
|
6
|
+
# Actions to be done when the module is included
|
7
7
|
end
|
8
8
|
|
9
9
|
private
|
10
10
|
|
11
|
-
def stream_notifications_for(model, options = {},
|
11
|
+
def stream_notifications_for(model, options = {}, &block)
|
12
|
+
@model = model
|
13
|
+
|
12
14
|
# Default options
|
13
|
-
options = {
|
15
|
+
@options = {
|
16
|
+
actions: [:create, :update, :destroy],
|
17
|
+
broadcasting: model.model_name.collection,
|
18
|
+
callback: nil,
|
19
|
+
coder: nil,
|
14
20
|
include_initial: false, # Send all records to the subscriber on connection
|
15
|
-
|
21
|
+
params: params,
|
22
|
+
scope: :all # Default collection scope
|
16
23
|
}.merge(options)
|
17
24
|
|
18
25
|
# Checks if model already includes notification callbacks
|
19
|
-
if
|
20
|
-
model.send('include', ActionCableNotifications::Callbacks)
|
26
|
+
if !@model.respond_to? :ActionCableNotificationsOptions
|
27
|
+
@model.send('include', ActionCableNotifications::Callbacks)
|
21
28
|
end
|
22
29
|
|
23
30
|
# Set specified options on model
|
24
|
-
model.send('
|
31
|
+
@model.send('action_cable_notification_options=', @options[:broadcasting], @options)
|
25
32
|
|
26
|
-
|
33
|
+
# Start streaming
|
34
|
+
stream_from(@options[:broadcasting], options[:callback] || block, @options.slice(:coder))
|
27
35
|
|
28
36
|
# Transmit initial state if required
|
29
|
-
if options[:include_initial]
|
30
|
-
|
37
|
+
if @options[:include_initial]
|
38
|
+
# XXX: Check if data should be transmitted
|
39
|
+
transmit @model.notify_initial @options[:broadcasting]
|
31
40
|
end
|
32
41
|
|
33
42
|
end
|
34
43
|
|
44
|
+
def unsubscribed
|
45
|
+
stop_all_streams
|
46
|
+
|
47
|
+
# Unset options for this channel on model
|
48
|
+
@model.send('action_cable_notification_options=', @options[:broadcasting], nil)
|
49
|
+
end
|
50
|
+
|
35
51
|
end
|
36
52
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: action_cable_notifications
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ByS Sistemas de Control
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-08-
|
11
|
+
date: 2016-08-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -94,6 +94,6 @@ rubyforge_project:
|
|
94
94
|
rubygems_version: 2.5.1
|
95
95
|
signing_key:
|
96
96
|
specification_version: 4
|
97
|
-
summary:
|
98
|
-
Cable and websockets
|
97
|
+
summary: Automatic realtime notification broadcast for ActiveRecord models changes
|
98
|
+
using Action Cable and websockets
|
99
99
|
test_files: []
|