view_component_reflex 0.2.4 → 0.5.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: e5ffa83358af12b225a6a5d4e7fc16a27275e1f2815b2f5e6105a555d7d3faa8
4
- data.tar.gz: 2822a3c179dac3550eb1be5c7d30f997cd8942422429d2af92f167880652f28c
3
+ metadata.gz: 730dc20cb5c31f500fb8c6fd63c5e21286a4a3ea28f63a64aaa06c1640ef4844
4
+ data.tar.gz: 911b8665fd95a69b28de2bf366ffa9edb4240070c8e29c145090d27ba8eaf271
5
5
  SHA512:
6
- metadata.gz: f85e4d51fef8d3ba242a48b9b8c3f5c800cbd746582c055d0cb68eba6b6309b2678362fd18b55fec103a1a26d08df728603304ccb5c2f7d0f9bca6c96ce24de6
7
- data.tar.gz: 0a9624dff9b4127e59d28ecb0a574186dda9214f772cf332cd23aeca740ef8a3633ff0066d9c8e323fc4c612efb87e81fb9ac24c70aaaab3fc8ece07561bd243
6
+ metadata.gz: 679abeb33702936136cfc32244be5bd7ed85cbe8526594eb3f3ed8138b0aec7e004507eab5c4624fa0accd19958999a62641122dbfd67693ab9c3bf11424fd9f
7
+ data.tar.gz: 97f3b2645a43893d09a253c3d8c780ce1c141aab599c67db3fb6bd47ed443780960d831da6b10ff2930a1bdb0eb063ff667c5292b1c23f1e6eb9f1292f890c40
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
  ```
@@ -49,6 +53,64 @@ lets ViewComponentReflex keep track of which state belongs to which component.
49
53
  </div>
50
54
  ```
51
55
 
56
+ ## Custom State Adapters
57
+
58
+ ViewComponentReflex uses session for its state by default. To change this, add
59
+ an initializer to `config/initializers/view_component_reflex.rb`.
60
+
61
+ ```ruby
62
+ ViewComponentReflex.configure do |config|
63
+ config.state_adapter = YourAdapter
64
+ end
65
+ ```
66
+
67
+ `YourAdapter` should implement
68
+
69
+ ```ruby
70
+ class YourAdapter
71
+ ##
72
+ # request - a rails request object
73
+ # key - a unique string that identifies the component instance
74
+ def self.state(request, key)
75
+ # Return state for a given key
76
+ end
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
+
86
+ ##
87
+ # request - a rails request object
88
+ # key - a unique string that identifies the component instance
89
+ # new_state - a hash containing the component state
90
+ def self.store_state(request, key, new_state = {})
91
+ # store the state
92
+ # this will be called twice, once with key, once with key_initial
93
+ # key_initial contains the initial, unmodified state.
94
+ # it should be used in reconcile_state to decide whether or not
95
+ # to re-initialize the state
96
+ end
97
+
98
+ ##
99
+ # request - a rails request object
100
+ # key - a unique string that identifies the component instance
101
+ # new_state - a hash containing the component state
102
+ def self.reconcile_state(request, key, new_state)
103
+ # The passed state should always match the initial state of the component
104
+ # if it doesn't, we need to reset the state to the passed value.
105
+ #
106
+ # This handles cases where your initialize_state param computes some value that changes
107
+ # initialize_state({ transaction: @customer.transactions.first })
108
+ # if you delete the first transaction, that ^ is no longer valid. We need to update the state.
109
+ end
110
+ end
111
+ ```
112
+
113
+
52
114
  ## Installation
53
115
  Add this line to your application's Gemfile:
54
116
 
@@ -9,7 +9,33 @@ module ViewComponentReflex
9
9
  klass = self
10
10
  @stimulus_reflex ||= Object.const_set(name + "Reflex", Class.new(StimulusReflex::Reflex) {
11
11
  def state
12
- session[element.dataset[:key]] ||= {}
12
+ ViewComponentReflex::Engine.state_adapter.state(request, element.dataset[:key])
13
+ end
14
+
15
+ def refresh!
16
+ @channel.send :render_page_and_broadcast_morph, self, ["[data-controller=\"#{stimulus_controller}\"]"], {
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 set_state(new_state = {})
28
+ ViewComponentReflex::Engine.state_adapter.set_state(self, element.dataset[:key], new_state)
29
+ refresh!
30
+ end
31
+
32
+ before_reflex do |reflex, *args|
33
+ instance_exec(*args, &self.class.callbacks[self.method_name.to_sym]) if self.class.callbacks.include?(self.method_name.to_sym)
34
+ throw :abort
35
+ end
36
+
37
+ def self.callbacks
38
+ @callbacks ||= {}
13
39
  end
14
40
 
15
41
  define_method :stimulus_controller do
@@ -17,8 +43,8 @@ module ViewComponentReflex
17
43
  end
18
44
 
19
45
  define_singleton_method(:reflex) do |name, &blk|
46
+ callbacks[name] = blk
20
47
  define_method(name) do |*args|
21
- instance_exec(*args, &blk)
22
48
  end
23
49
  end
24
50
  })
@@ -29,25 +55,29 @@ module ViewComponentReflex
29
55
  @state = obj
30
56
  end
31
57
 
58
+ def stimulus_reflex?
59
+ helpers.controller.instance_variable_get(:@stimulus_reflex)
60
+ end
61
+
32
62
  # key is required if you're using state
33
- # We can't initialize the session state in the initiale method
63
+ # We can't initialize the session state in the initial method
34
64
  # because it doesn't have a view_context yet
35
65
  # This is the next best place to do it
36
66
  def key
37
67
  @key ||= caller.find { |p| p.include? ".html.erb" }&.hash.to_s
38
68
 
39
69
  # initialize session state
40
- if session[@key].nil?
41
- session[@key] = {}
42
- (@state ||= {}).each do |key, v|
43
- session[@key][key] = v
44
- end
70
+ if !stimulus_reflex? || session[@key].nil?
71
+ ViewComponentReflex::Engine.state_adapter.store_state(request, @key, @state)
72
+ ViewComponentReflex::Engine.state_adapter.store_state(request, "#{@key}_initial", @state)
73
+ else
74
+ # ViewComponentReflex::Engine.state_adapter.reconcile_state(request, @key, @state)
45
75
  end
46
76
  @key
47
77
  end
48
78
 
49
79
  def state
50
- session[key]
80
+ ViewComponentReflex::Engine.state_adapter.state(request, key)
51
81
  end
52
82
  end
53
83
  end
@@ -1,3 +1,4 @@
1
+ require "view_component_reflex/state_adapter/session"
1
2
  require "view_component_reflex/engine"
2
3
  require 'stimulus_reflex'
3
4
 
@@ -1,16 +1,13 @@
1
1
  module ViewComponentReflex
2
2
  class Engine < ::Rails::Engine
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
3
+ class << self
4
+ mattr_accessor :state_adapter
13
5
 
6
+ self.state_adapter = StateAdapter::Session
7
+ end
8
+
9
+ def self.configure
10
+ yield self if block_given?
14
11
  end
15
12
  end
16
13
  end
@@ -0,0 +1,38 @@
1
+ module ViewComponentReflex
2
+ module StateAdapter
3
+ class Session
4
+ def self.state(request, key)
5
+ request.session[key] ||= {}
6
+ end
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
+
16
+ def self.store_state(request, key, new_state = {})
17
+ request.session[key] = {}
18
+ new_state.each do |k, v|
19
+ request.session[key][k] = v
20
+ end
21
+ end
22
+
23
+ # The passed state should always match the initial state of the component
24
+ # if it doesn't, we need to reset the state to the passed value.
25
+ #
26
+ # This handles cases where your initialize_state param computes some value that changes
27
+ # initialize_state({ transaction: @customer.transactions.first })
28
+ # if you delete the first transaction, that ^ is no longer valid. We need to update the state.
29
+ def self.reconcile_state(request, key, new_state)
30
+ request.session["#{key}_initial"].each do |k, v|
31
+ if new_state[k] != v
32
+ request.session[key][k] = new_state[k]
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,3 +1,3 @@
1
1
  module ViewComponentReflex
2
- VERSION = '0.2.4'
2
+ VERSION = '0.5.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.2.4
4
+ version: 0.5.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-04 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
@@ -71,6 +71,7 @@ files:
71
71
  - app/components/view_component_reflex/component.rb
72
72
  - lib/view_component_reflex.rb
73
73
  - lib/view_component_reflex/engine.rb
74
+ - lib/view_component_reflex/state_adapter/session.rb
74
75
  - lib/view_component_reflex/version.rb
75
76
  homepage: https://github.com/joshleblanc/view_component_reflex
76
77
  licenses: