view_component_reflex 2.1.1 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +32 -2
- data/app/components/view_component_reflex/component.rb +17 -12
- data/lib/view_component_reflex.rb +1 -0
- data/{app/reflexes → lib}/view_component_reflex/reflex.rb +11 -4
- data/lib/view_component_reflex/state_adapter/memory.rb +1 -1
- data/lib/view_component_reflex/version.rb +1 -1
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ac0e4b7fc45ba1963f6d260f66463a00a37fccfb6db10855f608beb8f54126e
|
4
|
+
data.tar.gz: eaa30fcda3a41ffc51f651398d490a51b89d1eb06fdf81619e2a9ee0535b62f7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa7fed3d3bd4aa4718c5c5358c798b6e0a9edb028d26bb26228989cf6c17f9348036a5ab4d3932c376cb632b7eb145d1e7e1f37a44405e6188609cf1b3942bd8
|
7
|
+
data.tar.gz: f761bbdc0c3a11edbcc01e49fe704fa4a8dac9fdf167a5068c2187d28bb35b400afe542edd2d2b7fe2964a0d5a224a299d00de660a47c26525162e12b8d5efea
|
data/README.md
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
ViewComponentReflex allows you to write reflexes right in your view component code.
|
4
4
|
|
5
|
+
It builds upon [stimulus_reflex](https://github.com/hopsoft/stimulus_reflex) and [view_component](https://github.com/github/view_component)
|
6
|
+
|
5
7
|
## Usage
|
6
8
|
|
7
9
|
You can add reflexes to your component by adding inheriting from `ViewComponentReflex::Component`.
|
@@ -88,12 +90,30 @@ This shares the same definition as `content_tag`, except it accepts a reflex as
|
|
88
90
|
|
89
91
|
Would add a click handler to the `increment` method on your component.
|
90
92
|
|
91
|
-
To use a non-click event, specific that with `->`
|
93
|
+
To use a non-click event, specific that with `->` notation
|
92
94
|
|
93
95
|
```erb
|
94
96
|
<%= reflex_tag "mouseenter->increment", :button, "Click me!" %>
|
95
97
|
```
|
96
98
|
|
99
|
+
### reflex_data_attributes(reflex)
|
100
|
+
|
101
|
+
This helper will give you the data attributes used in the reflex_tag above if you want to build your own elements.
|
102
|
+
|
103
|
+
Build your own tag:
|
104
|
+
|
105
|
+
```erb
|
106
|
+
<%= link_to (image_tag photo.image.url(:medium)), data: reflex_data_attributes(:increment) %>
|
107
|
+
```
|
108
|
+
|
109
|
+
Render a ViewComponent
|
110
|
+
|
111
|
+
```erb
|
112
|
+
<%= render ButtonComponent.new(data: reflex_data_attributes("mouseenter->increment")) %>
|
113
|
+
```
|
114
|
+
|
115
|
+
Make sure that you assign the reflex_data_attributes to the correct element in your component.
|
116
|
+
|
97
117
|
### collection_key
|
98
118
|
If you're rendering a component as a collection with `MyComponent.with_collection(SomeCollection)`, you must define this method to return some unique value for the component.
|
99
119
|
This is used to reconcile state in the background.
|
@@ -219,7 +239,7 @@ ViewComponentReflex uses session for its state by default. To change this, add
|
|
219
239
|
an initializer to `config/initializers/view_component_reflex.rb`.
|
220
240
|
|
221
241
|
```ruby
|
222
|
-
ViewComponentReflex.configure do |config|
|
242
|
+
ViewComponentReflex::Engine.configure do |config|
|
223
243
|
config.state_adapter = YourAdapter
|
224
244
|
end
|
225
245
|
```
|
@@ -279,9 +299,19 @@ Or install it yourself as:
|
|
279
299
|
$ gem install view_component_reflex
|
280
300
|
```
|
281
301
|
|
302
|
+
# Common problems
|
303
|
+
|
304
|
+
## Uninitialized constants \<component\>Reflex
|
305
|
+
A component needs to be wrapped in `<%= component_controller do %>` in order to properly initialize, otherwise the Reflex class won't get created.
|
306
|
+
|
282
307
|
## License
|
283
308
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
284
309
|
|
285
310
|
## Caveats
|
286
311
|
|
287
312
|
State uses session to maintain state as of right now. It also assumes your component view is written with the file extension `.html.erb`
|
313
|
+
|
314
|
+
## Support me
|
315
|
+
|
316
|
+
<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>
|
317
|
+
|
@@ -2,10 +2,10 @@ module ViewComponentReflex
|
|
2
2
|
class Component < ViewComponent::Base
|
3
3
|
class << self
|
4
4
|
def init_stimulus_reflex
|
5
|
-
if name.include? "::"
|
6
|
-
|
5
|
+
@stimulus_reflex ||= if name.include? "::"
|
6
|
+
module_parent.const_set(name.split("::").last + "Reflex", Class.new(Reflex))
|
7
7
|
else
|
8
|
-
|
8
|
+
Object.const_set(name + "Reflex", Class.new(Reflex))
|
9
9
|
end
|
10
10
|
@stimulus_reflex.component_class = self
|
11
11
|
end
|
@@ -24,11 +24,11 @@ module ViewComponentReflex
|
|
24
24
|
init_key
|
25
25
|
|
26
26
|
tag = :div
|
27
|
-
if opts_or_tag.is_a? Hash
|
28
|
-
|
27
|
+
options = if opts_or_tag.is_a? Hash
|
28
|
+
opts_or_tag
|
29
29
|
else
|
30
30
|
tag = opts_or_tag
|
31
|
-
|
31
|
+
opts
|
32
32
|
end
|
33
33
|
options[:data] = {
|
34
34
|
controller: self.class.stimulus_controller,
|
@@ -54,20 +54,25 @@ module ViewComponentReflex
|
|
54
54
|
@key = key
|
55
55
|
end
|
56
56
|
|
57
|
-
|
57
|
+
# Helper to use to create the proper reflex data attributes for an element
|
58
|
+
def reflex_data_attributes(reflex)
|
58
59
|
action, method = reflex.to_s.split("->")
|
59
60
|
if method.nil?
|
60
61
|
method = action
|
61
62
|
action = "click"
|
62
63
|
end
|
63
|
-
|
64
|
+
|
65
|
+
{
|
64
66
|
reflex: "#{action}->#{self.class.name}##{method}",
|
65
67
|
key: key
|
66
68
|
}
|
69
|
+
end
|
70
|
+
|
71
|
+
def reflex_tag(reflex, name, content_or_options_with_block = {}, options = {}, escape = true, &block)
|
67
72
|
if content_or_options_with_block.is_a?(Hash)
|
68
|
-
merge_data_attributes(content_or_options_with_block,
|
73
|
+
merge_data_attributes(content_or_options_with_block, reflex_data_attributes(reflex))
|
69
74
|
else
|
70
|
-
merge_data_attributes(options,
|
75
|
+
merge_data_attributes(options, reflex_data_attributes(reflex))
|
71
76
|
end
|
72
77
|
content_tag(name, content_or_options_with_block, options, escape, &block)
|
73
78
|
end
|
@@ -97,8 +102,8 @@ module ViewComponentReflex
|
|
97
102
|
ViewComponentReflex::Engine.state_adapter.state(request, @key).each do |k, v|
|
98
103
|
instance_value = instance_variable_get(k)
|
99
104
|
if permit_parameter?(initial_state[k], instance_value)
|
100
|
-
ViewComponentReflex::Engine.state_adapter.set_state(request, controller, "#{@key}_initial", {
|
101
|
-
ViewComponentReflex::Engine.state_adapter.set_state(request, controller, @key, {
|
105
|
+
ViewComponentReflex::Engine.state_adapter.set_state(request, controller, "#{@key}_initial", {k => instance_value})
|
106
|
+
ViewComponentReflex::Engine.state_adapter.set_state(request, controller, @key, {k => instance_value})
|
102
107
|
else
|
103
108
|
instance_variable_set(k, v)
|
104
109
|
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)
|
@@ -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.2.0
|
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-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -34,16 +34,16 @@ dependencies:
|
|
34
34
|
name: stimulus_reflex
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
|
-
- -
|
37
|
+
- - '='
|
38
38
|
- !ruby/object:Gem::Version
|
39
|
-
version:
|
39
|
+
version: 3.3.0.pre2
|
40
40
|
type: :runtime
|
41
41
|
prerelease: false
|
42
42
|
version_requirements: !ruby/object:Gem::Requirement
|
43
43
|
requirements:
|
44
|
-
- -
|
44
|
+
- - '='
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version:
|
46
|
+
version: 3.3.0.pre2
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: view_component
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
@@ -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
|