view_component_reflex 0.3.0 → 0.6.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: e02789888b71896b7005d2e854834926ebc5cb4839c4d3d8eb57a2a7ca9f1c16
4
- data.tar.gz: e51ad3e1aabaa02a81751533688bbc29cc3a227aaf26ed77f6ee61465ff2ff5b
3
+ metadata.gz: a9049a220598d64907ca4d9a7b77dc9df6e8913070ced348aa5182b666c35395
4
+ data.tar.gz: 1521d4623ee081f03d12e8ce40dc2cdbca67f5475a63351d84541a5a3955c1c8
5
5
  SHA512:
6
- metadata.gz: ccd9589f0c94f331d0e361c255803cf4d37ad2372e77e7752db1ba509ee528d87c08a1e2a43c45e80212674857e7176a0a7bde103bce1b0c5ce9c5cbba4c3ff0
7
- data.tar.gz: 5407c04db562748c569aac3ab7901a978e2fbfff1b7beab69c5cf6eb9b4baa7927092c3d6fddbced17b25b32263effd5bdd1b4ce6fdf3389f44c275a3081b3a2
6
+ metadata.gz: a3f7da6a7bca3500190a8edc7087159bdfd3d0ae49008919701849515223145b72364a821bf7e64897485c55da94647d7d74bd394aa5f98d285f16db611ae96c
7
+ data.tar.gz: baf53aeee357f38bc4c2d89efde2d3fc4fcbfb2ec7a935ed9968b71f09dc3788e7e6510ae5ef8935e08f3aaff5adea127e34fc8ff2110085667200bb27d71721
data/README.md CHANGED
@@ -11,15 +11,19 @@ To add a reflex to your component, use the `reflex` method.
11
11
  ```ruby
12
12
  reflex :my_cool_reflex do
13
13
  # do stuff
14
+ refresh!
14
15
  end
15
16
  ```
16
17
 
17
18
  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
19
  using stimulus reflex.
19
20
 
21
+ #####note: A reflex will not automatically re-render the component upon its completion. A component will re-render whenever the `set_state` or `refresh!` method is called.
22
+
20
23
  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
24
 
22
- You can access state with the `state` helper. See the code below for an example.
25
+ You can access state with the `state` helper. See the code below for an example. Calling `set_state` will set the state,
26
+ and also re-render your component.
23
27
 
24
28
  If you're using state add `data-key="<%= key %>"` to any html element using a reflex. This
25
29
  lets ViewComponentReflex keep track of which state belongs to which component.
@@ -36,7 +40,7 @@ lets ViewComponentReflex keep track of which state belongs to which component.
36
40
  end
37
41
 
38
42
  reflex :increment do
39
- state[:count] = state[:count] + 1
43
+ set_state(count: state[:count] + 1)
40
44
  end
41
45
  end
42
46
  ```
@@ -71,6 +75,14 @@ class YourAdapter
71
75
  # Return state for a given key
72
76
  end
73
77
 
78
+ ##
79
+ # reflex - The reflex instance that's trying to set the state
80
+ # key - a unique string that identifies the component
81
+ # new_state - the new state to set
82
+ def self.set_state(reflex, key, new_state)
83
+ end
84
+
85
+
74
86
  ##
75
87
  # request - a rails request object
76
88
  # key - a unique string that identifies the component instance
@@ -12,13 +12,43 @@ module ViewComponentReflex
12
12
  ViewComponentReflex::Engine.state_adapter.state(request, element.dataset[:key])
13
13
  end
14
14
 
15
+ def refresh!(primary_selector = "[data-controller=\"#{stimulus_controller}\"]", *selectors)
16
+ @channel.send :render_page_and_broadcast_morph, self, [primary_selector, *selectors], {
17
+ dataset: element.dataset.to_h,
18
+ args: [],
19
+ attrs: element.attributes.to_h,
20
+ selectors: ['body'],
21
+ target: "#{self.class.name}##{method_name}",
22
+ url: request.url,
23
+ permanentAttributeName: "data-reflex-permanent"
24
+ }
25
+ end
26
+
27
+ def refresh_all!
28
+ refresh!(['body'])
29
+ end
30
+
31
+ def set_state(new_state = {})
32
+ ViewComponentReflex::Engine.state_adapter.set_state(self, element.dataset[:key], new_state)
33
+ refresh!
34
+ end
35
+
36
+ before_reflex do |reflex, *args|
37
+ instance_exec(*args, &self.class.callbacks[self.method_name.to_sym]) if self.class.callbacks.include?(self.method_name.to_sym)
38
+ throw :abort
39
+ end
40
+
41
+ def self.callbacks
42
+ @callbacks ||= {}
43
+ end
44
+
15
45
  define_method :stimulus_controller do
16
46
  klass.name.chomp("Component").underscore.dasherize
17
47
  end
18
48
 
19
49
  define_singleton_method(:reflex) do |name, &blk|
50
+ callbacks[name] = blk
20
51
  define_method(name) do |*args|
21
- instance_exec(*args, &blk)
22
52
  end
23
53
  end
24
54
  })
@@ -29,6 +59,10 @@ module ViewComponentReflex
29
59
  @state = obj
30
60
  end
31
61
 
62
+ def stimulus_reflex?
63
+ helpers.controller.instance_variable_get(:@stimulus_reflex)
64
+ end
65
+
32
66
  # key is required if you're using state
33
67
  # We can't initialize the session state in the initial method
34
68
  # because it doesn't have a view_context yet
@@ -37,11 +71,11 @@ module ViewComponentReflex
37
71
  @key ||= caller.find { |p| p.include? ".html.erb" }&.hash.to_s
38
72
 
39
73
  # initialize session state
40
- if session[@key].nil?
74
+ if !stimulus_reflex? || session[@key].nil?
41
75
  ViewComponentReflex::Engine.state_adapter.store_state(request, @key, @state)
42
76
  ViewComponentReflex::Engine.state_adapter.store_state(request, "#{@key}_initial", @state)
43
77
  else
44
- ViewComponentReflex::Engine.state_adapter.reconcile_state(request, @key, @state)
78
+ # ViewComponentReflex::Engine.state_adapter.reconcile_state(request, @key, @state)
45
79
  end
46
80
  @key
47
81
  end
@@ -9,18 +9,5 @@ module ViewComponentReflex
9
9
  def self.configure
10
10
  yield self if block_given?
11
11
  end
12
-
13
- config.to_prepare do
14
- class StimulusReflex::Channel < ActionCable::Channel::Base
15
- def render_page_and_broadcast_morph(reflex, selectors, data = {})
16
- html = render_page(reflex)
17
- if reflex.respond_to? :stimulus_controller
18
- selectors = ["[data-controller=\"#{reflex.stimulus_controller}\"]"]
19
- end
20
- broadcast_morphs selectors, data, html if html.present?
21
- end
22
- end
23
-
24
- end
25
12
  end
26
13
  end
@@ -5,6 +5,14 @@ module ViewComponentReflex
5
5
  request.session[key] ||= {}
6
6
  end
7
7
 
8
+ def self.set_state(reflex, key, new_state)
9
+ new_state.each do |k, v|
10
+ state(reflex.request, key)[k] = v
11
+ end
12
+ store = reflex.request.session.instance_variable_get("@by")
13
+ store.commit_session reflex.request, reflex.controller.response
14
+ end
15
+
8
16
  def self.store_state(request, key, new_state = {})
9
17
  request.session[key] = {}
10
18
  new_state.each do |k, v|
@@ -1,3 +1,3 @@
1
1
  module ViewComponentReflex
2
- VERSION = '0.3.0'
2
+ VERSION = '0.6.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: 0.3.0
4
+ version: 0.6.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-06-05 00:00:00.000000000 Z
11
+ date: 2020-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails