stimulus_reflex 3.2.3 → 3.3.0.pre0

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: 93021d6229f977ed0e6c37e224b4f066902bce77275c002bd9c94088a936fdde
4
- data.tar.gz: d8a1673ffdbd790b003dec9123c4b01458132b645b0164d27e831632680d64b7
3
+ metadata.gz: 541ecc905c542c182d7abd3989aee4e0b7e2cbcc0708a741d292af0447f657b7
4
+ data.tar.gz: 47b2665deb288d4f63e99ab3a72256f7f39c095f57cc3c71444038f91ebdc170
5
5
  SHA512:
6
- metadata.gz: 6e69633c961caa4768e60b2e276018031f33383481c8acf4b4e851d8b05743faf39f72e89c562eca7bdef993e07a1987aa7d904a3978441eca0007e902bc7615
7
- data.tar.gz: 6147c6ec0f9024d86a2c9ad6d47cc9de90798d68be9f079077197f5ae0b999d38ba8ec913b9d44288ca795aa583e13cd8b1b8a0a53bae302a8cfa513ee73d295
6
+ metadata.gz: e63680e0a55bfff333385be622ab7bca8850846896befc9269b5d806a8baea6906cd0b29df90f65d586b6ad50f057575d8bce1b8fc3cf09c4a877c7692324209
7
+ data.tar.gz: 0e748c92fef73ad6038c334348eaf38e40e11dcad3edfab69c4e5f6bef6286d594d100357af95a69cbcaf69a04ee31ad579e562d7389f651a2e56af26eb114c2
@@ -2,7 +2,51 @@
2
2
 
3
3
  ## [Unreleased](https://github.com/hopsoft/stimulus_reflex/tree/HEAD)
4
4
 
5
- [Full Changelog](https://github.com/hopsoft/stimulus_reflex/compare/v3.2.2...HEAD)
5
+ [Full Changelog](https://github.com/hopsoft/stimulus_reflex/compare/v3.2.3...HEAD)
6
+
7
+ **Implemented enhancements:**
8
+
9
+ - Returns more helpful error message if Reflex doesn't exist [\#254](https://github.com/hopsoft/stimulus_reflex/pull/254) ([leastbad](https://github.com/leastbad))
10
+ - Update install.rake to handle Typescript [\#241](https://github.com/hopsoft/stimulus_reflex/pull/241) ([iv-mexx](https://github.com/iv-mexx))
11
+ - Morph Modes: page, selector and nothing [\#211](https://github.com/hopsoft/stimulus_reflex/pull/211) ([leastbad](https://github.com/leastbad))
12
+
13
+ **Fixed bugs:**
14
+
15
+ - Limit MutationObserver mutations [\#256](https://github.com/hopsoft/stimulus_reflex/pull/256) ([jasoncharnes](https://github.com/jasoncharnes))
16
+
17
+ **Closed issues:**
18
+
19
+ - beforeUpdate/updateSuccess/updateError functions deprecated? [\#255](https://github.com/hopsoft/stimulus_reflex/issues/255)
20
+ - Error handling will fail if reflex is not defined [\#253](https://github.com/hopsoft/stimulus_reflex/issues/253)
21
+ - Select with data-reflex in Firefox flickers [\#251](https://github.com/hopsoft/stimulus_reflex/issues/251)
22
+ - data-reflex-attributes vs data-reflex-dataset [\#237](https://github.com/hopsoft/stimulus_reflex/issues/237)
23
+ - Shorthand action notations corresponding to stimulus [\#233](https://github.com/hopsoft/stimulus_reflex/issues/233)
24
+ - Lifecycle methods only called for one reflex [\#225](https://github.com/hopsoft/stimulus_reflex/issues/225)
25
+ - Tweak the generator so we can specify reflex actions [\#219](https://github.com/hopsoft/stimulus_reflex/issues/219)
26
+ - Docs: Clarify forcing DOM update with authentication [\#123](https://github.com/hopsoft/stimulus_reflex/issues/123)
27
+ - ActiveJob integration example [\#112](https://github.com/hopsoft/stimulus_reflex/issues/112)
28
+
29
+ **Merged pull requests:**
30
+
31
+ - Prep for pre release of 3.3.0 [\#259](https://github.com/hopsoft/stimulus_reflex/pull/259) ([hopsoft](https://github.com/hopsoft))
32
+ - Fallback to first Stimulus controller in array [\#257](https://github.com/hopsoft/stimulus_reflex/pull/257) ([jasoncharnes](https://github.com/jasoncharnes))
33
+ - Fix cases where plural reflexes were unresolved [\#252](https://github.com/hopsoft/stimulus_reflex/pull/252) ([joshleblanc](https://github.com/joshleblanc))
34
+ - warn against collections of identical elements that trigger reflexes [\#250](https://github.com/hopsoft/stimulus_reflex/pull/250) ([leastbad](https://github.com/leastbad))
35
+ - always calls params to persist them into controller action [\#249](https://github.com/hopsoft/stimulus_reflex/pull/249) ([RolandStuder](https://github.com/RolandStuder))
36
+ - Update deployment.md [\#248](https://github.com/hopsoft/stimulus_reflex/pull/248) ([user073](https://github.com/user073))
37
+ - Update reflexes.md [\#247](https://github.com/hopsoft/stimulus_reflex/pull/247) ([user073](https://github.com/user073))
38
+ - Bump actionpack from 6.0.3.1 to 6.0.3.2 [\#245](https://github.com/hopsoft/stimulus_reflex/pull/245) ([dependabot[bot]](https://github.com/apps/dependabot))
39
+ - Bump rack from 2.2.2 to 2.2.3 [\#244](https://github.com/hopsoft/stimulus_reflex/pull/244) ([dependabot[bot]](https://github.com/apps/dependabot))
40
+ - Revert "Revert "Add instructions for pulling the user id out of session storage"" [\#240](https://github.com/hopsoft/stimulus_reflex/pull/240) ([leastbad](https://github.com/leastbad))
41
+ - Revert "Add instructions for pulling the user id out of session storage" [\#239](https://github.com/hopsoft/stimulus_reflex/pull/239) ([leastbad](https://github.com/leastbad))
42
+ - Add instructions for pulling the user id out of session storage [\#238](https://github.com/hopsoft/stimulus_reflex/pull/238) ([mtomov](https://github.com/mtomov))
43
+ - adds params documentation [\#230](https://github.com/hopsoft/stimulus_reflex/pull/230) ([RolandStuder](https://github.com/RolandStuder))
44
+ - Fix calling wrong controller lifecycle methods [\#226](https://github.com/hopsoft/stimulus_reflex/pull/226) ([davidalejandroaguilar](https://github.com/davidalejandroaguilar))
45
+ - Allow to pass reflex action names to reflex generator [\#224](https://github.com/hopsoft/stimulus_reflex/pull/224) ([marcoroth](https://github.com/marcoroth))
46
+
47
+ ## [v3.2.3](https://github.com/hopsoft/stimulus_reflex/tree/v3.2.3) (2020-06-15)
48
+
49
+ [Full Changelog](https://github.com/hopsoft/stimulus_reflex/compare/v3.2.2...v3.2.3)
6
50
 
7
51
  **Fixed bugs:**
8
52
 
@@ -285,7 +329,6 @@
285
329
  **Implemented enhancements:**
286
330
 
287
331
  - Explicit and implicit registering of the ActionCable consumer [\#116](https://github.com/hopsoft/stimulus_reflex/pull/116) ([hopsoft](https://github.com/hopsoft))
288
- - Add schema support [\#94](https://github.com/hopsoft/stimulus_reflex/pull/94) ([hopsoft](https://github.com/hopsoft))
289
332
 
290
333
  ## [v2.1.9](https://github.com/hopsoft/stimulus_reflex/tree/v2.1.9) (2020-02-20)
291
334
 
@@ -340,6 +383,7 @@
340
383
  **Implemented enhancements:**
341
384
 
342
385
  - Custom Stimulus schema breaks Reflex [\#91](https://github.com/hopsoft/stimulus_reflex/issues/91)
386
+ - Add schema support [\#94](https://github.com/hopsoft/stimulus_reflex/pull/94) ([hopsoft](https://github.com/hopsoft))
343
387
  - inherit stimulus schema [\#92](https://github.com/hopsoft/stimulus_reflex/pull/92) ([nickyvanurk](https://github.com/nickyvanurk))
344
388
  - Single source of truth [\#76](https://github.com/hopsoft/stimulus_reflex/pull/76) ([leastbad](https://github.com/leastbad))
345
389
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- stimulus_reflex (3.2.3)
4
+ stimulus_reflex (3.3.0.pre0)
5
5
  cable_ready (>= 4.1.2)
6
6
  nokogiri
7
7
  rack
@@ -10,56 +10,56 @@ PATH
10
10
  GEM
11
11
  remote: https://rubygems.org/
12
12
  specs:
13
- actioncable (6.0.3.1)
14
- actionpack (= 6.0.3.1)
13
+ actioncable (6.0.3.2)
14
+ actionpack (= 6.0.3.2)
15
15
  nio4r (~> 2.0)
16
16
  websocket-driver (>= 0.6.1)
17
- actionmailbox (6.0.3.1)
18
- actionpack (= 6.0.3.1)
19
- activejob (= 6.0.3.1)
20
- activerecord (= 6.0.3.1)
21
- activestorage (= 6.0.3.1)
22
- activesupport (= 6.0.3.1)
17
+ actionmailbox (6.0.3.2)
18
+ actionpack (= 6.0.3.2)
19
+ activejob (= 6.0.3.2)
20
+ activerecord (= 6.0.3.2)
21
+ activestorage (= 6.0.3.2)
22
+ activesupport (= 6.0.3.2)
23
23
  mail (>= 2.7.1)
24
- actionmailer (6.0.3.1)
25
- actionpack (= 6.0.3.1)
26
- actionview (= 6.0.3.1)
27
- activejob (= 6.0.3.1)
24
+ actionmailer (6.0.3.2)
25
+ actionpack (= 6.0.3.2)
26
+ actionview (= 6.0.3.2)
27
+ activejob (= 6.0.3.2)
28
28
  mail (~> 2.5, >= 2.5.4)
29
29
  rails-dom-testing (~> 2.0)
30
- actionpack (6.0.3.1)
31
- actionview (= 6.0.3.1)
32
- activesupport (= 6.0.3.1)
30
+ actionpack (6.0.3.2)
31
+ actionview (= 6.0.3.2)
32
+ activesupport (= 6.0.3.2)
33
33
  rack (~> 2.0, >= 2.0.8)
34
34
  rack-test (>= 0.6.3)
35
35
  rails-dom-testing (~> 2.0)
36
36
  rails-html-sanitizer (~> 1.0, >= 1.2.0)
37
- actiontext (6.0.3.1)
38
- actionpack (= 6.0.3.1)
39
- activerecord (= 6.0.3.1)
40
- activestorage (= 6.0.3.1)
41
- activesupport (= 6.0.3.1)
37
+ actiontext (6.0.3.2)
38
+ actionpack (= 6.0.3.2)
39
+ activerecord (= 6.0.3.2)
40
+ activestorage (= 6.0.3.2)
41
+ activesupport (= 6.0.3.2)
42
42
  nokogiri (>= 1.8.5)
43
- actionview (6.0.3.1)
44
- activesupport (= 6.0.3.1)
43
+ actionview (6.0.3.2)
44
+ activesupport (= 6.0.3.2)
45
45
  builder (~> 3.1)
46
46
  erubi (~> 1.4)
47
47
  rails-dom-testing (~> 2.0)
48
48
  rails-html-sanitizer (~> 1.1, >= 1.2.0)
49
- activejob (6.0.3.1)
50
- activesupport (= 6.0.3.1)
49
+ activejob (6.0.3.2)
50
+ activesupport (= 6.0.3.2)
51
51
  globalid (>= 0.3.6)
52
- activemodel (6.0.3.1)
53
- activesupport (= 6.0.3.1)
54
- activerecord (6.0.3.1)
55
- activemodel (= 6.0.3.1)
56
- activesupport (= 6.0.3.1)
57
- activestorage (6.0.3.1)
58
- actionpack (= 6.0.3.1)
59
- activejob (= 6.0.3.1)
60
- activerecord (= 6.0.3.1)
52
+ activemodel (6.0.3.2)
53
+ activesupport (= 6.0.3.2)
54
+ activerecord (6.0.3.2)
55
+ activemodel (= 6.0.3.2)
56
+ activesupport (= 6.0.3.2)
57
+ activestorage (6.0.3.2)
58
+ actionpack (= 6.0.3.2)
59
+ activejob (= 6.0.3.2)
60
+ activerecord (= 6.0.3.2)
61
61
  marcel (~> 0.3.1)
62
- activesupport (6.0.3.1)
62
+ activesupport (6.0.3.2)
63
63
  concurrent-ruby (~> 1.0, >= 1.0.2)
64
64
  i18n (>= 0.7, < 2)
65
65
  minitest (~> 5.1)
@@ -67,7 +67,7 @@ GEM
67
67
  zeitwerk (~> 2.2, >= 2.2.2)
68
68
  ast (2.4.1)
69
69
  builder (3.2.4)
70
- cable_ready (4.2.0)
70
+ cable_ready (4.3.0)
71
71
  rails (>= 5.2)
72
72
  coderay (1.1.3)
73
73
  concurrent-ruby (1.1.6)
@@ -77,7 +77,7 @@ GEM
77
77
  activesupport (>= 4.2.0)
78
78
  i18n (1.8.3)
79
79
  concurrent-ruby (~> 1.0)
80
- loofah (2.5.0)
80
+ loofah (2.6.0)
81
81
  crass (~> 1.0.2)
82
82
  nokogiri (>= 1.5.9)
83
83
  mail (2.7.1)
@@ -92,40 +92,40 @@ GEM
92
92
  nio4r (2.5.2)
93
93
  nokogiri (1.10.9)
94
94
  mini_portile2 (~> 2.4.0)
95
- parallel (1.19.1)
96
- parser (2.7.1.3)
97
- ast (~> 2.4.0)
95
+ parallel (1.19.2)
96
+ parser (2.7.1.4)
97
+ ast (~> 2.4.1)
98
98
  pry (0.12.2)
99
99
  coderay (~> 1.1.0)
100
100
  method_source (~> 0.9.0)
101
101
  pry-nav (0.3.0)
102
102
  pry (>= 0.9.10, < 0.13.0)
103
- rack (2.2.2)
103
+ rack (2.2.3)
104
104
  rack-test (1.1.0)
105
105
  rack (>= 1.0, < 3)
106
- rails (6.0.3.1)
107
- actioncable (= 6.0.3.1)
108
- actionmailbox (= 6.0.3.1)
109
- actionmailer (= 6.0.3.1)
110
- actionpack (= 6.0.3.1)
111
- actiontext (= 6.0.3.1)
112
- actionview (= 6.0.3.1)
113
- activejob (= 6.0.3.1)
114
- activemodel (= 6.0.3.1)
115
- activerecord (= 6.0.3.1)
116
- activestorage (= 6.0.3.1)
117
- activesupport (= 6.0.3.1)
106
+ rails (6.0.3.2)
107
+ actioncable (= 6.0.3.2)
108
+ actionmailbox (= 6.0.3.2)
109
+ actionmailer (= 6.0.3.2)
110
+ actionpack (= 6.0.3.2)
111
+ actiontext (= 6.0.3.2)
112
+ actionview (= 6.0.3.2)
113
+ activejob (= 6.0.3.2)
114
+ activemodel (= 6.0.3.2)
115
+ activerecord (= 6.0.3.2)
116
+ activestorage (= 6.0.3.2)
117
+ activesupport (= 6.0.3.2)
118
118
  bundler (>= 1.3.0)
119
- railties (= 6.0.3.1)
119
+ railties (= 6.0.3.2)
120
120
  sprockets-rails (>= 2.0.0)
121
121
  rails-dom-testing (2.0.3)
122
122
  activesupport (>= 4.2.0)
123
123
  nokogiri (>= 1.6)
124
124
  rails-html-sanitizer (1.3.0)
125
125
  loofah (~> 2.3)
126
- railties (6.0.3.1)
127
- actionpack (= 6.0.3.1)
128
- activesupport (= 6.0.3.1)
126
+ railties (6.0.3.2)
127
+ actionpack (= 6.0.3.2)
128
+ activesupport (= 6.0.3.2)
129
129
  method_source
130
130
  rake (>= 0.8.7)
131
131
  thor (>= 0.20.3, < 2.0)
@@ -142,7 +142,7 @@ GEM
142
142
  rubocop-ast (>= 0.0.3)
143
143
  ruby-progressbar (~> 1.7)
144
144
  unicode-display_width (>= 1.4.0, < 2.0)
145
- rubocop-ast (0.0.3)
145
+ rubocop-ast (0.1.0)
146
146
  parser (>= 2.7.0.1)
147
147
  rubocop-performance (1.6.1)
148
148
  rubocop (>= 0.71.0)
@@ -167,7 +167,7 @@ GEM
167
167
  websocket-driver (0.7.2)
168
168
  websocket-extensions (>= 0.1.0)
169
169
  websocket-extensions (0.1.5)
170
- zeitwerk (2.3.0)
170
+ zeitwerk (2.3.1)
171
171
 
172
172
  PLATFORMS
173
173
  ruby
@@ -2,7 +2,7 @@ Description:
2
2
  Generate boilerplate files to help get you up and running with StimulusReflex
3
3
 
4
4
  Example:
5
- rails generate stimulus_reflex User
5
+ rails generate stimulus_reflex User update do_stuff [options]
6
6
 
7
7
  This will create, but not overwrite the following files:
8
8
 
@@ -5,33 +5,22 @@ require "rails/generators"
5
5
  class StimulusReflexGenerator < Rails::Generators::NamedBase
6
6
  source_root File.expand_path("templates", __dir__)
7
7
 
8
- def initialize_application_reflexes
9
- copy_application_files if behavior == :invoke
10
- end
11
-
12
- def initialize_reflexes
13
- copy_reflex_files
14
- end
15
-
16
- def initialize_controllers
17
- copy_controller_files
18
- end
8
+ argument :name, type: :string, required: true, banner: "NAME"
9
+ argument :actions, type: :array, default: [], banner: "action action"
19
10
 
20
- private
11
+ def execute
12
+ actions.map!(&:underscore)
21
13
 
22
- CONTROLLER_BASE_PATH = "app/javascript/controllers"
23
- REFLEX_BASE_PATH = "app/reflexes"
14
+ copy_application_files if behavior == :invoke
24
15
 
25
- def copy_reflex_files
26
- template "custom_reflex.rb", File.join(REFLEX_BASE_PATH, "#{name.underscore}_reflex.rb")
16
+ template "app/reflexes/%file_name%_reflex.rb"
17
+ template "app/javascript/controllers/%file_name%_controller.js"
27
18
  end
28
19
 
29
- def copy_controller_files
30
- template "custom_controller.js", File.join(CONTROLLER_BASE_PATH, "#{name.underscore}_controller.js")
31
- end
20
+ private
32
21
 
33
22
  def copy_application_files
34
- template "application_reflex.rb", File.join(REFLEX_BASE_PATH, "application_reflex.rb")
35
- template "application_controller.js", File.join(CONTROLLER_BASE_PATH, "application_controller.js")
23
+ template "app/reflexes/application_reflex.rb"
24
+ template "app/javascript/controllers/application_controller.js"
36
25
  end
37
26
  end
@@ -22,6 +22,7 @@ export default class extends ApplicationController {
22
22
  * error - error message from the server
23
23
  */
24
24
 
25
+ <% if actions.empty? -%>
25
26
  // beforeUpdate(element, reflex) {
26
27
  // element.innerText = 'Updating...'
27
28
  // }
@@ -34,4 +35,23 @@ export default class extends ApplicationController {
34
35
  // console.error('updateError', error);
35
36
  // element.innerText = 'Update Failed!'
36
37
  // }
38
+ <% end -%>
39
+ <% actions.each do |action| -%>
40
+ // <%= "before_#{action}".camelize(:lower) %>(element, reflex) {
41
+ // console.log("before <%= action %>", element, reflex)
42
+ // }
43
+
44
+ // <%= "#{action}_success".camelize(:lower) %>(element, reflex) {
45
+ // console.log("<%= action %> success", element, reflex)
46
+ // }
47
+
48
+ // <%= "#{action}_error".camelize(:lower) %>(element, reflex, error) {
49
+ // console.error("<%= action %> error", element, reflex, error)
50
+ // }
51
+
52
+ // <%= "after_#{action}".camelize(:lower) %>(element, reflex, error) {
53
+ // console.log("after <%= action %>", element, reflex, error)
54
+ // }
55
+ <%= "\n" unless action == actions.last -%>
56
+ <% end -%>
37
57
  }
@@ -21,4 +21,10 @@ class <%= class_name %>Reflex < ApplicationReflex
21
21
  # end
22
22
  #
23
23
  # Learn more at: https://docs.stimulusreflex.com
24
+
25
+ <% actions.each do |action| -%>
26
+ def <%= action %>
27
+ end
28
+ <%= "\n" unless action == actions.last -%>
29
+ <% end -%>
24
30
  end
@@ -11,7 +11,12 @@ require "cable_ready"
11
11
  require "stimulus_reflex/version"
12
12
  require "stimulus_reflex/reflex"
13
13
  require "stimulus_reflex/element"
14
+ require "stimulus_reflex/broadcaster"
15
+ require "stimulus_reflex/morph_mode"
14
16
  require "stimulus_reflex/channel"
17
+ require "stimulus_reflex/morph_mode/nothing_morph_mode"
18
+ require "stimulus_reflex/morph_mode/page_morph_mode"
19
+ require "stimulus_reflex/morph_mode/selector_morph_mode"
15
20
  require "generators/stimulus_reflex_generator"
16
21
 
17
22
  module StimulusReflex
@@ -0,0 +1,53 @@
1
+ module StimulusReflex
2
+ module Broadcaster
3
+ include CableReady::Broadcaster
4
+
5
+ def render_page_and_broadcast_morph(reflex, selectors, data = {})
6
+ html = render_page(reflex)
7
+ broadcast_morphs selectors, data, html if html.present?
8
+ end
9
+
10
+ def render_page(reflex)
11
+ reflex.controller.process reflex.url_params[:action]
12
+ reflex.controller.response.body
13
+ end
14
+
15
+ def broadcast_morphs(selectors, data, html)
16
+ document = Nokogiri::HTML(html)
17
+ selectors = selectors.select { |s| document.css(s).present? }
18
+ selectors.each do |selector|
19
+ cable_ready[stream_name].morph(
20
+ selector: selector,
21
+ html: document.css(selector).inner_html,
22
+ children_only: true,
23
+ permanent_attribute_name: data["permanent_attribute_name"],
24
+ stimulus_reflex: data.merge({
25
+ last: selector == selectors.last,
26
+ morph_mode: "page"
27
+ })
28
+ )
29
+ end
30
+ cable_ready.broadcast
31
+ end
32
+
33
+ def broadcast_message(subject:, body: nil, data: {})
34
+ message = {
35
+ subject: subject,
36
+ body: body
37
+ }
38
+
39
+ logger.error "\e[31m#{body}\e[0m" if subject == "error"
40
+
41
+ data[:morph_mode] = "page"
42
+ data[:server_message] = message
43
+ data[:morph_mode] = "selector" if subject == "selector"
44
+ data[:morph_mode] = "nothing" if subject == "nothing"
45
+
46
+ cable_ready[stream_name].dispatch_event(
47
+ name: "stimulus-reflex:server-message",
48
+ detail: {stimulus_reflex: data}
49
+ )
50
+ cable_ready.broadcast
51
+ end
52
+ end
53
+ end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class StimulusReflex::Channel < ActionCable::Channel::Base
4
- include CableReady::Broadcaster
4
+ include StimulusReflex::Broadcaster
5
5
 
6
6
  def stream_name
7
7
  ids = connection.identifiers.map { |identifier| send(identifier).try(:id) || send(identifier) }
@@ -21,19 +21,20 @@ class StimulusReflex::Channel < ActionCable::Channel::Base
21
21
  selectors = data["selectors"] = ["body"] if selectors.blank?
22
22
  target = data["target"].to_s
23
23
  reflex_name, method_name = target.split("#")
24
- reflex_name = reflex_name.classify
24
+ reflex_name = reflex_name.camelize
25
25
  reflex_name = reflex_name.end_with?("Reflex") ? reflex_name : "#{reflex_name}Reflex"
26
26
  arguments = (data["args"] || []).map { |arg| object_with_indifferent_access arg }
27
27
  element = StimulusReflex::Element.new(data)
28
+ permanent_attribute_name = data["permanent_attribute_name"]
28
29
  params = data["params"] || {}
29
30
 
30
31
  begin
31
32
  begin
32
33
  reflex_class = reflex_name.constantize.tap { |klass| raise ArgumentError.new("#{reflex_name} is not a StimulusReflex::Reflex") unless is_reflex?(klass) }
33
- reflex = reflex_class.new(self, url: url, element: element, selectors: selectors, method_name: method_name, params: params)
34
+ reflex = reflex_class.new(self, url: url, element: element, selectors: selectors, method_name: method_name, permanent_attribute_name: permanent_attribute_name, params: params)
34
35
  delegate_call_to_reflex reflex, method_name, arguments
35
36
  rescue => invoke_error
36
- reflex.rescue_with_handler(invoke_error)
37
+ reflex&.rescue_with_handler(invoke_error)
37
38
  message = exception_message_with_backtrace(invoke_error)
38
39
  return broadcast_message subject: "error", body: "StimulusReflex::Channel Failed to invoke #{target}! #{url} #{message}", data: data
39
40
  end
@@ -42,7 +43,8 @@ class StimulusReflex::Channel < ActionCable::Channel::Base
42
43
  broadcast_message subject: "halted", data: data
43
44
  else
44
45
  begin
45
- render_page_and_broadcast_morph reflex, selectors, data
46
+ reflex.morph_mode.stream_name = stream_name
47
+ reflex.morph_mode.broadcast(reflex, selectors, data)
46
48
  rescue => render_error
47
49
  reflex.rescue_with_handler(render_error)
48
50
  message = exception_message_with_backtrace(render_error)
@@ -80,11 +82,6 @@ class StimulusReflex::Channel < ActionCable::Channel::Base
80
82
  end
81
83
  end
82
84
 
83
- def render_page_and_broadcast_morph(reflex, selectors, data = {})
84
- html = render_page(reflex)
85
- broadcast_morphs selectors, data, html if html.present?
86
- end
87
-
88
85
  def commit_session(reflex)
89
86
  store = reflex.request.session.instance_variable_get("@by")
90
87
  store.commit_session reflex.request, reflex.controller.response
@@ -93,41 +90,6 @@ class StimulusReflex::Channel < ActionCable::Channel::Base
93
90
  logger.error "\e[31m#{message}\e[0m"
94
91
  end
95
92
 
96
- def render_page(reflex)
97
- reflex.controller.process reflex.url_params[:action]
98
- reflex.controller.response.body
99
- end
100
-
101
- def broadcast_morphs(selectors, data, html)
102
- document = Nokogiri::HTML(html)
103
- selectors = selectors.select { |s| document.css(s).present? }
104
- selectors.each do |selector|
105
- cable_ready[stream_name].morph(
106
- selector: selector,
107
- html: document.css(selector).inner_html,
108
- children_only: true,
109
- permanent_attribute_name: data["permanent_attribute_name"],
110
- stimulus_reflex: data.merge(last: selector == selectors.last)
111
- )
112
- end
113
- cable_ready.broadcast
114
- end
115
-
116
- def broadcast_message(subject:, body: nil, data: {})
117
- message = {
118
- subject: subject,
119
- body: body
120
- }
121
-
122
- logger.error "\e[31m#{body}\e[0m" if subject == "error"
123
-
124
- cable_ready[stream_name].dispatch_event(
125
- name: "stimulus-reflex:server-message",
126
- detail: {stimulus_reflex: data.merge(server_message: message)}
127
- )
128
- cable_ready.broadcast
129
- end
130
-
131
93
  def exception_message_with_backtrace(exception)
132
94
  "#{exception} #{exception.backtrace.first}"
133
95
  end
@@ -0,0 +1,19 @@
1
+ module StimulusReflex
2
+ class MorphMode
3
+ include StimulusReflex::Broadcaster
4
+
5
+ attr_accessor :stream_name
6
+
7
+ def page?
8
+ false
9
+ end
10
+
11
+ def nothing?
12
+ false
13
+ end
14
+
15
+ def selector?
16
+ false
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+ module StimulusReflex
2
+ class NothingMorphMode < MorphMode
3
+ def broadcast(reflex, selectors, data)
4
+ broadcast_message subject: "nothing", data: data
5
+ end
6
+
7
+ def to_sym
8
+ :nothing
9
+ end
10
+
11
+ def nothing?
12
+ true
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module StimulusReflex
2
+ class PageMorphMode < MorphMode
3
+ def broadcast(reflex, selectors, data)
4
+ render_page_and_broadcast_morph reflex, selectors, data
5
+ end
6
+
7
+ def to_sym
8
+ :page
9
+ end
10
+
11
+ def page?
12
+ true
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module StimulusReflex
2
+ class SelectorMorphMode < MorphMode
3
+ def broadcast(reflex, selectors, data)
4
+ broadcast_message subject: "selector", data: data
5
+ end
6
+
7
+ def to_sym
8
+ :selector
9
+ end
10
+
11
+ def selector?
12
+ true
13
+ end
14
+ end
15
+ end
@@ -3,6 +3,7 @@
3
3
  class StimulusReflex::Reflex
4
4
  include ActiveSupport::Rescuable
5
5
  include ActiveSupport::Callbacks
6
+ include CableReady::Broadcaster
6
7
 
7
8
  define_callbacks :process, skip_after_callbacks_if_terminated: true
8
9
 
@@ -42,18 +43,21 @@ class StimulusReflex::Reflex
42
43
  end
43
44
  end
44
45
 
45
- attr_reader :channel, :url, :element, :selectors, :method_name
46
+ attr_reader :channel, :url, :element, :selectors, :method_name, :morph_mode, :permanent_attribute_name
46
47
 
47
48
  delegate :connection, to: :channel
48
49
  delegate :session, to: :request
49
50
 
50
- def initialize(channel, url: nil, element: nil, selectors: [], method_name: nil, params: {})
51
+ def initialize(channel, url: nil, element: nil, selectors: [], method_name: nil, stream_name: nil, permanent_attribute_name: nil, params: {})
51
52
  @channel = channel
52
53
  @url = url
53
54
  @element = element
54
55
  @selectors = selectors
55
56
  @method_name = method_name
56
57
  @params = params
58
+ @permanent_attribute_name = permanent_attribute_name
59
+ @morph_mode = StimulusReflex::PageMorphMode.new
60
+ self.params
57
61
  end
58
62
 
59
63
  def request
@@ -82,6 +86,36 @@ class StimulusReflex::Reflex
82
86
  end
83
87
  end
84
88
 
89
+ def morph(selectors, html = "")
90
+ case selectors
91
+ when :page
92
+ raise StandardError.new("Cannot call :page morph after :#{@morph_mode.to_sym} morph") unless @morph_mode.page?
93
+ when :nothing
94
+ raise StandardError.new("Cannot call :nothing morph after :selector morph") if @morph_mode.selector?
95
+ @morph_mode = StimulusReflex::NothingMorphMode.new
96
+ else
97
+ raise StandardError.new("Cannot call :selector morph after :nothing morph") if @morph_mode.nothing?
98
+ @morph_mode = StimulusReflex::SelectorMorphMode.new
99
+ if selectors.is_a?(Hash)
100
+ selectors.each do |selector, html|
101
+ enqueue_selector_broadcast selector, html
102
+ end
103
+ else
104
+ enqueue_selector_broadcast selectors, html
105
+ end
106
+ cable_ready.broadcast
107
+ end
108
+ end
109
+
110
+ def enqueue_selector_broadcast(selector, html)
111
+ cable_ready[channel.stream_name].morph(
112
+ selector: selector,
113
+ html: html,
114
+ children_only: true,
115
+ permanent_attribute_name: permanent_attribute_name
116
+ )
117
+ end
118
+
85
119
  def controller
86
120
  @controller ||= begin
87
121
  request.controller_class.new.tap do |c|
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StimulusReflex
4
- VERSION = "3.2.3"
4
+ VERSION = "3.3.0.pre0"
5
5
  end
@@ -11,11 +11,16 @@ namespace :stimulus_reflex do
11
11
  FileUtils.mkdir_p Rails.root.join("app/javascript/controllers"), verbose: true
12
12
  FileUtils.mkdir_p Rails.root.join("app/reflexes"), verbose: true
13
13
 
14
- filepath = if File.exist? Rails.root.join("app/javascript/controllers/index.js")
15
- Rails.root.join("app/javascript/controllers/index.js")
16
- else
17
- Rails.root.join("app/javascript/packs/application.js")
18
- end
14
+ filepath = %w[
15
+ app/javascript/controllers/index.js
16
+ app/javascript/controllers/index.ts
17
+ app/javascript/packs/application.js
18
+ app/javascript/packs/application.ts
19
+ ]
20
+ .select { |path| File.exist?(path) }
21
+ .map { |path| Rails.root.join(path) }
22
+ .first
23
+
19
24
  puts "Updating #{filepath}"
20
25
  lines = File.open(filepath, "r") { |f| f.readlines }
21
26
 
@@ -23,4 +23,27 @@ class StimulusReflexGeneratorTest < Rails::Generators::TestCase
23
23
  assert_file "app/reflexes/application_reflex.rb"
24
24
  assert_file "app/reflexes/posts_reflex.rb", /PostsReflex/
25
25
  end
26
+
27
+ test "creates reflex with given reflex actions" do
28
+ run_generator %w[User update do_stuff DoMoreStuff]
29
+ assert_file "app/reflexes/user_reflex.rb" do |reflex|
30
+ assert_instance_method :update, reflex
31
+ assert_instance_method :do_stuff, reflex
32
+ assert_instance_method :do_more_stuff, reflex
33
+ end
34
+ assert_file "app/javascript/controllers/user_controller.js" do |controller|
35
+ assert_match(/beforeUpdate/, controller)
36
+ assert_match(/updateSuccess/, controller)
37
+ assert_match(/updateError/, controller)
38
+ assert_match(/afterUpdate/, controller)
39
+ assert_match(/beforeDoStuff/, controller)
40
+ assert_match(/doStuffSuccess/, controller)
41
+ assert_match(/doStuffError/, controller)
42
+ assert_match(/afterDoStuff/, controller)
43
+ assert_match(/beforeDoMoreStuff/, controller)
44
+ assert_match(/doMoreStuffSuccess/, controller)
45
+ assert_match(/doMoreStuffError/, controller)
46
+ assert_match(/afterDoMoreStuff/, controller)
47
+ end
48
+ end
26
49
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class PostsReflex < ApplicationReflex
3
+ class DemoReflex < ApplicationReflex
4
4
  # Add Reflex methods in this file.
5
5
  #
6
6
  # All Reflex instances expose the following properties:
@@ -21,4 +21,5 @@ class PostsReflex < ApplicationReflex
21
21
  # end
22
22
  #
23
23
  # Learn more at: https://docs.stimulusreflex.com
24
+
24
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stimulus_reflex
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.3
4
+ version: 3.3.0.pre0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Hopkins
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-15 00:00:00.000000000 Z
11
+ date: 2020-07-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -136,7 +136,7 @@ dependencies:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
- description:
139
+ description:
140
140
  email:
141
141
  - natehop@gmail.com
142
142
  executables: []
@@ -157,13 +157,18 @@ files:
157
157
  - bin/standardize
158
158
  - lib/generators/USAGE
159
159
  - lib/generators/stimulus_reflex_generator.rb
160
- - lib/generators/templates/application_controller.js
161
- - lib/generators/templates/application_reflex.rb
162
- - lib/generators/templates/custom_controller.js
163
- - lib/generators/templates/custom_reflex.rb
160
+ - lib/generators/templates/app/javascript/controllers/%file_name%_controller.js.tt
161
+ - lib/generators/templates/app/javascript/controllers/application_controller.js.tt
162
+ - lib/generators/templates/app/reflexes/%file_name%_reflex.rb.tt
163
+ - lib/generators/templates/app/reflexes/application_reflex.rb.tt
164
164
  - lib/stimulus_reflex.rb
165
+ - lib/stimulus_reflex/broadcaster.rb
165
166
  - lib/stimulus_reflex/channel.rb
166
167
  - lib/stimulus_reflex/element.rb
168
+ - lib/stimulus_reflex/morph_mode.rb
169
+ - lib/stimulus_reflex/morph_mode/nothing_morph_mode.rb
170
+ - lib/stimulus_reflex/morph_mode/page_morph_mode.rb
171
+ - lib/stimulus_reflex/morph_mode/selector_morph_mode.rb
167
172
  - lib/stimulus_reflex/reflex.rb
168
173
  - lib/stimulus_reflex/version.rb
169
174
  - lib/tasks/stimulus_reflex/install.rake
@@ -172,7 +177,7 @@ files:
172
177
  - test/generators/stimulus_reflex_generator_test.rb
173
178
  - test/test_helper.rb
174
179
  - test/tmp/app/reflexes/application_reflex.rb
175
- - test/tmp/app/reflexes/posts_reflex.rb
180
+ - test/tmp/app/reflexes/demo_reflex.rb
176
181
  homepage: https://github.com/hopsoft/stimulus_reflex
177
182
  licenses:
178
183
  - MIT
@@ -197,16 +202,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
197
202
  version: '0'
198
203
  required_rubygems_version: !ruby/object:Gem::Requirement
199
204
  requirements:
200
- - - ">="
205
+ - - ">"
201
206
  - !ruby/object:Gem::Version
202
- version: '0'
207
+ version: 1.3.1
203
208
  requirements: []
204
209
  rubygems_version: 3.0.3
205
- signing_key:
210
+ signing_key:
206
211
  specification_version: 4
207
212
  summary: Build reactive applications with the Rails tooling you already know and love.
208
213
  test_files:
209
214
  - test/test_helper.rb
210
215
  - test/generators/stimulus_reflex_generator_test.rb
216
+ - test/tmp/app/reflexes/demo_reflex.rb
211
217
  - test/tmp/app/reflexes/application_reflex.rb
212
- - test/tmp/app/reflexes/posts_reflex.rb