model_updates 0.0.12 → 0.0.13

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: 7ce52aac9e4372e8367b4f38870755ac6504f7a9
4
- data.tar.gz: '09643ad37dda2f57123e4d09d7638ec1a23f5300'
3
+ metadata.gz: 73c1dd0902ad412ea17983bbc6c3faf942e90897
4
+ data.tar.gz: 223a72ce8bf6ca3d13ac335d899be87b8690d453
5
5
  SHA512:
6
- metadata.gz: c42fe2e6821234087a1b764dc62a4144b10541f356bf985f00e7b7afd39d8f2f4cc6f683b29361ed589553c7944c9fe246694fc98b4093dd07fc7baf3e12a320
7
- data.tar.gz: '009db022e778a84744276567092167ecf3de57fb0134a3e216e264c39b5306421b7b0b1f11327d3d3888fd78d14bbdc7ce86955a1771f9cd3b06c3908454f81e'
6
+ metadata.gz: c95fe1dede8256bbaadefb647a9069aa9847940f1f2f8984d53895457ef5111531c931a166946274bc4448db062f29605f7f3c6fcd58c013e5fc3eb4307abf0a
7
+ data.tar.gz: b3fa1bd6fcdff3c7d488ce497fc81158d5b0e4b8ecf7f4045edc57c44bcfb9a956e4b56c43fa206da98e25fb3dbe489f3bf2626c90667270c50e558a53a69b3d
data/README.md CHANGED
@@ -1,7 +1,19 @@
1
1
  # ModelUpdates
2
2
 
3
- Rails gem to push updates to models into the frontend through ActionCable.
3
+ Rails gem to push updates through models into the frontend through ActionCable easily like this:
4
4
 
5
+ JavaScript:
6
+ ```js
7
+ ModelUpdates.connectModel("User", userId, "changed-name", function(args) {
8
+ console.log("User changed his name to: " + args.new_name)
9
+ })
10
+ ```
11
+
12
+ Ruby:
13
+ ```ruby
14
+ user = User.find(user_id)
15
+ user.model_updates_call("changed-name", new_name: "test")
16
+ ```
5
17
 
6
18
  ## Installation
7
19
  Add this line to your application's Gemfile:
@@ -107,6 +119,8 @@ Now that element should update automatically when the model is changed
107
119
 
108
120
  ## Callbacks
109
121
 
122
+ ### Updates
123
+
110
124
  You can also do a callback, once the value is changed.
111
125
 
112
126
  ```erb
@@ -136,6 +150,17 @@ data = {
136
150
  }
137
151
  ```
138
152
 
153
+ You can also do this with pure JavaScript instead of tags like this:
154
+
155
+ ```js
156
+ ModelUpdates.connectChanged("Task", taskId, function(data) {
157
+ $(".task-element").text(data.changes.name)
158
+ })
159
+ ModelUpdates.update()
160
+ ```
161
+
162
+ ### Creates
163
+
139
164
  You can receive create callbacks like this:
140
165
 
141
166
  ```js
@@ -144,6 +169,8 @@ ModelUpdates.Create.connect({model: "MyModel", onCreated: function(data) {
144
169
  })
145
170
  ```
146
171
 
172
+ ### Destroys
173
+
147
174
  If you want an element automatically removed on destroy:
148
175
  ```erb
149
176
  <div class="model-updates" data-model-updates-model="<%= model.class.name %>" data-model-updates-id="<%= model.id %>" data-model-updates-remove-on-destroy="true">
@@ -175,25 +202,25 @@ ModelUpdates.update()
175
202
  Call an event on a specific model:
176
203
  ```ruby
177
204
  user = User.find(user_id)
178
- user.model_updates_call("changed-password", new_password: "test")
205
+ user.model_updates_call("changed-name", new_name: "test")
179
206
  ```
180
207
 
181
208
  Connect to a specific model:
182
209
  ```js
183
- ModelUpdates.connectModel("User", userId, "changed-password", function(args) {
184
- console.log("User changed his password to: " + args.new_password)
210
+ ModelUpdates.connectModel("User", userId, "changed-name", function(args) {
211
+ console.log("User changed his name to: " + args.new_name)
185
212
  })
186
213
  ```
187
214
 
188
215
  Call an event on a model class:
189
216
  ```ruby
190
- User.model_updates_call("changed-password", new_password: "test")
217
+ User.model_updates_call("changed-name", new_name: "test")
191
218
  ```
192
219
 
193
220
  Connect to events called on a models class:
194
221
  ```js
195
- ModelUpdates.connectModelClass("User", "changed-password", function(args) {
196
- console.log("Someone his password to: " + args.new_password)
222
+ ModelUpdates.connectModelClass("User", "changed-name", function(args) {
223
+ console.log("Someone his name to: " + args.new_name)
197
224
  })
198
225
  ```
199
226
 
@@ -8,3 +8,7 @@
8
8
  $(document).ready(function() {
9
9
  ModelUpdates.update()
10
10
  })
11
+
12
+ document.addEventListener("turbolinks:load", function() {
13
+ ModelUpdates.update()
14
+ })
@@ -13,8 +13,8 @@ ModelUpdates.Update = class Update {
13
13
 
14
14
  for(var key in callbacks) {
15
15
  var callback = callbacks[key]
16
- ModelUpdates.debug("Calling callback")
17
- callback.apply(json)
16
+ ModelUpdates.debug("Calling callback: " + JSON.stringify(json))
17
+ callback(json)
18
18
  }
19
19
  } else {
20
20
  console.log("No callbacks for " + json.model + "(" + json.id + ")")
@@ -16,7 +16,7 @@ module ModelUpdates::ModelExtensions
16
16
  attribute_changes = {}
17
17
 
18
18
  args.fetch(:attributes).each do |attribute_name|
19
- method_changed = "#{attribute_name}_changed?"
19
+ method_changed = "saved_changed_to_#{attribute_name}?"
20
20
  next if respond_to?(method_changed) && !__send__(method_changed)
21
21
  attribute_changes[attribute_name] = __send__(attribute_name)
22
22
  end
@@ -1,3 +1,3 @@
1
1
  module ModelUpdates
2
- VERSION = "0.0.12".freeze
2
+ VERSION = "0.0.13".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.12
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-07 00:00:00.000000000 Z
11
+ date: 2018-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 5.0.0
19
+ version: 5.1.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 5.0.0
26
+ version: 5.1.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: sprockets-es6
29
29
  requirement: !ruby/object:Gem::Requirement