view_component_reflex 3.0.5 → 3.1.2

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
  SHA256:
3
- metadata.gz: 0c063a5b69263cf48e6a92937e65a73cd1a2c5e00577734f62b7cbf6186604d3
4
- data.tar.gz: 26ec1a060c0283a906df2bcda4cd11110bb31ebe69e9d5c741cf568d6dabd7de
3
+ metadata.gz: 62484c714396c468b4f5724a7595ec25607a6771def2063c0042f3e8b31b0903
4
+ data.tar.gz: 2a92435832619cd6a76369eabfff420263f50c25f0c7b0604907326fbe79c9db
5
5
  SHA512:
6
- metadata.gz: b7f55e4e85f42a9c9b0044710f8f2c295e0c13350bfc26f1c940626b581e68a851feb1c7cf4c54124a7e96ac9ade6565b1e3361f789e0efbc16644c78bc906b2
7
- data.tar.gz: aa24fa075eae92847e04931a1278b20358472fc9fdb00079bb1f0b774d84f5a3a047f90d61e42295a578999a7c69ec12e152847d77ecf6a4236554d4b4c38605
6
+ metadata.gz: aa91a67dd1ec6d2ca21eb2f973ec9a64692e0aa2120f7b2a3d3518fa513c20b8124ba78e06312646d549764df72eeaabef9b967a1113fd5e6698b1429670dedd
7
+ data.tar.gz: 67f5534034010e7e28682e5b013cd5add648a6a306ad4e99bff58fe9157a02d939795184c27c76721418000fadf0d490d94b90c18be24aea892ffb6d883b4346
data/README.md CHANGED
@@ -176,6 +176,17 @@ def do_some_global_action
176
176
  end
177
177
  ```
178
178
 
179
+ ### stream_to(channel)
180
+ Stream to a custom channel, rather than the default stimulus reflex one
181
+
182
+ ```ruby
183
+ def do_something
184
+ stream_to MyChannel
185
+
186
+ @foo = :bar
187
+ end
188
+ ```
189
+
179
190
  ### key
180
191
  This is a key unique to a particular component. It's used to reconcile state between renders, and should be passed as a data attribute whenever a reflex is called
181
192
 
@@ -19,28 +19,27 @@ module ViewComponentReflex
19
19
  [primary_selector, *rest].each do |s|
20
20
  html = document.css(s)
21
21
  if html.present?
22
- CableReady::Channels.instance[stream_name].morph(
22
+ CableReady::Channels.instance[stream].morph(
23
23
  selector: s,
24
24
  html: html.inner_html,
25
25
  children_only: true,
26
26
  permanent_attribute_name: "data-reflex-permanent",
27
- stimulus_reflex: {
28
- reflex_id: reflex_id,
29
- xpath: xpath,
30
- c_xpath: c_xpath,
31
- target: target,
32
- reflex_controller: reflex_controller,
33
- url: url,
34
- morph: :page,
35
- attrs: {key: element.dataset[:key]}
36
- }
27
+ stimulus_reflex: stimulus_reflex_data
37
28
  )
38
29
  end
39
30
  end
40
31
  else
41
32
  refresh_component!
42
33
  end
43
- cable_ready.broadcast
34
+ CableReady::Channels.instance.broadcast
35
+ end
36
+
37
+ def stream
38
+ @stream ||= stream_name
39
+ end
40
+
41
+ def stream_to(channel)
42
+ @stream = channel
44
43
  end
45
44
 
46
45
  def refresh_component!
@@ -50,24 +49,30 @@ module ViewComponentReflex
50
49
  end
51
50
  end
52
51
  document = Nokogiri::HTML(component.render_in(controller.view_context))
53
- CableReady::Channels.instance[stream_name].morph(
52
+ CableReady::Channels.instance[stream].morph(
54
53
  selector: selector,
55
54
  children_only: true,
56
55
  html: document.css(selector).inner_html,
57
56
  permanent_attribute_name: "data-reflex-permanent",
58
- stimulus_reflex: {
59
- reflex_id: reflex_id,
60
- xpath: xpath,
61
- target: target,
62
- c_xpath: c_xpath,
63
- reflex_controller: reflex_controller,
64
- url: url,
65
- morph: :page,
66
- attrs: {key: element.dataset[:key]}
67
- }
57
+ stimulus_reflex: stimulus_reflex_data
68
58
  )
69
59
  end
70
60
 
61
+ def stimulus_reflex_data
62
+ {
63
+ reflex_id: reflex_id,
64
+ xpath: xpath,
65
+ target: target,
66
+ c_xpath: c_xpath,
67
+ reflex_controller: reflex_controller,
68
+ url: url,
69
+ morph: :page,
70
+ attrs: {
71
+ key: element.dataset[:key]
72
+ }
73
+ }
74
+ end
75
+
71
76
  def target
72
77
  "#{component_class}##{method_name}"
73
78
  end
@@ -127,27 +132,50 @@ module ViewComponentReflex
127
132
  end
128
133
 
129
134
  def stimulate(target, data)
130
- dataset = {}
131
- data.each do |k, v|
132
- dataset["data-#{k}"] = v.to_s
135
+ data_to_receive = {}
136
+
137
+ stimulus_reflex_data.each do |k, v|
138
+ data_to_receive[k.to_s.camelize(:lower)] = v
139
+ end
140
+
141
+ data_to_receive["dataset"] = data.each_with_object({}) do |(k, v), o|
142
+ o["data-#{k}"] = v
133
143
  end
134
- channel.receive({
135
- "target" => target,
136
- "attrs" => element.attributes.to_h.symbolize_keys,
137
- "dataset" => dataset
138
- })
144
+
145
+ data_to_receive["attrs"] = element.attributes.to_h.symbolize_keys
146
+ data_to_receive["target"] = target
147
+
148
+ channel.receive data_to_receive
139
149
  end
140
150
 
141
151
  def component
142
152
  return @component if @component
143
153
  @component = component_class.allocate
144
154
  reflex = self
145
- exposed_methods = [:params, :request, :connection, :element, :refresh!, :refresh_all!, :stimulus_controller, :session, :prevent_refresh!, :selector, :stimulate]
155
+ exposed_methods = [
156
+ :params,
157
+ :request,
158
+ :connection,
159
+ :element,
160
+ :refresh!,
161
+ :refresh_all!,
162
+ :stimulus_controller,
163
+ :session,
164
+ :prevent_refresh!,
165
+ :selector,
166
+ :stimulate,
167
+ :stream_to
168
+ ]
146
169
  exposed_methods.each do |meth|
147
170
  @component.define_singleton_method(meth) do |*a|
148
171
  reflex.send(meth, *a)
149
172
  end
150
173
  end
174
+
175
+ @component.define_singleton_method(:reflex) do
176
+ reflex
177
+ end
178
+
151
179
  @component
152
180
  end
153
181
 
@@ -1,3 +1,3 @@
1
1
  module ViewComponentReflex
2
- VERSION = '3.0.5'
2
+ VERSION = '3.1.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: view_component_reflex
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.5
4
+ version: 3.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua LeBlanc
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-04 00:00:00.000000000 Z
11
+ date: 2021-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails