model_updates 0.0.7 → 0.0.9

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
2
  SHA1:
3
- metadata.gz: b6e5c02e29a478245a15cbca2daf7238ef762f83
4
- data.tar.gz: 0c4c00c9c6404c72a3ea21b3168ddc5a19be18b9
3
+ metadata.gz: f43d80cfa164be08bec5f099089518e873e4edb7
4
+ data.tar.gz: c7d5eeefdfab73846b9ae909bce65d32086d9058
5
5
  SHA512:
6
- metadata.gz: c686af4abd42d5f9a6ca9f895c6c08f62fa461c060b419d282625b38e7ee9597653fbe55dff27b5a841822c9733614936728d49a61d37d777e2decacdf10a158
7
- data.tar.gz: ea03a51dd7f1468e0950af0ffe6fb5ea6b3eee76e816d49c9d58fb5e2ac6eea8e3fc7780e7fd96394401c87ebac960ec7b583e0c07cfc914e0e8dd3b710a084e
6
+ metadata.gz: f2a59b69019b2b9b75d4b9867d8039becab8c02f90b64dce8c675e188173e5e947d7a684d1f6e189d1e332f3c9dcc4fd088b12d1d23d6018e6e6c75f9c10e49b
7
+ data.tar.gz: e04a7d37da13e364fd1cb95c15735a58b20d7f11abe693becb4093e5bc0e5f1ecb3ab6de315b9752e6aec36d9b947e2c3982bb0d2af222f8255b33e76b1dc4ed
data/README.md CHANGED
@@ -42,7 +42,7 @@ private
42
42
  delegate :authorize!, :can?, to: :current_ability
43
43
 
44
44
  def current_ability
45
- @_current_ability ||= PeakFlowAbility.new(user: current_user)
45
+ @_current_ability ||= CanCanAbility.new(user: current_user)
46
46
  end
47
47
 
48
48
  def current_user
@@ -51,6 +51,15 @@ private
51
51
  end
52
52
  ```
53
53
 
54
+ In order to get the current user with Devise, you also have to add the following `config/initializers/warden_hooks.rb`:
55
+ ```ruby
56
+ Warden::Manager.after_set_user do |user, auth, opts|
57
+ scope = opts[:scope]
58
+ auth.cookies.signed["#{scope}.id"] = user.id
59
+ auth.cookies.signed["#{scope}.expires_at"] = 70.minutes.from_now
60
+ end
61
+ ```
62
+
54
63
  You can define `authorize!(ability, model)` yourself, if you aren't using CanCan.
55
64
 
56
65
  Choose which attributes should be broadcasted automatically:
@@ -79,6 +88,7 @@ end
79
88
 
80
89
  ## Usage
81
90
 
91
+ Update content on page live automatically
82
92
 
83
93
  Do like this in your views if you are using HAML to receive automatic updates:
84
94
 
@@ -154,6 +164,14 @@ ModelUpdates.Destroy.connect({
154
164
  })
155
165
  ```
156
166
 
167
+ ### Live updating new elements added dynamically after page load
168
+
169
+ You can refresh elements with a simple call like this:
170
+
171
+ ```js
172
+ ModelUpdates.update()
173
+ ```
174
+
157
175
  ## Contributing
158
176
 
159
177
  Contribution directions go here.
@@ -0,0 +1,89 @@
1
+ ModelUpdates.Activator = class Activator {
2
+ constructor() {
3
+ ModelUpdates.debug("Activator constructor")
4
+
5
+ this.modelSubscriptions = {}
6
+ this.modelDestroys = {}
7
+ this.connectedUpdates = {}
8
+ this.connectedDestroyes = {}
9
+ }
10
+
11
+ update() {
12
+ ModelUpdates.debug("Update was called")
13
+ this.updateFoundElements()
14
+ this.updateSubscribedUpdates()
15
+ this.updateSubscribedDestroys()
16
+ }
17
+
18
+ updateFoundElements() {
19
+ ModelUpdates.debug("Activator#updateFoundElements called")
20
+
21
+ // Find all models that should be subscribed to
22
+ var that = this
23
+ $(".model-updates").each(function() {
24
+ var element = $(this)
25
+
26
+ var model = element.data("model-updates-model")
27
+ var id = element.data("model-updates-id")
28
+
29
+ ModelUpdates.debug("Model found: " + model + "(" + id + ")")
30
+
31
+ if (!that.modelSubscriptions[model])
32
+ that.modelSubscriptions[model] = {}
33
+
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
+ }
43
+ })
44
+ }
45
+
46
+ updateSubscribedUpdates() {
47
+ ModelUpdates.debug("Activator#updateSubscribedUpdates called")
48
+
49
+ for(var model in this.modelSubscriptions) {
50
+ for(var id in this.modelSubscriptions[model]) {
51
+ if (!this.connectedUpdates[model])
52
+ this.connectedUpdates[model] = {}
53
+
54
+ if (!this.connectedUpdates[model][id]) {
55
+ 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
+ })
63
+ }
64
+ }
65
+ }
66
+ }
67
+
68
+ updateSubscribedDestroys() {
69
+ ModelUpdates.debug("Activator#updateSubscribedDestroys called")
70
+
71
+ for(var model in this.modelDestroys) {
72
+ for(var id in this.modelDestroys[model]) {
73
+ if (!this.connectedDestroyes[model])
74
+ this.connectedDestroyes[model] = {}
75
+
76
+ if (!this.connectedDestroyes[model][id]) {
77
+ 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
+ })
85
+ }
86
+ }
87
+ }
88
+ }
89
+ }
@@ -4,6 +4,30 @@ class ModelUpdates {
4
4
  console.log("ModelUpdates: " + message)
5
5
  }
6
6
  }
7
+
8
+ static current() {
9
+ if (!this.currentElement)
10
+ this.currentElement = new ModelUpdates()
11
+
12
+ return this.currentElement
13
+ }
14
+
15
+ static update() {
16
+ ModelUpdates.debug("Static update called")
17
+ ModelUpdates.current().update()
18
+ }
19
+
20
+ activator() {
21
+ if (!this.activatorElement)
22
+ this.activatorElement = new ModelUpdates.Activator()
23
+
24
+ return this.activatorElement
25
+ }
26
+
27
+ update() {
28
+ ModelUpdates.debug("Instance method update called")
29
+ this.activator().update()
30
+ }
7
31
  }
8
32
 
9
33
  ModelUpdates.configuration = {
@@ -1,6 +1,10 @@
1
1
  //= require model_updates/model_updates_class
2
- //= require model_updates/activate_elements
2
+ //= require model_updates/activator
3
3
  //= require model_updates/create
4
4
  //= require model_updates/destroy
5
5
  //= require model_updates/formatters
6
6
  //= require model_updates/update
7
+
8
+ $(document).ready(function() {
9
+ ModelUpdates.update()
10
+ })
@@ -1,3 +1,3 @@
1
1
  module ModelUpdates
2
- VERSION = "0.0.7".freeze
2
+ VERSION = "0.0.9".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.7
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-10 00:00:00.000000000 Z
11
+ date: 2018-01-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -50,7 +50,7 @@ files:
50
50
  - Rakefile
51
51
  - app/assets/config/model_updates_manifest.js
52
52
  - app/assets/javascripts/model_updates.js
53
- - app/assets/javascripts/model_updates/activate_elements.js
53
+ - app/assets/javascripts/model_updates/activator.es6
54
54
  - app/assets/javascripts/model_updates/create.es6
55
55
  - app/assets/javascripts/model_updates/destroy.es6
56
56
  - app/assets/javascripts/model_updates/formatters.js
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  version: '0'
94
94
  requirements: []
95
95
  rubyforge_project:
96
- rubygems_version: 2.6.8
96
+ rubygems_version: 2.6.13
97
97
  signing_key:
98
98
  specification_version: 4
99
99
  summary: Rails gem to push updates to models into the frontend through ActionCable
@@ -1,44 +0,0 @@
1
- $(document).ready(function() {
2
- // Find all models that should be subscribed to
3
- modelSubscriptions = {}
4
- modelDestroys = {}
5
-
6
- $(".model-updates").each(function() {
7
- element = $(this)
8
-
9
- model = element.data("model-updates-model")
10
- id = element.data("model-updates-id")
11
-
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
- }
24
- })
25
-
26
- // Subscribe to the found models
27
- for(var model in modelSubscriptions) {
28
- for(var id in modelSubscriptions[model]) {
29
- ModelUpdates.Update.connect({
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
41
- })
42
- }
43
- }
44
- })