view_component_reflex 2.5.0 → 2.6.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 +4 -4
- data/README.md +361 -345
- data/app/components/view_component_reflex/component.rb +220 -214
- data/lib/view_component_reflex.rb +11 -10
- data/lib/view_component_reflex/engine.rb +36 -36
- data/lib/view_component_reflex/reflex.rb +147 -147
- data/lib/view_component_reflex/reflex_factory.rb +55 -55
- data/lib/view_component_reflex/state_adapter/memory.rb +29 -25
- data/lib/view_component_reflex/state_adapter/redis.rb +74 -0
- data/lib/view_component_reflex/state_adapter/session.rb +28 -24
- data/lib/view_component_reflex/version.rb +3 -3
- metadata +4 -4
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
module ViewComponentReflex
|
|
2
|
-
class Engine < ::Rails::Engine
|
|
3
|
-
class << self
|
|
4
|
-
mattr_accessor :state_adapter
|
|
5
|
-
|
|
6
|
-
self.state_adapter = StateAdapter::Session
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
config.to_prepare do
|
|
10
|
-
StimulusReflex::Channel.class_eval do
|
|
11
|
-
unless instance_methods.include?(:receive_original)
|
|
12
|
-
alias_method :receive_original, :receive
|
|
13
|
-
def receive(data)
|
|
14
|
-
target = data["target"].to_s
|
|
15
|
-
reflex_name, _ = target.split("#")
|
|
16
|
-
reflex_name = reflex_name.camelize
|
|
17
|
-
component_name = reflex_name.end_with?("Reflex") ? reflex_name[0...-6] : reflex_name
|
|
18
|
-
if component_name.end_with?("Component")
|
|
19
|
-
begin
|
|
20
|
-
component_name.constantize.init_stimulus_reflex
|
|
21
|
-
rescue
|
|
22
|
-
p "Tried to initialize view_component_reflex on #{component_name}, but it's not a view_component_reflex"
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
end
|
|
26
|
-
receive_original(data)
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
def self.configure
|
|
33
|
-
yield self if block_given?
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
end
|
|
1
|
+
module ViewComponentReflex
|
|
2
|
+
class Engine < ::Rails::Engine
|
|
3
|
+
class << self
|
|
4
|
+
mattr_accessor :state_adapter
|
|
5
|
+
|
|
6
|
+
self.state_adapter = StateAdapter::Session
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
config.to_prepare do
|
|
10
|
+
StimulusReflex::Channel.class_eval do
|
|
11
|
+
unless instance_methods.include?(:receive_original)
|
|
12
|
+
alias_method :receive_original, :receive
|
|
13
|
+
def receive(data)
|
|
14
|
+
target = data["target"].to_s
|
|
15
|
+
reflex_name, _ = target.split("#")
|
|
16
|
+
reflex_name = reflex_name.camelize
|
|
17
|
+
component_name = reflex_name.end_with?("Reflex") ? reflex_name[0...-6] : reflex_name
|
|
18
|
+
if component_name.end_with?("Component")
|
|
19
|
+
begin
|
|
20
|
+
component_name.constantize.init_stimulus_reflex
|
|
21
|
+
rescue
|
|
22
|
+
p "Tried to initialize view_component_reflex on #{component_name}, but it's not a view_component_reflex"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
end
|
|
26
|
+
receive_original(data)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.configure
|
|
33
|
+
yield self if block_given?
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -1,147 +1,147 @@
|
|
|
1
|
-
module ViewComponentReflex
|
|
2
|
-
class Reflex < StimulusReflex::Reflex
|
|
3
|
-
include CableReady::Broadcaster
|
|
4
|
-
|
|
5
|
-
class << self
|
|
6
|
-
attr_accessor :component_class
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
def refresh!(primary_selector = nil, *rest)
|
|
10
|
-
save_state
|
|
11
|
-
|
|
12
|
-
if primary_selector.nil? && !component.can_render_to_string?
|
|
13
|
-
primary_selector = selector
|
|
14
|
-
end
|
|
15
|
-
if primary_selector
|
|
16
|
-
prevent_refresh!
|
|
17
|
-
|
|
18
|
-
controller.process(url_params[:action])
|
|
19
|
-
document = Nokogiri::HTML(controller.response.body)
|
|
20
|
-
[primary_selector, *rest].each do |s|
|
|
21
|
-
html = document.css(s)
|
|
22
|
-
if html.present?
|
|
23
|
-
cable_ready[channel.stream_name].morph(
|
|
24
|
-
selector: s,
|
|
25
|
-
html: html.inner_html,
|
|
26
|
-
children_only: true,
|
|
27
|
-
permanent_attribute_name: "data-reflex-permanent"
|
|
28
|
-
)
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
else
|
|
32
|
-
refresh_component!
|
|
33
|
-
end
|
|
34
|
-
cable_ready.broadcast
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
def refresh_component!
|
|
38
|
-
component.tap do |k|
|
|
39
|
-
k.define_singleton_method(:key) do
|
|
40
|
-
element.dataset[:key]
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
document = Nokogiri::HTML(controller.render_component_to_string(component))
|
|
44
|
-
cable_ready[channel.stream_name].morph(
|
|
45
|
-
selector: selector,
|
|
46
|
-
children_only: true,
|
|
47
|
-
html: document.css(selector).inner_html,
|
|
48
|
-
permanent_attribute_name: "data-reflex-permanent"
|
|
49
|
-
)
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
def refresh_all!
|
|
53
|
-
refresh!("body")
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
def selector
|
|
57
|
-
"[data-controller~=\"#{stimulus_controller}\"][data-key=\"#{element.dataset[:key]}\"]"
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
# SR's delegate_call_to_reflex in channel.rb
|
|
61
|
-
# uses method to gather the method parameters, but since we're abusing
|
|
62
|
-
# method_missing here, that'll always fail
|
|
63
|
-
def method(name)
|
|
64
|
-
component.method(name.to_sym)
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
def respond_to_missing?(name, _ = false)
|
|
68
|
-
!!name.to_proc
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
# this is copied out of stimulus_reflex/reflex.rb and modified
|
|
72
|
-
def morph(selectors, html = "")
|
|
73
|
-
case selectors
|
|
74
|
-
when :nothing
|
|
75
|
-
@broadcaster = StimulusReflex::NothingBroadcaster.new(self)
|
|
76
|
-
else
|
|
77
|
-
@broadcaster = StimulusReflex::SelectorBroadcaster.new(self) unless broadcaster.selector?
|
|
78
|
-
broadcaster.morphs << [selectors, html]
|
|
79
|
-
end
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
def method_missing(name, *args)
|
|
83
|
-
morph :nothing
|
|
84
|
-
super unless respond_to_missing?(name)
|
|
85
|
-
state.each do |k, v|
|
|
86
|
-
component.instance_variable_set(k, v)
|
|
87
|
-
end
|
|
88
|
-
name.to_proc.call(component, *args)
|
|
89
|
-
refresh! unless @prevent_refresh
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
def prevent_refresh!
|
|
93
|
-
@prevent_refresh = true
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
private
|
|
97
|
-
|
|
98
|
-
def component_class
|
|
99
|
-
self.class.component_class
|
|
100
|
-
end
|
|
101
|
-
|
|
102
|
-
def stimulus_controller
|
|
103
|
-
component_class.stimulus_controller
|
|
104
|
-
end
|
|
105
|
-
|
|
106
|
-
def stimulate(target, data)
|
|
107
|
-
dataset = {}
|
|
108
|
-
data.each do |k, v|
|
|
109
|
-
dataset["data-#{k}"] = v.to_s
|
|
110
|
-
end
|
|
111
|
-
channel.receive({
|
|
112
|
-
"target" => target,
|
|
113
|
-
"attrs" => element.attributes.to_h.symbolize_keys,
|
|
114
|
-
"dataset" => dataset
|
|
115
|
-
})
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
def component
|
|
119
|
-
return @component if @component
|
|
120
|
-
@component = component_class.allocate
|
|
121
|
-
reflex = self
|
|
122
|
-
exposed_methods = [:params, :request, :connection, :element, :refresh!, :refresh_all!, :stimulus_controller, :session, :prevent_refresh!, :selector, :stimulate]
|
|
123
|
-
exposed_methods.each do |meth|
|
|
124
|
-
@component.define_singleton_method(meth) do |*a|
|
|
125
|
-
reflex.send(meth, *a)
|
|
126
|
-
end
|
|
127
|
-
end
|
|
128
|
-
@component
|
|
129
|
-
end
|
|
130
|
-
|
|
131
|
-
def set_state(new_state = {})
|
|
132
|
-
ViewComponentReflex::Engine.state_adapter.set_state(request, controller, element.dataset[:key], new_state)
|
|
133
|
-
end
|
|
134
|
-
|
|
135
|
-
def state
|
|
136
|
-
ViewComponentReflex::Engine.state_adapter.state(request, element.dataset[:key])
|
|
137
|
-
end
|
|
138
|
-
|
|
139
|
-
def save_state
|
|
140
|
-
new_state = {}
|
|
141
|
-
component.safe_instance_variables.each do |k|
|
|
142
|
-
new_state[k] = component.instance_variable_get(k)
|
|
143
|
-
end
|
|
144
|
-
set_state(new_state)
|
|
145
|
-
end
|
|
146
|
-
end
|
|
147
|
-
end
|
|
1
|
+
module ViewComponentReflex
|
|
2
|
+
class Reflex < StimulusReflex::Reflex
|
|
3
|
+
include CableReady::Broadcaster
|
|
4
|
+
|
|
5
|
+
class << self
|
|
6
|
+
attr_accessor :component_class
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def refresh!(primary_selector = nil, *rest)
|
|
10
|
+
save_state
|
|
11
|
+
|
|
12
|
+
if primary_selector.nil? && !component.can_render_to_string?
|
|
13
|
+
primary_selector = selector
|
|
14
|
+
end
|
|
15
|
+
if primary_selector
|
|
16
|
+
prevent_refresh!
|
|
17
|
+
|
|
18
|
+
controller.process(url_params[:action])
|
|
19
|
+
document = Nokogiri::HTML(controller.response.body)
|
|
20
|
+
[primary_selector, *rest].each do |s|
|
|
21
|
+
html = document.css(s)
|
|
22
|
+
if html.present?
|
|
23
|
+
cable_ready[channel.stream_name].morph(
|
|
24
|
+
selector: s,
|
|
25
|
+
html: html.inner_html,
|
|
26
|
+
children_only: true,
|
|
27
|
+
permanent_attribute_name: "data-reflex-permanent"
|
|
28
|
+
)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
else
|
|
32
|
+
refresh_component!
|
|
33
|
+
end
|
|
34
|
+
cable_ready.broadcast
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def refresh_component!
|
|
38
|
+
component.tap do |k|
|
|
39
|
+
k.define_singleton_method(:key) do
|
|
40
|
+
element.dataset[:key]
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
document = Nokogiri::HTML(controller.render_component_to_string(component))
|
|
44
|
+
cable_ready[channel.stream_name].morph(
|
|
45
|
+
selector: selector,
|
|
46
|
+
children_only: true,
|
|
47
|
+
html: document.css(selector).inner_html,
|
|
48
|
+
permanent_attribute_name: "data-reflex-permanent"
|
|
49
|
+
)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def refresh_all!
|
|
53
|
+
refresh!("body")
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def selector
|
|
57
|
+
"[data-controller~=\"#{stimulus_controller}\"][data-key=\"#{element.dataset[:key]}\"]"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# SR's delegate_call_to_reflex in channel.rb
|
|
61
|
+
# uses method to gather the method parameters, but since we're abusing
|
|
62
|
+
# method_missing here, that'll always fail
|
|
63
|
+
def method(name)
|
|
64
|
+
component.method(name.to_sym)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def respond_to_missing?(name, _ = false)
|
|
68
|
+
!!name.to_proc
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# this is copied out of stimulus_reflex/reflex.rb and modified
|
|
72
|
+
def morph(selectors, html = "")
|
|
73
|
+
case selectors
|
|
74
|
+
when :nothing
|
|
75
|
+
@broadcaster = StimulusReflex::NothingBroadcaster.new(self)
|
|
76
|
+
else
|
|
77
|
+
@broadcaster = StimulusReflex::SelectorBroadcaster.new(self) unless broadcaster.selector?
|
|
78
|
+
broadcaster.morphs << [selectors, html]
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def method_missing(name, *args)
|
|
83
|
+
morph :nothing
|
|
84
|
+
super unless respond_to_missing?(name)
|
|
85
|
+
state.each do |k, v|
|
|
86
|
+
component.instance_variable_set(k, v)
|
|
87
|
+
end
|
|
88
|
+
name.to_proc.call(component, *args)
|
|
89
|
+
refresh! unless @prevent_refresh
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def prevent_refresh!
|
|
93
|
+
@prevent_refresh = true
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
private
|
|
97
|
+
|
|
98
|
+
def component_class
|
|
99
|
+
self.class.component_class
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def stimulus_controller
|
|
103
|
+
component_class.stimulus_controller
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def stimulate(target, data)
|
|
107
|
+
dataset = {}
|
|
108
|
+
data.each do |k, v|
|
|
109
|
+
dataset["data-#{k}"] = v.to_s
|
|
110
|
+
end
|
|
111
|
+
channel.receive({
|
|
112
|
+
"target" => target,
|
|
113
|
+
"attrs" => element.attributes.to_h.symbolize_keys,
|
|
114
|
+
"dataset" => dataset
|
|
115
|
+
})
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def component
|
|
119
|
+
return @component if @component
|
|
120
|
+
@component = component_class.allocate
|
|
121
|
+
reflex = self
|
|
122
|
+
exposed_methods = [:params, :request, :connection, :element, :refresh!, :refresh_all!, :stimulus_controller, :session, :prevent_refresh!, :selector, :stimulate]
|
|
123
|
+
exposed_methods.each do |meth|
|
|
124
|
+
@component.define_singleton_method(meth) do |*a|
|
|
125
|
+
reflex.send(meth, *a)
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
@component
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def set_state(new_state = {})
|
|
132
|
+
ViewComponentReflex::Engine.state_adapter.set_state(request, controller, element.dataset[:key], new_state)
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def state
|
|
136
|
+
ViewComponentReflex::Engine.state_adapter.state(request, element.dataset[:key])
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def save_state
|
|
140
|
+
new_state = {}
|
|
141
|
+
component.safe_instance_variables.each do |k|
|
|
142
|
+
new_state[k] = component.instance_variable_get(k)
|
|
143
|
+
end
|
|
144
|
+
set_state(new_state)
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
|
@@ -1,55 +1,55 @@
|
|
|
1
|
-
module ViewComponentReflex
|
|
2
|
-
class ReflexFactory
|
|
3
|
-
def initialize(component)
|
|
4
|
-
@component = component
|
|
5
|
-
@new = false
|
|
6
|
-
reflex.component_class = component
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
def nested?
|
|
10
|
-
@nested ||= @component.name.include?("::")
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
def reflex_name
|
|
14
|
-
@reflex_name ||= if nested?
|
|
15
|
-
@component.name.split("::").last
|
|
16
|
-
else
|
|
17
|
-
@component.name
|
|
18
|
-
end + "Reflex"
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
def new?
|
|
22
|
-
@new
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
def reflex
|
|
26
|
-
@reflex ||= if nested?
|
|
27
|
-
reflex_from_nested_component
|
|
28
|
-
else
|
|
29
|
-
reflex_from_component
|
|
30
|
-
end
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
def reflex_instance
|
|
34
|
-
@reflex_instance ||= Class.new(@component.reflex_base_class)
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
def reflex_from_nested_component
|
|
38
|
-
if @component.module_parent.const_defined?(reflex_name)
|
|
39
|
-
@component.module_parent.const_get(reflex_name)
|
|
40
|
-
else
|
|
41
|
-
@new = true
|
|
42
|
-
@component.module_parent.const_set(reflex_name, reflex_instance)
|
|
43
|
-
end
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
def reflex_from_component
|
|
47
|
-
if Object.const_defined?(reflex_name)
|
|
48
|
-
Object.const_get(reflex_name)
|
|
49
|
-
else
|
|
50
|
-
@new = true
|
|
51
|
-
Object.const_set(reflex_name, reflex_instance)
|
|
52
|
-
end
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
end
|
|
1
|
+
module ViewComponentReflex
|
|
2
|
+
class ReflexFactory
|
|
3
|
+
def initialize(component)
|
|
4
|
+
@component = component
|
|
5
|
+
@new = false
|
|
6
|
+
reflex.component_class = component
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def nested?
|
|
10
|
+
@nested ||= @component.name.include?("::")
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def reflex_name
|
|
14
|
+
@reflex_name ||= if nested?
|
|
15
|
+
@component.name.split("::").last
|
|
16
|
+
else
|
|
17
|
+
@component.name
|
|
18
|
+
end + "Reflex"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def new?
|
|
22
|
+
@new
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def reflex
|
|
26
|
+
@reflex ||= if nested?
|
|
27
|
+
reflex_from_nested_component
|
|
28
|
+
else
|
|
29
|
+
reflex_from_component
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def reflex_instance
|
|
34
|
+
@reflex_instance ||= Class.new(@component.reflex_base_class)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def reflex_from_nested_component
|
|
38
|
+
if @component.module_parent.const_defined?(reflex_name)
|
|
39
|
+
@component.module_parent.const_get(reflex_name)
|
|
40
|
+
else
|
|
41
|
+
@new = true
|
|
42
|
+
@component.module_parent.const_set(reflex_name, reflex_instance)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def reflex_from_component
|
|
47
|
+
if Object.const_defined?(reflex_name)
|
|
48
|
+
Object.const_get(reflex_name)
|
|
49
|
+
else
|
|
50
|
+
@new = true
|
|
51
|
+
Object.const_set(reflex_name, reflex_instance)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|