view_component_reflex 0.2.0 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9c0c42d6658ac5c2c6af6bc78a4de1c04cfaaa38bdf4a206106975cbf7c5c049
4
- data.tar.gz: 5bc792233916b8d16dd364eadb02eb9183ede8da619ed67908b47b410a9c80d5
3
+ metadata.gz: fbeda9f2f7f4f327afd6da54bcd82ac6871eab33efa07ae1901ccd8d002801d2
4
+ data.tar.gz: bb5afb39f319dadfcf68272cbee6762286d475c1ce93382e611f722dd6d46baa
5
5
  SHA512:
6
- metadata.gz: 2747491eede7da7ccfab22ec260426755a44ee46a28218da5297ef3b1108cf6fe8c9cb38559f03cc20b68e6f0c00e79d5480fddd5a6595f1e0c979e2f2865e4a
7
- data.tar.gz: c979f87cc33842ced72066d2dc262e876ff77cd5b3764052c0e388d5139a060c7b22fdcc9963fef5f8a3a0f2e82c8a6b35ede0feaaca4bdc70f32ffe59a1abee
6
+ metadata.gz: 27d42f4f6a349223d3a0d354b31a0d37dacd51fef18450c254c8353cadb8018bc62dbccefcb99e2a5cff3bf6d61240b6836eebfb8af0974448baf57f2c4a9bc3
7
+ data.tar.gz: fac906b5ad974f51b0023f04a58ba9164d38348b181a6f22e6193664d77c93ef5dd53da31abe4a94cf9f39d6ecffdd5fbf2654831f07de810c712751237c3204
data/README.md CHANGED
@@ -21,7 +21,7 @@ In addition to calling reflexes, there is a rudimentary state system. You can in
21
21
 
22
22
  You can access state with the `state` helper. See the code below for an example.
23
23
 
24
- If you're using state, include `super()` at the beginning of your initialize method, and add `data-key="<%= key %>"` to any html element using a reflex. This
24
+ If you're using state add `data-key="<%= key %>"` to any html element using a reflex. This
25
25
  lets ViewComponentReflex keep track of which state belongs to which component.
26
26
 
27
27
 
@@ -30,7 +30,6 @@ lets ViewComponentReflex keep track of which state belongs to which component.
30
30
  class CounterComponent < ViewComponentReflex::Component
31
31
 
32
32
  def initialize
33
- super()
34
33
  initialize_state({
35
34
  count: 0
36
35
  })
@@ -30,7 +30,7 @@ module ViewComponentReflex
30
30
  end
31
31
 
32
32
  # key is required if you're using state
33
- # We can't initialize the session state in the initiale method
33
+ # We can't initialize the session state in the initial method
34
34
  # because it doesn't have a view_context yet
35
35
  # This is the next best place to do it
36
36
  def key
@@ -38,10 +38,10 @@ module ViewComponentReflex
38
38
 
39
39
  # initialize session state
40
40
  if session[@key].nil?
41
- session[@key] = {}
42
- (@state ||= {}).each do |key, v|
43
- session[@key][key] = v
44
- end
41
+ store_state(@key)
42
+ store_state("#{@key}_initial")
43
+ else
44
+ reconcile_state
45
45
  end
46
46
  @key
47
47
  end
@@ -49,5 +49,28 @@ module ViewComponentReflex
49
49
  def state
50
50
  session[key]
51
51
  end
52
+
53
+ private
54
+
55
+ # The passed state should always match the initial state of the component
56
+ # if it doesn't, we need to reset the state to the passed value.
57
+ #
58
+ # This handles cases where your initialize_state param computes some value that changes
59
+ # initialize_state({ transaction: @customer.transactions.first })
60
+ # if you delete the first transaction, that ^ is no longer valid. We need to update the state.
61
+ def reconcile_state
62
+ session["#{@key}_initial"].each do |k, v|
63
+ if @state[k] != v
64
+ session[@key][k] = @state[k]
65
+ end
66
+ end
67
+ end
68
+
69
+ def store_state(a_key)
70
+ session[a_key] = {}
71
+ (@state ||= {}).each do |key, v|
72
+ session[a_key][key] = v
73
+ end
74
+ end
52
75
  end
53
76
  end
@@ -1,6 +1,5 @@
1
1
  require "view_component_reflex/engine"
2
2
  require 'stimulus_reflex'
3
- require_relative 'stimulus_reflex/channel'
4
3
 
5
4
  module ViewComponentReflex
6
5
  # Your code goes here...
@@ -1,5 +1,16 @@
1
1
  module ViewComponentReflex
2
2
  class Engine < ::Rails::Engine
3
- isolate_namespace ViewComponentReflex
3
+ config.to_prepare do
4
+ class StimulusReflex::Channel < ActionCable::Channel::Base
5
+ def render_page_and_broadcast_morph(reflex, selectors, data = {})
6
+ html = render_page(reflex)
7
+ if reflex.respond_to? :stimulus_controller
8
+ selectors = ["[data-controller=\"#{reflex.stimulus_controller}\"]"]
9
+ end
10
+ broadcast_morphs selectors, data, html if html.present?
11
+ end
12
+ end
13
+
14
+ end
4
15
  end
5
16
  end
@@ -1,3 +1,3 @@
1
1
  module ViewComponentReflex
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.5'
3
3
  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.2.0
4
+ version: 0.2.5
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-04 00:00:00.000000000 Z
11
+ date: 2020-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -69,7 +69,6 @@ files:
69
69
  - README.md
70
70
  - Rakefile
71
71
  - app/components/view_component_reflex/component.rb
72
- - lib/stimulus_reflex/channel.rb
73
72
  - lib/view_component_reflex.rb
74
73
  - lib/view_component_reflex/engine.rb
75
74
  - lib/view_component_reflex/version.rb
@@ -1,9 +0,0 @@
1
- class StimulusReflex::Channel < ActionCable::Channel::Base
2
- def render_page_and_broadcast_morph(reflex, selectors, data = {})
3
- html = render_page(reflex)
4
- if reflex.respond_to? :stimulus_controller
5
- selectors = ["[data-controller=\"#{reflex.stimulus_controller}\"]"]
6
- end
7
- broadcast_morphs selectors, data, html if html.present?
8
- end
9
- end