view_component_reflex 0.3.0 → 0.4.0

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: e02789888b71896b7005d2e854834926ebc5cb4839c4d3d8eb57a2a7ca9f1c16
4
- data.tar.gz: e51ad3e1aabaa02a81751533688bbc29cc3a227aaf26ed77f6ee61465ff2ff5b
3
+ metadata.gz: 6604db3319eb30b4cf0ac65502e46c3c1107094290b7ba2c9efda8c16735b250
4
+ data.tar.gz: d644b0e76cc36cd6f969406569c4f28d32719e574a848bc360db0979398a0dbc
5
5
  SHA512:
6
- metadata.gz: ccd9589f0c94f331d0e361c255803cf4d37ad2372e77e7752db1ba509ee528d87c08a1e2a43c45e80212674857e7176a0a7bde103bce1b0c5ce9c5cbba4c3ff0
7
- data.tar.gz: 5407c04db562748c569aac3ab7901a978e2fbfff1b7beab69c5cf6eb9b4baa7927092c3d6fddbced17b25b32263effd5bdd1b4ce6fdf3389f44c275a3081b3a2
6
+ metadata.gz: 3af567ec00951af7ba672cee153b30971f0c9ec679c148447c4e2379687e1af7592d3c23960135a9efd8d1d752bb9f482bcafd83731a88d60f6740175acce586
7
+ data.tar.gz: a4a16cf03448c3251dd94714a98f269e06c983a7f1351aab7622a65a80a9ef3794c277fbd0cbb4b2ba7de91c16e4de4f4c4eff49dee4afa2f3a605d56980b14c
data/README.md CHANGED
@@ -19,7 +19,8 @@ using stimulus reflex.
19
19
 
20
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
21
 
22
- You can access state with the `state` helper. See the code below for an example.
22
+ You can access state with the `state` helper. See the code below for an example. Calling `set_state` will set the state,
23
+ and also re-render your component.
23
24
 
24
25
  If you're using state add `data-key="<%= key %>"` to any html element using a reflex. This
25
26
  lets ViewComponentReflex keep track of which state belongs to which component.
@@ -36,7 +37,7 @@ lets ViewComponentReflex keep track of which state belongs to which component.
36
37
  end
37
38
 
38
39
  reflex :increment do
39
- state[:count] = state[:count] + 1
40
+ set_state(count: state[:count] + 1)
40
41
  end
41
42
  end
42
43
  ```
@@ -71,6 +72,14 @@ class YourAdapter
71
72
  # Return state for a given key
72
73
  end
73
74
 
75
+ ##
76
+ # reflex - The reflex instance that's trying to set the state
77
+ # key - a unique string that identifies the component
78
+ # new_state - the new state to set
79
+ def self.set_state(reflex, key, new_state)
80
+ end
81
+
82
+
74
83
  ##
75
84
  # request - a rails request object
76
85
  # key - a unique string that identifies the component instance
@@ -12,13 +12,35 @@ module ViewComponentReflex
12
12
  ViewComponentReflex::Engine.state_adapter.state(request, element.dataset[:key])
13
13
  end
14
14
 
15
+ def set_state(new_state)
16
+ ViewComponentReflex::Engine.state_adapter.set_state(self, element.dataset[:key], new_state)
17
+ @channel.render_page_and_broadcast_morph(self, nil, {
18
+ dataset: element.dataset.to_h,
19
+ args: [],
20
+ attrs: element.attributes.to_h,
21
+ selectors: ['body'],
22
+ target: "#{self.class.name}##{method_name}",
23
+ url: request.url,
24
+ permanentAttributeName: "data-reflex-permanent"
25
+ })
26
+ end
27
+
28
+ before_reflex do |reflex, *args|
29
+ instance_exec(*args, &self.class.callbacks[self.method_name.to_sym]) if self.class.callbacks.include?(self.method_name.to_sym)
30
+ throw :abort
31
+ end
32
+
33
+ def self.callbacks
34
+ @callbacks ||= {}
35
+ end
36
+
15
37
  define_method :stimulus_controller do
16
38
  klass.name.chomp("Component").underscore.dasherize
17
39
  end
18
40
 
19
41
  define_singleton_method(:reflex) do |name, &blk|
42
+ callbacks[name] = blk
20
43
  define_method(name) do |*args|
21
- instance_exec(*args, &blk)
22
44
  end
23
45
  end
24
46
  })
@@ -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.4.0'
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.4.0
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-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails