model_updates 0.0.9 → 0.0.10

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: f43d80cfa164be08bec5f099089518e873e4edb7
4
- data.tar.gz: c7d5eeefdfab73846b9ae909bce65d32086d9058
3
+ metadata.gz: 119170b8e338b457db7b0d07526da4ecec937be0
4
+ data.tar.gz: 310de0c583969abcb04c3b9346ea82e6501e8570
5
5
  SHA512:
6
- metadata.gz: f2a59b69019b2b9b75d4b9867d8039becab8c02f90b64dce8c675e188173e5e947d7a684d1f6e189d1e332f3c9dcc4fd088b12d1d23d6018e6e6c75f9c10e49b
7
- data.tar.gz: e04a7d37da13e364fd1cb95c15735a58b20d7f11abe693becb4093e5bc0e5f1ecb3ab6de315b9752e6aec36d9b947e2c3982bb0d2af222f8255b33e76b1dc4ed
6
+ metadata.gz: cbeaac93bf5dbce1a1592815821a1fd3f33e1933f5396d0a39da76eeeefb29a4039364b6db013e6ff3196c634dfaf5a804c8f019b9d74334a67e4e29ff0de914
7
+ data.tar.gz: 923620988ae8f85bc021a60579821cb944e96894f0f07ca9e21718c1a9a9b0f60f5927ed6fe83ccabb231f6c0a2c682e74f4ce7cd273e6493e9b4358f8ad37f4
data/README.md CHANGED
@@ -39,12 +39,12 @@ Add required CanCan access methods to your `ApplicationCable::Channel`:
39
39
  class ApplicationCable::Channel < ActionCable::Channel::Base
40
40
  private
41
41
 
42
- delegate :authorize!, :can?, to: :current_ability
43
-
42
+ # Used to authorize which resources the user can read from (security)
44
43
  def current_ability
45
44
  @_current_ability ||= CanCanAbility.new(user: current_user)
46
45
  end
47
46
 
47
+ # Get user from Devise
48
48
  def current_user
49
49
  @_current_user ||= env["warden"].user
50
50
  end
@@ -60,8 +60,6 @@ Warden::Manager.after_set_user do |user, auth, opts|
60
60
  end
61
61
  ```
62
62
 
63
- You can define `authorize!(ability, model)` yourself, if you aren't using CanCan.
64
-
65
63
  Choose which attributes should be broadcasted automatically:
66
64
 
67
65
  ```ruby
@@ -172,6 +170,13 @@ You can refresh elements with a simple call like this:
172
170
  ModelUpdates.update()
173
171
  ```
174
172
 
173
+ ### Debugging
174
+
175
+ In case you want to enable debug output from ModelUpdates from JavaScript:
176
+ ```js
177
+ ModelUpdates.configuration.debug = true
178
+ ```
179
+
175
180
  ## Contributing
176
181
 
177
182
  Contribution directions go here.
@@ -46,44 +46,46 @@ ModelUpdates.Activator = class Activator {
46
46
  updateSubscribedUpdates() {
47
47
  ModelUpdates.debug("Activator#updateSubscribedUpdates called")
48
48
 
49
+ var connectToModels = {}
49
50
  for(var model in this.modelSubscriptions) {
51
+ var ids = []
52
+ connectToModels[model] = ids
53
+
50
54
  for(var id in this.modelSubscriptions[model]) {
51
55
  if (!this.connectedUpdates[model])
52
56
  this.connectedUpdates[model] = {}
53
57
 
54
58
  if (!this.connectedUpdates[model][id]) {
55
59
  this.connectedUpdates[model][id] = true
56
-
57
- ModelUpdates.debug("Add subscription for update of " + model + "(" + id + ")")
58
-
59
- ModelUpdates.Update.connect({
60
- "id": id,
61
- "model": model
62
- })
60
+ ids.push(id)
63
61
  }
64
62
  }
65
63
  }
64
+
65
+ if (Object.keys(connectToModels).length > 0)
66
+ ModelUpdates.Update.connect({"ids": connectToModels})
66
67
  }
67
68
 
68
69
  updateSubscribedDestroys() {
69
70
  ModelUpdates.debug("Activator#updateSubscribedDestroys called")
70
71
 
72
+ var connectToModels = {}
71
73
  for(var model in this.modelDestroys) {
74
+ var ids = []
75
+ connectToModels[model] = ids
76
+
72
77
  for(var id in this.modelDestroys[model]) {
73
78
  if (!this.connectedDestroyes[model])
74
79
  this.connectedDestroyes[model] = {}
75
80
 
76
81
  if (!this.connectedDestroyes[model][id]) {
77
82
  this.connectedDestroyes[model][id] = true
78
-
79
- ModelUpdates.debug("Add subscription for destruction of " + model + "(" + id + ")")
80
-
81
- ModelUpdates.Destroy.connect({
82
- "id": id,
83
- "model": model
84
- })
83
+ ids.push(id)
85
84
  }
86
85
  }
87
86
  }
87
+
88
+ if (Object.keys(connectToModels).length > 0)
89
+ ModelUpdates.Destroy.connect({"ids": connectToModels})
88
90
  }
89
91
  }
@@ -1,9 +1,9 @@
1
1
  ModelUpdates.Destroy = class Destroy {
2
2
  static connect(args) {
3
- ModelUpdates.debug("Connecting to destroy channel for " + args.model + "(" + args.id + ")")
3
+ ModelUpdates.debug("Connecting to destroy channel for " + JSON.stringify(args.ids))
4
4
 
5
5
  App.cable.subscriptions.create(
6
- {channel: "ModelUpdates::DestroyChannel", id: args.id, model: args.model},
6
+ {channel: "ModelUpdates::DestroyChannel", ids: args.ids},
7
7
  {
8
8
  received: function(json) {
9
9
  var elements = $(".model-updates[data-model-updates-model='" + json.model + "'][data-model-updates-id='" + json.id + "'][data-model-updates-remove-on-destroy='true']")
@@ -1,9 +1,9 @@
1
1
  ModelUpdates.Update = class Update {
2
2
  static connect(args) {
3
- ModelUpdates.debug("Connecting to update channel for " + args.model + "(" + args.id + ")")
3
+ ModelUpdates.debug("Connecting to update channel for " + JSON.stringify(args.ids))
4
4
 
5
5
  App.cable.subscriptions.create(
6
- {channel: "ModelUpdates::UpdateChannel", id: args.id, model: args.model},
6
+ {channel: "ModelUpdates::UpdateChannel", ids: args.ids},
7
7
  {
8
8
  received: function(json) {
9
9
  ModelUpdates.debug("Received update for " + json.model + "(" + json.id + ")")
@@ -1,7 +1,15 @@
1
1
  class ModelUpdates::DestroyChannel < ApplicationCable::Channel
2
2
  def subscribed
3
- model = params[:model].safe_constantize.find(params[:id])
4
- authorize! :read, model
5
- stream_for model
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
6
14
  end
7
15
  end
@@ -1,7 +1,15 @@
1
1
  class ModelUpdates::UpdateChannel < ApplicationCable::Channel
2
2
  def subscribed
3
- model = params[:model].safe_constantize.find(params[:id])
4
- authorize! :read, model
5
- stream_for model
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
6
14
  end
7
15
  end
@@ -11,26 +11,29 @@ module ModelUpdates::ModelExtensions
11
11
  def model_updates_broadcast_attributes(args)
12
12
  model_updates_data[:attributes] = args.fetch(:attributes)
13
13
 
14
- after_commit on: :update do |model|
15
- changes = {}
14
+ # Need to remember what changes before callbacks, since it might get changed by gems like AwesomeNestedSet before after_commit is called
15
+ before_save on: :update do
16
+ attribute_changes = {}
16
17
 
17
18
  args.fetch(:attributes).each do |attribute_name|
18
- if Rails::VERSION::MAJOR >= 5 && Rails::VERSION::MINOR >= 1
19
- method_changed = "saved_change_to_#{attribute_name}?"
20
- else
21
- method_changed = "#{attribute_name}_changed?"
22
- end
23
-
19
+ method_changed = "#{attribute_name}_changed?"
24
20
  next if respond_to?(method_changed) && !__send__(method_changed)
25
- changes[attribute_name] = __send__(attribute_name)
21
+ attribute_changes[attribute_name] = __send__(attribute_name)
26
22
  end
27
23
 
28
- if changes.any?
24
+ @_model_updates_changes = attribute_changes
25
+ end
26
+
27
+ after_commit on: :update do |model|
28
+ attribute_changes = @_model_updates_changes
29
+ @_model_updates_changes = nil
30
+
31
+ if attribute_changes.any?
29
32
  ModelUpdates::UpdateChannel.broadcast_to(
30
33
  model,
31
34
  id: id,
32
35
  model: model.class.name,
33
- changes: changes
36
+ changes: attribute_changes
34
37
  )
35
38
  end
36
39
  end
@@ -1,3 +1,3 @@
1
1
  module ModelUpdates
2
- VERSION = "0.0.9".freeze
2
+ VERSION = "0.0.10".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.9
4
+ version: 0.0.10
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-03 00:00:00.000000000 Z
11
+ date: 2018-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails