view_component_reflex 0.2.5 → 0.3.0

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: fbeda9f2f7f4f327afd6da54bcd82ac6871eab33efa07ae1901ccd8d002801d2
4
- data.tar.gz: bb5afb39f319dadfcf68272cbee6762286d475c1ce93382e611f722dd6d46baa
3
+ metadata.gz: e02789888b71896b7005d2e854834926ebc5cb4839c4d3d8eb57a2a7ca9f1c16
4
+ data.tar.gz: e51ad3e1aabaa02a81751533688bbc29cc3a227aaf26ed77f6ee61465ff2ff5b
5
5
  SHA512:
6
- metadata.gz: 27d42f4f6a349223d3a0d354b31a0d37dacd51fef18450c254c8353cadb8018bc62dbccefcb99e2a5cff3bf6d61240b6836eebfb8af0974448baf57f2c4a9bc3
7
- data.tar.gz: fac906b5ad974f51b0023f04a58ba9164d38348b181a6f22e6193664d77c93ef5dd53da31abe4a94cf9f39d6ecffdd5fbf2654831f07de810c712751237c3204
6
+ metadata.gz: ccd9589f0c94f331d0e361c255803cf4d37ad2372e77e7752db1ba509ee528d87c08a1e2a43c45e80212674857e7176a0a7bde103bce1b0c5ce9c5cbba4c3ff0
7
+ data.tar.gz: 5407c04db562748c569aac3ab7901a978e2fbfff1b7beab69c5cf6eb9b4baa7927092c3d6fddbced17b25b32263effd5bdd1b4ce6fdf3389f44c275a3081b3a2
data/README.md CHANGED
@@ -49,6 +49,56 @@ lets ViewComponentReflex keep track of which state belongs to which component.
49
49
  </div>
50
50
  ```
51
51
 
52
+ ## Custom State Adapters
53
+
54
+ ViewComponentReflex uses session for its state by default. To change this, add
55
+ an initializer to `config/initializers/view_component_reflex.rb`.
56
+
57
+ ```ruby
58
+ ViewComponentReflex.configure do |config|
59
+ config.state_adapter = YourAdapter
60
+ end
61
+ ```
62
+
63
+ `YourAdapter` should implement
64
+
65
+ ```ruby
66
+ class YourAdapter
67
+ ##
68
+ # request - a rails request object
69
+ # key - a unique string that identifies the component instance
70
+ def self.state(request, key)
71
+ # Return state for a given key
72
+ end
73
+
74
+ ##
75
+ # request - a rails request object
76
+ # key - a unique string that identifies the component instance
77
+ # new_state - a hash containing the component state
78
+ def self.store_state(request, key, new_state = {})
79
+ # store the state
80
+ # this will be called twice, once with key, once with key_initial
81
+ # key_initial contains the initial, unmodified state.
82
+ # it should be used in reconcile_state to decide whether or not
83
+ # to re-initialize the state
84
+ end
85
+
86
+ ##
87
+ # request - a rails request object
88
+ # key - a unique string that identifies the component instance
89
+ # new_state - a hash containing the component state
90
+ def self.reconcile_state(request, key, new_state)
91
+ # The passed state should always match the initial state of the component
92
+ # if it doesn't, we need to reset the state to the passed value.
93
+ #
94
+ # This handles cases where your initialize_state param computes some value that changes
95
+ # initialize_state({ transaction: @customer.transactions.first })
96
+ # if you delete the first transaction, that ^ is no longer valid. We need to update the state.
97
+ end
98
+ end
99
+ ```
100
+
101
+
52
102
  ## Installation
53
103
  Add this line to your application's Gemfile:
54
104
 
@@ -9,7 +9,7 @@ module ViewComponentReflex
9
9
  klass = self
10
10
  @stimulus_reflex ||= Object.const_set(name + "Reflex", Class.new(StimulusReflex::Reflex) {
11
11
  def state
12
- session[element.dataset[:key]] ||= {}
12
+ ViewComponentReflex::Engine.state_adapter.state(request, element.dataset[:key])
13
13
  end
14
14
 
15
15
  define_method :stimulus_controller do
@@ -38,39 +38,16 @@ module ViewComponentReflex
38
38
 
39
39
  # initialize session state
40
40
  if session[@key].nil?
41
- store_state(@key)
42
- store_state("#{@key}_initial")
41
+ ViewComponentReflex::Engine.state_adapter.store_state(request, @key, @state)
42
+ ViewComponentReflex::Engine.state_adapter.store_state(request, "#{@key}_initial", @state)
43
43
  else
44
- reconcile_state
44
+ ViewComponentReflex::Engine.state_adapter.reconcile_state(request, @key, @state)
45
45
  end
46
46
  @key
47
47
  end
48
48
 
49
49
  def state
50
- session[key]
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
50
+ ViewComponentReflex::Engine.state_adapter.state(request, key)
74
51
  end
75
52
  end
76
53
  end
@@ -1,3 +1,4 @@
1
+ require "view_component_reflex/state_adapter/session"
1
2
  require "view_component_reflex/engine"
2
3
  require 'stimulus_reflex'
3
4
 
@@ -1,5 +1,15 @@
1
1
  module ViewComponentReflex
2
2
  class Engine < ::Rails::Engine
3
+ class << self
4
+ mattr_accessor :state_adapter
5
+
6
+ self.state_adapter = StateAdapter::Session
7
+ end
8
+
9
+ def self.configure
10
+ yield self if block_given?
11
+ end
12
+
3
13
  config.to_prepare do
4
14
  class StimulusReflex::Channel < ActionCable::Channel::Base
5
15
  def render_page_and_broadcast_morph(reflex, selectors, data = {})
@@ -0,0 +1,30 @@
1
+ module ViewComponentReflex
2
+ module StateAdapter
3
+ class Session
4
+ def self.state(request, key)
5
+ request.session[key] ||= {}
6
+ end
7
+
8
+ def self.store_state(request, key, new_state = {})
9
+ request.session[key] = {}
10
+ new_state.each do |k, v|
11
+ request.session[key][k] = v
12
+ end
13
+ end
14
+
15
+ # The passed state should always match the initial state of the component
16
+ # if it doesn't, we need to reset the state to the passed value.
17
+ #
18
+ # This handles cases where your initialize_state param computes some value that changes
19
+ # initialize_state({ transaction: @customer.transactions.first })
20
+ # if you delete the first transaction, that ^ is no longer valid. We need to update the state.
21
+ def self.reconcile_state(request, key, new_state)
22
+ request.session["#{key}_initial"].each do |k, v|
23
+ if new_state[k] != v
24
+ request.session[key][k] = new_state[k]
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,3 +1,3 @@
1
1
  module ViewComponentReflex
2
- VERSION = '0.2.5'
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: view_component_reflex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua LeBlanc
@@ -71,6 +71,7 @@ files:
71
71
  - app/components/view_component_reflex/component.rb
72
72
  - lib/view_component_reflex.rb
73
73
  - lib/view_component_reflex/engine.rb
74
+ - lib/view_component_reflex/state_adapter/session.rb
74
75
  - lib/view_component_reflex/version.rb
75
76
  homepage: https://github.com/joshleblanc/view_component_reflex
76
77
  licenses: