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
@@ -6,44 +6,49 @@
6
6
  # ActionCable.server.config.logger = Logger.new(nil)
7
7
 
8
8
  StimulusReflex.configure do |config|
9
- # Enable/disable exiting / warning when the sanity checks fail options:
9
+ # Enable/disable exiting / warning when the sanity checks fail:
10
10
  # `:exit` or `:warn` or `:ignore`
11
-
11
+ #
12
12
  # config.on_failed_sanity_checks = :exit
13
13
 
14
- # Enable/disable exiting / warning when there's a new StimulusReflex release
15
- # `:exit` or `:warn` or `:ignore`
16
-
17
- # config.on_new_version_available = :ignore
18
-
19
14
  # Enable/disable exiting / warning when there is no default URLs specified in environment config
20
15
  # `:warn` or `:ignore`
21
-
16
+ #
22
17
  # config.on_missing_default_urls = :warn
23
18
 
24
- # Override the parent class that the StimulusReflex ActionCable channel inherits from
19
+ # Enable/disable assets compilation
20
+ # `true` or `false`
21
+ #
22
+ # config.precompile_assets = true
25
23
 
24
+ # Override the CableReady operation used for morphing and replacing content
25
+ #
26
+ # config.morph_operation = :morph
27
+ # config.replace_operation = :inner_html
28
+
29
+ # Override the parent class that the StimulusReflex ActionCable channel inherits from
30
+ #
26
31
  # config.parent_channel = "ApplicationCable::Channel"
27
32
 
28
33
  # Override the logger that the StimulusReflex uses; default is Rails' logger
29
34
  # eg. Logger.new(RAILS_ROOT + "/log/reflex.log")
30
-
35
+ #
31
36
  # config.logger = Rails.logger
32
37
 
33
38
  # Customize server-side Reflex logging format, with optional colorization:
34
- # Available tokens: session_id, session_id_full, reflex_info, operation, reflex_id, reflex_id_full, mode, selector, operation_counter, connection_id, connection_id_full, timestamp
39
+ # Available tokens: session_id, session_id_full, reflex_info, operation, id, id_full, mode, selector, operation_counter, connection_id, connection_id_full, timestamp
35
40
  # Available colors: red, green, yellow, blue, magenta, cyan, white
36
41
  # You can also use attributes from your ActionCable Connection's identifiers that resolve to valid ActiveRecord models
37
42
  # eg. if your connection is `identified_by :current_user` and your User model has an email attribute, you can access r.email (it will display `-` if the user isn't logged in)
38
43
  # Learn more at: https://docs.stimulusreflex.com/appendices/troubleshooting#stimulusreflex-logging
39
-
44
+ #
40
45
  # config.logging = proc { "[#{session_id}] #{operation_counter.magenta} #{reflex_info.green} -> #{selector.cyan} via #{mode} Morph (#{operation.yellow})" }
41
46
 
42
47
  # Optimized for speed, StimulusReflex doesn't enable Rack middleware by default.
43
48
  # If you are using Page Morphs and your app uses Rack middleware to rewrite part of the request path, you must enable those middleware modules in StimulusReflex.
44
49
  #
45
50
  # Learn more about registering Rack middleware in Rails here: https://guides.rubyonrails.org/rails_on_rack.html#configuring-middleware-stack
46
-
51
+ #
47
52
  # config.middleware.use FirstRackMiddleware
48
53
  # config.middleware.use SecondRackMiddleware
49
54
  end
@@ -0,0 +1,94 @@
1
+ #!/usr/bin/env node
2
+
3
+ // Esbuild is configured with 3 modes:
4
+ //
5
+ // `yarn build` - Build JavaScript and exit
6
+ // `yarn build --watch` - Rebuild JavaScript on change
7
+ // `yarn build --reload` - Reloads page when views, JavaScript, or stylesheets change
8
+ //
9
+ // Minify is enabled when "RAILS_ENV=production"
10
+ // Sourcemaps are enabled in non-production environments
11
+
12
+ import * as esbuild from "esbuild"
13
+ import path from "path"
14
+ import rails from "esbuild-rails"
15
+ import chokidar from "chokidar"
16
+ import http from "http"
17
+ import { setTimeout } from "timers/promises"
18
+
19
+ const clients = []
20
+
21
+ const entryPoints = [
22
+ "application.js"
23
+ ]
24
+
25
+ const watchDirectories = [
26
+ "./app/javascript/**/*.js",
27
+ "./app/views/**/*.html.erb",
28
+ "./app/assets/builds/**/*.css", // Wait for cssbundling changes
29
+ ]
30
+
31
+ const config = {
32
+ absWorkingDir: path.join(process.cwd(), "app/javascript"),
33
+ bundle: true,
34
+ entryPoints: entryPoints,
35
+ minify: process.env.RAILS_ENV == "production",
36
+ outdir: path.join(process.cwd(), "app/assets/builds"),
37
+ plugins: [rails()],
38
+ sourcemap: process.env.RAILS_ENV != "production"
39
+ }
40
+
41
+ async function buildAndReload() {
42
+ // Foreman & Overmind assign a separate PORT for each process
43
+ const port = parseInt(process.env.PORT)
44
+ const context = await esbuild.context({
45
+ ...config,
46
+ banner: {
47
+ js: ` (() => new EventSource("http://localhost:${port}").onmessage = () => location.reload())();`,
48
+ }
49
+ })
50
+
51
+ // Reload uses an HTTP server as an even stream to reload the browser
52
+ http.createServer((req, res) => {
53
+ return clients.push(
54
+ res.writeHead(200, {
55
+ "Content-Type": "text/event-stream",
56
+ "Cache-Control": "no-cache",
57
+ "Access-Control-Allow-Origin": "*",
58
+ Connection: "keep-alive",
59
+ })
60
+ )
61
+ }).listen(port)
62
+
63
+ await context.rebuild()
64
+ console.log("[reload] initial build succeeded")
65
+
66
+ let ready = false
67
+ chokidar.watch(watchDirectories).on("ready", () => {
68
+ console.log("[reload] ready")
69
+ ready = true
70
+ }).on("all", async (event, path) => {
71
+ if (ready === false) return
72
+
73
+ if (path.includes("javascript")) {
74
+ try {
75
+ await setTimeout(20)
76
+ await context.rebuild()
77
+ console.log("[reload] build succeeded")
78
+ } catch (error) {
79
+ console.error("[reload] build failed", error)
80
+ }
81
+ }
82
+ clients.forEach((res) => res.write("data: update\n\n"))
83
+ clients.length = 0
84
+ })
85
+ }
86
+
87
+ if (process.argv.includes("--reload")) {
88
+ buildAndReload()
89
+ } else if (process.argv.includes("--watch")) {
90
+ let context = await esbuild.context({...config, logLevel: 'info'})
91
+ context.watch()
92
+ } else {
93
+ esbuild.build(config)
94
+ }
@@ -0,0 +1,155 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ # verify that Action Cable is installed
6
+ if defined?(ActionCable::Engine)
7
+ say "⏩ ActionCable::Engine is already loaded and in scope. Skipping"
8
+ else
9
+ halt "ActionCable::Engine is not loaded, please add or uncomment `require \"action_cable/engine\"` to your `config/application.rb`"
10
+ return
11
+ end
12
+
13
+ return if pack_path_missing?
14
+
15
+ # verify that the Action Cable pubsub config is created
16
+ cable_config = Rails.root.join("config/cable.yml")
17
+
18
+ if cable_config.exist?
19
+ say "⏩ config/cable.yml is already present. Skipping."
20
+ else
21
+ inside "config" do
22
+ template "cable.yml"
23
+ end
24
+
25
+ say "✅ Created config/cable.yml"
26
+ end
27
+
28
+ # verify that the Action Cable pubsub is set to use redis in development
29
+ yaml = YAML.safe_load(cable_config.read)
30
+ app_name = Rails.application.class.module_parent.name.underscore
31
+
32
+ if yaml["development"]["adapter"] == "redis"
33
+ say "⏩ config/cable.yml is already configured to use the redis adapter in development. Skipping."
34
+ elsif yaml["development"]["adapter"] == "async"
35
+ yaml["development"] = {
36
+ "adapter" => "redis",
37
+ "url" => "<%= ENV.fetch(\"REDIS_URL\") { \"redis://localhost:6379/1\" } %>",
38
+ "channel_prefix" => "#{app_name}_development"
39
+ }
40
+ backup(cable_config) do
41
+ cable_config.write(yaml.to_yaml)
42
+ end
43
+ say "✅ config/cable.yml was updated to use the redis adapter in development"
44
+ else
45
+ say "🤷 config/cable.yml should use the redis adapter - or something like it - in development. You have something else specified, and we trust that you know what you're doing."
46
+ end
47
+
48
+ if gemfile.match?(/gem ['"]redis['"]/)
49
+ say "⏩ redis gem is already present in Gemfile. Skipping."
50
+ elsif Rails::VERSION::MAJOR >= 7
51
+ add_gem "redis@~> 5"
52
+ else
53
+ add_gem "redis@~> 4"
54
+ end
55
+
56
+ # install action-cable-redis-backport gem if using Action Cable < 7.1
57
+ unless ActionCable::VERSION::MAJOR >= 7 && ActionCable::VERSION::MINOR >= 1
58
+ if gemfile.match?(/gem ['"]action-cable-redis-backport['"]/)
59
+ say "⏩ action-cable-redis-backport gem is already present in Gemfile. Skipping."
60
+ else
61
+ add_gem "action-cable-redis-backport@~> 1"
62
+ end
63
+ end
64
+
65
+ # verify that the Action Cable channels folder and consumer class is available
66
+ step_path = "/app/javascript/channels/"
67
+ channels_path = Rails.root.join(entrypoint, "channels")
68
+ consumer_src = fetch(step_path, "consumer.js.tt")
69
+ consumer_path = channels_path / "consumer.js"
70
+ index_src = fetch(step_path, "index.js.#{bundler}.tt")
71
+ index_path = channels_path / "index.js"
72
+ friendly_index_path = index_path.relative_path_from(Rails.root).to_s
73
+
74
+ empty_directory channels_path unless channels_path.exist?
75
+
76
+ copy_file(consumer_src, consumer_path) unless consumer_path.exist?
77
+
78
+ if index_path.exist?
79
+ if index_path.read == index_src.read
80
+ say "⏩ #{friendly_index_path} is already present. Skipping."
81
+ else
82
+ backup(index_path) do
83
+ copy_file(index_src, index_path, verbose: false)
84
+ end
85
+ say "✅ #{friendly_index_path} has been updated"
86
+ end
87
+ else
88
+ copy_file(index_src, index_path)
89
+ say "✅ #{friendly_index_path} has been created"
90
+ end
91
+
92
+ # import Action Cable channels into application pack
93
+ channels_pattern = /import ['"](\.\.\/|\.\/)?channels['"]/
94
+ channels_commented_pattern = /\s*\/\/\s*#{channels_pattern}/
95
+ channel_import = "import \"#{prefix}channels\"\n"
96
+
97
+ if pack.match?(channels_pattern)
98
+ if pack.match?(channels_commented_pattern)
99
+ proceed = if options.key? "uncomment"
100
+ options["uncomment"]
101
+ else
102
+ !no?("✨ Action Cable seems to be commented out in your application.js. Do you want to uncomment it? (Y/n)")
103
+ end
104
+
105
+ if proceed
106
+ # uncomment_lines only works with Ruby comments 🙄
107
+ lines = pack_path.readlines
108
+ matches = lines.select { |line| line =~ channels_commented_pattern }
109
+ lines[lines.index(matches.last).to_i] = channel_import
110
+ pack_path.write lines.join
111
+ say "✅ Uncommented channels import in #{friendly_pack_path}"
112
+ else
113
+ say "🤷 your Action Cable channels are not being imported in your application.js. We trust that you have a reason for this."
114
+ end
115
+ else
116
+ say "⏩ channels are already being imported in #{friendly_pack_path}. Skipping."
117
+ end
118
+ else
119
+ lines = pack_path.readlines
120
+ matches = lines.select { |line| line =~ /^import / }
121
+ lines.insert lines.index(matches.last).to_i + 1, channel_import
122
+ pack_path.write lines.join
123
+ say "✅ channels imported in #{friendly_pack_path}"
124
+ end
125
+
126
+ # create working copy of Action Cable initializer in tmp
127
+ if action_cable_initializer_path.exist?
128
+ FileUtils.cp(action_cable_initializer_path, action_cable_initializer_working_path)
129
+
130
+ say "⏩ Action Cable initializer already exists. Skipping"
131
+ else
132
+ # create Action Cable initializer if it doesn't already exist
133
+ create_file(action_cable_initializer_working_path, verbose: false) do
134
+ <<~RUBY
135
+ # frozen_string_literal: true
136
+
137
+ RUBY
138
+ end
139
+ say "✅ Action Cable initializer created"
140
+ end
141
+
142
+ # silence notoriously chatty Action Cable logs
143
+ if action_cable_initializer_working_path.read.match?(/^[^#]*ActionCable.server.config.logger/)
144
+ say "⏩ Action Cable logger is already being silenced. Skipping"
145
+ else
146
+ append_file(action_cable_initializer_working_path, verbose: false) do
147
+ <<~RUBY
148
+ ActionCable.server.config.logger = Logger.new(nil)
149
+
150
+ RUBY
151
+ end
152
+ say "✅ Action Cable logger silenced for performance and legibility"
153
+ end
154
+
155
+ complete_step :action_cable
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ def needs_broadcaster?(path)
6
+ return false unless path.exist?
7
+
8
+ !path.readlines.index { |line| line =~ /^\s*include CableReady::Broadcaster/ }
9
+ end
10
+
11
+ channel_path = Rails.root.join("app/channels/application_cable/channel.rb")
12
+ controller_path = Rails.root.join("app/controllers/application_controller.rb")
13
+ job_path = Rails.root.join("app/jobs/application_job.rb")
14
+ model_path = Rails.root.join(application_record_path)
15
+
16
+ include_in_channel = needs_broadcaster?(channel_path)
17
+ include_in_controller = needs_broadcaster?(controller_path)
18
+ include_in_job = needs_broadcaster?(job_path)
19
+ include_in_model = needs_broadcaster?(model_path)
20
+
21
+ proceed = [include_in_channel, include_in_controller, include_in_job, include_in_model].reduce(:|)
22
+
23
+ unless proceed
24
+ complete_step :broadcaster
25
+
26
+ puts "⏩ CableReady::Broadcaster already included in all files. Skipping."
27
+ return
28
+ end
29
+
30
+ proceed = if options.key? "broadcaster"
31
+ options["broadcaster"]
32
+ else
33
+ !no?("✨ Make CableReady::Broadcaster available to channels, controllers, jobs and models? (Y/n)")
34
+ end
35
+
36
+ unless proceed
37
+ complete_step :broadcaster
38
+
39
+ puts "⏩ Skipping."
40
+ return
41
+ end
42
+
43
+ broadcaster_include = "\n include CableReady::Broadcaster\n"
44
+
45
+ # include CableReady::Broadcaster in Action Cable Channel classes
46
+ if include_in_channel
47
+ backup(channel_path) do
48
+ inject_into_file channel_path, broadcaster_include, after: /class (ApplicationCable::)?Channel < ActionCable::Channel::Base/, verbose: false
49
+ end
50
+
51
+ puts "✅ include CableReady::Broadcaster in ApplicationCable::Channel"
52
+ else
53
+ puts "⏩ Not including CableReady::Broadcaster in ApplicationCable::Channel channels. Skipping."
54
+ end
55
+
56
+ # include CableReady::Broadcaster in Action Controller classes
57
+ if include_in_controller
58
+ backup(controller_path) do
59
+ inject_into_class controller_path, "ApplicationController", broadcaster_include, verbose: false
60
+ end
61
+
62
+ puts "✅ include CableReady::Broadcaster in ApplicationController"
63
+ else
64
+ puts "⏩ Not including CableReady::Broadcaster in ApplicationController. Skipping."
65
+ end
66
+
67
+ # include CableReady::Broadcaster in Active Job classes, if present
68
+
69
+ if include_in_job
70
+ backup(job_path) do
71
+ inject_into_class job_path, "ApplicationJob", broadcaster_include, verbose: false
72
+ end
73
+
74
+ puts "✅ include CableReady::Broadcaster in ApplicationJob"
75
+ else
76
+ puts "⏩ Not including CableReady::Broadcaster in ApplicationJob. Skipping."
77
+ end
78
+
79
+ # include CableReady::Broadcaster in Active Record model classes
80
+ if include_in_model
81
+ backup(application_record_path) do
82
+ inject_into_class application_record_path, "ApplicationRecord", broadcaster_include, verbose: false
83
+ end
84
+
85
+ puts "✅ include CableReady::Broadcaster in ApplicationRecord"
86
+ else
87
+ puts "⏩ Not including CableReady::Broadcaster in ApplicationRecord. Skipping"
88
+ end
89
+
90
+ complete_step :broadcaster
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ hash = gemfile_hash
6
+
7
+ # run bundle only when gems are waiting to be added or removed
8
+ add = add_gem_list.exist? ? add_gem_list.readlines.map(&:chomp) : []
9
+ remove = remove_gem_list.exist? ? remove_gem_list.readlines.map(&:chomp) : []
10
+
11
+ if add.present? || remove.present?
12
+ lines = gemfile_path.readlines
13
+
14
+ remove.each do |name|
15
+ index = lines.index { |line| line =~ /gem ['"]#{name}['"]/ }
16
+ if index
17
+ if /^[^#]*gem ['"]#{name}['"]/.match?(lines[index])
18
+ lines[index] = "# #{lines[index]}"
19
+ say "✅ #{name} gem has been disabled"
20
+ else
21
+ say "⏩ #{name} gem is already disabled. Skipping."
22
+ end
23
+ end
24
+ end
25
+
26
+ add.each do |package|
27
+ matches = package.match(/(.+)@(.+)/)
28
+ name, version = matches[1], matches[2]
29
+
30
+ index = lines.index { |line| line =~ /gem ['"]#{name}['"]/ }
31
+ if index
32
+ if !lines[index].match(/^[^#]*gem ['"]#{name}['"].*#{version}['"]/)
33
+ lines[index] = "\ngem \"#{name}\", \"#{version}\"\n"
34
+ say "✅ #{name} gem has been installed"
35
+ else
36
+ say "⏩ #{name} gem is already installed. Skipping."
37
+ end
38
+ else
39
+ lines << "\ngem \"#{name}\", \"#{version}\"\n"
40
+ end
41
+ end
42
+
43
+ gemfile_path.write lines.join
44
+
45
+ bundle_command("install --quiet", "BUNDLE_IGNORE_MESSAGES" => "1") if hash != gemfile_hash
46
+ else
47
+ say "⏩ No rubygems depedencies to install. Skipping."
48
+ end
49
+
50
+ FileUtils.cp(development_working_path, development_path)
51
+ say "✅ development environment configuration installed"
52
+
53
+ FileUtils.cp(action_cable_initializer_working_path, action_cable_initializer_path)
54
+ say "✅ Action Cable initializer installed"
55
+
56
+ complete_step :bundle
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ initializer = action_cable_initializer_working_path.read
6
+
7
+ if gemfile.match?(/gem ['"]permessage_deflate['"]/)
8
+ say "⏩ permessage_deflate already present in Gemfile. Skipping."
9
+ else
10
+ add_gem "permessage_deflate@>= 0.1"
11
+ end
12
+
13
+ # add permessage_deflate config to Action Cable initializer
14
+ if initializer.exclude? "PermessageDeflate.configure"
15
+ create_or_append(action_cable_initializer_working_path, verbose: false) do
16
+ <<~RUBY
17
+ module ActionCable
18
+ module Connection
19
+ class ClientSocket
20
+ alias_method :old_initialize, :initialize
21
+ def initialize(env, event_target, event_loop, protocols)
22
+ old_initialize(env, event_target, event_loop, protocols)
23
+ @driver.add_extension(
24
+ PermessageDeflate.configure(
25
+ level: Zlib::BEST_COMPRESSION,
26
+ max_window_bits: 13
27
+ )
28
+ )
29
+ end
30
+ end
31
+ end
32
+ end
33
+ RUBY
34
+ end
35
+
36
+ say "✅ Action Cable initializer patched to deflate websocket traffic"
37
+ else
38
+ say "⏩ Action Cable initializer is already patched to deflate websocket traffic. Skipping."
39
+ end
40
+
41
+ complete_step :compression
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stimulus_reflex/installer"
4
+
5
+ return if pack_path_missing?
6
+
7
+ step_path = "/app/javascript/config/"
8
+ index_src = fetch(step_path, "index.js.tt")
9
+ index_path = config_path / "index.js"
10
+ friendly_index_path = index_path.relative_path_from(Rails.root).to_s
11
+ stimulus_reflex_src = fetch(step_path, "stimulus_reflex.js.tt")
12
+ stimulus_reflex_path = config_path / "stimulus_reflex.js"
13
+ friendly_stimulus_reflex_path = stimulus_reflex_path.relative_path_from(Rails.root).to_s
14
+ cable_ready_src = fetch(step_path, "cable_ready.js.tt")
15
+ cable_ready_path = config_path / "cable_ready.js"
16
+
17
+ empty_directory config_path unless config_path.exist?
18
+
19
+ if index_path.exist?
20
+ say "⏩ #{friendly_index_path} already exists. Skipping"
21
+ else
22
+ backup(index_path, delete: true) do
23
+ copy_file(index_src, index_path)
24
+ end
25
+ say "✅ Created #{friendly_index_path}"
26
+ end
27
+
28
+ index_pattern = /import ['"](\.\.\/|\.\/)?config['"]/
29
+ index_commented_pattern = /\s*\/\/\s*#{index_pattern}/
30
+ index_import = "import \"#{prefix}config\"\n"
31
+
32
+ if pack.match?(index_pattern)
33
+ if pack.match?(index_commented_pattern)
34
+ lines = pack_path.readlines
35
+ matches = lines.select { |line| line =~ index_commented_pattern }
36
+ lines[lines.index(matches.last).to_i] = index_import
37
+ pack_path.write lines.join
38
+
39
+ say "✅ Uncommented StimulusReflex and CableReady configs imports in #{friendly_pack_path}"
40
+ else
41
+ say "⏩ StimulusReflex and CableReady configs are already being imported in #{friendly_pack_path}. Skipping"
42
+ end
43
+ else
44
+ lines = pack_path.readlines
45
+ matches = lines.select { |line| line =~ /^import / }
46
+ lines.insert lines.index(matches.last).to_i + 1, index_import
47
+ pack_path.write lines.join
48
+
49
+ say "✅ StimulusReflex and CableReady configs will be imported in #{friendly_pack_path}"
50
+ end
51
+
52
+ # create entrypoint/config/cable_ready.js and make sure it's imported in application.js
53
+ copy_file(cable_ready_src, cable_ready_path) unless cable_ready_path.exist?
54
+
55
+ # create entrypoint/config/stimulus_reflex.js and make sure it's imported in application.js
56
+ copy_file(stimulus_reflex_src, stimulus_reflex_path) unless stimulus_reflex_path.exist?
57
+
58
+ if stimulus_reflex_path.read.include?("StimulusReflex.debug =")
59
+ say "⏩ Development environment options are already set in #{friendly_stimulus_reflex_path}. Skipping"
60
+ else
61
+ if ["webpacker", "shakapacker"].include?(bundler)
62
+ append_file(stimulus_reflex_path, <<~JS, verbose: false)
63
+
64
+ if (process.env.RAILS_ENV === 'development') {
65
+ StimulusReflex.debug = true
66
+ }
67
+ JS
68
+ elsif bundler == "vite"
69
+ append_file(stimulus_reflex_path, <<~JS, verbose: false) unless stimulus_reflex_path.read.include?("StimulusReflex.debug")
70
+
71
+ if (import.meta.env.MODE === "development") {
72
+ StimulusReflex.debug = true
73
+ }
74
+ JS
75
+ else
76
+ append_file(stimulus_reflex_path, <<~JS, verbose: false)
77
+
78
+ // consider removing these options in production
79
+ StimulusReflex.debug = true
80
+ // end remove
81
+ JS
82
+ end
83
+
84
+ say "✅ Set useful development environment options in #{friendly_stimulus_reflex_path}"
85
+ end
86
+
87
+ complete_step :config