view_component_reflex 2.3.1 → 2.3.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,147 +1,141 @@
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, :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
+ morph(s, html.inner_html)
24
+ end
25
+ end
26
+ else
27
+ refresh_component!
28
+ end
29
+ end
30
+
31
+ def refresh_component!
32
+ component.tap do |k|
33
+ k.define_singleton_method(:key) do
34
+ element.dataset[:key]
35
+ end
36
+ end
37
+ document = Nokogiri::HTML(controller.render_component_to_string(component))
38
+ cable_ready[channel.stream_name].morph(
39
+ selector: selector,
40
+ children_only: true,
41
+ html: document.css(selector).inner_html,
42
+ permanent_attribute_name: "data-reflex-permanent"
43
+ )
44
+ end
45
+
46
+ def refresh_all!
47
+ refresh!("body")
48
+ end
49
+
50
+ def selector
51
+ "[data-controller~=\"#{stimulus_controller}\"][data-key=\"#{element.dataset[:key]}\"]"
52
+ end
53
+
54
+ # SR's delegate_call_to_reflex in channel.rb
55
+ # uses method to gather the method parameters, but since we're abusing
56
+ # method_missing here, that'll always fail
57
+ def method(name)
58
+ component.method(name.to_sym)
59
+ end
60
+
61
+ def respond_to_missing?(name, _ = false)
62
+ !!name.to_proc
63
+ end
64
+
65
+ # this is copied out of stimulus_reflex/reflex.rb and modified
66
+ def morph(selectors, html = "")
67
+ case selectors
68
+ when :nothing
69
+ @broadcaster = StimulusReflex::NothingBroadcaster.new(self)
70
+ else
71
+ @broadcaster = StimulusReflex::SelectorBroadcaster.new(self) unless broadcaster.selector?
72
+ broadcaster.morphs << [selectors, html]
73
+ end
74
+ end
75
+
76
+ def method_missing(name, *args)
77
+ morph :nothing
78
+ super unless respond_to_missing?(name)
79
+ state.each do |k, v|
80
+ component.instance_variable_set(k, v)
81
+ end
82
+ name.to_proc.call(component, *args)
83
+ refresh! unless @prevent_refresh
84
+ end
85
+
86
+ def prevent_refresh!
87
+ @prevent_refresh = true
88
+ end
89
+
90
+ private
91
+
92
+ def component_class
93
+ self.class.component_class
94
+ end
95
+
96
+ def stimulus_controller
97
+ component_class.stimulus_controller
98
+ end
99
+
100
+ def stimulate(target, data)
101
+ dataset = {}
102
+ data.each do |k, v|
103
+ dataset["data-#{k}"] = v.to_s
104
+ end
105
+ channel.receive({
106
+ "target" => target,
107
+ "attrs" => element.attributes.to_h.symbolize_keys,
108
+ "dataset" => dataset
109
+ })
110
+ end
111
+
112
+ def component
113
+ return @component if @component
114
+ @component = component_class.allocate
115
+ reflex = self
116
+ exposed_methods = [:params, :request, :element, :refresh!, :refresh_all!, :stimulus_controller, :session, :prevent_refresh!, :selector, :stimulate]
117
+ exposed_methods.each do |meth|
118
+ @component.define_singleton_method(meth) do |*a|
119
+ reflex.send(meth, *a)
120
+ end
121
+ end
122
+ @component
123
+ end
124
+
125
+ def set_state(new_state = {})
126
+ ViewComponentReflex::Engine.state_adapter.set_state(request, controller, element.dataset[:key], new_state)
127
+ end
128
+
129
+ def state
130
+ ViewComponentReflex::Engine.state_adapter.state(request, element.dataset[:key])
131
+ end
132
+
133
+ def save_state
134
+ new_state = {}
135
+ component.safe_instance_variables.each do |k|
136
+ new_state[k] = component.instance_variable_get(k)
137
+ end
138
+ set_state(new_state)
139
+ end
140
+ end
141
+ 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
@@ -1,25 +1,25 @@
1
- VIEW_COMPONENT_REFLEX_MEMORY_STATE = {}
2
- module ViewComponentReflex
3
- module StateAdapter
4
- class Memory
5
- def self.state(request, key)
6
- VIEW_COMPONENT_REFLEX_MEMORY_STATE[request.session.id.to_s] ||= {}
7
- VIEW_COMPONENT_REFLEX_MEMORY_STATE[request.session.id.to_s][key] ||= {}
8
- end
9
-
10
- def self.set_state(request, _, key, new_state)
11
- new_state.each do |k, v|
12
- state(request, key)[k] = v
13
- end
14
- end
15
-
16
- def self.store_state(request, key, new_state = {})
17
- VIEW_COMPONENT_REFLEX_MEMORY_STATE[request.session.id.to_s] ||= {}
18
- VIEW_COMPONENT_REFLEX_MEMORY_STATE[request.session.id.to_s][key] = {}
19
- new_state.each do |k, v|
20
- VIEW_COMPONENT_REFLEX_MEMORY_STATE[request.session.id.to_s][key][k] = v
21
- end
22
- end
23
- end
24
- end
25
- end
1
+ VIEW_COMPONENT_REFLEX_MEMORY_STATE = {}
2
+ module ViewComponentReflex
3
+ module StateAdapter
4
+ class Memory
5
+ def self.state(request, key)
6
+ VIEW_COMPONENT_REFLEX_MEMORY_STATE[request.session.id.to_s] ||= {}
7
+ VIEW_COMPONENT_REFLEX_MEMORY_STATE[request.session.id.to_s][key] ||= {}
8
+ end
9
+
10
+ def self.set_state(request, _, key, new_state)
11
+ new_state.each do |k, v|
12
+ state(request, key)[k] = v
13
+ end
14
+ end
15
+
16
+ def self.store_state(request, key, new_state = {})
17
+ VIEW_COMPONENT_REFLEX_MEMORY_STATE[request.session.id.to_s] ||= {}
18
+ VIEW_COMPONENT_REFLEX_MEMORY_STATE[request.session.id.to_s][key] = {}
19
+ new_state.each do |k, v|
20
+ VIEW_COMPONENT_REFLEX_MEMORY_STATE[request.session.id.to_s][key][k] = v
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,24 +1,24 @@
1
- module ViewComponentReflex
2
- module StateAdapter
3
- class Session
4
- def self.state(request, key)
5
- request.session[key] ||= {}
6
- end
7
-
8
- def self.set_state(request, controller, key, new_state)
9
- new_state.each do |k, v|
10
- state(request, key)[k] = v
11
- end
12
- store = request.session.instance_variable_get("@by")
13
- store.commit_session request, controller.response
14
- end
15
-
16
- def self.store_state(request, key, new_state = {})
17
- request.session[key] = {}
18
- new_state.each do |k, v|
19
- request.session[key][k] = v
20
- end
21
- end
22
- end
23
- end
24
- end
1
+ module ViewComponentReflex
2
+ module StateAdapter
3
+ class Session
4
+ def self.state(request, key)
5
+ request.session[key] ||= {}
6
+ end
7
+
8
+ def self.set_state(request, controller, key, new_state)
9
+ new_state.each do |k, v|
10
+ state(request, key)[k] = v
11
+ end
12
+ store = request.session.instance_variable_get("@by")
13
+ store.commit_session request, controller.response
14
+ end
15
+
16
+ def self.store_state(request, key, new_state = {})
17
+ request.session[key] = {}
18
+ new_state.each do |k, v|
19
+ request.session[key][k] = v
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end