view_component_reflex 3.1.7 → 3.1.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +41 -0
- data/app/components/view_component_reflex/component.rb +16 -1
- data/lib/view_component_reflex/reflex.rb +29 -7
- data/lib/view_component_reflex/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 041ef93f09303abacb6ef4385394ede9d98bc8e9491a1083eb6478af27a8ce39
|
4
|
+
data.tar.gz: d13347dac95081f9d3d79e3fdfee881bf51909c7a0c8fcc64f677f4d8454e6be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c90db4e5c4a2b9190b1cf3952569d261fdf83a373f94ec9821205f30c0b108c38f57bd352e8bd978f2183a365feeb6c2ece4f5ad8dbc25083e4a5814d14cf7ee
|
7
|
+
data.tar.gz: 27be6c9f4c2b1920c7b36135cb4cc3920e1ccc78cf89c39be5ac9a823f5c6d9e18e847c466dee97032031f3ae03182967e6f4c8b00e09f85ace274f5a3be0526
|
data/README.md
CHANGED
@@ -55,6 +55,22 @@ end
|
|
55
55
|
<%= render(TodoComponent.with_collection(Todo.all)) %>
|
56
56
|
```
|
57
57
|
|
58
|
+
In case you're rendering a collection of empty models, use a UUID of some sort to address the correct component instance on your page:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
class TodoComponent < ViewComponentReflex::Component
|
62
|
+
def initialize(todo:)
|
63
|
+
@todo = todo
|
64
|
+
end
|
65
|
+
|
66
|
+
def collection_key
|
67
|
+
@todo.id || SecureRandom.hex(16)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
#
|
71
|
+
<%= render(TodoComponent.with_collection((0..5).map { Todo.new })) %>
|
72
|
+
```
|
73
|
+
|
58
74
|
## API
|
59
75
|
|
60
76
|
### permit_parameter?(initial_param, new_params)
|
@@ -385,6 +401,31 @@ end
|
|
385
401
|
```
|
386
402
|
Please use a different name to be able to save them to the session.
|
387
403
|
|
404
|
+
## Foo Can't Be Dumped
|
405
|
+
|
406
|
+
If you are getting errors that e.g. MatchData, Singleton etc. can't be dumped, ensure that you do not set any instance variables in your components (or any class you inject into them, for that matter) that cannot be marshaled.
|
407
|
+
|
408
|
+
This can be easily remedied though, by providing a list of unmarshalable instance variables and overwriting `marshal_dump` and `marshal_load` (from [https://stackoverflow.com/a/32877159/4341756](https://stackoverflow.com/a/32877159/4341756)):
|
409
|
+
|
410
|
+
```rb
|
411
|
+
class MarshalTest
|
412
|
+
UNMARSHALED_VARIABLES = [:@foo, :@bar]
|
413
|
+
|
414
|
+
def marshal_dump
|
415
|
+
instance_variables.reject{|m| UNMARSHALED_VARIABLES.include? m}.inject({}) do |vars, attr|
|
416
|
+
vars[attr] = instance_variable_get(attr)
|
417
|
+
vars
|
418
|
+
end
|
419
|
+
end
|
420
|
+
|
421
|
+
def marshal_load(vars)
|
422
|
+
vars.each do |attr, value|
|
423
|
+
instance_variable_set(attr, value) unless UNMARSHALED_VARIABLES.include?(attr)
|
424
|
+
end
|
425
|
+
end
|
426
|
+
end
|
427
|
+
```
|
428
|
+
|
388
429
|
## Anycable
|
389
430
|
|
390
431
|
@sebyx07 provided a solution to use anycable (https://github.com/joshleblanc/view_component_reflex/issues/23#issue-721786338)
|
@@ -201,10 +201,23 @@ module ViewComponentReflex
|
|
201
201
|
[
|
202
202
|
:@view_context, :@lookup_context, :@view_renderer, :@view_flow,
|
203
203
|
:@virtual_path, :@variant, :@current_template, :@output_buffer, :@key,
|
204
|
-
:@helpers, :@controller, :@request, :@tag_builder, :@state_initialized
|
204
|
+
:@helpers, :@controller, :@request, :@tag_builder, :@state_initialized,
|
205
|
+
:@_content_evaluated, :@_render_in_block
|
205
206
|
]
|
206
207
|
end
|
207
208
|
|
209
|
+
def content
|
210
|
+
if cached_content && !@_render_in_block
|
211
|
+
cached_content
|
212
|
+
else
|
213
|
+
super
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
def cached_content
|
218
|
+
@__cached_content__
|
219
|
+
end
|
220
|
+
|
208
221
|
def create_safe_state
|
209
222
|
new_state = {}
|
210
223
|
|
@@ -213,6 +226,8 @@ module ViewComponentReflex
|
|
213
226
|
new_state[k] = instance_variable_get(k)
|
214
227
|
end
|
215
228
|
|
229
|
+
new_state[:@__cached_content__] = content
|
230
|
+
|
216
231
|
new_state
|
217
232
|
end
|
218
233
|
|
@@ -5,6 +5,13 @@ module ViewComponentReflex
|
|
5
5
|
attr_accessor :component_class
|
6
6
|
end
|
7
7
|
|
8
|
+
# pretty sure I can't memoize this because we need
|
9
|
+
# to re-render every time
|
10
|
+
def controller_document
|
11
|
+
controller.process(params[:action])
|
12
|
+
Nokogiri::HTML(controller.response.body)
|
13
|
+
end
|
14
|
+
|
8
15
|
def refresh!(primary_selector = nil, *rest)
|
9
16
|
save_state
|
10
17
|
|
@@ -13,9 +20,8 @@ module ViewComponentReflex
|
|
13
20
|
end
|
14
21
|
if primary_selector
|
15
22
|
prevent_refresh!
|
16
|
-
|
17
|
-
|
18
|
-
document = Nokogiri::HTML(controller.response.body)
|
23
|
+
|
24
|
+
document = controller_document
|
19
25
|
[primary_selector, *rest].each do |s|
|
20
26
|
html = document.css(s)
|
21
27
|
if html.present?
|
@@ -41,7 +47,7 @@ module ViewComponentReflex
|
|
41
47
|
@stream = channel
|
42
48
|
end
|
43
49
|
|
44
|
-
def
|
50
|
+
def component_document
|
45
51
|
component.tap do |k|
|
46
52
|
k.define_singleton_method(:initialize_component) do
|
47
53
|
@key = element.dataset[:key]
|
@@ -49,14 +55,27 @@ module ViewComponentReflex
|
|
49
55
|
end
|
50
56
|
|
51
57
|
document = Nokogiri::HTML(component.render_in(controller.view_context))
|
58
|
+
end
|
59
|
+
|
60
|
+
def refresh_component!
|
52
61
|
CableReady::Channels.instance[stream].morph(
|
53
62
|
selector: selector,
|
54
63
|
children_only: true,
|
55
|
-
html:
|
64
|
+
html: component_document.css(selector).inner_html,
|
56
65
|
permanent_attribute_name: "data-reflex-permanent",
|
57
66
|
)
|
58
67
|
end
|
59
68
|
|
69
|
+
def default_morph
|
70
|
+
save_state
|
71
|
+
html = if component.can_render_to_string?
|
72
|
+
component_document.css(selector).to_html
|
73
|
+
else
|
74
|
+
controller_document.css(selector).to_html
|
75
|
+
end
|
76
|
+
morph selector, html
|
77
|
+
end
|
78
|
+
|
60
79
|
def stimulus_reflex_data
|
61
80
|
{
|
62
81
|
reflex_id: reflex_id,
|
@@ -96,7 +115,6 @@ module ViewComponentReflex
|
|
96
115
|
end
|
97
116
|
|
98
117
|
def method_missing(name, *args, &blk)
|
99
|
-
morph :nothing
|
100
118
|
super unless respond_to_missing?(name)
|
101
119
|
|
102
120
|
state.each do |k, v|
|
@@ -105,7 +123,11 @@ module ViewComponentReflex
|
|
105
123
|
|
106
124
|
component.send(name, *args, &blk)
|
107
125
|
|
108
|
-
|
126
|
+
if @prevent_refresh
|
127
|
+
morph :nothing
|
128
|
+
else
|
129
|
+
default_morph
|
130
|
+
end
|
109
131
|
end
|
110
132
|
|
111
133
|
def prevent_refresh!
|
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.1.
|
4
|
+
version: 3.1.12
|
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-
|
11
|
+
date: 2021-05-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -50,14 +50,14 @@ dependencies:
|
|
50
50
|
requirements:
|
51
51
|
- - ">="
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version:
|
53
|
+
version: 2.28.0
|
54
54
|
type: :runtime
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
57
|
requirements:
|
58
58
|
- - ">="
|
59
59
|
- !ruby/object:Gem::Version
|
60
|
-
version:
|
60
|
+
version: 2.28.0
|
61
61
|
- !ruby/object:Gem::Dependency
|
62
62
|
name: sqlite3
|
63
63
|
requirement: !ruby/object:Gem::Requirement
|