view_component_reflex 2.0.2 → 2.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +25 -1
- data/app/components/view_component_reflex/component.rb +13 -5
- data/lib/view_component_reflex.rb +1 -0
- data/{app/reflexes → lib}/view_component_reflex/reflex.rb +24 -5
- data/lib/view_component_reflex/state_adapter/memory.rb +1 -1
- data/lib/view_component_reflex/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2fdf34d064e1a90d42f689dbeac337bfcefdecb34da14005a3333985de710d32
|
4
|
+
data.tar.gz: 8de6a22148047c13913d7363fb87c61cee167c7cb7d6b3ddc841f4d5af259233
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f4f49aca8e48a7addfdc6590cff4d2e8bf21c2aca5c9588af7ed673b90614f12d043cdfdd760a6653efaa6a064c536c693e1373be1d89130e78c4d21fc3845b
|
7
|
+
data.tar.gz: b0e40d72439efa641a044eef1c36842a6d3532a27ff6c029b7d0fa0798a61c72e468879289c5f85f4f2cb95db561c35f39af0d59653ad110e54f1ee2f620f35c
|
data/README.md
CHANGED
@@ -108,6 +108,20 @@ def collection_key
|
|
108
108
|
end
|
109
109
|
```
|
110
110
|
|
111
|
+
### stimulate(target, data)
|
112
|
+
Stimulate another reflex from within your component. This typically requires the key of the component you're stimulating
|
113
|
+
which can be passed in via parameters.
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
def initialize(parent_key)
|
117
|
+
@parent_key = parent_key
|
118
|
+
end
|
119
|
+
|
120
|
+
def stimulate_other
|
121
|
+
stimulate("OtherComponent#method", { key: @parent_key })
|
122
|
+
end
|
123
|
+
```
|
124
|
+
|
111
125
|
### refresh!(selectors)
|
112
126
|
Refresh a specific element on the page. Using this will implicitly run `prevent_render!`.
|
113
127
|
If you want to render a specific element, as well as the component, a common pattern would be to pass `selector` as one of the parameters
|
@@ -205,7 +219,7 @@ ViewComponentReflex uses session for its state by default. To change this, add
|
|
205
219
|
an initializer to `config/initializers/view_component_reflex.rb`.
|
206
220
|
|
207
221
|
```ruby
|
208
|
-
ViewComponentReflex.configure do |config|
|
222
|
+
ViewComponentReflex::Engine.configure do |config|
|
209
223
|
config.state_adapter = YourAdapter
|
210
224
|
end
|
211
225
|
```
|
@@ -265,9 +279,19 @@ Or install it yourself as:
|
|
265
279
|
$ gem install view_component_reflex
|
266
280
|
```
|
267
281
|
|
282
|
+
## Common problems
|
283
|
+
|
284
|
+
# Uninitialized constants \<component\>Reflex
|
285
|
+
A component needs to be wrapped in `<%= component_controller do %>` in order to properly initialize, otherwise the Reflex class won't get created.
|
286
|
+
|
268
287
|
## License
|
269
288
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
270
289
|
|
271
290
|
## Caveats
|
272
291
|
|
273
292
|
State uses session to maintain state as of right now. It also assumes your component view is written with the file extension `.html.erb`
|
293
|
+
|
294
|
+
## Support me
|
295
|
+
|
296
|
+
<a href="https://www.buymeacoffee.com/jleblanc" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="40" ></a>
|
297
|
+
|
@@ -2,7 +2,11 @@ module ViewComponentReflex
|
|
2
2
|
class Component < ViewComponent::Base
|
3
3
|
class << self
|
4
4
|
def init_stimulus_reflex
|
5
|
-
@stimulus_reflex ||=
|
5
|
+
@stimulus_reflex ||= if name.include? "::"
|
6
|
+
module_parent.const_set(name.split("::").last + "Reflex", Class.new(Reflex))
|
7
|
+
else
|
8
|
+
Object.const_set(name + "Reflex", Class.new(Reflex))
|
9
|
+
end
|
6
10
|
@stimulus_reflex.component_class = self
|
7
11
|
end
|
8
12
|
end
|
@@ -20,11 +24,11 @@ module ViewComponentReflex
|
|
20
24
|
init_key
|
21
25
|
|
22
26
|
tag = :div
|
23
|
-
if opts_or_tag.is_a? Hash
|
24
|
-
|
27
|
+
options = if opts_or_tag.is_a? Hash
|
28
|
+
opts_or_tag
|
25
29
|
else
|
26
30
|
tag = opts_or_tag
|
27
|
-
|
31
|
+
opts
|
28
32
|
end
|
29
33
|
options[:data] = {
|
30
34
|
controller: self.class.stimulus_controller,
|
@@ -91,7 +95,11 @@ module ViewComponentReflex
|
|
91
95
|
else
|
92
96
|
initial_state = ViewComponentReflex::Engine.state_adapter.state(request, "#{@key}_initial")
|
93
97
|
ViewComponentReflex::Engine.state_adapter.state(request, @key).each do |k, v|
|
94
|
-
|
98
|
+
instance_value = instance_variable_get(k)
|
99
|
+
if permit_parameter?(initial_state[k], instance_value)
|
100
|
+
ViewComponentReflex::Engine.state_adapter.set_state(request, controller, "#{@key}_initial", { k => instance_value })
|
101
|
+
ViewComponentReflex::Engine.state_adapter.set_state(request, controller, @key, { k => instance_value })
|
102
|
+
else
|
95
103
|
instance_variable_set(k, v)
|
96
104
|
end
|
97
105
|
end
|
@@ -2,6 +2,7 @@ require "view_component_reflex/state_adapter/session"
|
|
2
2
|
require "view_component_reflex/state_adapter/memory"
|
3
3
|
require "view_component_reflex/engine"
|
4
4
|
require "stimulus_reflex"
|
5
|
+
require "view_component_reflex/reflex"
|
5
6
|
|
6
7
|
module ViewComponentReflex
|
7
8
|
# Your code goes here...
|
@@ -61,19 +61,26 @@ module ViewComponentReflex
|
|
61
61
|
# uses method to gather the method parameters, but since we're abusing
|
62
62
|
# method_missing here, that'll always fail
|
63
63
|
def method(name)
|
64
|
-
name.to_sym
|
64
|
+
component.method(name.to_sym)
|
65
65
|
end
|
66
66
|
|
67
67
|
def respond_to_missing?(name, _ = false)
|
68
68
|
!!name.to_proc
|
69
69
|
end
|
70
70
|
|
71
|
-
|
72
|
-
|
73
|
-
|
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
|
74
80
|
end
|
75
81
|
|
76
82
|
def method_missing(name, *args)
|
83
|
+
morph :nothing
|
77
84
|
super unless respond_to_missing?(name)
|
78
85
|
state.each do |k, v|
|
79
86
|
component.instance_variable_set(k, v)
|
@@ -96,11 +103,23 @@ module ViewComponentReflex
|
|
96
103
|
component_class.stimulus_controller
|
97
104
|
end
|
98
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
|
+
|
99
118
|
def component
|
100
119
|
return @component if @component
|
101
120
|
@component = component_class.allocate
|
102
121
|
reflex = self
|
103
|
-
exposed_methods = [:params, :request, :element, :refresh!, :refresh_all!, :stimulus_controller, :session, :prevent_refresh!, :selector]
|
122
|
+
exposed_methods = [:params, :request, :element, :refresh!, :refresh_all!, :stimulus_controller, :session, :prevent_refresh!, :selector, :stimulate]
|
104
123
|
exposed_methods.each do |meth|
|
105
124
|
@component.define_singleton_method(meth) do |*a|
|
106
125
|
reflex.send(meth, *a)
|
@@ -15,7 +15,7 @@ module ViewComponentReflex
|
|
15
15
|
|
16
16
|
def self.store_state(request, key, new_state = {})
|
17
17
|
VIEW_COMPONENT_REFLEX_MEMORY_STATE[request.session.id.to_s] ||= {}
|
18
|
-
VIEW_COMPONENT_REFLEX_MEMORY_STATE[request.session.id.to_s][key]
|
18
|
+
VIEW_COMPONENT_REFLEX_MEMORY_STATE[request.session.id.to_s][key] = {}
|
19
19
|
new_state.each do |k, v|
|
20
20
|
VIEW_COMPONENT_REFLEX_MEMORY_STATE[request.session.id.to_s][key][k] = v
|
21
21
|
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.
|
4
|
+
version: 2.1.4
|
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
|
+
date: 2020-08-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -69,9 +69,9 @@ files:
|
|
69
69
|
- README.md
|
70
70
|
- Rakefile
|
71
71
|
- app/components/view_component_reflex/component.rb
|
72
|
-
- app/reflexes/view_component_reflex/reflex.rb
|
73
72
|
- lib/view_component_reflex.rb
|
74
73
|
- lib/view_component_reflex/engine.rb
|
74
|
+
- lib/view_component_reflex/reflex.rb
|
75
75
|
- lib/view_component_reflex/state_adapter/memory.rb
|
76
76
|
- lib/view_component_reflex/state_adapter/session.rb
|
77
77
|
- lib/view_component_reflex/version.rb
|