view_component_reflex 2.6.2 → 3.0.0

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: a3a9136d14d056ea6ca9ca73d54b905fbad80c7c65d4bdd453dcb37519cc6a3c
4
- data.tar.gz: 33561fcf8eeff01af4c975f4367886cedfc1efcd69f38f50b9c8a9808d231eb8
3
+ metadata.gz: 047aa78feb4675abeb3bb5a5ed86ea4071c6a22fef3e06fb2806b04d651898cd
4
+ data.tar.gz: 5c0cc3c3c6fca877e8b2d8a595c601ddea90b892a62367af6abed6099a6b1b81
5
5
  SHA512:
6
- metadata.gz: bc56428b8d278bec1e50d69f36180a2714fd67a093c1bd3d32a49ee5277393eb5f6d33a72ccc33b3df412ae2fecdaab1a4422f6bb148418c4f660165bce573db
7
- data.tar.gz: 36a563499b5b313243a19d1a8e3feeb510ea0fc4126515b82ac9c0390206af754221953fffd80bcc36076970638b1c2685b3069f0985976b33ff98afa6644069
6
+ metadata.gz: 0f3f2e6f15c43e47aa3cdcd39632f4e2d7d76dd7065dcfd38b718db0e34ac100fed7268371660994c402a9c0aacd07635a935f088cec49cebbbcbccae6533aea
7
+ data.tar.gz: 56fa191797a8d3f757457a7a8c94f4394304ca0dd201639c0d47225a7f7be7ceb9b349641805783538325e884431e483a5b51714c4f75f26826912c953474d7c
data/README.md CHANGED
@@ -207,7 +207,7 @@ end
207
207
  ```
208
208
 
209
209
  ## Custom reflex base class
210
- Reflexes typically inherit from a base ApplicationReflex. You can define the base class for a view_component_reflex by using the `reflex_base_class` method.
210
+ Reflexes typically inherit from a base ApplicationReflex. You can define the base class for a view_component_reflex by using the `reflex_base_class` accessor.
211
211
  The parent class must inherit ViewComponentReflex::Reflex, and will throw an error if it does not.
212
212
 
213
213
  ```ruby
@@ -217,7 +217,7 @@ end
217
217
 
218
218
 
219
219
  class MyComponent < ViewComponentReflex::Component
220
- reflex_base_class ApplicationReflex
220
+ MyComponent.reflex_base_class = ApplicationReflex
221
221
  end
222
222
  ```
223
223
 
@@ -359,6 +359,20 @@ end
359
359
 
360
360
  StimulusReflex 3.4 introduced a fix that merges the current `request.env` and provides the CSRF token to fetch the session.
361
361
 
362
+ ## Anycable
363
+
364
+ @sebyx07 provided a solution to use anycable (https://github.com/joshleblanc/view_component_reflex/issues/23#issue-721786338)
365
+
366
+ Leaving this, might help others:
367
+
368
+ I tried this with any cable and I had to add this to development.rb
369
+ Otherwise @instance_variables were nil after a reflex
370
+
371
+ ```ruby
372
+ config.cache_store = :redis_cache_store, { url: "redis://localhost:6379/1", driver: :hiredis }
373
+ config.session_store :cache_store
374
+ ```
375
+
362
376
  ## License
363
377
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
364
378
 
@@ -1,5 +1,7 @@
1
1
  module ViewComponentReflex
2
2
  class Component < ViewComponent::Base
3
+ class_attribute :reflex_base_class, default: ViewComponentReflex::Reflex
4
+
3
5
  class << self
4
6
  def init_stimulus_reflex
5
7
  factory = ViewComponentReflex::ReflexFactory.new(self)
@@ -7,18 +9,6 @@ module ViewComponentReflex
7
9
  wire_up_callbacks if factory.new?
8
10
  end
9
11
 
10
- def reflex_base_class(new_base_class = nil)
11
- if new_base_class.nil?
12
- @reflex_base_class ||= ViewComponentReflex::Reflex
13
- else
14
- if new_base_class <= ViewComponentReflex::Reflex
15
- @reflex_base_class = new_base_class
16
- else
17
- raise StandardError.new("The reflex base class must inherit from ViewComponentReflex::Reflex")
18
- end
19
- end
20
- end
21
-
22
12
  def queue_callback(key, args, blk)
23
13
  callbacks(key).push({
24
14
  args: args,
@@ -151,7 +141,7 @@ module ViewComponentReflex
151
141
  adapter = ViewComponentReflex::Engine.state_adapter
152
142
 
153
143
  # initialize session state
154
- if !stimulus_reflex? || adapter.state(request, @key).empty?
144
+ if (!stimulus_reflex? || adapter.state(request, @key).empty?) && !@initialized_state
155
145
 
156
146
  new_state = create_safe_state
157
147
 
@@ -179,13 +169,14 @@ module ViewComponentReflex
179
169
  end
180
170
  end
181
171
  after_state_initialized(parameters_changed)
182
- @initialized_state = true
183
172
  end
173
+
174
+ @initialized_state = true
184
175
  @key
185
176
  end
186
177
 
187
178
  def safe_instance_variables
188
- instance_variables - unsafe_instance_variables
179
+ instance_variables - unsafe_instance_variables - omitted_from_state
189
180
  end
190
181
 
191
182
  private
@@ -203,7 +194,7 @@ module ViewComponentReflex
203
194
 
204
195
  # this will almost certainly break
205
196
  safe_instance_variables.each do |k|
206
- new_state[k] = instance_variable_get(k) unless omitted_from_state.include?(k)
197
+ new_state[k] = instance_variable_get(k)
207
198
  end
208
199
  new_state
209
200
  end
@@ -1,6 +1,5 @@
1
1
  module ViewComponentReflex
2
2
  class Reflex < StimulusReflex::Reflex
3
- include CableReady::Broadcaster
4
3
 
5
4
  class << self
6
5
  attr_accessor :component_class
@@ -15,16 +14,26 @@ module ViewComponentReflex
15
14
  if primary_selector
16
15
  prevent_refresh!
17
16
 
18
- controller.process(url_params[:action])
17
+ controller.process(params[:action])
19
18
  document = Nokogiri::HTML(controller.response.body)
20
19
  [primary_selector, *rest].each do |s|
21
20
  html = document.css(s)
22
21
  if html.present?
23
- cable_ready[channel.stream_name].morph(
22
+ CableReady::Channels.instance[stream_name].morph(
24
23
  selector: s,
25
24
  html: html.inner_html,
26
25
  children_only: true,
27
- permanent_attribute_name: "data-reflex-permanent"
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
+ }
28
37
  )
29
38
  end
30
39
  end
@@ -41,14 +50,28 @@ module ViewComponentReflex
41
50
  end
42
51
  end
43
52
  document = Nokogiri::HTML(component.render_in(controller.view_context))
44
- cable_ready[channel.stream_name].morph(
53
+ CableReady::Channels.instance[stream_name].morph(
45
54
  selector: selector,
46
55
  children_only: true,
47
56
  html: document.css(selector).inner_html,
48
- permanent_attribute_name: "data-reflex-permanent"
57
+ 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
+ }
49
68
  )
50
69
  end
51
70
 
71
+ def target
72
+ "#{component_class}##{method_name}"
73
+ end
74
+
52
75
  def refresh_all!
53
76
  refresh!("body")
54
77
  end
@@ -1,3 +1,3 @@
1
1
  module ViewComponentReflex
2
- VERSION = '2.6.2'
2
+ VERSION = '3.0.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: 2.6.2
4
+ version: 3.0.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-11-13 00:00:00.000000000 Z
11
+ date: 2020-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -34,22 +34,16 @@ dependencies:
34
34
  name: stimulus_reflex
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - ">="
38
- - !ruby/object:Gem::Version
39
- version: 3.3.0
40
- - - "<"
37
+ - - '='
41
38
  - !ruby/object:Gem::Version
42
- version: '3.4'
39
+ version: 3.4.0.pre8
43
40
  type: :runtime
44
41
  prerelease: false
45
42
  version_requirements: !ruby/object:Gem::Requirement
46
43
  requirements:
47
- - - ">="
48
- - !ruby/object:Gem::Version
49
- version: 3.3.0
50
- - - "<"
44
+ - - '='
51
45
  - !ruby/object:Gem::Version
52
- version: '3.4'
46
+ version: 3.4.0.pre8
53
47
  - !ruby/object:Gem::Dependency
54
48
  name: view_component
55
49
  requirement: !ruby/object:Gem::Requirement