dexiecable 0.1.1 → 0.1.3
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 +4 -4
- data/README.md +27 -9
- data/lib/dexiecable/active_record_ext.rb +27 -19
- data/lib/dexiecable/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4ad9a4f94d6491f661821d79ac6a8fa3c36527927f8fb0916eee26c87294ebdf
|
|
4
|
+
data.tar.gz: d50524564c23e50423e186edd1ed5883d9a4e1575fc687d9d80036fcac6201ec
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 017f3a4b81fe35bbc8fc17ad5b22cd8a0400ce3e4a14aa20942a517f8030c1cbf08c67b1e590b090726f20dd67c5b71869319fa568b07e01ebdc320867a829be
|
|
7
|
+
data.tar.gz: 9ccd364a0a6a6b1960bcd5c9af17475b5e972dbfc2ee839bb86d9cb3bf69fedfeb3ccbc20b471e9d6dfe42f9e458f59f32cdd158d1f2c5f6f40f1b022e41f9d8
|
data/README.md
CHANGED
|
@@ -100,18 +100,20 @@ Any Dexie.js write operation triggers an immediate broadcast:
|
|
|
100
100
|
# Single insert
|
|
101
101
|
UserChannel[current_user].table("messages").add(id: 1, text: "hello")
|
|
102
102
|
|
|
103
|
-
#
|
|
104
|
-
UserChannel[current_user]
|
|
105
|
-
.table("messages")
|
|
106
|
-
.where(:room_id).equals(room.id)
|
|
107
|
-
.bulkAdd(messages)
|
|
103
|
+
# Bulk insert
|
|
104
|
+
UserChannel[current_user].table("messages").bulkAdd(messages)
|
|
108
105
|
|
|
109
|
-
# Update
|
|
106
|
+
# Update (using modify)
|
|
110
107
|
UserChannel[current_user]
|
|
111
108
|
.table("messages")
|
|
112
109
|
.where(:id).equals(msg.id)
|
|
113
110
|
.modify(read: true)
|
|
114
111
|
|
|
112
|
+
# Update (using update)
|
|
113
|
+
UserChannel[current_user]
|
|
114
|
+
.table("messages")
|
|
115
|
+
.update(msg.id, text: "updated text")
|
|
116
|
+
|
|
115
117
|
# Delete
|
|
116
118
|
UserChannel[current_user]
|
|
117
119
|
.table("messages")
|
|
@@ -127,9 +129,11 @@ Add to any ActiveRecord model:
|
|
|
127
129
|
|
|
128
130
|
```ruby
|
|
129
131
|
class Message < ApplicationRecord
|
|
132
|
+
# Sync to a single user.
|
|
130
133
|
syncs_to_dexie via: -> { UserChannel[sender] }
|
|
131
|
-
|
|
132
|
-
|
|
134
|
+
|
|
135
|
+
# Sync to all participants of a conversation.
|
|
136
|
+
syncs_to_dexie via: -> { conversation.participants.map { |u| UserChannel[u] } }
|
|
133
137
|
end
|
|
134
138
|
```
|
|
135
139
|
|
|
@@ -143,9 +147,23 @@ end
|
|
|
143
147
|
|
|
144
148
|
| Option | Default | Description |
|
|
145
149
|
|---|---|---|
|
|
146
|
-
| `via:` | *(required)* | Proc (evaluated in record context), channel
|
|
150
|
+
| `via:` | *(required)* | Proc (evaluated in record context), a channel, or an array of channels |
|
|
147
151
|
| `table:` | model's `table_name` | Override the Dexie table name. A Proc is evaluated in the record's context. |
|
|
148
152
|
| `only:` | `[:create, :update, :destroy]` | Limit which events trigger a sync |
|
|
153
|
+
| `if:` | *(none)* | Symbol (method name) or Proc — only sync when it returns truthy |
|
|
154
|
+
| `unless:` | *(none)* | Symbol (method name) or Proc — skip sync when it returns truthy |
|
|
155
|
+
|
|
156
|
+
You can combine multiple `syncs_to_dexie` declarations, each with different conditions:
|
|
157
|
+
|
|
158
|
+
```ruby
|
|
159
|
+
class Message < ApplicationRecord
|
|
160
|
+
syncs_to_dexie via: -> { UserChannel[sender] },
|
|
161
|
+
if: :published?
|
|
162
|
+
|
|
163
|
+
syncs_to_dexie via: -> { AdminChannel },
|
|
164
|
+
unless: -> { draft? }
|
|
165
|
+
end
|
|
166
|
+
```
|
|
149
167
|
|
|
150
168
|
#### Customizing the synced payload
|
|
151
169
|
|
|
@@ -14,45 +14,53 @@ module DexieCable
|
|
|
14
14
|
# syncs_to_dexie via: PublicChannel
|
|
15
15
|
# end
|
|
16
16
|
#
|
|
17
|
-
# @param via [Proc, DexieCable] Proc evaluated in
|
|
18
|
-
# must return a channel (
|
|
19
|
-
#
|
|
17
|
+
# @param via [Proc, DexieCable, Array<DexieCable>] Proc evaluated in
|
|
18
|
+
# record context, must return a channel (or array of
|
|
19
|
+
# channels) that responds to +table+. Skipped if nil.
|
|
20
20
|
# @param table [String, Symbol, Proc] Override the Dexie table name
|
|
21
21
|
# (defaults to the model's table_name). A Proc is
|
|
22
22
|
# evaluated in the record's context.
|
|
23
23
|
# @param only [Array<Symbol>] Limit which lifecycle events sync.
|
|
24
24
|
# Default: [:create, :update, :destroy].
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
# @param if [Symbol, Proc] Only sync if the given method or proc
|
|
26
|
+
# returns truthy (evaluated in the record's context).
|
|
27
|
+
# @param unless [Symbol, Proc] Skip sync if the given method or proc
|
|
28
|
+
# returns truthy (evaluated in the record's context).
|
|
29
|
+
def syncs_to_dexie(via:, table: nil, only: nil, **options)
|
|
30
|
+
events = Array(only || %i[create update destroy])
|
|
31
|
+
conditions = options.slice(:if, :unless)
|
|
27
32
|
|
|
28
33
|
@dexie_sync_configs ||= []
|
|
29
|
-
@dexie_sync_configs << { via: via, table: table, only: events }
|
|
34
|
+
@dexie_sync_configs << { via: via, table: table, only: events, **conditions }
|
|
30
35
|
|
|
31
36
|
if events.include?(:destroy)
|
|
32
37
|
before_destroy :dexie_sync_before_destroy
|
|
33
38
|
|
|
34
|
-
after_commit on: :destroy do
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
39
|
+
after_commit on: :destroy, **conditions do
|
|
40
|
+
Array(resolve_channel(via)).each do |channel|
|
|
41
|
+
next unless channel
|
|
42
|
+
channel.table(resolve_table(table)).delete(dexie_destroy_id)
|
|
43
|
+
end
|
|
38
44
|
end
|
|
39
45
|
end
|
|
40
46
|
|
|
41
47
|
if events.include?(:create)
|
|
42
|
-
after_commit on: :create do
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
48
|
+
after_commit on: :create, **conditions do
|
|
49
|
+
Array(resolve_channel(via)).each do |channel|
|
|
50
|
+
next unless channel
|
|
51
|
+
channel.table(resolve_table(table)).add(as_json_for_dexie)
|
|
52
|
+
end
|
|
46
53
|
end
|
|
47
54
|
end
|
|
48
55
|
|
|
49
56
|
if events.include?(:update)
|
|
50
|
-
after_commit on: :update do
|
|
51
|
-
|
|
52
|
-
|
|
57
|
+
after_commit on: :update, **conditions do
|
|
58
|
+
Array(resolve_channel(via)).each do |channel|
|
|
59
|
+
next unless channel
|
|
53
60
|
|
|
54
|
-
|
|
55
|
-
|
|
61
|
+
changes = as_json_for_dexie.slice(*saved_changes.keys)
|
|
62
|
+
channel.table(resolve_table(table)).update(id, changes)
|
|
63
|
+
end
|
|
56
64
|
end
|
|
57
65
|
end
|
|
58
66
|
end
|
data/lib/dexiecable/version.rb
CHANGED