action_cable_notifications 0.1.26 → 0.1.27

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: 128e7b1e0ecccea4d7e4fe2dcf659e51e99eb618
4
- data.tar.gz: 31a94217b247da02df04bc12adaad216caa9fe24
3
+ metadata.gz: bbf5459391f70a5ea26fcbe49d73d658ee0018e1
4
+ data.tar.gz: 139cee45492e3dbac434fa59428784ee779ffc14
5
5
  SHA512:
6
- metadata.gz: 2e27bee4eb7e04565e158b1d3f846bca3ac6257aced6a4d8168f1110c9b9ea8a4e144e7db882ae0da4ed9fdb5bbec77ba0ebaf1d93873ed33f4db830ff8bdaed
7
- data.tar.gz: b0ab50fcad5961c25c9be9e368be89d2440927830c06015583f663334b98b92ad6cfb04dfeb6d105693f6403f4fd7ef554d11d8297cde06b5fa6a8c57f93c50f
6
+ metadata.gz: f81b284758a85fb93d2c189ae4ec8b4f642ba4855309534017e0f3def93260851545d35a23a4ea5a1ba125c58c331c9df50cc29d9fa939e563786b272a1c9164
7
+ data.tar.gz: dbdf1d02cc3bc63458ddc7e460f8e47a48d58031a2b6008ca22943b1fc1a84fbda856962241dba5e43ad3b8773a1d018a4954aaf2563248935758f9b4641f885
data/README.md CHANGED
@@ -1,8 +1,13 @@
1
1
  # ActionCableNotifications
2
2
  [![Gem Version](https://badge.fury.io/rb/action_cable_notifications.svg)](https://badge.fury.io/rb/action_cable_notifications)
3
3
 
4
+ ## Description
4
5
  **This gem is being developed as part of an internal proyect. It's constantly changing and is not ready for production usage. Use at your own risk!!**
5
6
 
7
+ This gem provides realtime sync of Model data between a Rails 5 app and its web clients. It was inspired in [meteor's](https://www.meteor.com/) [ddp](https://github.com/meteor/meteor/tree/devel/packages/ddp) and [minimongo](https://github.com/meteor/meteor/tree/devel/packages/minimongo) for data syncing and client side storage. It uses new Rails 5 Action Cable to communicate with the server and sync collections in realtime.
8
+
9
+ Check the sample [todo app](/examples/todo-vuejs) that uses this gem with [VueJS](http://vuejs.org/) for rendering. Will try to upload more examples shortly.
10
+
6
11
  ## Usage
7
12
 
8
13
  ### Server side
@@ -11,10 +16,12 @@ On **server-side**, create a new channel (`rails g cahnnel Test`) or modify exis
11
16
  ```ruby
12
17
  class TestChannel < ApplicationCable::Channel
13
18
 
14
- include ActionCableNotifications::Streams
19
+ include ActionCableNotifications::Channel
15
20
 
16
21
  def subscribed
17
- stream_notifications_for Users, include_initial: true, scope: [:all, [:limit, 5], [:order, :id]]
22
+ stream_notifications_for Customer
23
+ # Can have more than one ActiveRecord model streaming per channel
24
+ stream_notifications_for Invoice, scope: { limit: 5, order: :id, select: [:id, :customer_id, :seller_id, :amount] }
18
25
  end
19
26
 
20
27
  def unsubscribed
@@ -35,24 +42,103 @@ stream_notifications_for(model, options = {}, &block)
35
42
  {
36
43
  actions: [:create, :update, :destroy], # Controller actions to attach to
37
44
  broadcasting: model.model_name.collection, # Name of the pubsub stream
38
- callback: nil, # Same as http://edgeapi.rubyonrails.org/classes/ActionCable/Channel/Streams.html
39
- coder: nil, # Pass `coder: ActiveSupport::JSON` to decode messages as JSON before passing to the callback.
40
- # Defaults to `coder: nil` which does no decoding, passes raw messages.
41
- include_initial: false, # Send all records to the subscriber upon connection
42
45
  params: params, # Params sent during subscription
43
- scope: :all # Default collection scope
46
+ scope: :all # Default collection scope. Can be an Array or Hash
44
47
  }
45
48
  ```
46
- * block: **(Proc)** - Same as options[:callback]
47
49
 
48
50
  ### Client side
49
- On **client-side**, use action_cable subscriptions as stated in the documentation. Received data will have the following format:
51
+ On **client-side**, you will need to create a subscription to the channel and then you can instantiate a **Store** and one or more **Collections** to keep the data synced with the server. You can register more than one store per application and more than one collection per store.
52
+
53
+ ```javascript
54
+ App.testChannel = App.cable.subscriptions.create("TestChannel", {
55
+ connected: function() {
56
+ return console.log("Connected")
57
+ },
58
+ disconnected: function() {
59
+ return console.log("Disconnected")
60
+ }
61
+ }
62
+
63
+ // Create a store
64
+ store = App.cableNotifications.registerStore('storeName')
65
+
66
+ // Create the collections and sync them with the server using testChannel
67
+ customersCollection = store.registerCollection('customers', App.testChannel)
68
+ invoicesCollection = store.registerCollection('invoices', App.testChannel)
69
+ ```
70
+
71
+ That's it! Now you have customers and invoices collections available in your clients. Data will be available as an array in customersCollection.data and invoicesCollection.data objects.
72
+
73
+ #### Stores
74
+ Stores are groups of collections. They hold a list of available collections and its options for syncing with the server using channels subscriptions. They expose the following methods:
75
+
76
+ ##### registerCollection(collectionName, channelSubscription, tableName)
77
+ Called to register a new collection into the store. **collectionName** must be unique in the store. **channelSubscription** specifies which channel to use to sync with the server. Multiple collections can share the same channel for syncing. If no channel is specified, the collection will work standalone. You can always turn on syncing for a collection later using *syncToChannel* method.
78
+
79
+ By default, *collectionName* is used on the server side to identify the table on the DB. If you are using a different tablename, you can specify it in the *tableName* parameter.
80
+
81
+ Example:
82
+ ```javascript
83
+ customersCollection = store.registerCollection('customers', App.testChannel)
84
+ ```
85
+
86
+ ##### syncToChannel()
87
+ Used to sync a standalone collection with the server using a channel.
88
+ ```javascript
89
+ store.syncToChannel(App.testChannel, customersCollection)
90
+ ```
91
+
92
+ #### Collections
93
+ Collections is where data is stored on clients. Collections can be standalone or synced with the server using channel subcriptions.
94
+
95
+ The registered collection object expose some methods to give easy access to data stored on clients. It uses [lodash](https://lodash.com) for data manipulation, so you can check its documentation for details on parameters.
96
+
97
+ ##### fetch(parameters)
98
+ Retrieves data from the server. You can specify a hash of parameters to send to server.
99
+ ```javascript
100
+ invoicesCollection.fetch({limit: 10, where: 'amount>100'})
101
+ ```
102
+ ##### filter(selector)
103
+ Filter data already present in the client and return all records that met the field values specified in *selector* parameter. You can specify a hash of options or a callback function.
104
+ ```javascript
105
+ invoices = invoicesCollection.where({customer_id: 14})
106
+ ```
107
+ ##### find(selector)
108
+ Same as filter but returns only the first record found.
109
+ ```javascript
110
+ invoice = invoicesCollection.find({customer_id: 14})
111
+ ```
112
+ ##### create(fields)
113
+ Create a new record having the specified field values and sync it with the server.
114
+ ```javascript
115
+ invoice = invoicesCollection.create({customer_id: 14, seller_id: 5, amount: 100})
116
+ ```
117
+ ##### update(selector, fields)
118
+ Updates an existing record identified by *selector* with the specified field values and sync it with the server.
119
+ ```javascript
120
+ invoice = invoicesCollection.update({id: 55},{seller_id: 6, amount: 150})
121
+ ```
122
+ ##### upsert(selector, fields)
123
+ If an existing record cannot be found for updating, a new record is created with the specified fields.
124
+ ```javascript
125
+ invoice = invoicesCollection.upsert({id: 55},{seller_id: 6, amount: 150})
126
+ ```
127
+ ##### destroy(selector)
128
+ Destroys an existing record identified by *selector* and sync it with the server.
129
+ ```javascript
130
+ invoicesCollection.destroy({id: 55})
131
+ ```
132
+
133
+ ### Server sent messages:
134
+
135
+ Received data will have the following format:
50
136
 
51
137
  #### Initial values for collection
52
138
  ```javascript
53
139
  {
54
140
  collection: 'users',
55
- msg: 'add_collection',
141
+ msg: 'upsert_many',
56
142
  data: [
57
143
  {
58
144
  id: 1,
@@ -68,6 +68,9 @@ class CableNotifications.Collection
68
68
  else
69
69
  _.filter(@data, selector)
70
70
 
71
+ filter: (selector={}) ->
72
+ @where(selector)
73
+
71
74
  # Find a record
72
75
  find: (selector={}, options={}) ->
73
76
  record = _.find(@data, selector)
@@ -96,7 +96,8 @@ module ActionCableNotifications
96
96
  def stream_notifications_for(model, options = {})
97
97
  # Default options
98
98
  options = {
99
- broadcasting: model.model_name.collection
99
+ broadcasting: model.model_name.collection,
100
+ params: nil
100
101
  }.merge(options)
101
102
 
102
103
  # These options cannot be overridden
@@ -1,3 +1,3 @@
1
1
  module ActionCableNotifications
2
- VERSION = '0.1.26'
2
+ VERSION = '0.1.27'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_cable_notifications
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.26
4
+ version: 0.1.27
5
5
  platform: ruby
6
6
  authors:
7
7
  - ByS Sistemas de Control
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-08 00:00:00.000000000 Z
11
+ date: 2017-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails