model_updates 0.0.10 → 0.0.11

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: 119170b8e338b457db7b0d07526da4ecec937be0
4
- data.tar.gz: 310de0c583969abcb04c3b9346ea82e6501e8570
3
+ metadata.gz: a5cb133f1b8f65abb2d507c21b386305040af2e0
4
+ data.tar.gz: c7d89af1f90f0a65637ba16cb070b71ff27eb422
5
5
  SHA512:
6
- metadata.gz: cbeaac93bf5dbce1a1592815821a1fd3f33e1933f5396d0a39da76eeeefb29a4039364b6db013e6ff3196c634dfaf5a804c8f019b9d74334a67e4e29ff0de914
7
- data.tar.gz: 923620988ae8f85bc021a60579821cb944e96894f0f07ca9e21718c1a9a9b0f60f5927ed6fe83ccabb231f6c0a2c682e74f4ce7cd273e6493e9b4358f8ad37f4
6
+ metadata.gz: ba0a40d206e4f5de631469bd8265b52c61ed5fbe4d149e2bbb0b0abb1eaa82bbc1d76768d63512d37f1715dcced6317aeadb9e05b717fd4ef17325405d3dac89
7
+ data.tar.gz: 9beab92f2e0173da0b9ae5c265199feb898265ab9e4d0f596cc9b5e1f657604ca62f5a260322b08d695f10ebb42f6de52dc8afde5dc439b245e029b8fb266084
@@ -3,7 +3,6 @@ ModelUpdates.Activator = class Activator {
3
3
  ModelUpdates.debug("Activator constructor")
4
4
 
5
5
  this.modelSubscriptions = {}
6
- this.modelDestroys = {}
7
6
  this.connectedUpdates = {}
8
7
  this.connectedDestroyes = {}
9
8
  }
@@ -12,7 +11,6 @@ ModelUpdates.Activator = class Activator {
12
11
  ModelUpdates.debug("Update was called")
13
12
  this.updateFoundElements()
14
13
  this.updateSubscribedUpdates()
15
- this.updateSubscribedDestroys()
16
14
  }
17
15
 
18
16
  updateFoundElements() {
@@ -31,15 +29,7 @@ ModelUpdates.Activator = class Activator {
31
29
  if (!that.modelSubscriptions[model])
32
30
  that.modelSubscriptions[model] = {}
33
31
 
34
- if (element.data("model-updates-key"))
35
- that.modelSubscriptions[model][id] = true
36
-
37
- if (element.data("model-updates-remove-on-destroy")) {
38
- if (!that.modelDestroys[model])
39
- that.modelDestroys[model] = {}
40
-
41
- that.modelDestroys[model][id] = true
42
- }
32
+ that.modelSubscriptions[model][id] = true
43
33
  })
44
34
  }
45
35
 
@@ -65,27 +55,4 @@ ModelUpdates.Activator = class Activator {
65
55
  if (Object.keys(connectToModels).length > 0)
66
56
  ModelUpdates.Update.connect({"ids": connectToModels})
67
57
  }
68
-
69
- updateSubscribedDestroys() {
70
- ModelUpdates.debug("Activator#updateSubscribedDestroys called")
71
-
72
- var connectToModels = {}
73
- for(var model in this.modelDestroys) {
74
- var ids = []
75
- connectToModels[model] = ids
76
-
77
- for(var id in this.modelDestroys[model]) {
78
- if (!this.connectedDestroyes[model])
79
- this.connectedDestroyes[model] = {}
80
-
81
- if (!this.connectedDestroyes[model][id]) {
82
- this.connectedDestroyes[model][id] = true
83
- ids.push(id)
84
- }
85
- }
86
- }
87
-
88
- if (Object.keys(connectToModels).length > 0)
89
- ModelUpdates.Destroy.connect({"ids": connectToModels})
90
- }
91
58
  }
@@ -6,31 +6,40 @@ ModelUpdates.Update = class Update {
6
6
  {channel: "ModelUpdates::UpdateChannel", ids: args.ids},
7
7
  {
8
8
  received: function(json) {
9
- ModelUpdates.debug("Received update for " + json.model + "(" + json.id + ")")
10
9
 
11
- for(var key in json.changes) {
12
- var elements = $(".model-updates[data-model-updates-model='" + json.model + "'][data-model-updates-id='" + json.id + "'][data-model-updates-key='" + key + "']")
13
- elements.each(function() {
14
- var element = $(this)
10
+ if (json.type == "destroy") {
11
+ var elements = $(".model-updates[data-model-updates-model='" + json.model + "'][data-model-updates-id='" + json.id + "'][data-model-updates-remove-on-destroy='true']")
12
+ elements.remove()
15
13
 
16
- if (element.data("model-updates-callback")) {
17
- var function_to_call = element.data("model-updates-callback")
14
+ if (args.onDestroyed)
15
+ args.onDestroyed(json)
16
+ } else if (json.type == "update") {
17
+ ModelUpdates.debug("Received update for " + json.model + "(" + json.id + ")")
18
18
 
19
- window[function_to_call]({
20
- changes: json.changes,
21
- element: element,
22
- id: json.id,
23
- key: key,
24
- model: json.model,
25
- value: json.changes[key]
26
- })
27
- } else if(json.changes[key]) {
28
- element.text(json.changes[key])
29
- } else {
30
- // Needs to check if it has a value, else it will print out "null" instead of nothing.
31
- element.text("")
32
- }
33
- })
19
+ for(var key in json.changes) {
20
+ var elements = $(".model-updates[data-model-updates-model='" + json.model + "'][data-model-updates-id='" + json.id + "'][data-model-updates-key='" + key + "']")
21
+ elements.each(function() {
22
+ var element = $(this)
23
+
24
+ if (element.data("model-updates-callback")) {
25
+ var function_to_call = element.data("model-updates-callback")
26
+
27
+ window[function_to_call]({
28
+ changes: json.changes,
29
+ element: element,
30
+ id: json.id,
31
+ key: key,
32
+ model: json.model,
33
+ value: json.changes[key]
34
+ })
35
+ } else if(json.changes[key]) {
36
+ element.text(json.changes[key])
37
+ } else {
38
+ // Needs to check if it has a value, else it will print out "null" instead of nothing.
39
+ element.text("")
40
+ }
41
+ })
42
+ }
34
43
  }
35
44
  }
36
45
  }
@@ -1,7 +1,6 @@
1
1
  //= require model_updates/model_updates_class
2
2
  //= require model_updates/activator
3
3
  //= require model_updates/create
4
- //= require model_updates/destroy
5
4
  //= require model_updates/formatters
6
5
  //= require model_updates/update
7
6
 
@@ -4,7 +4,7 @@ class ModelUpdates::UpdateChannel < ApplicationCable::Channel
4
4
  models = model_class.safe_constantize.accessible_by(current_ability).where(id: ids)
5
5
 
6
6
  ids_found = {}
7
- models.each do |model|
7
+ models.find_each do |model|
8
8
  next if ids_found.key?(model.id)
9
9
  ids_found[model.id] = true
10
10
 
@@ -33,7 +33,8 @@ module ModelUpdates::ModelExtensions
33
33
  model,
34
34
  id: id,
35
35
  model: model.class.name,
36
- changes: attribute_changes
36
+ changes: attribute_changes,
37
+ type: :update
37
38
  )
38
39
  end
39
40
  end
@@ -63,11 +64,12 @@ module ModelUpdates::ModelExtensions
63
64
  attributes[attribute_name] = __send__(attribute_name)
64
65
  end
65
66
 
66
- ModelUpdates::DestroyChannel.broadcast_to(
67
+ ModelUpdates::UpdateChannel.broadcast_to(
67
68
  model,
68
69
  id: id,
69
70
  model: model.class.name,
70
- attributes: attributes
71
+ attributes: attributes,
72
+ type: :destroy
71
73
  )
72
74
  end
73
75
  end
@@ -1,3 +1,3 @@
1
1
  module ModelUpdates
2
- VERSION = "0.0.10".freeze
2
+ VERSION = "0.0.11".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: model_updates
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-04 00:00:00.000000000 Z
11
+ date: 2018-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -52,13 +52,11 @@ files:
52
52
  - app/assets/javascripts/model_updates.js
53
53
  - app/assets/javascripts/model_updates/activator.es6
54
54
  - app/assets/javascripts/model_updates/create.es6
55
- - app/assets/javascripts/model_updates/destroy.es6
56
55
  - app/assets/javascripts/model_updates/formatters.js
57
56
  - app/assets/javascripts/model_updates/model_updates_class.es6
58
57
  - app/assets/javascripts/model_updates/update.es6
59
58
  - app/assets/stylesheets/model_updates/application.css
60
59
  - app/channels/model_updates/create_channel.rb
61
- - app/channels/model_updates/destroy_channel.rb
62
60
  - app/channels/model_updates/update_channel.rb
63
61
  - app/controllers/model_updates/application_controller.rb
64
62
  - app/helpers/model_updates/application_helper.rb
@@ -1,18 +0,0 @@
1
- ModelUpdates.Destroy = class Destroy {
2
- static connect(args) {
3
- ModelUpdates.debug("Connecting to destroy channel for " + JSON.stringify(args.ids))
4
-
5
- App.cable.subscriptions.create(
6
- {channel: "ModelUpdates::DestroyChannel", ids: args.ids},
7
- {
8
- received: function(json) {
9
- var elements = $(".model-updates[data-model-updates-model='" + json.model + "'][data-model-updates-id='" + json.id + "'][data-model-updates-remove-on-destroy='true']")
10
- elements.remove()
11
-
12
- if (args.onDestroyed)
13
- args.onDestroyed(json)
14
- }
15
- }
16
- )
17
- }
18
- }
@@ -1,15 +0,0 @@
1
- class ModelUpdates::DestroyChannel < ApplicationCable::Channel
2
- def subscribed
3
- params[:ids].each do |model_class, ids|
4
- models = model_class.safe_constantize.accessible_by(current_ability).where(id: ids)
5
-
6
- ids_found = {}
7
- models.each do |model|
8
- next if ids_found.key?(model.id)
9
- ids_found[model.id] = true
10
-
11
- stream_for model
12
- end
13
- end
14
- end
15
- end