view_component_reflex 1.5.0 → 1.7.2

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: aed815b44b0d4708ba9bb85e9a08912f15f4d66442432df5d0933866f66e3aba
4
- data.tar.gz: 96262b95434700007b142be8411d115dc50cdd0d5b94baf5200b582317ce858b
3
+ metadata.gz: 7e5577d7e345a14f59567739802588506a300b2caa96bba74bd4f12a93752a04
4
+ data.tar.gz: be4d877d89d50555bde7f1ba929ef4fd5b1e60c72f2e7e921e33e2623574bfa7
5
5
  SHA512:
6
- metadata.gz: 8cdd11ece267f4cc206af3e12d3e62b4881fdde56afb28a23b078437448842eb54e7aa1da43a68dd4f071544bb360e95c15db2b4ea4599da9ee7e57e7887a2d8
7
- data.tar.gz: 64c8053422748319fd6460881170c3b5a9b84da0577180ebbf8a3dd3e62e5b1563c5d9e17c617de798141466f85894b42b281377aa553ff9a3840d9c522564d8
6
+ metadata.gz: 60530d55c39126bd6be3d5be688f08cbe4e84e0b16e444334e438d99ec1a6b05a1f08c102407bea04b9c73ca5ace4bbb4ec013529810bc5363c6bd09e4224d70
7
+ data.tar.gz: ceebff13ff2407be15ad1d3e851f78130a7bec2d00cdd260bd3ed78d32ebfce596f1763d6608b822d628f0ce91b164a53c47ee2296755733ddac01888ae1bf8c
data/README.md CHANGED
@@ -30,7 +30,7 @@ end
30
30
  # counter_component.html.erb
31
31
  <%= component_controller do %>
32
32
  <p><%= @count %></p>
33
- <button type="button" data-reflex="click->CounterComponentReflex#increment" data-key="<%= key %>">Click</button>
33
+ <%= reflex_tag :increment, :button, "Click" %>
34
34
  <% end %>
35
35
  ```
36
36
 
@@ -70,8 +70,8 @@ end
70
70
  ```
71
71
 
72
72
  ### omitted_from_state
73
- Return an array of instance variables you want to omit from state. Useful if you have an object
74
- that isn't serializable as an instance variable, like a form.
73
+ Return an array of instance variables you want to omit from state. Only really useful if you're using the session state
74
+ adapter, and you have an instance variable that can't be serialized.
75
75
 
76
76
  ```ruby
77
77
  def omitted_from_state
@@ -181,6 +181,11 @@ end
181
181
  <% end
182
182
  ```
183
183
 
184
+ ## State
185
+
186
+ By default, view_component_reflex stores component state in memory. You can optionally set the state adapter
187
+ to use the session by changing `config.state_adapter` to `ViewComponentReflex::StateAdapter::Session`
188
+
184
189
  ## Custom State Adapters
185
190
 
186
191
  ViewComponentReflex uses session for its state by default. To change this, add
@@ -85,7 +85,7 @@ module ViewComponentReflex
85
85
 
86
86
  def save_state
87
87
  new_state = {}
88
- component.instance_variables.each do |k|
88
+ component.safe_instance_variables.each do |k|
89
89
  new_state[k] = component.instance_variable_get(k)
90
90
  end
91
91
  set_state(new_state)
@@ -102,15 +102,23 @@ module ViewComponentReflex
102
102
  helpers.controller.instance_variable_get(:@stimulus_reflex)
103
103
  end
104
104
 
105
- def component_controller(opts = {}, &blk)
105
+ def component_controller(opts_or_tag = :div, opts = {}, &blk)
106
106
  self.class.init_stimulus_reflex
107
107
  init_key
108
- opts[:data] = {
108
+
109
+ tag = :div
110
+ if opts_or_tag.is_a? Hash
111
+ options = opts_or_tag
112
+ else
113
+ tag = opts_or_tag
114
+ options = opts
115
+ end
116
+ options[:data] = {
109
117
  controller: self.class.stimulus_controller,
110
118
  key: key,
111
- **(opts[:data] || {})
119
+ **(options[:data] || {})
112
120
  }
113
- content_tag :div, capture(&blk), opts
121
+ content_tag tag, capture(&blk), options
114
122
  end
115
123
 
116
124
  # key is required if you're using state
@@ -125,8 +133,8 @@ module ViewComponentReflex
125
133
  @key = key
126
134
  end
127
135
 
128
- def reflex_tag(reflex, name, content_or_options_with_block = nil, options = nil, escape = true, &block)
129
- action, method = reflex.split("->")
136
+ def reflex_tag(reflex, name, content_or_options_with_block = {}, options = {}, escape = true, &block)
137
+ action, method = reflex.to_s.split("->")
130
138
  if method.nil?
131
139
  method = action
132
140
  action = "click"
@@ -157,18 +165,10 @@ module ViewComponentReflex
157
165
 
158
166
  def key
159
167
  # initialize session state
160
- if !stimulus_reflex? || session[@key].nil?
161
- new_state = {}
162
-
163
- # this will almost certainly break
164
- blacklist = [
165
- :@view_context, :@lookup_context, :@view_renderer, :@view_flow,
166
- :@virtual_path, :@variant, :@current_template, :@output_buffer, :@key,
167
- :@helpers, :@controller, :@request, :@content
168
- ]
169
- instance_variables.reject { |k| blacklist.include?(k) }.each do |k|
170
- new_state[k] = instance_variable_get(k) unless omitted_from_state.include?(k)
171
- end
168
+ if !stimulus_reflex? || ViewComponentReflex::Engine.state_adapter.state(request, @key).empty?
169
+
170
+ new_state = create_safe_state
171
+
172
172
  ViewComponentReflex::Engine.state_adapter.store_state(request, @key, new_state)
173
173
  ViewComponentReflex::Engine.state_adapter.store_state(request, "#{@key}_initial", new_state)
174
174
  else
@@ -182,8 +182,30 @@ module ViewComponentReflex
182
182
  @key
183
183
  end
184
184
 
185
+ def safe_instance_variables
186
+ instance_variables - unsafe_instance_variables
187
+ end
188
+
185
189
  private
186
190
 
191
+ def unsafe_instance_variables
192
+ [
193
+ :@view_context, :@lookup_context, :@view_renderer, :@view_flow,
194
+ :@virtual_path, :@variant, :@current_template, :@output_buffer, :@key,
195
+ :@helpers, :@controller, :@request, :@content, :@tag_builder
196
+ ]
197
+ end
198
+
199
+ def create_safe_state
200
+ new_state = {}
201
+
202
+ # this will almost certainly break
203
+ safe_instance_variables.each do |k|
204
+ new_state[k] = instance_variable_get(k) unless omitted_from_state.include?(k)
205
+ end
206
+ new_state
207
+ end
208
+
187
209
  def merge_data_attributes(options, attributes)
188
210
  data = options[:data]
189
211
  if data.nil?
@@ -1,7 +1,8 @@
1
- require "view_component_reflex/state_adapter/session"
2
- require "view_component_reflex/engine"
3
- require 'stimulus_reflex'
4
-
5
- module ViewComponentReflex
6
- # Your code goes here...
7
- end
1
+ require "view_component_reflex/state_adapter/session"
2
+ require "view_component_reflex/state_adapter/memory"
3
+ require "view_component_reflex/engine"
4
+ require 'stimulus_reflex'
5
+
6
+ module ViewComponentReflex
7
+ # Your code goes here...
8
+ end
@@ -1,13 +1,13 @@
1
- module ViewComponentReflex
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
- end
13
- end
1
+ module ViewComponentReflex
2
+ class Engine < ::Rails::Engine
3
+ class << self
4
+ mattr_accessor :state_adapter
5
+
6
+ self.state_adapter = StateAdapter::Memory
7
+ end
8
+
9
+ def self.configure
10
+ yield self if block_given?
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,25 @@
1
+ VIEW_COMPONENT_REFLEX_MEMORY_STATE = {}
2
+ module ViewComponentReflex
3
+ module StateAdapter
4
+ class Memory
5
+ def self.state(request, key)
6
+ VIEW_COMPONENT_REFLEX_MEMORY_STATE[request.session.id.to_s] ||= {}
7
+ VIEW_COMPONENT_REFLEX_MEMORY_STATE[request.session.id.to_s][key] ||= {}
8
+ end
9
+
10
+ def self.set_state(request, _, key, new_state)
11
+ new_state.each do |k, v|
12
+ state(request, key)[k] = v
13
+ end
14
+ end
15
+
16
+ def self.store_state(request, key, new_state = {})
17
+ VIEW_COMPONENT_REFLEX_MEMORY_STATE[request.session.id.to_s] ||= {}
18
+ VIEW_COMPONENT_REFLEX_MEMORY_STATE[request.session.id.to_s][key] ||= {}
19
+ new_state.each do |k, v|
20
+ VIEW_COMPONENT_REFLEX_MEMORY_STATE[request.session.id.to_s][key][k] = v
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,3 +1,3 @@
1
- module ViewComponentReflex
2
- VERSION = '1.5.0'
3
- end
1
+ module ViewComponentReflex
2
+ VERSION = '1.7.2'
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: 1.5.0
4
+ version: 1.7.2
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-22 00:00:00.000000000 Z
11
+ date: 2020-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -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/memory.rb
74
75
  - lib/view_component_reflex/state_adapter/session.rb
75
76
  - lib/view_component_reflex/version.rb
76
77
  homepage: https://github.com/joshleblanc/view_component_reflex