live_record 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +26 -19
- data/app/assets/javascripts/live_record/model/create.coffee +4 -4
- data/lib/live_record/version.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fdfd93a26658fb2c904c1099d412baa3b340590a
|
4
|
+
data.tar.gz: 40bea540c784bb1d010b53765c7022528e9e4bb2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 033ea790203cfa5c9edeb48eb03af908197224c434b78f99ff2fad781ccf1af15c65ed61d146d844f20524082dcc695a239a84824baecacef619a3006c09bd98
|
7
|
+
data.tar.gz: 41573f26ab8d05431c6577344131105ff5c48d3b0fc4376eec2295483a11034a5a3796b8c50958295fb855e753cc6ddd3a52619a19123ec0b1ad6aeeb05074e3
|
data/README.md
CHANGED
@@ -40,7 +40,7 @@
|
|
40
40
|
|
41
41
|
// now, we can just simply add a "create" callback, to apply our own logic whenever a new Book record is streamed from the backend
|
42
42
|
LiveRecord.Model.all.Book.addCallback('after:create', function() {
|
43
|
-
// let's say you have a code here that adds this new Book on the page
|
43
|
+
// let's say you have a code here that adds this new Book on the page
|
44
44
|
// `this` refers to the Book record that has been created
|
45
45
|
console.log(this);
|
46
46
|
})
|
@@ -52,9 +52,9 @@
|
|
52
52
|
// instantiate a Book object (only requirement is you pass the ID so it can be referenced when updates/destroy happen)
|
53
53
|
var book = new LiveRecord.Model.all.Book({id: 1})
|
54
54
|
|
55
|
-
// ...or you can also initialise with other attributes
|
55
|
+
// ...or you can also initialise with other attributes
|
56
56
|
// var book = new LiveRecord.Model.all.Book({id: 1, title: 'Harry Potter', created_at: '2017-08-02T12:39:49.238Z'})
|
57
|
-
|
57
|
+
|
58
58
|
// then store this Book object into the JS store
|
59
59
|
book.create();
|
60
60
|
|
@@ -114,7 +114,7 @@
|
|
114
114
|
1. Add the following to your `Gemfile`:
|
115
115
|
|
116
116
|
```ruby
|
117
|
-
gem 'live_record', '~> 0.2.
|
117
|
+
gem 'live_record', '~> 0.2.2'
|
118
118
|
```
|
119
119
|
|
120
120
|
2. Run:
|
@@ -159,7 +159,7 @@
|
|
159
159
|
### Example 1 - Simple Usage
|
160
160
|
|
161
161
|
```ruby
|
162
|
-
# app/models/book.rb
|
162
|
+
# app/models/book.rb
|
163
163
|
class Book < ApplicationRecord
|
164
164
|
include LiveRecord::Model::Callbacks
|
165
165
|
has_many :live_record_updates, as: :recordable, dependent: :destroy
|
@@ -175,11 +175,11 @@
|
|
175
175
|
### Example 2 - Advanced Usage
|
176
176
|
|
177
177
|
```ruby
|
178
|
-
# app/models/book.rb
|
178
|
+
# app/models/book.rb
|
179
179
|
class Book < ApplicationRecord
|
180
180
|
include LiveRecord::Model::Callbacks
|
181
181
|
has_many :live_record_updates, as: :recordable, dependent: :destroy
|
182
|
-
|
182
|
+
|
183
183
|
def self.live_record_whitelisted_attributes(book, current_user)
|
184
184
|
# Notice that from above, you also have access to `book` (the record currently requested by the client to be synced),
|
185
185
|
# and the `current_user`, the current user who is trying to sync the `book` record.
|
@@ -273,6 +273,9 @@
|
|
273
273
|
|
274
274
|
8. Load the records into the JS Model-store through JSON REST (i.e.):
|
275
275
|
|
276
|
+
* Any record created/loaded in the JS-store is automatically synced whenever it is updated from the backend
|
277
|
+
* When reconnected after losing connection, the records in the store are synced automatically.
|
278
|
+
|
276
279
|
### Example 1 - Using Default Loader (Requires JQuery)
|
277
280
|
|
278
281
|
> Your controller must also support responding with JSON in addition to HTML. If you used scaffold or controller generator, this should already work immediately.
|
@@ -289,13 +292,13 @@
|
|
289
292
|
```html
|
290
293
|
<!-- app/views/books/index.html.erb -->
|
291
294
|
<script>
|
292
|
-
// `loadRecords` you may also specify a URL to loadRecords (`url` defaults to `window.location.href` which is the current page)
|
295
|
+
// `loadRecords` you may also specify a URL to loadRecords (`url` defaults to `window.location.href` which is the current page)
|
293
296
|
LiveRecord.helpers.loadRecords({modelName: 'Book', url: '/some/url/that/returns_books_as_a_json'})
|
294
297
|
</script>
|
295
298
|
```
|
296
299
|
|
297
300
|
```html
|
298
|
-
<!-- app/views/
|
301
|
+
<!-- app/views/books/index.html.erb -->
|
299
302
|
<script>
|
300
303
|
// You may also pass in a callback for synchronous logic
|
301
304
|
LiveRecord.helpers.loadRecords({
|
@@ -336,7 +339,7 @@
|
|
336
339
|
|
337
340
|
```js
|
338
341
|
// subscribe
|
339
|
-
subscription = LiveRecord.Model.all.Book.subscribe();
|
342
|
+
var subscription = LiveRecord.Model.all.Book.subscribe();
|
340
343
|
|
341
344
|
// ...or subscribe only to certain conditions (i.e. when `is_enabled` attribute value is `true`)
|
342
345
|
// For the list of supported operators (like `..._eq`), see JS API `MODEL.subscribe(CONFIG)` below
|
@@ -344,24 +347,24 @@
|
|
344
347
|
|
345
348
|
// now, we can just simply add a "create" callback, to apply our own logic whenever a new Book record is streamed from the backend
|
346
349
|
LiveRecord.Model.all.Book.addCallback('after:create', function() {
|
347
|
-
// let's say you have a code here that adds this new Book on the page
|
350
|
+
// let's say you have a code here that adds this new Book on the page
|
348
351
|
// `this` refers to the Book record that has been created
|
349
352
|
console.log(this);
|
350
353
|
})
|
351
354
|
|
352
355
|
// you may also add callbacks specific to this `subscription`, as you may want to have multiple subscriptions. Then, see JS API `MODEL.subscribe(CONFIG)` below for information
|
353
356
|
|
354
|
-
//
|
357
|
+
// you may also want to unsubscribe as you wish
|
355
358
|
LiveRecord.Model.all.Book.unsubscribe(subscription);
|
356
359
|
```
|
357
360
|
|
358
361
|
### Ransack Search Queries (Optional)
|
359
|
-
|
362
|
+
|
360
363
|
* If you need more complex queries to pass into the `.subscribe(where: { ... })` above, [ransack](https://github.com/activerecord-hackery/ransack) gem is supported.
|
361
364
|
* For example you can then do:
|
362
365
|
```js
|
363
366
|
// querying upon the `belongs_to :user`
|
364
|
-
subscription = LiveRecord.Model.all.Book.subscribe({where: {user_is_admin_eq: true,
|
367
|
+
subscription = LiveRecord.Model.all.Book.subscribe({where: {user_is_admin_eq: true, is_enabled_eq: true}});
|
365
368
|
|
366
369
|
// or querying "OR" conditions
|
367
370
|
subscription = LiveRecord.Model.all.Book.subscribe({where: {title_eq: 'I am Batman', content_eq: 'I am Batman', m: 'or'}});
|
@@ -392,9 +395,9 @@
|
|
392
395
|
end
|
393
396
|
```
|
394
397
|
|
395
|
-
### Reconnection Streaming (when client got disconnected)
|
398
|
+
### Reconnection Streaming For New Records (when client got disconnected)
|
396
399
|
|
397
|
-
*
|
400
|
+
* To be able to stream newly created records upon reconnection, the only requirement is that you should have a `created_at` attribute on your Models, which by default should already be there. However, to speed up queries, I highly suggest to add index on `created_at` with the following
|
398
401
|
|
399
402
|
```bash
|
400
403
|
# this will create a file under db/migrate folder, then edit that file (see the ruby code below)
|
@@ -476,7 +479,7 @@
|
|
476
479
|
* `before:create`: (function Object)
|
477
480
|
* `after:create`: (function Object)
|
478
481
|
* subscribes to the `LiveRecord::PublicationsChannel`, which then automatically receives new records from the backend.
|
479
|
-
* you can also pass in `callbacks` (see above). These callbacks
|
482
|
+
* you can also pass in `callbacks` (see above). These callbacks are only applicable to this subscription, and is independent of the Model and Instance callbacks.
|
480
483
|
* `ATTRIBUTENAME_OPERATOR` means something like (for example): `is_enabled_eq`, where `is_enabled` is the `ATTRIBUTENAME` and `eq` is the `OPERATOR`.
|
481
484
|
* you can have as many `ATTRIBUTENAME_OPERATOR` as you like, but keep in mind that the logic applied to them is "AND", and not "OR". For "OR" conditions, use `ransack`
|
482
485
|
|
@@ -569,6 +572,10 @@
|
|
569
572
|
* MIT
|
570
573
|
|
571
574
|
## Changelog
|
572
|
-
* 0.2
|
575
|
+
* 0.2.2
|
576
|
+
* minor fix: "new records" subscription: `.modelName` was not being referenced properly, but should have not affected any functionalities.
|
577
|
+
* 0.2.1
|
578
|
+
* you can now access what attributes have changed; see [`MODEL.changes`](#modelinstancechanges) above.
|
579
|
+
* 0.2.0
|
573
580
|
* Ability to subscribe to new records (supports lost connection auto-restreaming)
|
574
|
-
* See [9th step of Setup above](#setup)
|
581
|
+
* See [9th step of Setup above](#setup)
|
@@ -14,7 +14,7 @@ LiveRecord.Model.create = (config) ->
|
|
14
14
|
this
|
15
15
|
|
16
16
|
Model.modelName = config.modelName
|
17
|
-
|
17
|
+
|
18
18
|
Model.all = {}
|
19
19
|
|
20
20
|
Model.subscriptions = []
|
@@ -61,7 +61,7 @@ LiveRecord.Model.create = (config) ->
|
|
61
61
|
)
|
62
62
|
|
63
63
|
subscription.liveRecord = {}
|
64
|
-
subscription.liveRecord.modelName =
|
64
|
+
subscription.liveRecord.modelName = Model.modelName
|
65
65
|
subscription.liveRecord.where = config.where
|
66
66
|
subscription.liveRecord.callbacks = config.callbacks
|
67
67
|
|
@@ -206,7 +206,7 @@ LiveRecord.Model.create = (config) ->
|
|
206
206
|
'before:destroy': [],
|
207
207
|
'after:destroy': []
|
208
208
|
}
|
209
|
-
|
209
|
+
|
210
210
|
Model.prototype._callbacks = {
|
211
211
|
'on:connect': [],
|
212
212
|
'on:disconnect': [],
|
@@ -268,4 +268,4 @@ LiveRecord.Model.create = (config) ->
|
|
268
268
|
# add new Model to collection
|
269
269
|
LiveRecord.Model.all[config.modelName] = Model
|
270
270
|
|
271
|
-
Model
|
271
|
+
Model
|
data/lib/live_record/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
module LiveRecord
|
2
|
-
VERSION = '0.2.
|
3
|
-
end
|
2
|
+
VERSION = '0.2.2'.freeze
|
3
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: live_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jules Roman B. Polidario
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-09-
|
11
|
+
date: 2017-09-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|