view_component_reflex 2.1.2 → 2.2.1

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: 0ab31dd62e6ea3047e6092dc2fed4fb4ec56c0c6b348217109a91a63a7f5f741
4
- data.tar.gz: 51d03bf7e07b25c12fd5f2babd8c0346815c4e9cd7d4e0bfff6ff7df9d0acfcd
3
+ metadata.gz: 9cafa687f8da8991e6041d58f72740f442cec58b151c5c1ad5ff55d0af9d28d0
4
+ data.tar.gz: 037d287bfb4f5a2f11576e622f7ef697591051efec23acfeba6b4f925b5949d1
5
5
  SHA512:
6
- metadata.gz: 305353ea23702dec039ddfb05304adbc894f40355649ab137ddce4a03e6fab42f2fc6f5f775236e29c12e2a2b24e0e28d2d6e2904b2135435e51b3aa2c7c3fbb
7
- data.tar.gz: 8e1dc76c9d0efe24a7609ce25ab307daef5d861495c23be8a66d5bb6f96c88a1bba3b8c28735d4e55595b5b4e572db89ab65a6bae50c2430edb497387eff7f6d
6
+ metadata.gz: 8ba0eca4adee91478b0c55810fa0f9749a807ad378d2814566b3ecc907920b88e6c8dd8473c09d85190e21f52a5a3f71f74a554e4afb606c08e5901e9886f516
7
+ data.tar.gz: bb71bd74efc7b3f6390dcd9758fd57888ae25cb309d9c151433d3fb3e789229ba747fc7003b93064303f186db852f11d6ba7b600e4e67760e0afcd6099fe1ef4
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  ViewComponentReflex allows you to write reflexes right in your view component code.
4
4
 
5
+ It builds upon [stimulus_reflex](https://github.com/hopsoft/stimulus_reflex) and [view_component](https://github.com/github/view_component)
6
+
5
7
  ## Usage
6
8
 
7
9
  You can add reflexes to your component by adding inheriting from `ViewComponentReflex::Component`.
@@ -88,12 +90,30 @@ This shares the same definition as `content_tag`, except it accepts a reflex as
88
90
 
89
91
  Would add a click handler to the `increment` method on your component.
90
92
 
91
- To use a non-click event, specific that with `->` notiation
93
+ To use a non-click event, specific that with `->` notation
92
94
 
93
95
  ```erb
94
96
  <%= reflex_tag "mouseenter->increment", :button, "Click me!" %>
95
97
  ```
96
98
 
99
+ ### reflex_data_attributes(reflex)
100
+
101
+ This helper will give you the data attributes used in the reflex_tag above if you want to build your own elements.
102
+
103
+ Build your own tag:
104
+
105
+ ```erb
106
+ <%= link_to (image_tag photo.image.url(:medium)), data: reflex_data_attributes(:increment) %>
107
+ ```
108
+
109
+ Render a ViewComponent
110
+
111
+ ```erb
112
+ <%= render ButtonComponent.new(data: reflex_data_attributes("mouseenter->increment")) %>
113
+ ```
114
+
115
+ Make sure that you assign the reflex_data_attributes to the correct element in your component.
116
+
97
117
  ### collection_key
98
118
  If you're rendering a component as a collection with `MyComponent.with_collection(SomeCollection)`, you must define this method to return some unique value for the component.
99
119
  This is used to reconcile state in the background.
@@ -219,7 +239,7 @@ ViewComponentReflex uses session for its state by default. To change this, add
219
239
  an initializer to `config/initializers/view_component_reflex.rb`.
220
240
 
221
241
  ```ruby
222
- ViewComponentReflex.configure do |config|
242
+ ViewComponentReflex::Engine.configure do |config|
223
243
  config.state_adapter = YourAdapter
224
244
  end
225
245
  ```
@@ -279,9 +299,9 @@ Or install it yourself as:
279
299
  $ gem install view_component_reflex
280
300
  ```
281
301
 
282
- ## Common problems
302
+ # Common problems
283
303
 
284
- # Uninitialized constants \<component\>Reflex
304
+ ## Uninitialized constants \<component\>Reflex
285
305
  A component needs to be wrapped in `<%= component_controller do %>` in order to properly initialize, otherwise the Reflex class won't get created.
286
306
 
287
307
  ## License
@@ -2,10 +2,10 @@ module ViewComponentReflex
2
2
  class Component < ViewComponent::Base
3
3
  class << self
4
4
  def init_stimulus_reflex
5
- if name.include? "::"
6
- @stimulus_reflex ||= module_parent.const_set(name.split("::").last + "Reflex", Class.new(Reflex))
5
+ @stimulus_reflex ||= if name.include? "::"
6
+ module_parent.const_set(name.split("::").last + "Reflex", Class.new(ViewComponentReflex::Reflex))
7
7
  else
8
- @stimulus_reflex ||= Object.const_set(name + "Reflex", Class.new(Reflex))
8
+ Object.const_set(name + "Reflex", Class.new(ViewComponentReflex::Reflex))
9
9
  end
10
10
  @stimulus_reflex.component_class = self
11
11
  end
@@ -24,11 +24,11 @@ module ViewComponentReflex
24
24
  init_key
25
25
 
26
26
  tag = :div
27
- if opts_or_tag.is_a? Hash
28
- options = opts_or_tag
27
+ options = if opts_or_tag.is_a? Hash
28
+ opts_or_tag
29
29
  else
30
30
  tag = opts_or_tag
31
- options = opts
31
+ opts
32
32
  end
33
33
  options[:data] = {
34
34
  controller: self.class.stimulus_controller,
@@ -54,20 +54,25 @@ module ViewComponentReflex
54
54
  @key = key
55
55
  end
56
56
 
57
- def reflex_tag(reflex, name, content_or_options_with_block = {}, options = {}, escape = true, &block)
57
+ # Helper to use to create the proper reflex data attributes for an element
58
+ def reflex_data_attributes(reflex)
58
59
  action, method = reflex.to_s.split("->")
59
60
  if method.nil?
60
61
  method = action
61
62
  action = "click"
62
63
  end
63
- data_attributes = {
64
+
65
+ {
64
66
  reflex: "#{action}->#{self.class.name}##{method}",
65
67
  key: key
66
68
  }
69
+ end
70
+
71
+ def reflex_tag(reflex, name, content_or_options_with_block = {}, options = {}, escape = true, &block)
67
72
  if content_or_options_with_block.is_a?(Hash)
68
- merge_data_attributes(content_or_options_with_block, data_attributes)
73
+ merge_data_attributes(content_or_options_with_block, reflex_data_attributes(reflex))
69
74
  else
70
- merge_data_attributes(options, data_attributes)
75
+ merge_data_attributes(options, reflex_data_attributes(reflex))
71
76
  end
72
77
  content_tag(name, content_or_options_with_block, options, escape, &block)
73
78
  end
@@ -97,8 +102,8 @@ module ViewComponentReflex
97
102
  ViewComponentReflex::Engine.state_adapter.state(request, @key).each do |k, v|
98
103
  instance_value = instance_variable_get(k)
99
104
  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 })
105
+ ViewComponentReflex::Engine.state_adapter.set_state(request, controller, "#{@key}_initial", {k => instance_value})
106
+ ViewComponentReflex::Engine.state_adapter.set_state(request, controller, @key, {k => instance_value})
102
107
  else
103
108
  instance_variable_set(k, v)
104
109
  end
@@ -1,8 +1,8 @@
1
+ require "stimulus_reflex"
1
2
  require "view_component_reflex/state_adapter/session"
2
3
  require "view_component_reflex/state_adapter/memory"
3
- require "view_component_reflex/engine"
4
- require "stimulus_reflex"
5
4
  require "view_component_reflex/reflex"
5
+ require "view_component_reflex/engine"
6
6
 
7
7
  module ViewComponentReflex
8
8
  # Your code goes here...
@@ -61,19 +61,26 @@ module ViewComponentReflex
61
61
  # uses method to gather the method parameters, but since we're abusing
62
62
  # method_missing here, that'll always fail
63
63
  def method(name)
64
- name.to_sym.to_proc
64
+ component.method(name.to_sym)
65
65
  end
66
66
 
67
67
  def respond_to_missing?(name, _ = false)
68
68
  !!name.to_proc
69
69
  end
70
70
 
71
- before_reflex do |a|
72
- a.send a.method_name
73
- 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
74
80
  end
75
81
 
76
82
  def method_missing(name, *args)
83
+ morph :nothing
77
84
  super unless respond_to_missing?(name)
78
85
  state.each do |k, v|
79
86
  component.instance_variable_set(k, v)
@@ -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.1.2'
2
+ VERSION = '2.2.1'
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.1.2
4
+ version: 2.2.1
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-08-03 00:00:00.000000000 Z
11
+ date: 2020-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -34,16 +34,16 @@ dependencies:
34
34
  name: stimulus_reflex
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - ">="
37
+ - - '='
38
38
  - !ruby/object:Gem::Version
39
- version: '0'
39
+ version: 3.3.0.pre2
40
40
  type: :runtime
41
41
  prerelease: false
42
42
  version_requirements: !ruby/object:Gem::Requirement
43
43
  requirements:
44
- - - ">="
44
+ - - '='
45
45
  - !ruby/object:Gem::Version
46
- version: '0'
46
+ version: 3.3.0.pre2
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: view_component
49
49
  requirement: !ruby/object:Gem::Requirement