dexiecable 0.1.8 → 0.1.9

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2bc64f029d73e3630a14e254852ea7e6b3cf2b00d369f144c0b40ebdd6d4de1f
4
- data.tar.gz: ea3df61fd1319c5f8fcc8697e99a7954213b5c4f97748ae55e3e012e00f156ac
3
+ metadata.gz: 390eddcb71f97e5e8755696c49a8ce2417e7f8636e0e43be8742acd4f31f4b59
4
+ data.tar.gz: 2058ae8d76759483bc047b4e74622154335eb55fa2124052f518f51d14539a0d
5
5
  SHA512:
6
- metadata.gz: f44765e378f1860f5a239cc58169c724bc164c9eada457241a7e70f8cfcbe8ef898e2357d0dfa6981f1a6b3b170f9852a5afdb06e0dee935f7deea0489e568c2
7
- data.tar.gz: f1227ada19e9a7ccb9825bb9665ec8f7c57be8afc4b05e9f8dd35c07d8da46f92cfb9cebdb74d5b7e5ef3fe87e3a549e14f039ba4412e4f373706e6e4535a318
6
+ metadata.gz: 9057f8f5bdbb694a2673c47773db75d2d99a0f3383043908b51152f3f6279a4aabae9f4436dffeebd31e6882d684ca9f505f7a30e5bf1942de89787199a07589
7
+ data.tar.gz: 39b6503f4ac13bf8abbaae9352ea6b2355bf34f3210dfd2cd99d87413a39a606f389f39236cafca0143aa94621f8bb0be5835fb34c08027cca0d0c2a7ac6feac
data/README.md CHANGED
@@ -34,7 +34,7 @@ An even more convenient way is to use the `syncs_to_dexie` macro (more info [bel
34
34
 
35
35
  ```ruby
36
36
  class Notification < ApplicationRecord
37
- syncs_to_dexie via: UserChannel, to: :user
37
+ syncs_to_dexie via: -> { UserChannel[user] }
38
38
  end
39
39
  ```
40
40
 
@@ -133,7 +133,15 @@ The full query chain is serialized as JSON and sent over ActionCable. The JS cli
133
133
 
134
134
  ### `syncs_to_dexie` — automatic model syncing
135
135
 
136
- Add to any ActiveRecord model. Just provide the channel class and the broadcast target.
136
+ Add to any ActiveRecord model. The simplest approach: pass a Proc to `via` that returns a scoped channel.
137
+
138
+ ```ruby
139
+ class Message < ApplicationRecord
140
+ syncs_to_dexie via: -> { UserChannel[current_user] }
141
+ end
142
+ ```
143
+
144
+ You can also pass a channel class with `to:` to have DexieCable handle the scoping:
137
145
 
138
146
  ```ruby
139
147
  class Message < ApplicationRecord
@@ -161,8 +169,8 @@ Internally, `syncs_to_dexie` sets up the following ActiveRecord callbacks:
161
169
 
162
170
  | Option | Default | Description |
163
171
  |---|---|---|
164
- | `via:` | *(required)* | A DexieCable channel class |
165
- | `to:` | *(none)* | The stream target passed to `broadcast_to`. Symbol → calls `send`. String → used as-is. Proc → evaluated in record context. Returns a single recipient or collection. |
172
+ | `via:` | *(required)* | A DexieCable channel class, or a Proc that returns a channel or scoped channel (e.g. `-> { UserChannel[current_user] }`) |
173
+ | `to:` | *(none)* | The stream target passed to `broadcast_to`. Symbol → calls `send`. String → used as-is. Proc → evaluated in record context. Returns a single recipient or collection. Only used when `via` is a channel class. |
166
174
  | `table:` | model's `table_name` | Override the Dexie table name. A Proc is evaluated in the record's context. |
167
175
  | `only:` | `[:create, :update, :destroy]` | Limit which events trigger a sync |
168
176
  | `with:` | `:as_json_for_dexie` | Method name (Symbol) or Proc for serializing records |
@@ -173,11 +181,9 @@ You can combine multiple `syncs_to_dexie` declarations, each with different cond
173
181
 
174
182
  ```ruby
175
183
  class Message < ApplicationRecord
176
- syncs_to_dexie via: UserChannel, to: -> { sender },
184
+ syncs_to_dexie via: -> { UserChannel[current_user] }
185
+ syncs_to_dexie via: -> { BoardChannel[board] },
177
186
  if: :published?
178
-
179
- syncs_to_dexie via: AdminChannel,
180
- unless: -> { draft? }
181
187
  end
182
188
  ```
183
189
 
@@ -188,14 +194,14 @@ Override `as_json_for_dexie` in your model, or use the `with` option to specify
188
194
  ```ruby
189
195
  class Message < ApplicationRecord
190
196
  # Using the default as_json_for_dexie override:
191
- syncs_to_dexie via: UserChannel, to: :sender
197
+ syncs_to_dexie via: -> { UserChannel[current_user] }
192
198
 
193
199
  def as_json_for_dexie
194
200
  super.merge(room_name: room.name)
195
201
  end
196
202
 
197
203
  # Or use a custom serializer method:
198
- syncs_to_dexie via: AdminChannel, to: :admin,
204
+ syncs_to_dexie via: AdminChannel,
199
205
  with: :admin_payload
200
206
 
201
207
  def admin_payload
@@ -9,17 +9,19 @@ module DexieCable
9
9
  # DexieCable channel.
10
10
  #
11
11
  # class Message < ApplicationRecord
12
- # syncs_to_dexie via: UserChannel, to: :sender
13
- # syncs_to_dexie via: UserChannel, to: "global_feed"
14
- # syncs_to_dexie via: UserChannel, to: -> { conversation.users }
12
+ # syncs_to_dexie via: -> { UserChannel[current_user] }
15
13
  # syncs_to_dexie via: PublicChannel
14
+ # syncs_to_dexie via: UserChannel, to: :user
15
+ # syncs_to_dexie via: UserChannel, to: -> { conversation.users }
16
+ # syncs_to_dexie via: -> { tenant_channel }
16
17
  # end
17
18
  #
18
- # @param via [Class] A DexieCable channel class. When +to+
19
+ # @param via [Class, Proc] A DexieCable channel class, or a Proc
20
+ # evaluated in record context that returns a channel
21
+ # class or a scoped channel (e.g.
22
+ # +-> { UserChannel[current_user] }+). When +to+
19
23
  # is given, each recipient is mapped through
20
24
  # +via[to]+ to produce scoped channels.
21
- # Without +to+, +via+ is used directly as an
22
- # unscoped channel.
23
25
  # @param to [Proc, Symbol, String] A Proc evaluated in record
24
26
  # context, a Symbol to call via +send+, or a String
25
27
  # used directly as the stream name for +broadcast_to+.
@@ -80,11 +82,12 @@ module DexieCable
80
82
  private
81
83
 
82
84
  def resolve_channel(via, to = nil)
85
+ channel = via.is_a?(Proc) ? instance_exec(&via) : via
83
86
  if to
84
87
  recipients = resolve_recipient(to)
85
- Array(recipients).map { |r| via[r] }
88
+ Array(recipients).map { |r| channel[r] }
86
89
  else
87
- via
90
+ channel
88
91
  end
89
92
  end
90
93
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DexieCable
4
- VERSION = "0.1.8"
4
+ VERSION = "0.1.9"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dexiecable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Buhrmester