view_component_reflex 2.0.1 → 2.1.3

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: 01c4ace6204896ecf089f5a494cad173b1bf73c1f5818bd5745dbaf976c3adec
4
- data.tar.gz: 24d655b966c025128802e4f2223cf7e324f4d15ecfc7c3554bd6cdeb10224aad
3
+ metadata.gz: f5ffbf1eef5f6b3a5d7d65f4d541dffb0d396125b23c4db8f768928a2e5d7477
4
+ data.tar.gz: 67e7902d699879181cf2bfba948a93967eda19d355b85e785b61f41a3f89e0dc
5
5
  SHA512:
6
- metadata.gz: 47efa7af3e0dbb78e86e5dd513e79dc3722279b69204c5a4c7385da717e6207b20587dc3e680659f48d56e19afa0e01800614a2c3a6f5f5672b426d40019d374
7
- data.tar.gz: c7e0a43ac12fa4bf42615c9469b108945b59a2d9f8b8c0890f772c8b2ca193cf5551c771f064df8ac7f200a80293795dd35476929f6543f21b7f2a98e52dd127
6
+ metadata.gz: 6912fa047d572bf7d666e5cb1f151164103709d93b473ab1a40642d2828b64807928fb064621ce1804a6144899483a98b5440f1d615eb7bf451e924d8e8f21ed
7
+ data.tar.gz: 5f4c565c378d6caad49fd2f18064dc474535fbff28f64c8480354398cb23e9d164a45138424cf2dc70c983bb1cbd6b842a8fed57c83356e4be2a9e3c268ae7c3
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
@@ -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...
@@ -23,7 +23,8 @@ module ViewComponentReflex
23
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
@@ -39,10 +40,12 @@ module ViewComponentReflex
39
40
  element.dataset[:key]
40
41
  end
41
42
  end
43
+ document = Nokogiri::HTML(controller.render_component_to_string(component))
42
44
  cable_ready[channel.stream_name].morph(
43
45
  selector: selector,
44
- children_only: false,
45
- 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"
46
49
  )
47
50
  end
48
51
 
@@ -58,19 +61,26 @@ module ViewComponentReflex
58
61
  # uses method to gather the method parameters, but since we're abusing
59
62
  # method_missing here, that'll always fail
60
63
  def method(name)
61
- name.to_sym.to_proc
64
+ component.method(name.to_sym)
62
65
  end
63
66
 
64
67
  def respond_to_missing?(name, _ = false)
65
68
  !!name.to_proc
66
69
  end
67
70
 
68
- before_reflex do |a|
69
- a.send a.method_name
70
- throw :abort
71
+ # this is copied out of stimulus_reflex/reflex.rb and modified
72
+ def morph(selectors, html = "")
73
+ case selectors
74
+ when :nothing
75
+ @broadcaster = StimulusReflex::NothingBroadcaster.new(self)
76
+ else
77
+ @broadcaster = StimulusReflex::SelectorBroadcaster.new(self) unless broadcaster.selector?
78
+ broadcaster.morphs << [selectors, html]
79
+ end
71
80
  end
72
81
 
73
82
  def method_missing(name, *args)
83
+ morph :nothing
74
84
  super unless respond_to_missing?(name)
75
85
  state.each do |k, v|
76
86
  component.instance_variable_set(k, v)
@@ -93,11 +103,23 @@ module ViewComponentReflex
93
103
  component_class.stimulus_controller
94
104
  end
95
105
 
106
+ def stimulate(target, data)
107
+ dataset = {}
108
+ data.each do |k, v|
109
+ dataset["data-#{k}"] = v.to_s
110
+ end
111
+ channel.receive({
112
+ "target" => target,
113
+ "attrs" => element.attributes.to_h.symbolize_keys,
114
+ "dataset" => dataset
115
+ })
116
+ end
117
+
96
118
  def component
97
119
  return @component if @component
98
120
  @component = component_class.allocate
99
121
  reflex = self
100
- exposed_methods = [:params, :request, :element, :refresh!, :refresh_all!, :stimulus_controller, :session, :prevent_refresh!, :selector]
122
+ exposed_methods = [:params, :request, :element, :refresh!, :refresh_all!, :stimulus_controller, :session, :prevent_refresh!, :selector, :stimulate]
101
123
  exposed_methods.each do |meth|
102
124
  @component.define_singleton_method(meth) do |*a|
103
125
  reflex.send(meth, *a)
@@ -15,7 +15,7 @@ module ViewComponentReflex
15
15
 
16
16
  def self.store_state(request, key, new_state = {})
17
17
  VIEW_COMPONENT_REFLEX_MEMORY_STATE[request.session.id.to_s] ||= {}
18
- VIEW_COMPONENT_REFLEX_MEMORY_STATE[request.session.id.to_s][key] ||= {}
18
+ VIEW_COMPONENT_REFLEX_MEMORY_STATE[request.session.id.to_s][key] = {}
19
19
  new_state.each do |k, v|
20
20
  VIEW_COMPONENT_REFLEX_MEMORY_STATE[request.session.id.to_s][key][k] = v
21
21
  end
@@ -1,3 +1,3 @@
1
1
  module ViewComponentReflex
2
- VERSION = '2.0.1'
2
+ VERSION = '2.1.3'
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.1
4
+ version: 2.1.3
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-06 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