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: 79722760dc7087e962c3dae93d08d9680d472472
4
- data.tar.gz: 39dfd3ec02a502ec8005fe3eb723275bde0ccd46
3
+ metadata.gz: 10872c8c2efdc7d4bd67ea4301331116774bdff2
4
+ data.tar.gz: e9b661dff6fafdf5076fedd5161020e11a1c82f8
5
5
  SHA512:
6
- metadata.gz: 8e3edd413a94cd4104ebf0499877b7c66cdf2be8d191b3cd66a08de8282ecaf0d3e47bc60ac7ddd16399137f596c4024ffc04cb4eb5ac23cfb57670f4eaa9014
7
- data.tar.gz: c975d1f2af7e6b0f869f6252468fc8204cf1f61be7ae05c247d50a3452ee7969dabee84b2d721ab82c80c324e51b44cc247d3fc671a4cc8ed5526eae832a87e7
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
- def set_action_cable_notification_options( options = {} )
17
- self.ActionCableNotificationsOptions = options
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
- def notify_initial
21
- data = {
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: 'add',
24
- data: self.all
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
- ActionCable.server.broadcast self.ActionCableNotificationsOptions[:broadcast_name],
31
- collection: self.model_name.collection,
32
- msg: 'create',
33
- id: self.id,
34
- fields: self
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
- ActionCable.server.broadcast self.ActionCableNotificationsOptions[:broadcast_name],
44
- collection: self.model_name.collection,
45
- msg: 'update',
46
- id: self.id,
47
- fields: changes
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
- ActionCable.server.broadcast self.ActionCableNotificationsOptions[:broadcast_name],
52
- collection: self.model_name.collection,
53
- msg: 'destroy',
54
- id: self.id
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 = {}, callback = nil)
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
- broadcast_name: model.model_name.collection
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 !model.respond_to? :ActionCableNotificationsOptions
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('set_action_cable_notification_options', options)
31
+ @model.send('action_cable_notification_options=', @options[:broadcasting], @options)
25
32
 
26
- stream_from(options[:broadcast_name], callback)
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
- transmit model.notify_initial
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
@@ -1,3 +1,3 @@
1
1
  module ActionCableNotifications
2
- VERSION = '0.1.3'
2
+ VERSION = '0.1.4'
3
3
  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.3
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-29 00:00:00.000000000 Z
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: Realtime notification broadcast for Ruby on Rails models changes using Action
98
- Cable and websockets
97
+ summary: Automatic realtime notification broadcast for ActiveRecord models changes
98
+ using Action Cable and websockets
99
99
  test_files: []