view_component_reflex 3.1.2 → 3.1.7
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 +4 -4
- data/README.md +412 -412
- data/app/components/view_component_reflex/component.rb +228 -211
- data/lib/view_component_reflex.rb +12 -12
- data/lib/view_component_reflex/engine.rb +37 -37
- data/lib/view_component_reflex/reflex.rb +201 -198
- data/lib/view_component_reflex/reflex_factory.rb +61 -61
- data/lib/view_component_reflex/state_adapter/base.rb +18 -18
- data/lib/view_component_reflex/state_adapter/memory.rb +33 -33
- data/lib/view_component_reflex/state_adapter/redis.rb +75 -75
- data/lib/view_component_reflex/state_adapter/session.rb +28 -28
- data/lib/view_component_reflex/version.rb +3 -3
- metadata +5 -5
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
module ViewComponentReflex
|
|
2
|
-
class Engine < ::Rails::Engine
|
|
3
|
-
|
|
4
|
-
mattr_accessor :state_adapter
|
|
5
|
-
Engine.state_adapter = StateAdapter::Session
|
|
6
|
-
|
|
7
|
-
config.to_prepare do
|
|
8
|
-
StimulusReflex::Channel.class_eval do
|
|
9
|
-
unless instance_methods.include?(:receive_original)
|
|
10
|
-
alias_method :receive_original, :receive
|
|
11
|
-
def receive(data)
|
|
12
|
-
target = data["target"].to_s
|
|
13
|
-
reflex_name, _ = target.split("#")
|
|
14
|
-
reflex_name = reflex_name.camelize
|
|
15
|
-
component_name = reflex_name.end_with?("Reflex") ? reflex_name[0...-6] : reflex_name
|
|
16
|
-
component = begin
|
|
17
|
-
component_name.constantize
|
|
18
|
-
rescue
|
|
19
|
-
# Since every reflex runs through this monkey patch, we're just going to ignore the ones that aren't for components
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
if component&.respond_to?(:init_stimulus_reflex)
|
|
23
|
-
component.init_stimulus_reflex
|
|
24
|
-
else
|
|
25
|
-
p "Tried to initialize view_component_reflex on #{component_name}, but it's not a view_component_reflex"
|
|
26
|
-
end
|
|
27
|
-
receive_original(data)
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
def self.configure
|
|
34
|
-
yield self if block_given?
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
end
|
|
1
|
+
module ViewComponentReflex
|
|
2
|
+
class Engine < ::Rails::Engine
|
|
3
|
+
|
|
4
|
+
mattr_accessor :state_adapter
|
|
5
|
+
Engine.state_adapter = StateAdapter::Session
|
|
6
|
+
|
|
7
|
+
config.to_prepare do
|
|
8
|
+
StimulusReflex::Channel.class_eval do
|
|
9
|
+
unless instance_methods.include?(:receive_original)
|
|
10
|
+
alias_method :receive_original, :receive
|
|
11
|
+
def receive(data)
|
|
12
|
+
target = data["target"].to_s
|
|
13
|
+
reflex_name, _ = target.split("#")
|
|
14
|
+
reflex_name = reflex_name.camelize
|
|
15
|
+
component_name = reflex_name.end_with?("Reflex") ? reflex_name[0...-6] : reflex_name
|
|
16
|
+
component = begin
|
|
17
|
+
component_name.constantize
|
|
18
|
+
rescue
|
|
19
|
+
# Since every reflex runs through this monkey patch, we're just going to ignore the ones that aren't for components
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
if component&.respond_to?(:init_stimulus_reflex)
|
|
23
|
+
component.init_stimulus_reflex
|
|
24
|
+
else
|
|
25
|
+
p "Tried to initialize view_component_reflex on #{component_name}, but it's not a view_component_reflex"
|
|
26
|
+
end
|
|
27
|
+
receive_original(data)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def self.configure
|
|
34
|
+
yield self if block_given?
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -1,198 +1,201 @@
|
|
|
1
|
-
module ViewComponentReflex
|
|
2
|
-
class Reflex < StimulusReflex::Reflex
|
|
3
|
-
|
|
4
|
-
class << self
|
|
5
|
-
attr_accessor :component_class
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
def refresh!(primary_selector = nil, *rest)
|
|
9
|
-
save_state
|
|
10
|
-
|
|
11
|
-
if primary_selector.nil? && !component.can_render_to_string?
|
|
12
|
-
primary_selector = selector
|
|
13
|
-
end
|
|
14
|
-
if primary_selector
|
|
15
|
-
prevent_refresh!
|
|
16
|
-
|
|
17
|
-
controller.process(params[:action])
|
|
18
|
-
document = Nokogiri::HTML(controller.response.body)
|
|
19
|
-
[primary_selector, *rest].each do |s|
|
|
20
|
-
html = document.css(s)
|
|
21
|
-
if html.present?
|
|
22
|
-
CableReady::Channels.instance[stream].morph(
|
|
23
|
-
selector: s,
|
|
24
|
-
html: html.inner_html,
|
|
25
|
-
children_only: true,
|
|
26
|
-
permanent_attribute_name: "data-reflex-permanent",
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
document = Nokogiri::HTML(component.render_in(controller.view_context))
|
|
52
|
-
CableReady::Channels.instance[stream].morph(
|
|
53
|
-
selector: selector,
|
|
54
|
-
children_only: true,
|
|
55
|
-
html: document.css(selector).inner_html,
|
|
56
|
-
permanent_attribute_name: "data-reflex-permanent",
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
target: target,
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
#
|
|
89
|
-
#
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
:
|
|
157
|
-
:
|
|
158
|
-
:
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
1
|
+
module ViewComponentReflex
|
|
2
|
+
class Reflex < StimulusReflex::Reflex
|
|
3
|
+
|
|
4
|
+
class << self
|
|
5
|
+
attr_accessor :component_class
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def refresh!(primary_selector = nil, *rest)
|
|
9
|
+
save_state
|
|
10
|
+
|
|
11
|
+
if primary_selector.nil? && !component.can_render_to_string?
|
|
12
|
+
primary_selector = selector
|
|
13
|
+
end
|
|
14
|
+
if primary_selector
|
|
15
|
+
prevent_refresh!
|
|
16
|
+
|
|
17
|
+
controller.process(params[:action])
|
|
18
|
+
document = Nokogiri::HTML(controller.response.body)
|
|
19
|
+
[primary_selector, *rest].each do |s|
|
|
20
|
+
html = document.css(s)
|
|
21
|
+
if html.present?
|
|
22
|
+
CableReady::Channels.instance[stream].morph(
|
|
23
|
+
selector: s,
|
|
24
|
+
html: html.inner_html,
|
|
25
|
+
children_only: true,
|
|
26
|
+
permanent_attribute_name: "data-reflex-permanent",
|
|
27
|
+
)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
else
|
|
31
|
+
refresh_component!
|
|
32
|
+
end
|
|
33
|
+
CableReady::Channels.instance[stream].broadcast
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def stream
|
|
37
|
+
@stream ||= stream_name
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def stream_to(channel)
|
|
41
|
+
@stream = channel
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def refresh_component!
|
|
45
|
+
component.tap do |k|
|
|
46
|
+
k.define_singleton_method(:initialize_component) do
|
|
47
|
+
@key = element.dataset[:key]
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
document = Nokogiri::HTML(component.render_in(controller.view_context))
|
|
52
|
+
CableReady::Channels.instance[stream].morph(
|
|
53
|
+
selector: selector,
|
|
54
|
+
children_only: true,
|
|
55
|
+
html: document.css(selector).inner_html,
|
|
56
|
+
permanent_attribute_name: "data-reflex-permanent",
|
|
57
|
+
)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def stimulus_reflex_data
|
|
61
|
+
{
|
|
62
|
+
reflex_id: reflex_id,
|
|
63
|
+
xpath_controller: xpath_controller,
|
|
64
|
+
xpath_element: xpath_element,
|
|
65
|
+
target: target,
|
|
66
|
+
reflex_controller: reflex_controller,
|
|
67
|
+
url: url,
|
|
68
|
+
morph: :page,
|
|
69
|
+
attrs: {
|
|
70
|
+
key: element.dataset[:key]
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def target
|
|
76
|
+
"#{component_class}##{method_name}"
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def refresh_all!
|
|
80
|
+
refresh!("body")
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def selector
|
|
84
|
+
"[data-controller~=\"#{stimulus_controller}\"][data-key=\"#{element.dataset[:key]}\"]"
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# SR's delegate_call_to_reflex in channel.rb
|
|
88
|
+
# uses method to gather the method parameters, but since we're abusing
|
|
89
|
+
# method_missing here, that'll always fail
|
|
90
|
+
def method(name)
|
|
91
|
+
component.method(name.to_sym)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def respond_to_missing?(name, _ = false)
|
|
95
|
+
!!name.to_proc
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def method_missing(name, *args, &blk)
|
|
99
|
+
morph :nothing
|
|
100
|
+
super unless respond_to_missing?(name)
|
|
101
|
+
|
|
102
|
+
state.each do |k, v|
|
|
103
|
+
component.instance_variable_set(k, v)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
component.send(name, *args, &blk)
|
|
107
|
+
|
|
108
|
+
refresh! unless @prevent_refresh
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def prevent_refresh!
|
|
112
|
+
@prevent_refresh = true
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
private
|
|
116
|
+
|
|
117
|
+
def component_class
|
|
118
|
+
self.class.component_class
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def stimulus_controller
|
|
122
|
+
component_class.stimulus_controller
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def stimulate(target, data)
|
|
126
|
+
data_to_receive = {}
|
|
127
|
+
|
|
128
|
+
stimulus_reflex_data.each do |k, v|
|
|
129
|
+
data_to_receive[k.to_s.camelize(:lower)] = v
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
data_to_receive["dataset"] = data.each_with_object({}) do |(k, v), o|
|
|
133
|
+
o["data-#{k}"] = v
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
data_to_receive["attrs"] = element.attributes.to_h.symbolize_keys
|
|
137
|
+
data_to_receive["target"] = target
|
|
138
|
+
|
|
139
|
+
channel.receive data_to_receive
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def component
|
|
143
|
+
return @component if @component
|
|
144
|
+
@component = component_class.allocate
|
|
145
|
+
reflex = self
|
|
146
|
+
exposed_methods = [
|
|
147
|
+
:params,
|
|
148
|
+
:request,
|
|
149
|
+
:connection,
|
|
150
|
+
:element,
|
|
151
|
+
:refresh!,
|
|
152
|
+
:refresh_all!,
|
|
153
|
+
:stimulus_controller,
|
|
154
|
+
:session,
|
|
155
|
+
:prevent_refresh!,
|
|
156
|
+
:selector,
|
|
157
|
+
:stimulate,
|
|
158
|
+
:stream_to
|
|
159
|
+
]
|
|
160
|
+
exposed_methods.each do |meth|
|
|
161
|
+
@component.define_singleton_method(meth) do |*a|
|
|
162
|
+
reflex.send(meth, *a)
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
@component.define_singleton_method(:reflex) do
|
|
167
|
+
reflex
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
@component
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def set_state(new_state = {})
|
|
174
|
+
state_adapter.set_state(request, controller, key, new_state)
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
def key
|
|
178
|
+
element.dataset[:key]
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def state_adapter
|
|
182
|
+
ViewComponentReflex::Engine.state_adapter
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def state
|
|
186
|
+
state_adapter.state(request, key)
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
def initial_state
|
|
190
|
+
state_adapter.state(request, "#{key}_initial")
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def save_state
|
|
194
|
+
new_state = {}
|
|
195
|
+
component.safe_instance_variables.each do |k|
|
|
196
|
+
new_state[k] = component.instance_variable_get(k)
|
|
197
|
+
end
|
|
198
|
+
set_state(new_state)
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
end
|