view_component_reflex 0.5.0 → 0.6.3
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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 25f3eb9ce0b0a130b3448c9aa350636e86f4497c9274a1a4ba7edfb564d11a16
|
4
|
+
data.tar.gz: 809b06a816a92e8ba1e3ce0198c55c6f3d812a0ad3c4b447300c56e4733102b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e3c0cf57dda891149ccd671d02fbc967aaa83277f789a1cd0c7c75192fa4a83e3e593177ef49f904cbc9cc8bb241b9506527bfafee0b90b049252b1822a4cb68
|
7
|
+
data.tar.gz: 94c5b424ae03b1f35f797ddebf0d6f24bf78b05960140c415cce86d4c2bf645ce4eef7445d7049ef57ab0d2f26925bac251b0a202199840f23f4f0adee22fc17
|
data/README.md
CHANGED
@@ -89,23 +89,6 @@ class YourAdapter
|
|
89
89
|
# new_state - a hash containing the component state
|
90
90
|
def self.store_state(request, key, new_state = {})
|
91
91
|
# store the state
|
92
|
-
# this will be called twice, once with key, once with key_initial
|
93
|
-
# key_initial contains the initial, unmodified state.
|
94
|
-
# it should be used in reconcile_state to decide whether or not
|
95
|
-
# to re-initialize the state
|
96
|
-
end
|
97
|
-
|
98
|
-
##
|
99
|
-
# request - a rails request object
|
100
|
-
# key - a unique string that identifies the component instance
|
101
|
-
# new_state - a hash containing the component state
|
102
|
-
def self.reconcile_state(request, key, new_state)
|
103
|
-
# The passed state should always match the initial state of the component
|
104
|
-
# if it doesn't, we need to reset the state to the passed value.
|
105
|
-
#
|
106
|
-
# This handles cases where your initialize_state param computes some value that changes
|
107
|
-
# initialize_state({ transaction: @customer.transactions.first })
|
108
|
-
# if you delete the first transaction, that ^ is no longer valid. We need to update the state.
|
109
92
|
end
|
110
93
|
end
|
111
94
|
```
|
@@ -12,8 +12,8 @@ module ViewComponentReflex
|
|
12
12
|
ViewComponentReflex::Engine.state_adapter.state(request, element.dataset[:key])
|
13
13
|
end
|
14
14
|
|
15
|
-
def refresh!
|
16
|
-
@channel.render_page_and_broadcast_morph
|
15
|
+
def refresh!(primary_selector = "[data-controller=\"#{stimulus_controller}\"]", *selectors)
|
16
|
+
@channel.send :render_page_and_broadcast_morph, self, [primary_selector, *selectors], {
|
17
17
|
dataset: element.dataset.to_h,
|
18
18
|
args: [],
|
19
19
|
attrs: element.attributes.to_h,
|
@@ -21,12 +21,16 @@ module ViewComponentReflex
|
|
21
21
|
target: "#{self.class.name}##{method_name}",
|
22
22
|
url: request.url,
|
23
23
|
permanentAttributeName: "data-reflex-permanent"
|
24
|
-
}
|
24
|
+
}
|
25
25
|
end
|
26
26
|
|
27
|
-
def
|
27
|
+
def refresh_all!
|
28
|
+
refresh!('body')
|
29
|
+
end
|
30
|
+
|
31
|
+
def set_state(new_state = {}, primary_selector = nil, *selectors)
|
28
32
|
ViewComponentReflex::Engine.state_adapter.set_state(self, element.dataset[:key], new_state)
|
29
|
-
refresh!
|
33
|
+
refresh!(primary_selector, *selectors)
|
30
34
|
end
|
31
35
|
|
32
36
|
before_reflex do |reflex, *args|
|
@@ -55,6 +59,10 @@ module ViewComponentReflex
|
|
55
59
|
@state = obj
|
56
60
|
end
|
57
61
|
|
62
|
+
def stimulus_reflex?
|
63
|
+
helpers.controller.instance_variable_get(:@stimulus_reflex)
|
64
|
+
end
|
65
|
+
|
58
66
|
# key is required if you're using state
|
59
67
|
# We can't initialize the session state in the initial method
|
60
68
|
# because it doesn't have a view_context yet
|
@@ -63,11 +71,8 @@ module ViewComponentReflex
|
|
63
71
|
@key ||= caller.find { |p| p.include? ".html.erb" }&.hash.to_s
|
64
72
|
|
65
73
|
# initialize session state
|
66
|
-
if session[@key].nil?
|
74
|
+
if !stimulus_reflex? || session[@key].nil?
|
67
75
|
ViewComponentReflex::Engine.state_adapter.store_state(request, @key, @state)
|
68
|
-
ViewComponentReflex::Engine.state_adapter.store_state(request, "#{@key}_initial", @state)
|
69
|
-
else
|
70
|
-
ViewComponentReflex::Engine.state_adapter.reconcile_state(request, @key, @state)
|
71
76
|
end
|
72
77
|
@key
|
73
78
|
end
|
@@ -9,18 +9,5 @@ module ViewComponentReflex
|
|
9
9
|
def self.configure
|
10
10
|
yield self if block_given?
|
11
11
|
end
|
12
|
-
|
13
|
-
config.to_prepare do
|
14
|
-
class StimulusReflex::Channel < ActionCable::Channel::Base
|
15
|
-
def render_page_and_broadcast_morph(reflex, selectors, data = {})
|
16
|
-
html = render_page(reflex)
|
17
|
-
if reflex.respond_to? :stimulus_controller
|
18
|
-
selectors = ["[data-controller=\"#{reflex.stimulus_controller}\"]"]
|
19
|
-
end
|
20
|
-
broadcast_morphs selectors, data, html if html.present?
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
12
|
end
|
26
13
|
end
|
@@ -19,20 +19,6 @@ module ViewComponentReflex
|
|
19
19
|
request.session[key][k] = v
|
20
20
|
end
|
21
21
|
end
|
22
|
-
|
23
|
-
# The passed state should always match the initial state of the component
|
24
|
-
# if it doesn't, we need to reset the state to the passed value.
|
25
|
-
#
|
26
|
-
# This handles cases where your initialize_state param computes some value that changes
|
27
|
-
# initialize_state({ transaction: @customer.transactions.first })
|
28
|
-
# if you delete the first transaction, that ^ is no longer valid. We need to update the state.
|
29
|
-
def self.reconcile_state(request, key, new_state)
|
30
|
-
request.session["#{key}_initial"].each do |k, v|
|
31
|
-
if new_state[k] != v
|
32
|
-
request.session[key][k] = new_state[k]
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
22
|
end
|
37
23
|
end
|
38
24
|
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: 0.
|
4
|
+
version: 0.6.3
|
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-06-
|
11
|
+
date: 2020-06-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|