view_component_reflex 2.0.0 → 2.1.2

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: 2a3e7ed47e432ef756d8359ace66447aedd995d240eb423007ec868c9dfa0402
4
- data.tar.gz: bb836fc180d285454b516ced91b92cc309320d9257c5da71a17c2789631fcd0f
3
+ metadata.gz: 0ab31dd62e6ea3047e6092dc2fed4fb4ec56c0c6b348217109a91a63a7f5f741
4
+ data.tar.gz: 51d03bf7e07b25c12fd5f2babd8c0346815c4e9cd7d4e0bfff6ff7df9d0acfcd
5
5
  SHA512:
6
- metadata.gz: 594bf37cfa1ef0798df664220190d694d3c164ee25516b0220ba6f884b4bf3667a52cfb0668d7d0ae2d684d17c18296862f5ae2473ace3b23ac8496f29825ba6
7
- data.tar.gz: d82970ac5abd4aea2ffb675f261ba7c6b840e1f866f9b93b51c368cead4fff06d9ac1e7c8211f679f0f58a4d6b78557fe2c553b66bec982ddaf7d1cf0c5e8433
6
+ metadata.gz: 305353ea23702dec039ddfb05304adbc894f40355649ab137ddce4a03e6fab42f2fc6f5f775236e29c12e2a2b24e0e28d2d6e2904b2135435e51b3aa2c7c3fbb
7
+ data.tar.gz: 8e1dc76c9d0efe24a7609ce25ab307daef5d861495c23be8a66d5bb6f96c88a1bba3b8c28735d4e55595b5b4e572db89ab65a6bae50c2430edb497387eff7f6d
data/README.md CHANGED
@@ -108,6 +108,20 @@ def collection_key
108
108
  end
109
109
  ```
110
110
 
111
+ ### stimulate(target, data)
112
+ Stimulate another reflex from within your component. This typically requires the key of the component you're stimulating
113
+ which can be passed in via parameters.
114
+
115
+ ```ruby
116
+ def initialize(parent_key)
117
+ @parent_key = parent_key
118
+ end
119
+
120
+ def stimulate_other
121
+ stimulate("OtherComponent#method", { key: @parent_key })
122
+ end
123
+ ```
124
+
111
125
  ### refresh!(selectors)
112
126
  Refresh a specific element on the page. Using this will implicitly run `prevent_render!`.
113
127
  If you want to render a specific element, as well as the component, a common pattern would be to pass `selector` as one of the parameters
@@ -265,9 +279,19 @@ Or install it yourself as:
265
279
  $ gem install view_component_reflex
266
280
  ```
267
281
 
282
+ ## Common problems
283
+
284
+ # Uninitialized constants \<component\>Reflex
285
+ A component needs to be wrapped in `<%= component_controller do %>` in order to properly initialize, otherwise the Reflex class won't get created.
286
+
268
287
  ## License
269
288
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
270
289
 
271
290
  ## Caveats
272
291
 
273
292
  State uses session to maintain state as of right now. It also assumes your component view is written with the file extension `.html.erb`
293
+
294
+ ## Support me
295
+
296
+ <a href="https://www.buymeacoffee.com/jleblanc" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="40" ></a>
297
+
@@ -2,7 +2,11 @@ module ViewComponentReflex
2
2
  class Component < ViewComponent::Base
3
3
  class << self
4
4
  def init_stimulus_reflex
5
- @stimulus_reflex ||= Object.const_set(name + "Reflex", Class.new(Reflex))
5
+ if name.include? "::"
6
+ @stimulus_reflex ||= module_parent.const_set(name.split("::").last + "Reflex", Class.new(Reflex))
7
+ else
8
+ @stimulus_reflex ||= Object.const_set(name + "Reflex", Class.new(Reflex))
9
+ end
6
10
  @stimulus_reflex.component_class = self
7
11
  end
8
12
  end
@@ -91,7 +95,11 @@ module ViewComponentReflex
91
95
  else
92
96
  initial_state = ViewComponentReflex::Engine.state_adapter.state(request, "#{@key}_initial")
93
97
  ViewComponentReflex::Engine.state_adapter.state(request, @key).each do |k, v|
94
- unless permit_parameter?(initial_state[k], instance_variable_get(k))
98
+ instance_value = instance_variable_get(k)
99
+ if permit_parameter?(initial_state[k], instance_value)
100
+ ViewComponentReflex::Engine.state_adapter.set_state(request, controller, "#{@key}_initial", { k => instance_value })
101
+ ViewComponentReflex::Engine.state_adapter.set_state(request, controller, @key, { k => instance_value })
102
+ else
95
103
  instance_variable_set(k, v)
96
104
  end
97
105
  end
@@ -109,7 +117,7 @@ module ViewComponentReflex
109
117
  [
110
118
  :@view_context, :@lookup_context, :@view_renderer, :@view_flow,
111
119
  :@virtual_path, :@variant, :@current_template, :@output_buffer, :@key,
112
- :@helpers, :@controller, :@request, :@content, :@tag_builder
120
+ :@helpers, :@controller, :@request, :@tag_builder
113
121
  ]
114
122
  end
115
123
 
@@ -2,6 +2,7 @@ require "view_component_reflex/state_adapter/session"
2
2
  require "view_component_reflex/state_adapter/memory"
3
3
  require "view_component_reflex/engine"
4
4
  require "stimulus_reflex"
5
+ require "view_component_reflex/reflex"
5
6
 
6
7
  module ViewComponentReflex
7
8
  # Your code goes here...
@@ -20,16 +20,18 @@ module ViewComponentReflex
20
20
  [primary_selector, *rest].each do |s|
21
21
  html = document.css(s)
22
22
  if html.present?
23
- cable_ready[channel.stream_name].inner_html(
23
+ cable_ready[channel.stream_name].morph(
24
24
  selector: s,
25
25
  html: html.inner_html,
26
- children_only: true
26
+ children_only: true,
27
+ permanent_attribute_name: "data-reflex-permanent"
27
28
  )
28
29
  end
29
30
  end
30
31
  else
31
32
  refresh_component!
32
33
  end
34
+ cable_ready.broadcast
33
35
  end
34
36
 
35
37
  def refresh_component!
@@ -38,9 +40,12 @@ module ViewComponentReflex
38
40
  element.dataset[:key]
39
41
  end
40
42
  end
41
- cable_ready[channel.stream_name].outer_html(
43
+ document = Nokogiri::HTML(controller.render_component_to_string(component))
44
+ cable_ready[channel.stream_name].morph(
42
45
  selector: selector,
43
- html: controller.render_component_to_string(component)
46
+ children_only: true,
47
+ html: document.css(selector).inner_html,
48
+ permanent_attribute_name: "data-reflex-permanent"
44
49
  )
45
50
  end
46
51
 
@@ -91,11 +96,23 @@ module ViewComponentReflex
91
96
  component_class.stimulus_controller
92
97
  end
93
98
 
99
+ def stimulate(target, data)
100
+ dataset = {}
101
+ data.each do |k, v|
102
+ dataset["data-#{k}"] = v.to_s
103
+ end
104
+ channel.receive({
105
+ "target" => target,
106
+ "attrs" => element.attributes.to_h.symbolize_keys,
107
+ "dataset" => dataset
108
+ })
109
+ end
110
+
94
111
  def component
95
112
  return @component if @component
96
113
  @component = component_class.allocate
97
114
  reflex = self
98
- exposed_methods = [:params, :request, :element, :refresh!, :refresh_all!, :stimulus_controller, :session, :prevent_refresh!, :selector]
115
+ exposed_methods = [:params, :request, :element, :refresh!, :refresh_all!, :stimulus_controller, :session, :prevent_refresh!, :selector, :stimulate]
99
116
  exposed_methods.each do |meth|
100
117
  @component.define_singleton_method(meth) do |*a|
101
118
  reflex.send(meth, *a)
@@ -1,3 +1,3 @@
1
1
  module ViewComponentReflex
2
- VERSION = '2.0.0'
2
+ VERSION = '2.1.2'
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: 2.0.0
4
+ version: 2.1.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-07-07 00:00:00.000000000 Z
11
+ date: 2020-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -69,9 +69,9 @@ files:
69
69
  - README.md
70
70
  - Rakefile
71
71
  - app/components/view_component_reflex/component.rb
72
- - app/reflexes/view_component_reflex/reflex.rb
73
72
  - lib/view_component_reflex.rb
74
73
  - lib/view_component_reflex/engine.rb
74
+ - lib/view_component_reflex/reflex.rb
75
75
  - lib/view_component_reflex/state_adapter/memory.rb
76
76
  - lib/view_component_reflex/state_adapter/session.rb
77
77
  - lib/view_component_reflex/version.rb