view_component_reflex 0.1.0 → 0.2.4

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: 42a18772bfb90827ea98389fcf0372d4e040263d9574daf7f6b5bd46d37abcff
4
- data.tar.gz: cc1adae2d0e275d0356d0431369bc3a4c99c4f9e46c943fff99d7e177f383857
3
+ metadata.gz: e5ffa83358af12b225a6a5d4e7fc16a27275e1f2815b2f5e6105a555d7d3faa8
4
+ data.tar.gz: 2822a3c179dac3550eb1be5c7d30f997cd8942422429d2af92f167880652f28c
5
5
  SHA512:
6
- metadata.gz: cb5b99238e5106761cb071296cb55c0717499d30afd6d4f11948c800faae31260bae5cb133c24c0f04400ba605b79adebdcd66c95118696f34f5e357dbef2c89
7
- data.tar.gz: a7eda1ce0386f187efd4d354ec0dbb23dee3103377db7088f94e0f74694b971d06b975b06fbf855bea52219aef06cd0c6cf5ab7ea68ee420526328041ce3daba
6
+ metadata.gz: f85e4d51fef8d3ba242a48b9b8c3f5c800cbd746582c055d0cb68eba6b6309b2678362fd18b55fec103a1a26d08df728603304ccb5c2f7d0f9bca6c96ce24de6
7
+ data.tar.gz: 0a9624dff9b4127e59d28ecb0a574186dda9214f772cf332cd23aeca740ef8a3633ff0066d9c8e323fc4c612efb87e81fb9ac24c70aaaab3fc8ece07561bd243
data/README.md CHANGED
@@ -1,28 +1,74 @@
1
- # ViewComponentReflex
2
- Short description and motivation.
3
-
4
- ## Usage
5
- How to use my plugin.
6
-
7
- ## Installation
8
- Add this line to your application's Gemfile:
9
-
10
- ```ruby
11
- gem 'view_component_reflex'
12
- ```
13
-
14
- And then execute:
15
- ```bash
16
- $ bundle
17
- ```
18
-
19
- Or install it yourself as:
20
- ```bash
21
- $ gem install view_component_reflex
22
- ```
23
-
24
- ## Contributing
25
- Contribution directions go here.
26
-
27
- ## License
28
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
1
+ # ViewComponentReflex
2
+
3
+ ViewComponentReflex allows you to write reflexes right in your view component code.
4
+
5
+ ## Usage
6
+
7
+ You can add reflexes to your component by adding inheriting from `ViewComponentReflex::Component`.
8
+
9
+ To add a reflex to your component, use the `reflex` method.
10
+
11
+ ```ruby
12
+ reflex :my_cool_reflex do
13
+ # do stuff
14
+ end
15
+ ```
16
+
17
+ This will act as if you created a reflex with the method `my_cool_stuff`. To call this reflex, add `data-reflex="click->MyComponentReflex#my_cool_reflex"`, just like you're
18
+ using stimulus reflex.
19
+
20
+ In addition to calling reflexes, there is a rudimentary state system. You can initialize component-local state with `initialize_state(obj)`, where `obj` is a hash.
21
+
22
+ You can access state with the `state` helper. See the code below for an example.
23
+
24
+ If you're using state add `data-key="<%= key %>"` to any html element using a reflex. This
25
+ lets ViewComponentReflex keep track of which state belongs to which component.
26
+
27
+
28
+ ```ruby
29
+ # counter_component.rb
30
+ class CounterComponent < ViewComponentReflex::Component
31
+
32
+ def initialize
33
+ initialize_state({
34
+ count: 0
35
+ })
36
+ end
37
+
38
+ reflex :increment do
39
+ state[:count] = state[:count] + 1
40
+ end
41
+ end
42
+ ```
43
+
44
+ ```erb
45
+ # counter_component.html.erb
46
+ <div data-controller="counter">
47
+ <p><%= state[:count] %></p>
48
+ <button type="button" data-reflex="click->CounterComponentReflex#increment" data-key="<%= key %>">Click</button>
49
+ </div>
50
+ ```
51
+
52
+ ## Installation
53
+ Add this line to your application's Gemfile:
54
+
55
+ ```ruby
56
+ gem 'view_component_reflex'
57
+ ```
58
+
59
+ And then execute:
60
+ ```bash
61
+ $ bundle
62
+ ```
63
+
64
+ Or install it yourself as:
65
+ ```bash
66
+ $ gem install view_component_reflex
67
+ ```
68
+
69
+ ## License
70
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
71
+
72
+ ## Caveats
73
+
74
+ State uses session to maintain state as of right now. It also assumes your component view is written with the file extension `.html.erb`
@@ -2,17 +2,20 @@ module ViewComponentReflex
2
2
  class Component < ViewComponent::Base
3
3
  class << self
4
4
  def reflex(name, &blk)
5
- @stimulus_reflex.reflex(name, &blk)
5
+ stimulus_reflex.reflex(name, &blk)
6
6
  end
7
7
 
8
- def connect_stimulus_reflex
8
+ def stimulus_reflex
9
+ klass = self
9
10
  @stimulus_reflex ||= Object.const_set(name + "Reflex", Class.new(StimulusReflex::Reflex) {
10
- include CableReady::Broadcaster
11
-
12
11
  def state
13
12
  session[element.dataset[:key]] ||= {}
14
13
  end
15
14
 
15
+ define_method :stimulus_controller do
16
+ klass.name.chomp("Component").underscore.dasherize
17
+ end
18
+
16
19
  define_singleton_method(:reflex) do |name, &blk|
17
20
  define_method(name) do |*args|
18
21
  instance_exec(*args, &blk)
@@ -22,21 +25,21 @@ module ViewComponentReflex
22
25
  end
23
26
  end
24
27
 
25
- def initialize
26
- @key = caller.find { |p| p.include? ".html.erb" }&.hash.to_s
27
- end
28
-
29
28
  def initialize_state(obj)
30
29
  @state = obj
31
- @state.each do |k, v|
32
- instance_variable_set("@#{k}", v)
33
- end
34
30
  end
35
31
 
32
+ # key is required if you're using state
33
+ # We can't initialize the session state in the initiale method
34
+ # because it doesn't have a view_context yet
35
+ # This is the next best place to do it
36
36
  def key
37
+ @key ||= caller.find { |p| p.include? ".html.erb" }&.hash.to_s
38
+
39
+ # initialize session state
37
40
  if session[@key].nil?
38
41
  session[@key] = {}
39
- @state.each do |key, v|
42
+ (@state ||= {}).each do |key, v|
40
43
  session[@key][key] = v
41
44
  end
42
45
  end
@@ -1,4 +1,5 @@
1
1
  require "view_component_reflex/engine"
2
+ require 'stimulus_reflex'
2
3
 
3
4
  module ViewComponentReflex
4
5
  # Your code goes here...
@@ -1,5 +1,16 @@
1
1
  module ViewComponentReflex
2
2
  class Engine < ::Rails::Engine
3
- isolate_namespace ViewComponentReflex
3
+ config.to_prepare do
4
+ class StimulusReflex::Channel < ActionCable::Channel::Base
5
+ def render_page_and_broadcast_morph(reflex, selectors, data = {})
6
+ html = render_page(reflex)
7
+ if reflex.respond_to? :stimulus_controller
8
+ selectors = ["[data-controller=\"#{reflex.stimulus_controller}\"]"]
9
+ end
10
+ broadcast_morphs selectors, data, html if html.present?
11
+ end
12
+ end
13
+
14
+ end
4
15
  end
5
16
  end
@@ -1,3 +1,3 @@
1
1
  module ViewComponentReflex
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.4'
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: 0.1.0
4
+ version: 0.2.4
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-02 00:00:00.000000000 Z
11
+ date: 2020-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails