view_component_reflex 3.0.3 → 3.1.0

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: ec3bf329ad0e7bbc776b52761cadec00a33ab1c78fba847d26271b27e8a30532
4
- data.tar.gz: 5b6d10305c220bbd86486d513b97091e88afa87d6b0b91669c95d04cb1539fb2
3
+ metadata.gz: c044811ae2dc2de577c5d2c358bc47717c6db79e833537ffe30912b1324ed67e
4
+ data.tar.gz: 776a2f724d7b32dbf742bbaf3a7ffb14755767f01c13702313b5cf6f40950e4e
5
5
  SHA512:
6
- metadata.gz: 44151d570ece600f3151acffd7eb7ff7a382bc26ac36d088683c7de6439d8335643742aeb5a9de337f19646967f913678402d39a2b11baef7506d1af3beb2eef
7
- data.tar.gz: 8de0f26c998b8a1061829128d4f5a8175ae11d3a115b2e676af6d6af3a5896150cb32b72df295a53c75d83009dbaaba136bf638dd125e1d69c56dd1a38200702
6
+ metadata.gz: b941db2658a7764e881383582d5244855dd09f8ec336d20165c07a9d9104d2ece87e0a4309f91b293f4329e70f0b22019f8fe8e31469dd3edb26519ff6d575b2
7
+ data.tar.gz: 3180e0b52a0709ecba31fc9200d5265646b8149d8e10ec45a674cf87d229a7bb706663edda3ad06a1a0cc22c707b570daea15c4ae58779c9e430e12d189a894f
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
 
@@ -47,7 +47,7 @@ module ViewComponentReflex
47
47
  end
48
48
 
49
49
  def self.stimulus_controller
50
- name.chomp("Component").underscore.dasherize
50
+ name.chomp("Component").underscore.dasherize.gsub("/", "--")
51
51
  end
52
52
 
53
53
  def stimulus_reflex?
@@ -57,7 +57,7 @@ module ViewComponentReflex
57
57
  def component_controller(opts_or_tag = :div, opts = {}, &blk)
58
58
  init_key
59
59
 
60
- tag = :div
60
+ tag = :di
61
61
  options = if opts_or_tag.is_a? Hash
62
62
  opts_or_tag
63
63
  else
@@ -1,5 +1,6 @@
1
1
  require "stimulus_reflex"
2
2
  require 'view_component_reflex/reflex_factory'
3
+ require "view_component_reflex/state_adapter/base"
3
4
  require "view_component_reflex/state_adapter/session"
4
5
  require "view_component_reflex/state_adapter/memory"
5
6
  require "view_component_reflex/state_adapter/redis"
@@ -19,21 +19,12 @@ 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
@@ -43,6 +34,14 @@ module ViewComponentReflex
43
34
  cable_ready.broadcast
44
35
  end
45
36
 
37
+ def stream
38
+ @stream ||= stream_name
39
+ end
40
+
41
+ def stream_to(channel)
42
+ @stream = channel
43
+ end
44
+
46
45
  def refresh_component!
47
46
  component.tap do |k|
48
47
  k.define_singleton_method(:key) do
@@ -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,49 @@ 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
+ ]
146
168
  exposed_methods.each do |meth|
147
169
  @component.define_singleton_method(meth) do |*a|
148
170
  reflex.send(meth, *a)
149
171
  end
150
172
  end
173
+
174
+ @component.define_singleton_method(:reflex) do
175
+ reflex
176
+ end
177
+
151
178
  @component
152
179
  end
153
180
 
@@ -0,0 +1,18 @@
1
+ module ViewComponentReflex
2
+ module StateAdapter
3
+ class Base
4
+
5
+ private
6
+
7
+ def self.extract_id(request)
8
+ session = request&.session
9
+ if session.respond_to? :id
10
+ session.id.to_s
11
+ else
12
+ nil
13
+ end
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -1,7 +1,7 @@
1
1
  VIEW_COMPONENT_REFLEX_MEMORY_STATE = {}
2
2
  module ViewComponentReflex
3
3
  module StateAdapter
4
- class Memory
4
+ class Memory < Base
5
5
  def self.state(request, key)
6
6
  id = extract_id(request)
7
7
 
@@ -28,17 +28,6 @@ module ViewComponentReflex
28
28
  def self.wrap_write_async
29
29
  yield
30
30
  end
31
-
32
- private
33
-
34
- def self.extract_id(request)
35
- session = request&.session
36
- if session.respond_to? :id
37
- session.id.to_s
38
- else
39
- nil
40
- end
41
- end
42
31
  end
43
32
  end
44
33
  end
@@ -10,7 +10,7 @@
10
10
 
11
11
  module ViewComponentReflex
12
12
  module StateAdapter
13
- class Redis
13
+ class Redis < Base
14
14
  attr_reader :client, :ttl
15
15
 
16
16
  def initialize(redis_opts:, ttl: 3600)
@@ -67,8 +67,9 @@ module ViewComponentReflex
67
67
  end
68
68
 
69
69
  def get_key(request, key)
70
- "#{request.session.id.to_s}_#{key}_session_reflex_redis"
70
+ id = Redis.extract_id(request)
71
+ "#{id}_#{key}_session_reflex_redis"
71
72
  end
72
73
  end
73
74
  end
74
- end
75
+ end
@@ -1,6 +1,6 @@
1
1
  module ViewComponentReflex
2
2
  module StateAdapter
3
- class Session
3
+ class Session < Base
4
4
  def self.state(request, key)
5
5
  request.session[key] ||= {}
6
6
  end
@@ -1,3 +1,3 @@
1
1
  module ViewComponentReflex
2
- VERSION = '3.0.3'
2
+ VERSION = '3.1.0'
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.3
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua LeBlanc
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-19 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
@@ -87,6 +87,7 @@ files:
87
87
  - lib/view_component_reflex/engine.rb
88
88
  - lib/view_component_reflex/reflex.rb
89
89
  - lib/view_component_reflex/reflex_factory.rb
90
+ - lib/view_component_reflex/state_adapter/base.rb
90
91
  - lib/view_component_reflex/state_adapter/memory.rb
91
92
  - lib/view_component_reflex/state_adapter/redis.rb
92
93
  - lib/view_component_reflex/state_adapter/session.rb