stimulus_reflex 3.4.1 → 3.5.0.pre0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of stimulus_reflex might be problematic. Click here for more details.

Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +587 -495
  3. data/Gemfile.lock +82 -86
  4. data/LATEST +1 -0
  5. data/README.md +10 -10
  6. data/app/channels/stimulus_reflex/channel.rb +40 -67
  7. data/lib/generators/USAGE +1 -1
  8. data/lib/generators/stimulus_reflex/{config_generator.rb → initializer_generator.rb} +3 -3
  9. data/lib/generators/stimulus_reflex/templates/app/reflexes/%file_name%_reflex.rb.tt +3 -2
  10. data/lib/generators/stimulus_reflex/templates/app/reflexes/application_reflex.rb.tt +1 -1
  11. data/lib/generators/stimulus_reflex/templates/config/initializers/stimulus_reflex.rb +6 -1
  12. data/lib/stimulus_reflex.rb +9 -2
  13. data/lib/stimulus_reflex/broadcasters/broadcaster.rb +7 -4
  14. data/lib/stimulus_reflex/broadcasters/page_broadcaster.rb +2 -2
  15. data/lib/stimulus_reflex/broadcasters/selector_broadcaster.rb +12 -5
  16. data/lib/stimulus_reflex/cable_ready_channels.rb +6 -2
  17. data/lib/stimulus_reflex/callbacks.rb +55 -5
  18. data/lib/stimulus_reflex/concern_enhancer.rb +37 -0
  19. data/lib/stimulus_reflex/configuration.rb +2 -1
  20. data/lib/stimulus_reflex/element.rb +31 -7
  21. data/lib/stimulus_reflex/policies/reflex_invocation_policy.rb +28 -0
  22. data/lib/stimulus_reflex/reflex.rb +35 -20
  23. data/lib/stimulus_reflex/reflex_data.rb +79 -0
  24. data/lib/stimulus_reflex/reflex_factory.rb +31 -0
  25. data/lib/stimulus_reflex/request_parameters.rb +19 -0
  26. data/lib/stimulus_reflex/{logger.rb → utils/logger.rb} +0 -2
  27. data/lib/stimulus_reflex/{sanity_checker.rb → utils/sanity_checker.rb} +58 -10
  28. data/lib/stimulus_reflex/version.rb +1 -1
  29. data/lib/tasks/stimulus_reflex/install.rake +6 -4
  30. data/package.json +6 -5
  31. data/stimulus_reflex.gemspec +5 -5
  32. data/test/broadcasters/broadcaster_test_case.rb +1 -1
  33. data/test/broadcasters/nothing_broadcaster_test.rb +5 -3
  34. data/test/broadcasters/page_broadcaster_test.rb +8 -4
  35. data/test/broadcasters/selector_broadcaster_test.rb +171 -55
  36. data/test/callbacks_test.rb +652 -0
  37. data/test/concern_enhancer_test.rb +54 -0
  38. data/test/element_test.rb +181 -0
  39. data/test/reflex_test.rb +1 -1
  40. data/test/test_helper.rb +4 -0
  41. data/test/tmp/app/reflexes/application_reflex.rb +2 -2
  42. data/test/tmp/app/reflexes/user_reflex.rb +3 -2
  43. data/yarn.lock +1138 -919
  44. metadata +39 -28
  45. data/tags +0 -156
data/Gemfile.lock CHANGED
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- stimulus_reflex (3.4.1)
5
- cable_ready (>= 4.5)
4
+ stimulus_reflex (3.5.0.pre0)
5
+ cable_ready (= 5.0.0.pre0)
6
6
  nokogiri
7
7
  rack
8
8
  rails (>= 5.2)
@@ -11,60 +11,60 @@ PATH
11
11
  GEM
12
12
  remote: https://rubygems.org/
13
13
  specs:
14
- actioncable (6.1.1)
15
- actionpack (= 6.1.1)
16
- activesupport (= 6.1.1)
14
+ actioncable (6.1.3.2)
15
+ actionpack (= 6.1.3.2)
16
+ activesupport (= 6.1.3.2)
17
17
  nio4r (~> 2.0)
18
18
  websocket-driver (>= 0.6.1)
19
- actionmailbox (6.1.1)
20
- actionpack (= 6.1.1)
21
- activejob (= 6.1.1)
22
- activerecord (= 6.1.1)
23
- activestorage (= 6.1.1)
24
- activesupport (= 6.1.1)
19
+ actionmailbox (6.1.3.2)
20
+ actionpack (= 6.1.3.2)
21
+ activejob (= 6.1.3.2)
22
+ activerecord (= 6.1.3.2)
23
+ activestorage (= 6.1.3.2)
24
+ activesupport (= 6.1.3.2)
25
25
  mail (>= 2.7.1)
26
- actionmailer (6.1.1)
27
- actionpack (= 6.1.1)
28
- actionview (= 6.1.1)
29
- activejob (= 6.1.1)
30
- activesupport (= 6.1.1)
26
+ actionmailer (6.1.3.2)
27
+ actionpack (= 6.1.3.2)
28
+ actionview (= 6.1.3.2)
29
+ activejob (= 6.1.3.2)
30
+ activesupport (= 6.1.3.2)
31
31
  mail (~> 2.5, >= 2.5.4)
32
32
  rails-dom-testing (~> 2.0)
33
- actionpack (6.1.1)
34
- actionview (= 6.1.1)
35
- activesupport (= 6.1.1)
33
+ actionpack (6.1.3.2)
34
+ actionview (= 6.1.3.2)
35
+ activesupport (= 6.1.3.2)
36
36
  rack (~> 2.0, >= 2.0.9)
37
37
  rack-test (>= 0.6.3)
38
38
  rails-dom-testing (~> 2.0)
39
39
  rails-html-sanitizer (~> 1.0, >= 1.2.0)
40
- actiontext (6.1.1)
41
- actionpack (= 6.1.1)
42
- activerecord (= 6.1.1)
43
- activestorage (= 6.1.1)
44
- activesupport (= 6.1.1)
40
+ actiontext (6.1.3.2)
41
+ actionpack (= 6.1.3.2)
42
+ activerecord (= 6.1.3.2)
43
+ activestorage (= 6.1.3.2)
44
+ activesupport (= 6.1.3.2)
45
45
  nokogiri (>= 1.8.5)
46
- actionview (6.1.1)
47
- activesupport (= 6.1.1)
46
+ actionview (6.1.3.2)
47
+ activesupport (= 6.1.3.2)
48
48
  builder (~> 3.1)
49
49
  erubi (~> 1.4)
50
50
  rails-dom-testing (~> 2.0)
51
51
  rails-html-sanitizer (~> 1.1, >= 1.2.0)
52
- activejob (6.1.1)
53
- activesupport (= 6.1.1)
52
+ activejob (6.1.3.2)
53
+ activesupport (= 6.1.3.2)
54
54
  globalid (>= 0.3.6)
55
- activemodel (6.1.1)
56
- activesupport (= 6.1.1)
57
- activerecord (6.1.1)
58
- activemodel (= 6.1.1)
59
- activesupport (= 6.1.1)
60
- activestorage (6.1.1)
61
- actionpack (= 6.1.1)
62
- activejob (= 6.1.1)
63
- activerecord (= 6.1.1)
64
- activesupport (= 6.1.1)
65
- marcel (~> 0.3.1)
66
- mimemagic (~> 0.3.2)
67
- activesupport (6.1.1)
55
+ activemodel (6.1.3.2)
56
+ activesupport (= 6.1.3.2)
57
+ activerecord (6.1.3.2)
58
+ activemodel (= 6.1.3.2)
59
+ activesupport (= 6.1.3.2)
60
+ activestorage (6.1.3.2)
61
+ actionpack (= 6.1.3.2)
62
+ activejob (= 6.1.3.2)
63
+ activerecord (= 6.1.3.2)
64
+ activesupport (= 6.1.3.2)
65
+ marcel (~> 1.0.0)
66
+ mini_mime (~> 1.0.2)
67
+ activesupport (6.1.3.2)
68
68
  concurrent-ruby (~> 1.0, >= 1.0.2)
69
69
  i18n (>= 1.6, < 2)
70
70
  minitest (>= 5.1)
@@ -72,7 +72,7 @@ GEM
72
72
  zeitwerk (~> 2.3)
73
73
  ast (2.4.2)
74
74
  builder (3.2.4)
75
- cable_ready (4.5.0)
75
+ cable_ready (5.0.0.pre0)
76
76
  rails (>= 5.2)
77
77
  thread-local (>= 1.1.0)
78
78
  coderay (1.1.3)
@@ -81,26 +81,22 @@ GEM
81
81
  erubi (1.10.0)
82
82
  globalid (0.4.2)
83
83
  activesupport (>= 4.2.0)
84
- i18n (1.8.7)
84
+ i18n (1.8.10)
85
85
  concurrent-ruby (~> 1.0)
86
- loofah (2.9.0)
86
+ loofah (2.9.1)
87
87
  crass (~> 1.0.2)
88
88
  nokogiri (>= 1.5.9)
89
89
  mail (2.7.1)
90
90
  mini_mime (>= 0.1.1)
91
- marcel (0.3.3)
92
- mimemagic (~> 0.3.2)
91
+ marcel (1.0.1)
93
92
  method_source (0.9.2)
94
- mimemagic (0.3.5)
95
- mini_mime (1.0.2)
96
- mini_portile2 (2.5.0)
97
- minitest (5.14.3)
98
- nio4r (2.5.4)
99
- nokogiri (1.11.1)
100
- mini_portile2 (~> 2.5.0)
93
+ mini_mime (1.0.3)
94
+ minitest (5.14.4)
95
+ nio4r (2.5.7)
96
+ nokogiri (1.11.5-arm64-darwin)
101
97
  racc (~> 1.4)
102
98
  parallel (1.20.1)
103
- parser (3.0.0.0)
99
+ parser (3.0.1.1)
104
100
  ast (~> 2.4.1)
105
101
  pry (0.12.2)
106
102
  coderay (~> 1.1.0)
@@ -111,50 +107,50 @@ GEM
111
107
  rack (2.2.3)
112
108
  rack-test (1.1.0)
113
109
  rack (>= 1.0, < 3)
114
- rails (6.1.1)
115
- actioncable (= 6.1.1)
116
- actionmailbox (= 6.1.1)
117
- actionmailer (= 6.1.1)
118
- actionpack (= 6.1.1)
119
- actiontext (= 6.1.1)
120
- actionview (= 6.1.1)
121
- activejob (= 6.1.1)
122
- activemodel (= 6.1.1)
123
- activerecord (= 6.1.1)
124
- activestorage (= 6.1.1)
125
- activesupport (= 6.1.1)
110
+ rails (6.1.3.2)
111
+ actioncable (= 6.1.3.2)
112
+ actionmailbox (= 6.1.3.2)
113
+ actionmailer (= 6.1.3.2)
114
+ actionpack (= 6.1.3.2)
115
+ actiontext (= 6.1.3.2)
116
+ actionview (= 6.1.3.2)
117
+ activejob (= 6.1.3.2)
118
+ activemodel (= 6.1.3.2)
119
+ activerecord (= 6.1.3.2)
120
+ activestorage (= 6.1.3.2)
121
+ activesupport (= 6.1.3.2)
126
122
  bundler (>= 1.15.0)
127
- railties (= 6.1.1)
123
+ railties (= 6.1.3.2)
128
124
  sprockets-rails (>= 2.0.0)
129
125
  rails-dom-testing (2.0.3)
130
126
  activesupport (>= 4.2.0)
131
127
  nokogiri (>= 1.6)
132
128
  rails-html-sanitizer (1.3.0)
133
129
  loofah (~> 2.3)
134
- railties (6.1.1)
135
- actionpack (= 6.1.1)
136
- activesupport (= 6.1.1)
130
+ railties (6.1.3.2)
131
+ actionpack (= 6.1.3.2)
132
+ activesupport (= 6.1.3.2)
137
133
  method_source
138
134
  rake (>= 0.8.7)
139
135
  thor (~> 1.0)
140
136
  rainbow (3.0.0)
141
137
  rake (13.0.3)
142
138
  redis (4.2.5)
143
- regexp_parser (2.0.3)
144
- rexml (3.2.4)
145
- rubocop (1.7.0)
139
+ regexp_parser (2.1.1)
140
+ rexml (3.2.5)
141
+ rubocop (1.14.0)
146
142
  parallel (~> 1.10)
147
- parser (>= 2.7.1.5)
143
+ parser (>= 3.0.0.0)
148
144
  rainbow (>= 2.2.2, < 4.0)
149
145
  regexp_parser (>= 1.8, < 3.0)
150
146
  rexml
151
- rubocop-ast (>= 1.2.0, < 2.0)
147
+ rubocop-ast (>= 1.5.0, < 2.0)
152
148
  ruby-progressbar (~> 1.7)
153
- unicode-display_width (>= 1.4.0, < 2.0)
154
- rubocop-ast (1.4.1)
155
- parser (>= 2.7.1.5)
156
- rubocop-performance (1.9.2)
157
- rubocop (>= 0.90.0, < 2.0)
149
+ unicode-display_width (>= 1.4.0, < 3.0)
150
+ rubocop-ast (1.5.0)
151
+ parser (>= 3.0.1.1)
152
+ rubocop-performance (1.11.2)
153
+ rubocop (>= 1.7.0, < 2.0)
158
154
  rubocop-ast (>= 0.4.0)
159
155
  ruby-progressbar (1.11.0)
160
156
  sprockets (4.0.2)
@@ -164,16 +160,16 @@ GEM
164
160
  actionpack (>= 4.0)
165
161
  activesupport (>= 4.0)
166
162
  sprockets (>= 3.0.0)
167
- standard (0.11.0)
168
- rubocop (= 1.7.0)
169
- rubocop-performance (= 1.9.2)
163
+ standard (1.1.1)
164
+ rubocop (= 1.14.0)
165
+ rubocop-performance (= 1.11.2)
170
166
  standardrb (1.0.0)
171
167
  standard
172
168
  thor (1.1.0)
173
169
  thread-local (1.1.0)
174
170
  tzinfo (2.0.4)
175
171
  concurrent-ruby (~> 1.0)
176
- unicode-display_width (1.7.0)
172
+ unicode-display_width (2.0.0)
177
173
  websocket-driver (0.7.3)
178
174
  websocket-extensions (>= 0.1.0)
179
175
  websocket-extensions (0.1.5)
@@ -187,8 +183,8 @@ DEPENDENCIES
187
183
  pry
188
184
  pry-nav
189
185
  rake
190
- standardrb
186
+ standardrb (~> 1.0)
191
187
  stimulus_reflex!
192
188
 
193
189
  BUNDLED WITH
194
- 2.1.4
190
+ 2.2.4
data/LATEST ADDED
@@ -0,0 +1 @@
1
+ 3.4.1
data/README.md CHANGED
@@ -7,11 +7,11 @@
7
7
  <a href="https://www.npmjs.com/package/stimulus_reflex">
8
8
  <img alt="downloads" src="https://img.shields.io/npm/dm/stimulus_reflex.svg?color=blue" target="_blank" />
9
9
  </a>
10
- <a href="https://github.com/hopsoft/stimulus_reflex/blob/master/LICENSE">
10
+ <a href="https://github.com/stimulusreflex/stimulus_reflex/blob/master/LICENSE">
11
11
  <img alt="License: MIT" src="https://img.shields.io/badge/license-MIT-brightgreen.svg" target="_blank" />
12
12
  </a>
13
13
  <a href="http://blog.codinghorror.com/the-best-code-is-no-code-at-all/" target="_blank">
14
- <img alt="Lines of Code" src="https://img.shields.io/badge/lines_of_code-1726-brightgreen.svg?style=flat" />
14
+ <img alt="Lines of Code" src="https://img.shields.io/badge/lines_of_code-2190-brightgreen.svg?style=flat" />
15
15
  </a>
16
16
  <a href="https://docs.stimulusreflex.com/" target="_blank">
17
17
  <img alt="Documentation" src="https://img.shields.io/badge/documentation-yes-brightgreen.svg" />
@@ -30,14 +30,14 @@
30
30
  <a href="https://www.codacy.com/manual/hopsoft/stimulus_reflex/dashboard?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=hopsoft/stimulus_reflex&amp;utm_campaign=Badge_Grade" target="_blank">
31
31
  <img alt="Code Quality" src="https://app.codacy.com/project/badge/Grade/d1d72a7060f5467b8696884351cf477f"/>
32
32
  </a>
33
- <a target="_blank" rel="noopener noreferrer" href="https://github.com/hopsoft/stimulus_reflex/workflows/Prettier-Standard/badge.svg">
34
- <img src="https://github.com/hopsoft/stimulus_reflex/workflows/Prettier-Standard/badge.svg" alt="Prettier-Standard" style="max-width:100%;">
33
+ <a target="_blank" rel="noopener noreferrer" href="https://github.com/stimulusreflex/stimulus_reflex/workflows/Prettier-Standard/badge.svg">
34
+ <img src="https://github.com/stimulusreflex/stimulus_reflex/workflows/Prettier-Standard/badge.svg" alt="Prettier-Standard" style="max-width:100%;">
35
35
  </a>
36
- <a target="_blank" rel="noopener noreferrer" href="https://github.com/hopsoft/stimulus_reflex/workflows/StandardRB/badge.svg">
37
- <img src="https://github.com/hopsoft/stimulus_reflex/workflows/StandardRB/badge.svg" alt="StandardRB" style="max-width:100%;">
36
+ <a target="_blank" rel="noopener noreferrer" href="https://github.com/stimulusreflex/stimulus_reflex/workflows/StandardRB/badge.svg">
37
+ <img src="https://github.com/stimulusreflex/stimulus_reflex/workflows/StandardRB/badge.svg" alt="StandardRB" style="max-width:100%;">
38
38
  </a>
39
- <a target="_blank" rel="noopener noreferrer" href="https://github.com/hopsoft/stimulus_reflex/workflows/Tests/badge.svg">
40
- <img src="https://github.com/hopsoft/stimulus_reflex/workflows/Tests/badge.svg" alt="Tests">
39
+ <a target="_blank" rel="noopener noreferrer" href="https://github.com/stimulusreflex/stimulus_reflex/workflows/Tests/badge.svg">
40
+ <img src="https://github.com/stimulusreflex/stimulus_reflex/workflows/Tests/badge.svg" alt="Tests">
41
41
  </a>
42
42
  </p>
43
43
  </p>
@@ -75,7 +75,7 @@ This project strives to live up to the vision outlined in [The Rails Doctrine](h
75
75
 
76
76
  ## 👩‍👩‍👧 Discord Community
77
77
 
78
- Please join over 1000 of us on [Discord](https://discord.gg/XveN625) for support getting started, as well as active discussions around Rails, StimulusJS and CableReady.
78
+ Please join over 1000 of us on [Discord](https://discord.gg/stimulus-reflex) for support getting started, as well as active discussions around Rails, StimulusJS and CableReady.
79
79
 
80
80
  ![](https://img.shields.io/discord/629472241427415060)
81
81
 
@@ -104,7 +104,7 @@ and [Prettier-Standard](https://github.com/sheerun/prettier-standard) for JavaSc
104
104
 
105
105
  Please run `./bin/standardize` prior to submitting pull requests.
106
106
 
107
- View the [wiki](https://github.com/hopsoft/stimulus_reflex/wiki/Editor-Configuration) to see recommendations for configuring your editor to work best with the project.
107
+ View the [wiki](https://github.com/stimulusreflex/stimulus_reflex/wiki/Editor-Configuration) to see recommendations for configuring your editor to work best with the project.
108
108
 
109
109
  ## 📦 Releasing
110
110
 
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class StimulusReflex::Channel < StimulusReflex.configuration.parent_channel.constantize
4
+ attr_reader :reflex_data
5
+
4
6
  def stream_name
5
7
  ids = connection.identifiers.map { |identifier| send(identifier).try(:id) || send(identifier) }
6
8
  [
@@ -11,50 +13,25 @@ class StimulusReflex::Channel < StimulusReflex.configuration.parent_channel.cons
11
13
 
12
14
  def subscribed
13
15
  super
14
- fix_environment!
15
16
  stream_from stream_name
16
17
  end
17
18
 
18
19
  def receive(data)
19
- url = data["url"].to_s
20
- selectors = (data["selectors"] || []).select(&:present?)
21
- selectors = data["selectors"] = ["body"] if selectors.blank?
22
- target = data["target"].to_s
23
- reflex_name, method_name = target.split("#")
24
- reflex_name = reflex_name.camelize
25
- reflex_name = reflex_name.end_with?("Reflex") ? reflex_name : "#{reflex_name}Reflex"
26
- arguments = (data["args"] || []).map { |arg| object_with_indifferent_access arg }
27
- element = StimulusReflex::Element.new(data)
28
- permanent_attribute_name = data["permanentAttributeName"]
29
- form_data = Rack::Utils.parse_nested_query(data["formData"])
30
- params = form_data.deep_merge(data["params"] || {})
31
-
20
+ @reflex_data = StimulusReflex::ReflexData.new(data)
32
21
  begin
33
22
  begin
34
- reflex_class = reflex_name.constantize.tap { |klass| raise ArgumentError.new("#{reflex_name} is not a StimulusReflex::Reflex") unless is_reflex?(klass) }
35
- reflex = reflex_class.new(self,
36
- url: url,
37
- element: element,
38
- selectors: selectors,
39
- method_name: method_name,
40
- params: params,
41
- client_attributes: {
42
- reflex_id: data["reflexId"],
43
- xpath_controller: data["xpathController"],
44
- xpath_element: data["xpathElement"],
45
- reflex_controller: data["reflexController"],
46
- permanent_attribute_name: permanent_attribute_name
47
- })
48
- delegate_call_to_reflex reflex, method_name, arguments
49
- rescue => invoke_error
50
- message = exception_message_with_backtrace(invoke_error)
51
- body = "Reflex #{target} failed: #{message} [#{url}]"
23
+ reflex = StimulusReflex::ReflexFactory.create_reflex_from_data(self, @reflex_data)
24
+ delegate_call_to_reflex reflex
25
+ rescue => exception
26
+ error = exception_with_backtrace(exception)
27
+ error_message = "\e[31mReflex #{reflex_data.target} failed: #{error[:message]} [#{reflex_data.url}]\e[0m\n#{error[:stack]}"
52
28
 
53
29
  if reflex
54
- reflex.rescue_with_handler(invoke_error)
55
- reflex.broadcast_message subject: "error", body: body, data: data, error: invoke_error
30
+ reflex.rescue_with_handler(exception)
31
+ puts error_message
32
+ reflex.broadcast_message subject: "error", data: data, error: exception
56
33
  else
57
- puts "\e[31m#{body}\e[0m"
34
+ puts error_message
58
35
 
59
36
  if body.to_s.include? "No route matches"
60
37
  initializer_path = Rails.root.join("config", "initializers", "stimulus_reflex.rb")
@@ -84,43 +61,35 @@ class StimulusReflex::Channel < StimulusReflex.configuration.parent_channel.cons
84
61
  reflex.broadcast_message subject: "halted", data: data
85
62
  else
86
63
  begin
87
- reflex.broadcast(selectors, data)
88
- rescue => render_error
89
- reflex.rescue_with_handler(render_error)
90
- message = exception_message_with_backtrace(render_error)
91
- body = "Reflex failed to re-render: #{message} [#{url}]"
92
- reflex.broadcast_message subject: "error", body: body, data: data, error: render_error
93
- puts "\e[31m#{body}\e[0m"
64
+ reflex.broadcast(reflex_data.selectors, data)
65
+ rescue => exception
66
+ reflex.rescue_with_handler(exception)
67
+ error = exception_with_backtrace(exception)
68
+ reflex.broadcast_message subject: "error", data: data, error: exception
69
+ puts "\e[31mReflex failed to re-render: #{error[:message]} [#{reflex_data.url}]\e[0m\n#{error[:stack]}"
94
70
  end
95
71
  end
96
72
  ensure
97
73
  if reflex
98
74
  commit_session(reflex)
99
- reflex.logger.print
75
+ report_failed_basic_auth(reflex) if reflex.controller?
76
+ reflex.logger&.print
100
77
  end
101
78
  end
102
79
  end
103
80
 
104
81
  private
105
82
 
106
- def object_with_indifferent_access(object)
107
- return object.with_indifferent_access if object.respond_to?(:with_indifferent_access)
108
- object.map! { |obj| object_with_indifferent_access obj } if object.is_a?(Array)
109
- object
110
- end
111
-
112
- def is_reflex?(reflex_class)
113
- reflex_class.ancestors.include? StimulusReflex::Reflex
114
- end
115
-
116
- def delegate_call_to_reflex(reflex, method_name, arguments = [])
83
+ def delegate_call_to_reflex(reflex)
84
+ method_name = reflex_data.method_name
85
+ arguments = reflex_data.arguments
117
86
  method = reflex.method(method_name)
118
- required_params = method.parameters.select { |(kind, _)| kind == :req }
119
- optional_params = method.parameters.select { |(kind, _)| kind == :opt }
120
87
 
121
- if arguments.size == 0 && required_params.size == 0
88
+ policy = StimulusReflex::ReflexMethodInvocationPolicy.new(method, arguments)
89
+
90
+ if policy.no_arguments?
122
91
  reflex.process(method_name)
123
- elsif arguments.size >= required_params.size && arguments.size <= required_params.size + optional_params.size
92
+ elsif policy.arguments?
124
93
  reflex.process(method_name, *arguments)
125
94
  else
126
95
  raise ArgumentError.new("wrong number of arguments (given #{arguments.inspect}, expected #{required_params.inspect}, optional #{optional_params.inspect})")
@@ -130,18 +99,22 @@ class StimulusReflex::Channel < StimulusReflex.configuration.parent_channel.cons
130
99
  def commit_session(reflex)
131
100
  store = reflex.request.session.instance_variable_get("@by")
132
101
  store.commit_session reflex.request, reflex.controller.response
133
- rescue => e
134
- message = "Failed to commit session! #{exception_message_with_backtrace(e)}"
135
- puts "\e[31m#{message}\e[0m"
102
+ rescue => exception
103
+ error = exception_with_backtrace(exception)
104
+ puts "\e[31mFailed to commit session! #{error[:message]}\e[0m\n#{error[:backtrace]}"
136
105
  end
137
106
 
138
- def exception_message_with_backtrace(exception)
139
- "#{exception}\n#{exception.backtrace.first}"
107
+ def report_failed_basic_auth(reflex)
108
+ if reflex.controller.response.status == 401
109
+ puts "\e[31mReflex failed to process controller action \"#{reflex.controller.class}##{reflex.controller.action_name}\" due to HTTP basic auth. Consider adding \"unless: -> { @stimulus_reflex }\" to the before_action or method responible for authentication.\e[0m"
110
+ end
140
111
  end
141
112
 
142
- def fix_environment!
143
- ([ApplicationController] + ApplicationController.descendants).each do |controller|
144
- controller.renderer.instance_variable_set(:@env, connection.env.merge(controller.renderer.instance_variable_get(:@env)))
145
- end
113
+ def exception_with_backtrace(exception)
114
+ {
115
+ message: exception.to_s,
116
+ backtrace: exception.backtrace.first,
117
+ stack: exception.backtrace.join("\n")
118
+ }
146
119
  end
147
120
  end