model_updates 0.0.6 → 0.0.7
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 +4 -4
- data/README.md +45 -0
- data/app/assets/javascripts/model_updates/activate_elements.js +30 -10
- data/app/assets/javascripts/model_updates/destroy.es6 +18 -0
- data/app/assets/javascripts/model_updates/model_updates_class.es6 +1 -1
- data/app/assets/javascripts/model_updates/update.es6 +2 -2
- data/app/assets/javascripts/model_updates.js +1 -0
- data/app/channels/model_updates/destroy_channel.rb +7 -0
- data/lib/model_updates/model_extensions.rb +20 -4
- data/lib/model_updates/version.rb +1 -1
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b6e5c02e29a478245a15cbca2daf7238ef762f83
|
4
|
+
data.tar.gz: 0c4c00c9c6404c72a3ea21b3168ddc5a19be18b9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c686af4abd42d5f9a6ca9f895c6c08f62fa461c060b419d282625b38e7ee9597653fbe55dff27b5a841822c9733614936728d49a61d37d777e2decacdf10a158
|
7
|
+
data.tar.gz: ea03a51dd7f1468e0950af0ffe6fb5ea6b3eee76e816d49c9d58fb5e2ac6eea8e3fc7780e7fd96394401c87ebac960ec7b583e0c07cfc914e0e8dd3b710a084e
|
data/README.md
CHANGED
@@ -34,6 +34,25 @@ class ApplicationRecord < ActiveRecord::Base
|
|
34
34
|
end
|
35
35
|
```
|
36
36
|
|
37
|
+
Add required CanCan access methods to your `ApplicationCable::Channel`:
|
38
|
+
```ruby
|
39
|
+
class ApplicationCable::Channel < ActionCable::Channel::Base
|
40
|
+
private
|
41
|
+
|
42
|
+
delegate :authorize!, :can?, to: :current_ability
|
43
|
+
|
44
|
+
def current_ability
|
45
|
+
@_current_ability ||= PeakFlowAbility.new(user: current_user)
|
46
|
+
end
|
47
|
+
|
48
|
+
def current_user
|
49
|
+
@_current_user ||= env["warden"].user
|
50
|
+
end
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
You can define `authorize!(ability, model)` yourself, if you aren't using CanCan.
|
55
|
+
|
37
56
|
Choose which attributes should be broadcasted automatically:
|
38
57
|
|
39
58
|
```ruby
|
@@ -50,6 +69,14 @@ class Model < ApplicationRecord
|
|
50
69
|
end
|
51
70
|
```
|
52
71
|
|
72
|
+
Or destroys:
|
73
|
+
```ruby
|
74
|
+
class Model < ApplicationRecord
|
75
|
+
model_updates_broadcast_attributes attributes: [:updated_at]
|
76
|
+
model_updates_broadcast_destroyed
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
53
80
|
## Usage
|
54
81
|
|
55
82
|
|
@@ -109,6 +136,24 @@ ModelUpdates.Create.connect({model: "MyModel", onCreated: function(data) {
|
|
109
136
|
})
|
110
137
|
```
|
111
138
|
|
139
|
+
If you want an element automatically removed on destroy:
|
140
|
+
```erb
|
141
|
+
<div class="model-updates" data-model-updates-model="<%= model.class.name %>" data-model-updates-id="<%= model.id %>" data-model-updates-remove-on-destroy="true">
|
142
|
+
<%= model.updated_at %>
|
143
|
+
</div>
|
144
|
+
```
|
145
|
+
|
146
|
+
You can also manually listen when a model gets destroyed:
|
147
|
+
```js
|
148
|
+
ModelUpdates.Destroy.connect({
|
149
|
+
"id": data.id,
|
150
|
+
"model": "BuildCommandExecution",
|
151
|
+
"onDestroyed": function(data) {
|
152
|
+
console.log("Model destroyed: " + data.id)
|
153
|
+
}
|
154
|
+
})
|
155
|
+
```
|
156
|
+
|
112
157
|
## Contributing
|
113
158
|
|
114
159
|
Contribution directions go here.
|
@@ -1,23 +1,43 @@
|
|
1
1
|
$(document).ready(function() {
|
2
2
|
// Find all models that should be subscribed to
|
3
|
-
|
3
|
+
modelSubscriptions = {}
|
4
|
+
modelDestroys = {}
|
4
5
|
|
5
6
|
$(".model-updates").each(function() {
|
6
|
-
|
7
|
-
model_id = $(this).data("model-updates-id")
|
7
|
+
element = $(this)
|
8
8
|
|
9
|
-
|
10
|
-
|
9
|
+
model = element.data("model-updates-model")
|
10
|
+
id = element.data("model-updates-id")
|
11
11
|
|
12
|
-
|
12
|
+
if (!modelSubscriptions[model])
|
13
|
+
modelSubscriptions[model] = {}
|
14
|
+
|
15
|
+
if (element.data("model-updates-key"))
|
16
|
+
modelSubscriptions[model][id] = true
|
17
|
+
|
18
|
+
if (element.data("model-updates-remove-on-destroy")) {
|
19
|
+
if (!modelDestroys[model])
|
20
|
+
modelDestroys[model] = {}
|
21
|
+
|
22
|
+
modelDestroys[model][id] = true
|
23
|
+
}
|
13
24
|
})
|
14
25
|
|
15
26
|
// Subscribe to the found models
|
16
|
-
for(var
|
17
|
-
for(var
|
27
|
+
for(var model in modelSubscriptions) {
|
28
|
+
for(var id in modelSubscriptions[model]) {
|
18
29
|
ModelUpdates.Update.connect({
|
19
|
-
"id":
|
20
|
-
"model":
|
30
|
+
"id": id,
|
31
|
+
"model": model
|
32
|
+
})
|
33
|
+
}
|
34
|
+
}
|
35
|
+
|
36
|
+
for(var model in modelDestroys) {
|
37
|
+
for(var id in modelDestroys[model]) {
|
38
|
+
ModelUpdates.Destroy.connect({
|
39
|
+
"id": id,
|
40
|
+
"model": model
|
21
41
|
})
|
22
42
|
}
|
23
43
|
}
|
@@ -0,0 +1,18 @@
|
|
1
|
+
ModelUpdates.Destroy = class Destroy {
|
2
|
+
static connect(args) {
|
3
|
+
ModelUpdates.debug("Connecting to destroy channel for " + args.model + "(" + args.id + ")")
|
4
|
+
|
5
|
+
App.cable.subscriptions.create(
|
6
|
+
{channel: "ModelUpdates::DestroyChannel", id: args.id, model: args.model},
|
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,12 +1,12 @@
|
|
1
1
|
ModelUpdates.Update = class Update {
|
2
2
|
static connect(args) {
|
3
|
-
ModelUpdates.debug("
|
3
|
+
ModelUpdates.debug("Connecting to update channel for " + args.model + "(" + args.id + ")")
|
4
4
|
|
5
5
|
App.cable.subscriptions.create(
|
6
6
|
{channel: "ModelUpdates::UpdateChannel", id: args.id, model: args.model},
|
7
7
|
{
|
8
8
|
received: function(json) {
|
9
|
-
ModelUpdates.debug("
|
9
|
+
ModelUpdates.debug("Received update for " + json.model + "(" + json.id + ")")
|
10
10
|
|
11
11
|
for(var key in json.changes) {
|
12
12
|
var elements = $(".model-updates[data-model-updates-model='" + json.model + "'][data-model-updates-id='" + json.id + "'][data-model-updates-key='" + key + "']")
|
@@ -11,7 +11,7 @@ module ModelUpdates::ModelExtensions
|
|
11
11
|
def model_updates_broadcast_attributes(args)
|
12
12
|
model_updates_data[:attributes] = args.fetch(:attributes)
|
13
13
|
|
14
|
-
|
14
|
+
after_commit on: :update do |model|
|
15
15
|
changes = {}
|
16
16
|
|
17
17
|
args.fetch(:attributes).each do |attribute_name|
|
@@ -27,9 +27,9 @@ module ModelUpdates::ModelExtensions
|
|
27
27
|
|
28
28
|
if changes.any?
|
29
29
|
ModelUpdates::UpdateChannel.broadcast_to(
|
30
|
-
|
30
|
+
model,
|
31
31
|
id: id,
|
32
|
-
model:
|
32
|
+
model: model.class.name,
|
33
33
|
changes: changes
|
34
34
|
)
|
35
35
|
end
|
@@ -41,7 +41,7 @@ module ModelUpdates::ModelExtensions
|
|
41
41
|
channel_name = "ModelUpdatesCreate#{model.class.name}"
|
42
42
|
|
43
43
|
attributes = {}
|
44
|
-
|
44
|
+
model.class.model_updates_data[:attributes].each do |attribute_name|
|
45
45
|
attributes[attribute_name] = __send__(attribute_name)
|
46
46
|
end
|
47
47
|
|
@@ -52,6 +52,22 @@ module ModelUpdates::ModelExtensions
|
|
52
52
|
)
|
53
53
|
end
|
54
54
|
end
|
55
|
+
|
56
|
+
def model_updates_broadcast_destroyed
|
57
|
+
after_commit on: :destroy do |model|
|
58
|
+
attributes = {}
|
59
|
+
model.class.model_updates_data[:attributes].each do |attribute_name|
|
60
|
+
attributes[attribute_name] = __send__(attribute_name)
|
61
|
+
end
|
62
|
+
|
63
|
+
ModelUpdates::DestroyChannel.broadcast_to(
|
64
|
+
model,
|
65
|
+
id: id,
|
66
|
+
model: model.class.name,
|
67
|
+
attributes: attributes
|
68
|
+
)
|
69
|
+
end
|
70
|
+
end
|
55
71
|
end
|
56
72
|
|
57
73
|
def model_updates_attrs(key, more = {})
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: model_updates
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kaspernj
|
@@ -52,11 +52,13 @@ files:
|
|
52
52
|
- app/assets/javascripts/model_updates.js
|
53
53
|
- app/assets/javascripts/model_updates/activate_elements.js
|
54
54
|
- app/assets/javascripts/model_updates/create.es6
|
55
|
+
- app/assets/javascripts/model_updates/destroy.es6
|
55
56
|
- app/assets/javascripts/model_updates/formatters.js
|
56
57
|
- app/assets/javascripts/model_updates/model_updates_class.es6
|
57
58
|
- app/assets/javascripts/model_updates/update.es6
|
58
59
|
- app/assets/stylesheets/model_updates/application.css
|
59
60
|
- app/channels/model_updates/create_channel.rb
|
61
|
+
- app/channels/model_updates/destroy_channel.rb
|
60
62
|
- app/channels/model_updates/update_channel.rb
|
61
63
|
- app/controllers/model_updates/application_controller.rb
|
62
64
|
- app/helpers/model_updates/application_helper.rb
|