stimulus_reflex 3.5.0.pre9 → 3.5.0.pre10

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 (103) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +0 -1
  3. data/Gemfile.lock +122 -127
  4. data/README.md +10 -16
  5. data/app/assets/javascripts/stimulus_reflex.js +710 -505
  6. data/app/assets/javascripts/stimulus_reflex.min.js +1 -1
  7. data/app/assets/javascripts/stimulus_reflex.min.js.map +1 -1
  8. data/app/assets/javascripts/stimulus_reflex.umd.js +660 -500
  9. data/app/assets/javascripts/stimulus_reflex.umd.min.js +660 -500
  10. data/app/assets/javascripts/stimulus_reflex.umd.min.js.map +1 -1
  11. data/app/channels/stimulus_reflex/channel.rb +9 -7
  12. data/bin/console +0 -2
  13. data/bin/standardize +2 -1
  14. data/lib/generators/stimulus_reflex/stimulus_reflex_generator.rb +72 -7
  15. data/lib/generators/stimulus_reflex/templates/app/controllers/examples_controller.rb.tt +9 -0
  16. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/consumer.js.tt +6 -0
  17. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.esbuild.tt +4 -0
  18. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.importmap.tt +2 -0
  19. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.shakapacker.tt +5 -0
  20. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.vite.tt +1 -0
  21. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.webpacker.tt +5 -0
  22. data/lib/generators/stimulus_reflex/templates/app/javascript/config/cable_ready.js.tt +4 -0
  23. data/lib/generators/stimulus_reflex/templates/app/javascript/config/index.js.tt +2 -0
  24. data/lib/generators/stimulus_reflex/templates/app/javascript/config/mrujs.js.tt +9 -0
  25. data/lib/generators/stimulus_reflex/templates/app/javascript/config/stimulus_reflex.js.tt +5 -0
  26. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/%file_name%_controller.js.tt +141 -0
  27. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/application.js.tt +11 -0
  28. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/application_controller.js.tt +74 -0
  29. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.esbuild.tt +7 -0
  30. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.importmap.tt +5 -0
  31. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.shakapacker.tt +5 -0
  32. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.vite.tt +5 -0
  33. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.webpacker.tt +5 -0
  34. data/{test/tmp/app/reflexes/user_reflex.rb → lib/generators/stimulus_reflex/templates/app/reflexes/%file_name%_reflex.rb.tt} +38 -9
  35. data/lib/generators/stimulus_reflex/templates/app/reflexes/application_reflex.rb.tt +27 -0
  36. data/lib/generators/stimulus_reflex/templates/app/views/examples/show.html.erb.tt +207 -0
  37. data/lib/generators/stimulus_reflex/templates/config/initializers/cable_ready.rb +22 -0
  38. data/lib/generators/stimulus_reflex/templates/config/initializers/stimulus_reflex.rb +18 -13
  39. data/lib/generators/stimulus_reflex/templates/esbuild.config.mjs.tt +94 -0
  40. data/lib/install/action_cable.rb +155 -0
  41. data/lib/install/broadcaster.rb +90 -0
  42. data/lib/install/bundle.rb +56 -0
  43. data/lib/install/compression.rb +41 -0
  44. data/lib/install/config.rb +87 -0
  45. data/lib/install/development.rb +110 -0
  46. data/lib/install/esbuild.rb +114 -0
  47. data/lib/install/example.rb +22 -0
  48. data/lib/install/importmap.rb +133 -0
  49. data/lib/install/initializers.rb +25 -0
  50. data/lib/install/mrujs.rb +133 -0
  51. data/lib/install/npm_packages.rb +25 -0
  52. data/lib/install/reflexes.rb +25 -0
  53. data/lib/install/shakapacker.rb +64 -0
  54. data/lib/install/spring.rb +54 -0
  55. data/lib/install/updatable.rb +34 -0
  56. data/lib/install/vite.rb +64 -0
  57. data/lib/install/webpacker.rb +90 -0
  58. data/lib/install/yarn.rb +55 -0
  59. data/lib/stimulus_reflex/broadcasters/broadcaster.rb +15 -8
  60. data/lib/stimulus_reflex/broadcasters/page_broadcaster.rb +7 -8
  61. data/lib/stimulus_reflex/broadcasters/selector_broadcaster.rb +10 -10
  62. data/lib/stimulus_reflex/broadcasters/update.rb +3 -0
  63. data/lib/stimulus_reflex/cable_readiness.rb +29 -0
  64. data/lib/stimulus_reflex/cable_ready_channels.rb +5 -5
  65. data/lib/stimulus_reflex/callbacks.rb +17 -1
  66. data/lib/stimulus_reflex/concern_enhancer.rb +6 -4
  67. data/lib/stimulus_reflex/configuration.rb +12 -2
  68. data/lib/stimulus_reflex/dataset.rb +11 -1
  69. data/lib/stimulus_reflex/engine.rb +20 -9
  70. data/lib/stimulus_reflex/html/document.rb +59 -0
  71. data/lib/stimulus_reflex/html/document_fragment.rb +13 -0
  72. data/lib/stimulus_reflex/importmap.rb +3 -0
  73. data/lib/stimulus_reflex/installer.rb +274 -0
  74. data/lib/stimulus_reflex/open_struct_fix.rb +2 -0
  75. data/lib/stimulus_reflex/reflex.rb +25 -25
  76. data/lib/stimulus_reflex/reflex_data.rb +15 -3
  77. data/lib/stimulus_reflex/reflex_factory.rb +4 -2
  78. data/lib/stimulus_reflex/request_parameters.rb +2 -0
  79. data/lib/stimulus_reflex/utils/logger.rb +10 -0
  80. data/lib/stimulus_reflex/utils/sanity_checker.rb +8 -48
  81. data/lib/stimulus_reflex/version.rb +1 -1
  82. data/lib/stimulus_reflex.rb +2 -0
  83. data/lib/tasks/stimulus_reflex/stimulus_reflex.rake +252 -0
  84. data/package.json +34 -28
  85. data/{rollup.config.js → rollup.config.mjs} +8 -7
  86. data/stimulus_reflex.gemspec +16 -19
  87. data/yarn.lock +1320 -742
  88. metadata +124 -77
  89. data/LATEST +0 -1
  90. data/lib/generators/stimulus_reflex/initializer_generator.rb +0 -14
  91. data/test/broadcasters/broadcaster_test.rb +0 -11
  92. data/test/broadcasters/broadcaster_test_case.rb +0 -39
  93. data/test/broadcasters/nothing_broadcaster_test.rb +0 -31
  94. data/test/broadcasters/page_broadcaster_test.rb +0 -79
  95. data/test/broadcasters/selector_broadcaster_test.rb +0 -173
  96. data/test/callbacks_test.rb +0 -652
  97. data/test/concern_enhancer_test.rb +0 -54
  98. data/test/element_test.rb +0 -254
  99. data/test/generators/stimulus_reflex_generator_test.rb +0 -58
  100. data/test/reflex_test.rb +0 -43
  101. data/test/test_helper.rb +0 -71
  102. data/test/tmp/app/reflexes/application_reflex.rb +0 -12
  103. data/yarn-error.log +0 -4964
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ spring_pattern = /^[^#]*gem ["']spring["']/
6
+
7
+ proceed = false
8
+ lines = gemfile_path.readlines
9
+
10
+ if lines.index { |line| line =~ spring_pattern }
11
+ proceed = if options.key? "spring"
12
+ options["spring"]
13
+ else
14
+ !no?("✨ Would you like to disable the spring gem? \nIt's been removed from Rails 7, and is the frequent culprit behind countless mystery bugs. (Y/n)")
15
+ end
16
+ else
17
+ say "⏩ Spring is not installed."
18
+ end
19
+
20
+ if proceed
21
+ spring_watcher_pattern = /^[^#]*gem ["']spring-watcher-listen["']/
22
+ bin_rails_pattern = /^[^#]*load File.expand_path\("spring", __dir__\)/
23
+
24
+ if (index = lines.index { |line| line =~ spring_pattern })
25
+ remove_gem :spring
26
+
27
+ bin_spring = Rails.root.join("bin/spring")
28
+ if bin_spring.exist?
29
+ run "bin/spring binstub --remove --all"
30
+ say "✅ Removed spring binstubs"
31
+ end
32
+
33
+ bin_rails = Rails.root.join("bin/rails")
34
+ bin_rails_content = bin_rails.readlines
35
+ if (index = bin_rails_content.index { |line| line =~ bin_rails_pattern })
36
+ backup(bin_rails) do
37
+ bin_rails_content[index] = "# #{bin_rails_content[index]}"
38
+ bin_rails.write bin_rails_content.join
39
+ end
40
+ say "✅ Removed spring from bin/rails"
41
+ end
42
+ create_file "tmp/stimulus_reflex_installer/kill_spring", verbose: false
43
+ else
44
+ say "✅ spring has been successfully removed"
45
+ end
46
+
47
+ if lines.index { |line| line =~ spring_watcher_pattern }
48
+ remove_gem "spring-watcher-listen"
49
+ end
50
+ else
51
+ say "⏩ Skipping."
52
+ end
53
+
54
+ complete_step :spring
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ if application_record_path.exist?
6
+ lines = application_record_path.readlines
7
+
8
+ if !lines.index { |line| line =~ /^\s*include CableReady::Updatable/ }
9
+ proceed = if options.key? "updatable"
10
+ options["updatable"]
11
+ else
12
+ !no?("✨ Include CableReady::Updatable in Active Record model classes? (Y/n)")
13
+ end
14
+
15
+ unless proceed
16
+ complete_step :updatable
17
+
18
+ puts "⏩ Skipping."
19
+ return
20
+ end
21
+
22
+ index = lines.index { |line| line.include?("class ApplicationRecord < ActiveRecord::Base") }
23
+ lines.insert index + 1, " include CableReady::Updatable\n"
24
+ application_record_path.write lines.join
25
+
26
+ say "✅ included CableReady::Updatable in ApplicationRecord"
27
+ else
28
+ say "⏩ CableReady::Updatable has already been included in Active Record model classes. Skipping."
29
+ end
30
+ else
31
+ say "⏩ ApplicationRecord doesn't exist. Skipping."
32
+ end
33
+
34
+ complete_step :updatable
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ return if pack_path_missing?
6
+
7
+ # verify that all critical dependencies are up to date; if not, queue for later
8
+ lines = package_json.readlines
9
+
10
+ if !lines.index { |line| line =~ /^\s*["']stimulus-vite-helpers["']: ["']\^3["']/ }
11
+ add_package "stimulus-vite-helpers@^3"
12
+ else
13
+ say "⏩ @stimulus-vite-helpers npm package is already present. Skipping."
14
+ end
15
+
16
+ step_path = "/app/javascript/controllers/"
17
+ application_controller_src = fetch(step_path, "application_controller.js.tt")
18
+ application_controller_path = controllers_path / "application_controller.js"
19
+ application_js_src = fetch(step_path, "application.js.tt")
20
+ application_js_path = controllers_path / "application.js"
21
+ index_src = fetch(step_path, "index.js.vite.tt")
22
+ index_path = controllers_path / "index.js"
23
+
24
+ # create entrypoint/controllers, as well as the index, application and application_controller
25
+ empty_directory controllers_path unless controllers_path.exist?
26
+
27
+ copy_file(application_controller_src, application_controller_path) unless application_controller_path.exist?
28
+ copy_file(application_js_src, application_js_path) unless application_js_path.exist?
29
+ copy_file(index_src, index_path) unless index_path.exist?
30
+
31
+ controllers_pattern = /import ['"](\.\.\/)?controllers['"]/
32
+ controllers_commented_pattern = /\s*\/\/\s*#{controllers_pattern}/
33
+ prefix = "..\/" # standard:disable Style/RedundantStringEscape
34
+
35
+ if pack.match?(controllers_pattern)
36
+ if pack.match?(controllers_commented_pattern)
37
+ proceed = if options.key? "uncomment"
38
+ options["uncomment"]
39
+ else
40
+ !no?("✨ Do you want to import your Stimulus controllers in application.js? (Y/n)")
41
+ end
42
+
43
+ if proceed
44
+ # uncomment_lines only works with Ruby comments 🙄
45
+ lines = pack_path.readlines
46
+ matches = lines.select { |line| line =~ controllers_commented_pattern }
47
+ lines[lines.index(matches.last).to_i] = "import \"#{prefix}controllers\"\n"
48
+ pack_path.write lines.join
49
+ say "✅ Uncommented Stimulus controllers import in #{friendly_pack_path}"
50
+ else
51
+ say "🤷 your Stimulus controllers are not being imported in your application.js. We trust that you have a reason for this."
52
+ end
53
+ else
54
+ say "⏩ Stimulus controllers are already being imported in #{friendly_pack_path}. Skipping."
55
+ end
56
+ else
57
+ lines = pack_path.readlines
58
+ matches = lines.select { |line| line =~ /^import / }
59
+ lines.insert lines.index(matches.last).to_i + 1, "import \"#{prefix}controllers\"\n"
60
+ pack_path.write lines.join
61
+ say "✅ Stimulus controllers imported in #{friendly_pack_path}"
62
+ end
63
+
64
+ complete_step :vite
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ return if pack_path_missing?
6
+
7
+ # verify that all critical dependencies are up to date; if not, queue for later
8
+ lines = package_json.readlines
9
+ if !lines.index { |line| line =~ /^\s*["']webpack["']: ["']\^4.46.0["']/ }
10
+ add_package "webpack@^4.46.0"
11
+ else
12
+ say "⏩ webpack npm package is already present. Skipping."
13
+ end
14
+
15
+ if !lines.index { |line| line =~ /^\s*["']webpack-cli["']: ["']\^3.3.12["']/ }
16
+ add_package "webpack-cli@^3.3.12"
17
+ else
18
+ say "⏩ webpack-cli npm package is already present. Skipping."
19
+ end
20
+
21
+ if !lines.index { |line| line =~ /^\s*["']@rails\/webpacker["']: ["']\^5.4.3["']/ }
22
+ add_package "@rails/webpacker@^5.4.3"
23
+ else
24
+ say "⏩ @rails/webpacker npm package is already present. Skipping."
25
+ end
26
+
27
+ if !lines.index { |line| line =~ /^\s*["']@hotwired\/stimulus-webpack-helpers["']: ["']\^1.0.1["']/ }
28
+ add_package "@hotwired/stimulus-webpack-helpers@^1.0.1"
29
+ else
30
+ say "⏩ @hotwired/stimulus-webpack-helpers npm package is already present. Skipping."
31
+ end
32
+
33
+ if !lines.index { |line| line =~ /^\s*["']webpack-dev-server["']: ["']\^3.11.3["']/ }
34
+ add_dev_package "webpack-dev-server@^3.11.3"
35
+ else
36
+ say "⏩ @webpack-dev-server is already present. Skipping."
37
+ end
38
+
39
+ step_path = "/app/javascript/controllers/"
40
+ application_controller_src = fetch(step_path, "application_controller.js.tt")
41
+ application_controller_path = controllers_path / "application_controller.js"
42
+ application_js_src = fetch(step_path, "application.js.tt")
43
+ application_js_path = controllers_path / "application.js"
44
+ index_src = fetch(step_path, "index.js.webpacker.tt")
45
+ index_path = controllers_path / "index.js"
46
+
47
+ # create entrypoint/controllers, as well as the index, application and application_controller
48
+ empty_directory controllers_path unless controllers_path.exist?
49
+
50
+ copy_file(application_controller_src, application_controller_path) unless application_controller_path.exist?
51
+ # webpacker 5.4 did not colloquially feature a controllers/application.js file
52
+ copy_file(application_js_src, application_js_path) unless application_js_path.exist?
53
+ copy_file(index_src, index_path) unless index_path.exist?
54
+
55
+ controllers_pattern = /import ['"]controllers['"]/
56
+ controllers_commented_pattern = /\s*\/\/\s*#{controllers_pattern}/
57
+
58
+ if pack.match?(controllers_pattern)
59
+ if pack.match?(controllers_commented_pattern)
60
+ proceed = if options.key? "uncomment"
61
+ options["uncomment"]
62
+ else
63
+ !no?("✨ Do you want to import your Stimulus controllers in application.js? (Y/n)")
64
+ end
65
+
66
+ if proceed
67
+ # uncomment_lines only works with Ruby comments 🙄
68
+ lines = pack_path.readlines
69
+ matches = lines.select { |line| line =~ controllers_commented_pattern }
70
+ lines[lines.index(matches.last).to_i] = "import \"controllers\"\n"
71
+ pack_path.write lines.join
72
+ say "✅ Uncommented Stimulus controllers import in #{friendly_pack_path}"
73
+ else
74
+ say "🤷 your Stimulus controllers are not being imported in your application.js. We trust that you have a reason for this."
75
+ end
76
+ else
77
+ say "⏩ Stimulus controllers are already being imported in #{friendly_pack_path}. Skipping."
78
+ end
79
+ else
80
+ lines = pack_path.readlines
81
+ matches = lines.select { |line| line =~ /^import / }
82
+ lines.insert lines.index(matches.last).to_i + 1, "import \"controllers\"\n"
83
+ pack_path.write lines.join
84
+ say "✅ Stimulus controllers imported in #{friendly_pack_path}"
85
+ end
86
+
87
+ # ensure webpacker is installed in the Gemfile
88
+ add_gem "webpacker@5.4.3"
89
+
90
+ complete_step :webpacker
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ if !package_json.exist?
6
+ say "⏩ No package.json file found. Skipping."
7
+
8
+ return
9
+ end
10
+
11
+ # run yarn install only when packages are waiting to be added or removed
12
+ add = package_list.exist? ? package_list.readlines.map(&:chomp) : []
13
+ dev = dev_package_list.exist? ? dev_package_list.readlines.map(&:chomp) : []
14
+ drop = drop_package_list.exist? ? drop_package_list.readlines.map(&:chomp) : []
15
+
16
+ json = JSON.parse(package_json.read)
17
+
18
+ if add.present? || dev.present? || drop.present?
19
+
20
+ add.each do |package|
21
+ matches = package.match(/(.+)@(.+)/)
22
+ name, version = matches[1], matches[2]
23
+ json["dependencies"] = {} unless json["dependencies"]
24
+ json["dependencies"][name] = version
25
+ end
26
+
27
+ dev.each do |package|
28
+ matches = package.match(/(.+)@(.+)/)
29
+ name, version = matches[1], matches[2]
30
+ json["devDependencies"] = {} unless json["devDependencies"]
31
+ json["devDependencies"][name] = version
32
+ end
33
+
34
+ drop.each do |package|
35
+ json["dependencies"].delete(package)
36
+ json["devDependencies"].delete(package)
37
+ end
38
+
39
+ package_json.write JSON.pretty_generate(json)
40
+
41
+ system "yarn install --silent"
42
+ else
43
+ say "⏩ No yarn depdencies to add or remove. Skipping."
44
+ end
45
+
46
+ if bundler == "esbuild" && json["scripts"]["build"] != "node esbuild.config.mjs"
47
+ json["scripts"]["build:default"] = json["scripts"]["build"]
48
+ json["scripts"]["build"] = "node esbuild.config.mjs"
49
+ package_json.write JSON.pretty_generate(json)
50
+ say "✅ Your yarn build script has been updated to use esbuild.config.mjs"
51
+ else
52
+ say "⏩ Your yarn build script is already setup. Skipping."
53
+ end
54
+
55
+ complete_step :yarn
@@ -2,16 +2,14 @@
2
2
 
3
3
  module StimulusReflex
4
4
  class Broadcaster
5
- attr_reader :reflex, :logger, :operations
6
- delegate :cable_ready, :permanent_attribute_name, :payload, to: :reflex
7
-
8
- DEFAULT_HTML_WITHOUT_FORMAT = Nokogiri::XML::Node::SaveOptions::DEFAULT_HTML &
9
- ~Nokogiri::XML::Node::SaveOptions::FORMAT
5
+ attr_reader :reflex, :cable_ready, :logger, :operations
6
+ delegate :permanent_attribute_name, :payload, to: :reflex
10
7
 
11
8
  def initialize(reflex)
12
9
  @reflex = reflex
13
10
  @logger = Rails.logger if defined?(Rails.logger)
14
11
  @operations = []
12
+ @cable_ready = StimulusReflex::CableReadyChannels.new(reflex)
15
13
  end
16
14
 
17
15
  def nothing?
@@ -26,7 +24,7 @@ module StimulusReflex
26
24
  false
27
25
  end
28
26
 
29
- def halted(data: {})
27
+ def broadcast_halt(data: {})
30
28
  operations << ["document", :dispatch_event]
31
29
  cable_ready.dispatch_event(
32
30
  name: "stimulus-reflex:morph-halted",
@@ -35,13 +33,22 @@ module StimulusReflex
35
33
  ).broadcast
36
34
  end
37
35
 
38
- def error(data: {}, body: nil)
36
+ def broadcast_forbid(data: {})
37
+ operations << ["document", :dispatch_event]
38
+ cable_ready.dispatch_event(
39
+ name: "stimulus-reflex:morph-forbidden",
40
+ payload: payload,
41
+ stimulus_reflex: data.merge(morph: to_sym)
42
+ ).broadcast
43
+ end
44
+
45
+ def broadcast_error(data: {}, error: nil)
39
46
  operations << ["document", :dispatch_event]
40
47
  cable_ready.dispatch_event(
41
48
  name: "stimulus-reflex:morph-error",
42
49
  payload: payload,
43
50
  stimulus_reflex: data.merge(morph: to_sym),
44
- body: body&.to_s
51
+ error: error&.to_s
45
52
  ).broadcast
46
53
  end
47
54
 
@@ -4,23 +4,22 @@ module StimulusReflex
4
4
  class PageBroadcaster < Broadcaster
5
5
  def broadcast(selectors, data)
6
6
  reflex.controller.process reflex.params[:action]
7
- page_html = reflex.controller.response.body
7
+ document = StimulusReflex::HTML::Document.new(reflex.controller.response.body)
8
8
 
9
- return unless page_html.present?
9
+ return if document.empty?
10
10
 
11
- document = Nokogiri::HTML.parse(page_html)
12
- selectors = selectors.select { |s| document.css(s).present? }
11
+ selectors = selectors.select { |s| document.match(s).present? }
13
12
  selectors.each do |selector|
14
- operations << [selector, :morph]
15
- html = document.css(selector).inner_html(save_with: Broadcaster::DEFAULT_HTML_WITHOUT_FORMAT)
16
- cable_ready.morph(
13
+ operations << [selector, StimulusReflex.config.morph_operation]
14
+ html = document.match(selector).inner_html
15
+ cable_ready.send StimulusReflex.config.morph_operation, {
17
16
  selector: selector,
18
17
  html: html,
19
18
  payload: payload,
20
19
  children_only: true,
21
20
  permanent_attribute_name: permanent_attribute_name,
22
21
  stimulus_reflex: data.merge(morph: to_sym)
23
- )
22
+ }
24
23
  end
25
24
  cable_ready.broadcast
26
25
  end
@@ -7,26 +7,26 @@ module StimulusReflex
7
7
  selectors, html = morph
8
8
  updates = create_update_collection(selectors, html)
9
9
  updates.each do |update|
10
- fragment = Nokogiri::HTML.fragment(update.html.to_s)
11
- match = fragment.at_css(update.selector)
10
+ document = StimulusReflex::HTML::DocumentFragment.new(update.html)
11
+ match = document.match(update.selector)
12
12
  if match.present?
13
- operations << [update.selector, :morph]
14
- cable_ready.morph(
13
+ operations << [update.selector, StimulusReflex.config.morph_operation]
14
+ cable_ready.send StimulusReflex.config.morph_operation, {
15
15
  selector: update.selector,
16
- html: match.inner_html(save_with: Broadcaster::DEFAULT_HTML_WITHOUT_FORMAT),
16
+ html: match.inner_html,
17
17
  payload: payload,
18
18
  children_only: true,
19
19
  permanent_attribute_name: permanent_attribute_name,
20
20
  stimulus_reflex: data.merge(morph: to_sym)
21
- )
21
+ }
22
22
  else
23
- operations << [update.selector, :inner_html]
24
- cable_ready.inner_html(
23
+ operations << [update.selector, StimulusReflex.config.replace_operation]
24
+ cable_ready.send StimulusReflex.config.replace_operation, {
25
25
  selector: update.selector,
26
- html: fragment.to_html,
26
+ html: update.html.to_s,
27
27
  payload: payload,
28
28
  stimulus_reflex: data.merge(morph: to_sym)
29
- )
29
+ }
30
30
  end
31
31
  end
32
32
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module StimulusReflex
2
4
  module Broadcasters
3
5
  class Update
@@ -14,6 +16,7 @@ module StimulusReflex
14
16
  end
15
17
 
16
18
  def html
19
+ return @value unless defined?(ActiveRecord)
17
20
  html = @reflex.render(@key) if @key.is_a?(ActiveRecord::Base) && @value.nil?
18
21
  html = @reflex.render_collection(@key) if @key.is_a?(ActiveRecord::Relation) && @value.nil?
19
22
  html || @value
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/concern"
4
+
5
+ module StimulusReflex
6
+ module CableReadiness
7
+ extend ActiveSupport::Concern
8
+
9
+ prepended do
10
+ attr_reader :cable_ready
11
+ end
12
+
13
+ def initialize(*args, **kwargs)
14
+ super(*args, **kwargs)
15
+
16
+ if is_a? CableReady::Broadcaster
17
+ message = <<~MSG
18
+
19
+ #{self.class.name} includes CableReady::Broadcaster, and you need to remove it.
20
+ Reflexes have their own CableReady interface. You can just assume that it's present.
21
+ See https://docs.stimulusreflex.com/guide/cableready#using-cableready-inside-a-reflex-action for more details.
22
+
23
+ MSG
24
+ raise TypeError.new(message.strip)
25
+ end
26
+ @cable_ready = StimulusReflex::CableReadyChannels.new(self)
27
+ end
28
+ end
29
+ end
@@ -4,9 +4,9 @@ module StimulusReflex
4
4
  class CableReadyChannels
5
5
  delegate :[], to: "cable_ready_channels"
6
6
 
7
- def initialize(stream_name, reflex_id)
8
- @stream_name = stream_name
9
- @reflex_id = reflex_id
7
+ def initialize(reflex)
8
+ @stream_name = reflex.stream_name
9
+ @id = reflex.id
10
10
  end
11
11
 
12
12
  def cable_ready_channels
@@ -20,9 +20,9 @@ module StimulusReflex
20
20
  def method_missing(name, *args)
21
21
  if stimulus_reflex_channel.respond_to?(name)
22
22
  if (options = args.find_index { |a| a.is_a? Hash })
23
- args[options][:reflex_id] = @reflex_id
23
+ args[options][:reflex_id] = @id
24
24
  elsif args.any?
25
- args << {reflex_id: @reflex_id}
25
+ args << {reflex_id: @id}
26
26
  end
27
27
  return stimulus_reflex_channel.public_send(name, *args)
28
28
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/concern"
2
4
 
3
5
  module StimulusReflex
@@ -6,7 +8,21 @@ module StimulusReflex
6
8
 
7
9
  included do
8
10
  include ActiveSupport::Callbacks
9
- define_callbacks :process, skip_after_callbacks_if_terminated: true
11
+ define_callbacks :process, skip_after_callbacks_if_terminated: true, terminator: ->(target, result_lambda) do
12
+ halted = true
13
+ forbidden = true
14
+ catch(:abort) do
15
+ catch(:forbidden) do
16
+ result_lambda.call
17
+ forbidden = false
18
+ end
19
+ halted = false
20
+ end
21
+ forbidden = false if halted == true
22
+ target.instance_variable_set(:@halted, halted)
23
+ target.instance_variable_set(:@forbidden, forbidden)
24
+ halted || forbidden
25
+ end
10
26
  end
11
27
 
12
28
  class_methods do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module StimulusReflex
2
4
  module ConcernEnhancer
3
5
  extend ActiveSupport::Concern
@@ -6,12 +8,12 @@ module StimulusReflex
6
8
  def method_missing(name, *args)
7
9
  case ancestors
8
10
  when ->(a) { !(a & [StimulusReflex::Reflex]).empty? }
9
- if (ActiveRecord::Base.public_methods + ActionController::Base.public_methods).include? name
11
+ if ((defined?(ActiveRecord) ? ActiveRecord::Base.public_methods : []) + ActionController::Base.public_methods).include? name
10
12
  nil
11
13
  else
12
14
  super
13
15
  end
14
- when ->(a) { !(a & [ActiveRecord::Base, ActionController::Base]).empty? }
16
+ when ->(a) { !(a & (defined?(ActiveRecord) ? [ActiveRecord::Base, ActionController::Base] : [ActionController::Base])).empty? }
15
17
  if StimulusReflex::Reflex.public_methods.include? name
16
18
  nil
17
19
  else
@@ -25,8 +27,8 @@ module StimulusReflex
25
27
  def respond_to_missing?(name, include_all = false)
26
28
  case ancestors
27
29
  when ->(a) { !(a & [StimulusReflex::Reflex]).empty? }
28
- (ActiveRecord::Base.public_methods + ActionController::Base.public_methods).include?(name) || super
29
- when ->(a) { !(a & [ActiveRecord::Base, ActionController::Base]).empty? }
30
+ ((defined?(ActiveRecord) ? ActiveRecord::Base.public_methods : []) + ActionController::Base.public_methods).include?(name) || super
31
+ when ->(a) { !(a & (defined?(ActiveRecord) ? [ActiveRecord::Base, ActionController::Base] : [ActionController::Base])).empty? }
30
32
  StimulusReflex::Reflex.public_methods.include?(name) || super
31
33
  else
32
34
  super
@@ -14,18 +14,28 @@ module StimulusReflex
14
14
  end
15
15
 
16
16
  class Configuration
17
- attr_accessor :on_failed_sanity_checks, :on_new_version_available, :on_missing_default_urls, :parent_channel, :logging, :logger, :middleware
17
+ attr_accessor :on_failed_sanity_checks, :on_missing_default_urls, :parent_channel, :logging, :logger, :middleware, :morph_operation, :replace_operation, :precompile_assets
18
18
 
19
19
  DEFAULT_LOGGING = proc { "[#{session_id}] #{operation_counter.magenta} #{reflex_info.green} -> #{selector.cyan} via #{mode} Morph (#{operation.yellow})" }
20
20
 
21
+ def on_new_version_available
22
+ warn "NOTICE: The `config.on_new_version_available` option has been removed from the StimulusReflex initializer. You can safely remove this option from your initializer."
23
+ end
24
+
25
+ def on_new_version_available=(_)
26
+ warn "NOTICE: The `config.on_new_version_available` option has been removed from the StimulusReflex initializer. You can safely remove this option from your initializer."
27
+ end
28
+
21
29
  def initialize
22
30
  @on_failed_sanity_checks = :exit
23
- @on_new_version_available = :ignore
24
31
  @on_missing_default_urls = :warn
25
32
  @parent_channel = "ApplicationCable::Channel"
26
33
  @logging = DEFAULT_LOGGING
27
34
  @logger = Rails.logger
28
35
  @middleware = ActionDispatch::MiddlewareStack.new
36
+ @morph_operation = :morph
37
+ @replace_operation = :inner_html
38
+ @precompile_assets = true
29
39
  end
30
40
  end
31
41
  end
@@ -25,10 +25,20 @@ class StimulusReflex::Dataset < OpenStruct
25
25
  end
26
26
 
27
27
  def boolean
28
- @boolean ||= ->(accessor) { ActiveModel::Type::Boolean.new.cast(self[accessor]) || self[accessor].blank? }
28
+ @boolean ||= ->(accessor) { cast_boolean(self[accessor]) || self[accessor].blank? }
29
29
  end
30
30
 
31
31
  def numeric
32
32
  @numeric ||= ->(accessor) { Float(self[accessor]) }
33
33
  end
34
+
35
+ private
36
+
37
+ def cast_boolean(value)
38
+ ((value == "") ? nil : !false_values.include?(value)) unless value.nil?
39
+ end
40
+
41
+ def false_values
42
+ @false_values ||= [false, 0, "0", "f", "F", "false", "FALSE", "off", "OFF"].to_set
43
+ end
34
44
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "rails/engine"
2
4
 
3
5
  module StimulusReflex
@@ -6,16 +8,25 @@ module StimulusReflex
6
8
  SanityChecker.check! unless Rails.env.production?
7
9
  end
8
10
 
11
+ # If you don't want to precompile StimulusReflex's assets (eg. because you're using webpack),
12
+ # you can do this in an initializer:
13
+ #
14
+ # config.after_initialize do
15
+ # config.assets.precompile -= StimulusReflex::Engine::PRECOMPILE_ASSETS
16
+ # end
17
+ #
18
+ PRECOMPILE_ASSETS = %w[
19
+ stimulus_reflex.js
20
+ stimulus_reflex.min.js
21
+ stimulus_reflex.min.js.map
22
+ stimulus_reflex.umd.js
23
+ stimulus_reflex.umd.min.js
24
+ stimulus_reflex.umd.min.js.map
25
+ ]
26
+
9
27
  initializer "stimulus_reflex.assets" do |app|
10
- if app.config.respond_to?(:assets)
11
- app.config.assets.precompile += %w[
12
- stimulus_reflex.js
13
- stimulus_reflex.min.js
14
- stimulus_reflex.min.js.map
15
- stimulus_reflex.umd.js
16
- stimulus_reflex.umd.min.js
17
- stimulus_reflex.umd.min.js.map
18
- ]
28
+ if app.config.respond_to?(:assets) && StimulusReflex.config.precompile_assets
29
+ app.config.assets.precompile += PRECOMPILE_ASSETS
19
30
  end
20
31
  end
21
32