model_updates 0.0.5 → 0.0.6

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: a2ffdae24565d3467a89c6be21d9c64ac0735957
4
- data.tar.gz: 28861eb35b9775b6792ceb67f5321bca1abddd66
3
+ metadata.gz: 05326da852e8df75c3e5334f89cabc3f38177947
4
+ data.tar.gz: 063a04512f41a1f72cde419c73f3584914fb562e
5
5
  SHA512:
6
- metadata.gz: c20c840d43c7c0fcc4758e1d85197dd2f891db26f8bc5387dad0c750713e07a10f27255b864f4344c57a702f4f6c430fa5174d02c1df2c24abedff0bc3e71420
7
- data.tar.gz: 22183590cfd0b5443e715d1d7e7f6ded411e2f7ec05df1f2b4f8bd41ad230762794c7f92b2e5898489e08ca8a4ef9ecd6eb3d4edd4eec07e98ddfa92b84b5528
6
+ metadata.gz: '093382959d411617d46eec433757d6e2657c36be156f4dbf999f73810a08a6d0ba7bab3fc5b97a74045c5e5995eda1ec7551bda6349f2dc31a03852e5021c29a'
7
+ data.tar.gz: 8a00157e300e125093f08fa5b4487a2f3af50f979e2a527c1b5e795e5ee5b49ab9d4b68731013b98a5ba4668629af858dd5ca1506c130b2c4aecbfac5c66e68e
data/README.md CHANGED
@@ -20,9 +20,6 @@ Or install it yourself as:
20
20
  $ gem install model_updates
21
21
  ```
22
22
 
23
-
24
- ## Usage
25
-
26
23
  Include it in your JavaScript:
27
24
 
28
25
  ```javascript
@@ -45,7 +42,18 @@ class Model < ApplicationRecord
45
42
  end
46
43
  ```
47
44
 
48
- Do like this in your views if you are using HAML:
45
+ If you also want creates broadcasted:
46
+ ```ruby
47
+ class Model < ApplicationRecord
48
+ model_updates_broadcast_attributes attributes: [:updated_at]
49
+ model_updates_broadcast_created
50
+ end
51
+ ```
52
+
53
+ ## Usage
54
+
55
+
56
+ Do like this in your views if you are using HAML to receive automatic updates:
49
57
 
50
58
  ```haml
51
59
  .model-updates{data: {model_updates: model.model_updates_data_attrs(:updated_at)}}
@@ -93,6 +101,14 @@ data = {
93
101
  }
94
102
  ```
95
103
 
104
+ You can receive create callbacks like this:
105
+
106
+ ```js
107
+ ModelUpdates.Create.connect({model: "MyModel", onCreated: function(data) {
108
+ console.log("New MyModel was created with ID: " + data.id)
109
+ })
110
+ ```
111
+
96
112
  ## Contributing
97
113
 
98
114
  Contribution directions go here.
@@ -1,2 +1,5 @@
1
+ //= require model_updates/model_updates_class
1
2
  //= require model_updates/activate_elements
3
+ //= require model_updates/create
2
4
  //= require model_updates/formatters
5
+ //= require model_updates/update
@@ -15,41 +15,10 @@ $(document).ready(function() {
15
15
  // Subscribe to the found models
16
16
  for(var model_type in model_subscriptions) {
17
17
  for(var model_id in model_subscriptions[model_type]) {
18
- console.log("Subscribing " + model_type + "(" + model_id + ")")
19
-
20
- App.cable.subscriptions.create(
21
- {channel: "ModelUpdates::ModelChannel", id: model_id, model: model_type},
22
- {
23
- received: function(json) {
24
- console.log("Update from: " + json.model + "(" + json.id + ")")
25
-
26
- for(key in json.changes) {
27
- elements = $(".model-updates[data-model-updates-model='" + json.model + "'][data-model-updates-id='" + json.id + "'][data-model-updates-key='" + key + "']")
28
- elements.each(function() {
29
- element = $(this)
30
-
31
- if (element.data("model-updates-callback")) {
32
- function_to_call = element.data("model-updates-callback")
33
-
34
- window[function_to_call]({
35
- changes: json.changes,
36
- element: element,
37
- id: json.id,
38
- key: key,
39
- model: json.model,
40
- value: json.changes[key]
41
- })
42
- } else if(json.changes[key]) {
43
- element.text(json.changes[key])
44
- } else {
45
- // Needs to check if it has a value, else it will print out "null" instead of nothing.
46
- element.text("")
47
- }
48
- })
49
- }
50
- }
51
- }
52
- )
18
+ ModelUpdates.Update.connect({
19
+ "id": model_id,
20
+ "model": model_type
21
+ })
53
22
  }
54
23
  }
55
24
  })
@@ -0,0 +1,12 @@
1
+ ModelUpdates.Create = class Create {
2
+ static connect(args) {
3
+ App.cable.subscriptions.create(
4
+ {channel: "ModelUpdates::CreateChannel", model: args.model},
5
+ {
6
+ received: function(json) {
7
+ args.onCreated(json)
8
+ }
9
+ }
10
+ )
11
+ }
12
+ }
@@ -1,3 +1,8 @@
1
1
  function model_updates_date_time_formatter(data) {
2
- element.text(moment(data.value).format("LLLL"))
2
+ if (data.value) {
3
+ newValue = moment(data.value).format("LLLL")
4
+ data.element.text(newValue)
5
+ } else {
6
+ data.element.text("")
7
+ }
3
8
  }
@@ -0,0 +1,11 @@
1
+ class ModelUpdates {
2
+ static debug(message) {
3
+ if (ModelUpdates.configuration.debug) {
4
+ console.log(message)
5
+ }
6
+ }
7
+ }
8
+
9
+ ModelUpdates.configuration = {
10
+ "debug": false
11
+ }
@@ -0,0 +1,39 @@
1
+ ModelUpdates.Update = class Update {
2
+ static connect(args) {
3
+ ModelUpdates.debug("ModelUpdates: Connecting to " + args.model + "(" + args.id + ")")
4
+
5
+ App.cable.subscriptions.create(
6
+ {channel: "ModelUpdates::UpdateChannel", id: args.id, model: args.model},
7
+ {
8
+ received: function(json) {
9
+ ModelUpdates.debug("ModelUpdates: Received update for " + json.model + "(" + json.id + ")")
10
+
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)
15
+
16
+ if (element.data("model-updates-callback")) {
17
+ var function_to_call = element.data("model-updates-callback")
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
+ })
34
+ }
35
+ }
36
+ }
37
+ )
38
+ }
39
+ }
@@ -0,0 +1,12 @@
1
+ class ModelUpdates::CreateChannel < ApplicationCable::Channel
2
+ def subscribed
3
+ model_class = params[:model].safe_constantize
4
+ channel_name = "ModelUpdatesCreate#{params[:model]}"
5
+
6
+ stream_from(channel_name, coder: ActiveSupport::JSON) do |data|
7
+ model = model_class.accessible_by(current_ability).find(data.fetch("id"))
8
+ next unless model
9
+ transmit data
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ class ModelUpdates::UpdateChannel < ApplicationCable::Channel
2
+ def subscribed
3
+ model = params[:model].safe_constantize.find(params[:id])
4
+ authorize! :read, model
5
+ stream_for model
6
+ end
7
+ end
data/lib/model_updates.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "model_updates/engine"
2
+ require "sprockets/es6"
2
3
 
3
4
  module ModelUpdates
4
5
  path = "#{File.dirname(__FILE__)}/model_updates"
@@ -4,7 +4,13 @@ module ModelUpdates::ModelExtensions
4
4
  end
5
5
 
6
6
  module ClassMethods
7
+ def model_updates_data
8
+ @_model_updates ||= {}
9
+ end
10
+
7
11
  def model_updates_broadcast_attributes(args)
12
+ model_updates_data[:attributes] = args.fetch(:attributes)
13
+
8
14
  after_save do
9
15
  changes = {}
10
16
 
@@ -15,12 +21,12 @@ module ModelUpdates::ModelExtensions
15
21
  method_changed = "#{attribute_name}_changed?"
16
22
  end
17
23
 
18
- next unless __send__(method_changed)
24
+ next if respond_to?(method_changed) && !__send__(method_changed)
19
25
  changes[attribute_name] = __send__(attribute_name)
20
26
  end
21
27
 
22
28
  if changes.any?
23
- ModelUpdates::ModelChannel.broadcast_to(
29
+ ModelUpdates::UpdateChannel.broadcast_to(
24
30
  self,
25
31
  id: id,
26
32
  model: self.class.name,
@@ -29,6 +35,23 @@ module ModelUpdates::ModelExtensions
29
35
  end
30
36
  end
31
37
  end
38
+
39
+ def model_updates_broadcast_created
40
+ after_commit on: :create do |model|
41
+ channel_name = "ModelUpdatesCreate#{model.class.name}"
42
+
43
+ attributes = {}
44
+ self.class.model_updates_data[:attributes].each do |attribute_name|
45
+ attributes[attribute_name] = __send__(attribute_name)
46
+ end
47
+
48
+ ActionCable.server.broadcast(
49
+ channel_name,
50
+ id: id,
51
+ attributes: attributes
52
+ )
53
+ end
54
+ end
32
55
  end
33
56
 
34
57
  def model_updates_attrs(key, more = {})
@@ -1,3 +1,3 @@
1
1
  module ModelUpdates
2
- VERSION = "0.0.5".freeze
2
+ VERSION = "0.0.6".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.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-25 00:00:00.000000000 Z
11
+ date: 2017-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 5.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: sprockets-es6
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.2
27
41
  description: Rails gem to push updates to models into the frontend through ActionCable
28
42
  email:
29
43
  - kaspernj@gmail.com
@@ -37,9 +51,13 @@ files:
37
51
  - app/assets/config/model_updates_manifest.js
38
52
  - app/assets/javascripts/model_updates.js
39
53
  - app/assets/javascripts/model_updates/activate_elements.js
54
+ - app/assets/javascripts/model_updates/create.es6
40
55
  - app/assets/javascripts/model_updates/formatters.js
56
+ - app/assets/javascripts/model_updates/model_updates_class.es6
57
+ - app/assets/javascripts/model_updates/update.es6
41
58
  - app/assets/stylesheets/model_updates/application.css
42
- - app/channels/model_updates/model_channel.rb
59
+ - app/channels/model_updates/create_channel.rb
60
+ - app/channels/model_updates/update_channel.rb
43
61
  - app/controllers/model_updates/application_controller.rb
44
62
  - app/helpers/model_updates/application_helper.rb
45
63
  - app/jobs/model_updates/application_job.rb
@@ -1,6 +0,0 @@
1
- class ModelUpdates::ModelChannel < ApplicationCable::Channel
2
- def subscribed
3
- model = params[:model].constantize.find(params[:id])
4
- stream_for model
5
- end
6
- end