view_component_reflex 3.3.4 → 3.3.6

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.
@@ -1,40 +1,40 @@
1
- module ViewComponentReflex
2
- module Dom
3
- module Component
4
-
5
- def component_controller(opts_or_tag = :div, opts = {}, &blk)
6
- initialize_component
7
-
8
- tag = :div
9
- options = if opts_or_tag.is_a? Hash
10
- opts_or_tag
11
- else
12
- tag = opts_or_tag
13
- opts
14
- end
15
-
16
- data = {
17
- "#{key}_state" => Verifier.generate(state(key)),
18
- "#{key}_initial" => Verifier.generate(state("#{key}_initial")),
19
- }
20
-
21
- options[:data] = {
22
- controller: self.class.stimulus_controller,
23
- key: key,
24
- **(options[:data] || {})
25
- }
26
-
27
- content_tag tag, options do
28
- concat(content_tag(:span, nil, { data: data, style: "display: none;" }))
29
- concat(capture(&blk))
30
- end
31
- end
32
-
33
- def reflex_data_attributes(reflex)
34
- super(reflex).tap do |attr|
35
- attr["reflex-dataset"] = "*"
36
- end
37
- end
38
- end
39
- end
40
- end
1
+ module ViewComponentReflex
2
+ module Dom
3
+ module Component
4
+
5
+ def component_controller(opts_or_tag = :div, opts = {}, &blk)
6
+ initialize_component
7
+
8
+ tag = :div
9
+ options = if opts_or_tag.is_a? Hash
10
+ opts_or_tag
11
+ else
12
+ tag = opts_or_tag
13
+ opts
14
+ end
15
+
16
+ data = {
17
+ "#{key}_state" => Verifier.generate(state(key)),
18
+ "#{key}_initial" => Verifier.generate(state("#{key}_initial")),
19
+ }
20
+
21
+ options[:data] = {
22
+ controller: self.class.stimulus_controller,
23
+ key: key,
24
+ **(options[:data] || {})
25
+ }
26
+
27
+ content_tag tag, options do
28
+ concat(content_tag(:span, nil, { data: data, style: "display: none;" }))
29
+ concat(capture(&blk))
30
+ end
31
+ end
32
+
33
+ def reflex_data_attributes(reflex)
34
+ super(reflex).tap do |attr|
35
+ attr["reflex-dataset"] = "*"
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,36 +1,36 @@
1
- ##
2
- # State adapters assume that the data lives outside of the rendering pipeline
3
- # By moving the state to the DOM, we need to "hydrate" the state when the reflex comes in with the data
4
- # We're overriding `inject_key_into_component` for this purpose, since it's run once during a reflex,
5
- # and _just_ after the component is initialized, but before it actually does anything.
6
- # This ensures that any actions that the component is taking is going to be operating on the correct
7
- # state
8
-
9
- module ViewComponentReflex
10
- module Dom
11
- module Reflex
12
- def state
13
- if element.dataset[:"#{key}_state"]
14
- Verifier.verify(element.dataset[:"#{key}_state"])
15
- else
16
- {}
17
- end
18
- end
19
-
20
- def inject_key_into_component
21
- super
22
-
23
- state_adapter.store_state(request, key, state)
24
- state_adapter.store_state(request, "#{key}_initial", initial_state)
25
- end
26
-
27
- def initial_state
28
- if element.dataset[:"#{key}_initial"]
29
- Verifier.verify(element.dataset[:"#{key}_initial"])
30
- else
31
- {}
32
- end
33
- end
34
- end
35
- end
36
- end
1
+ ##
2
+ # State adapters assume that the data lives outside of the rendering pipeline
3
+ # By moving the state to the DOM, we need to "hydrate" the state when the reflex comes in with the data
4
+ # We're overriding `inject_key_into_component` for this purpose, since it's run once during a reflex,
5
+ # and _just_ after the component is initialized, but before it actually does anything.
6
+ # This ensures that any actions that the component is taking is going to be operating on the correct
7
+ # state
8
+
9
+ module ViewComponentReflex
10
+ module Dom
11
+ module Reflex
12
+ def state
13
+ if element.dataset[:"#{key}_state"]
14
+ Verifier.verify(element.dataset[:"#{key}_state"])
15
+ else
16
+ {}
17
+ end
18
+ end
19
+
20
+ def inject_key_into_component
21
+ super
22
+
23
+ state_adapter.store_state(request, key, state)
24
+ state_adapter.store_state(request, "#{key}_initial", initial_state)
25
+ end
26
+
27
+ def initial_state
28
+ if element.dataset[:"#{key}_initial"]
29
+ Verifier.verify(element.dataset[:"#{key}_initial"])
30
+ else
31
+ {}
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -1,15 +1,15 @@
1
- module ViewComponentReflex
2
- module Dom
3
- class Verifier
4
- class << self
5
- attr_reader :verifier
6
-
7
- delegate :generate, :verified, :verify, :valid_message?, to: :verifier
8
-
9
- def verifier
10
- Rails.application.message_verifier(:view_component_reflex)
11
- end
12
- end
13
- end
14
- end
15
- end
1
+ module ViewComponentReflex
2
+ module Dom
3
+ class Verifier
4
+ class << self
5
+ attr_reader :verifier
6
+
7
+ delegate :generate, :verified, :verify, :valid_message?, to: :verifier
8
+
9
+ def verifier
10
+ Rails.application.message_verifier(:view_component_reflex)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,40 +1,40 @@
1
- module ViewComponentReflex
2
- class Engine < ::Rails::Engine
3
-
4
- mattr_accessor :state_adapter
5
- Engine.state_adapter = StateAdapter::Session
6
-
7
- config.to_prepare do
8
- StimulusReflex::Channel.class_eval do
9
- unless instance_methods.include?(:receive_original)
10
- alias_method :receive_original, :receive
11
- def receive(data)
12
- target = data["target"].to_s
13
- reflex_name, _ = target.split("#")
14
- reflex_name = reflex_name.camelize
15
- component_name = reflex_name.end_with?("Reflex") ? reflex_name[0...-6] : reflex_name
16
- component = begin
17
- component_name.constantize
18
- rescue
19
- # Since every reflex runs through this monkey patch, we're just going to ignore the ones that aren't for components
20
- end
21
-
22
- if component
23
- if component.respond_to?(:init_stimulus_reflex)
24
- component.init_stimulus_reflex
25
- else
26
- Rails.logger.info "Tried to initialize view_component_reflex on #{component_name}, but it's not a view_component_reflex"
27
- end
28
- end
29
-
30
- receive_original(data)
31
- end
32
- end
33
- end
34
- end
35
-
36
- def self.configure
37
- yield self if block_given?
38
- end
39
- end
40
- end
1
+ module ViewComponentReflex
2
+ class Engine < ::Rails::Engine
3
+
4
+ mattr_accessor :state_adapter
5
+ Engine.state_adapter = StateAdapter::Session
6
+
7
+ config.to_prepare do
8
+ StimulusReflex::Channel.class_eval do
9
+ unless instance_methods.include?(:receive_original)
10
+ alias_method :receive_original, :receive
11
+ def receive(data)
12
+ target = data["target"].to_s
13
+ reflex_name, _ = target.split("#")
14
+ reflex_name = reflex_name.camelize
15
+ component_name = reflex_name.end_with?("Reflex") ? reflex_name[0...-6] : reflex_name
16
+ component = begin
17
+ component_name.constantize
18
+ rescue
19
+ # Since every reflex runs through this monkey patch, we're just going to ignore the ones that aren't for components
20
+ end
21
+
22
+ if component
23
+ if component.respond_to?(:init_stimulus_reflex)
24
+ component.init_stimulus_reflex
25
+ else
26
+ Rails.logger.info "Tried to initialize view_component_reflex on #{component_name}, but it's not a view_component_reflex"
27
+ end
28
+ end
29
+
30
+ receive_original(data)
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ def self.configure
37
+ yield self if block_given?
38
+ end
39
+ end
40
+ end