action_cable_notifications 0.1.30 → 0.1.37

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 64f6e0167fd97bc6b64dc43e1292c331cf7baff7
4
- data.tar.gz: bf24e8703a9953a7be75a3d80f73deb27a118508
2
+ SHA256:
3
+ metadata.gz: eed25d65596c50eb50861264f38578a4dc1099460b917021b48095535fc9705b
4
+ data.tar.gz: f86ec3d3a6cc618d84698cec5a583d1444bf117e3ad727f08dfac7b7fe3f5238
5
5
  SHA512:
6
- metadata.gz: 624a3425d47cd3ea0291abdc706a7b3bc5c12c5d19715775012c364b70d4b00d30907399b3c161357012bca7b33ca4f9afd48ab6f09b4fd95f21c048444b8d1b
7
- data.tar.gz: 2848f179da19c2af48b313e07ae81638e481beac62cef85734d9c888e86c10622ffda3230bc2aa091f3589a9434624eebffd805a8d76a9fec6cbc5bfc7c7cb03
6
+ metadata.gz: 9c77638379a2d3d0bc9343ee4208f0b32d75e6edc18078755b2248a88ecc866a4a8e07ed7a2dc7bb06973e00b685b437fbb653c35adb3c46a4958b01a7f2ffa1
7
+ data.tar.gz: 71bc0203f12b1de417796a76b0990539a7791358e36b9af5d69cacf0ff68b754e21f023453ac55ba8e9f4eac05a8f0ac2c71054297e879a94c76c98e86b7186b
@@ -28,9 +28,6 @@ class CableNotifications.Collection
28
28
  # Cleanup performed commands
29
29
  _.remove(@commandsCache, {performed: true})
30
30
 
31
- # Clears the data array before fetching data
32
- @data.splice(0,@data.length)
33
-
34
31
  # Fetch data from upstream server when connection is resumed
35
32
  @fetch()
36
33
 
@@ -122,8 +119,8 @@ class CableNotifications.Collection
122
119
  @data.push (fields)
123
120
  @callbacks?.create?.call(this, fields)
124
121
  @callbacks?.changed?.call(this, @data) unless options.batching
125
-
126
- upstream.call(this, "create", {fields: fields})
122
+ else
123
+ upstream.call(this, "create", {fields: fields})
127
124
  fields
128
125
 
129
126
  # Update an existing record
@@ -139,8 +136,8 @@ class CableNotifications.Collection
139
136
  @callbacks?.update?.call(this, selector, fields, options)
140
137
  _.extend(record, fields)
141
138
  @callbacks?.changed?.call(this, @data) unless options.batching
142
-
143
- upstream.call(this, "update", {id: record.id, fields: fields})
139
+ else
140
+ upstream.call(this, "update", {id: record.id, fields: fields})
144
141
  record
145
142
 
146
143
  # Update an existing record or inserts a new one if there is no match
@@ -148,7 +145,7 @@ class CableNotifications.Collection
148
145
  @update(selector, fields, _.extend(options, {upsert: true}))
149
146
 
150
147
  # Destroy an existing record
151
- destroy: (selector={}) ->
148
+ destroy: (selector={}, options={}) ->
152
149
  index = _.findIndex(@data, selector)
153
150
  if index < 0
154
151
  console.warn("[destroy] Couldn't find a matching record:", selector)
@@ -158,6 +155,6 @@ class CableNotifications.Collection
158
155
  @data.splice(index, 1)
159
156
  @callbacks?.destroy?.call(this, selector)
160
157
  @callbacks?.changed?.call(this, @data) unless options.batching
161
-
162
- upstream.call(this, "destroy", {id: record.id})
158
+ else
159
+ upstream.call(this, "destroy", {id: record.id})
163
160
  record
@@ -13,11 +13,16 @@ module ActionCableNotifications
13
13
 
14
14
  # Get results using provided parameters and model configured scope
15
15
  begin
16
+ temp_scope = data[:model_options][:scope].deep_dup || {}
17
+ if (data[:model_options][:scope].is_a? Hash) && (data[:model_options][:scope][:where].is_a? Hash)
18
+ #temp_scope = temp_scope.merge(params)
19
+ temp_scope[:where].each{|k,v| v.is_a?(Proc) ? temp_scope[:where][k]=v.call() : nil }
20
+ end
16
21
  results = data[:model].
17
22
  select(params[:select] || []).
18
23
  limit(params[:limit]).
19
24
  where(params[:where] || {}).
20
- scoped_collection(data[:model_options][:scope]).
25
+ scoped_collection(temp_scope).
21
26
  to_a() rescue []
22
27
 
23
28
  response = {
@@ -13,6 +13,16 @@ module ActionCableNotifications
13
13
  after_create :notify_create
14
14
  after_destroy :notify_destroy
15
15
 
16
+ def record_within_scope records
17
+ if records.respond_to?(:where)
18
+ found_record = records.where(id: self.id).first
19
+ elsif records.respond_to?(:detect) and (found_record = records.detect{|e| e["id"]==self.id})
20
+ found_record
21
+ else
22
+ nil
23
+ end
24
+ end
25
+
16
26
  end
17
27
 
18
28
  class_methods do
@@ -74,12 +84,16 @@ module ActionCableNotifications
74
84
  def notify_create
75
85
  self.ChannelPublications.each do |publication, options|
76
86
  if options[:actions].include? :create
77
- # Checks if record is within scope before broadcasting
78
- if options[:scope]==:all or self.class.scoped_collection(options[:scope]).where(id: self.id).present?
87
+ # Checks if records is within scope before broadcasting
88
+ records = self.class.scoped_collection(options[:scope])
89
+
90
+ if options[:scope]==:all or record_within_scope(records)
79
91
  ActionCable.server.broadcast publication,
80
- msg: 'create',
81
- id: self.id,
82
- data: self
92
+ {
93
+ msg: 'create',
94
+ id: self.id,
95
+ data: self
96
+ }
83
97
  end
84
98
  end
85
99
  end
@@ -91,9 +105,9 @@ module ActionCableNotifications
91
105
  if options[:scope]==:all
92
106
  options[:records].push self
93
107
  else
94
- record = self.class.scoped_collection(options[:scope]).where(id: self.id)
108
+ record = record_within_scope(self.class.scoped_collection(options[:scope]))
95
109
  if record.present?
96
- options[:records].push record.first
110
+ options[:records].push record
97
111
  end
98
112
  end
99
113
  end
@@ -117,8 +131,9 @@ module ActionCableNotifications
117
131
  self.ChannelPublications.each do |publication, options|
118
132
  if options[:actions].include? :update
119
133
  # Checks if previous record was within scope
120
- record = options[:records].detect{|r| r.id==self.id}
134
+ record = record_within_scope(options[:records])
121
135
  was_in_scope = record.present?
136
+
122
137
  options[:records].delete(record) if was_in_scope
123
138
 
124
139
  # Checks if current record is within scope
@@ -128,9 +143,8 @@ module ActionCableNotifications
128
143
  record = self
129
144
  is_in_scope = true
130
145
  else
131
- record = self.class.scoped_collection(options[:scope]).where(id: self.id)
146
+ record = record_within_scope(self.class.scoped_collection(options[:scope]))
132
147
  if record.present?
133
- record = record.first
134
148
  is_in_scope = true
135
149
  end
136
150
  end
@@ -146,20 +160,26 @@ module ActionCableNotifications
146
160
 
147
161
  if !changes.empty?
148
162
  ActionCable.server.broadcast publication,
149
- msg: 'update',
150
- id: self.id,
151
- data: changes
163
+ {
164
+ msg: 'update',
165
+ id: self.id,
166
+ data: changes
167
+ }
152
168
  end
153
169
  else
154
170
  ActionCable.server.broadcast publication,
155
- msg: 'create',
156
- id: record.id,
157
- data: record
171
+ {
172
+ msg: 'create',
173
+ id: record.id,
174
+ data: record
175
+ }
158
176
  end
159
177
  elsif was_in_scope # checks if needs to delete the record if its no longer in scope
160
178
  ActionCable.server.broadcast publication,
161
- msg: 'destroy',
162
- id: self.id
179
+ {
180
+ msg: 'destroy',
181
+ id: self.id
182
+ }
163
183
  end
164
184
  end
165
185
  end
@@ -173,10 +193,12 @@ module ActionCableNotifications
173
193
  self.ChannelPublications.each do |publication, options|
174
194
  if options[:scope]==:all or options[:actions].include? :destroy
175
195
  # Checks if record is within scope before broadcasting
176
- if options[:scope]==:all or self.class.scoped_collection(options[:scope]).where(id: self.id).present?
196
+ if options[:scope]==:all or record_within_scope(self.class.scoped_collection(options[:scope])).present?
177
197
  ActionCable.server.broadcast publication,
178
- msg: 'destroy',
179
- id: self.id
198
+ {
199
+ msg: 'destroy',
200
+ id: self.id
201
+ }
180
202
  end
181
203
  end
182
204
  end
@@ -1,3 +1,3 @@
1
1
  module ActionCableNotifications
2
- VERSION = '0.1.30'
2
+ VERSION = '0.1.37'
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.30
4
+ version: 0.1.37
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: 2017-05-19 00:00:00.000000000 Z
11
+ date: 2022-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -149,8 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
149
149
  - !ruby/object:Gem::Version
150
150
  version: '0'
151
151
  requirements: []
152
- rubyforge_project:
153
- rubygems_version: 2.5.1
152
+ rubygems_version: 3.3.16
154
153
  signing_key:
155
154
  specification_version: 4
156
155
  summary: Automatic realtime notification broadcast for ActiveRecord models changes