stimulus_reflex 3.5.0.pre8 → 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 (108) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +2 -1218
  3. data/Gemfile +0 -1
  4. data/Gemfile.lock +145 -193
  5. data/README.md +48 -20
  6. data/Rakefile +0 -8
  7. data/app/assets/javascripts/stimulus_reflex.js +1174 -0
  8. data/app/assets/javascripts/stimulus_reflex.min.js +2 -0
  9. data/app/assets/javascripts/stimulus_reflex.min.js.map +1 -0
  10. data/app/assets/javascripts/stimulus_reflex.umd.js +1064 -0
  11. data/app/assets/javascripts/stimulus_reflex.umd.min.js +1065 -0
  12. data/app/assets/javascripts/stimulus_reflex.umd.min.js.map +1 -0
  13. data/app/channels/stimulus_reflex/channel.rb +28 -7
  14. data/bin/console +0 -2
  15. data/bin/standardize +2 -1
  16. data/lib/generators/stimulus_reflex/stimulus_reflex_generator.rb +72 -7
  17. data/lib/generators/stimulus_reflex/templates/app/controllers/examples_controller.rb.tt +9 -0
  18. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/consumer.js.tt +6 -0
  19. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.esbuild.tt +4 -0
  20. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.importmap.tt +2 -0
  21. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.shakapacker.tt +5 -0
  22. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.vite.tt +1 -0
  23. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.webpacker.tt +5 -0
  24. data/lib/generators/stimulus_reflex/templates/app/javascript/config/cable_ready.js.tt +4 -0
  25. data/lib/generators/stimulus_reflex/templates/app/javascript/config/index.js.tt +2 -0
  26. data/lib/generators/stimulus_reflex/templates/app/javascript/config/mrujs.js.tt +9 -0
  27. data/lib/generators/stimulus_reflex/templates/app/javascript/config/stimulus_reflex.js.tt +5 -0
  28. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/%file_name%_controller.js.tt +114 -74
  29. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/application.js.tt +11 -0
  30. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/application_controller.js.tt +49 -35
  31. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.esbuild.tt +7 -0
  32. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.importmap.tt +5 -0
  33. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.shakapacker.tt +5 -0
  34. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.vite.tt +5 -0
  35. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.webpacker.tt +5 -0
  36. data/lib/generators/stimulus_reflex/templates/app/reflexes/%file_name%_reflex.rb.tt +38 -7
  37. data/lib/generators/stimulus_reflex/templates/app/reflexes/application_reflex.rb.tt +10 -2
  38. data/lib/generators/stimulus_reflex/templates/app/views/examples/show.html.erb.tt +207 -0
  39. data/lib/generators/stimulus_reflex/templates/config/initializers/cable_ready.rb +22 -0
  40. data/lib/generators/stimulus_reflex/templates/config/initializers/stimulus_reflex.rb +18 -13
  41. data/lib/generators/stimulus_reflex/templates/esbuild.config.mjs.tt +94 -0
  42. data/lib/install/action_cable.rb +155 -0
  43. data/lib/install/broadcaster.rb +90 -0
  44. data/lib/install/bundle.rb +56 -0
  45. data/lib/install/compression.rb +41 -0
  46. data/lib/install/config.rb +87 -0
  47. data/lib/install/development.rb +110 -0
  48. data/lib/install/esbuild.rb +114 -0
  49. data/lib/install/example.rb +22 -0
  50. data/lib/install/importmap.rb +133 -0
  51. data/lib/install/initializers.rb +25 -0
  52. data/lib/install/mrujs.rb +133 -0
  53. data/lib/install/npm_packages.rb +25 -0
  54. data/lib/install/reflexes.rb +25 -0
  55. data/lib/install/shakapacker.rb +64 -0
  56. data/lib/install/spring.rb +54 -0
  57. data/lib/install/updatable.rb +34 -0
  58. data/lib/install/vite.rb +64 -0
  59. data/lib/install/webpacker.rb +90 -0
  60. data/lib/install/yarn.rb +55 -0
  61. data/lib/stimulus_reflex/broadcasters/broadcaster.rb +15 -8
  62. data/lib/stimulus_reflex/broadcasters/page_broadcaster.rb +7 -8
  63. data/lib/stimulus_reflex/broadcasters/selector_broadcaster.rb +10 -10
  64. data/lib/stimulus_reflex/broadcasters/update.rb +3 -0
  65. data/lib/stimulus_reflex/cable_readiness.rb +29 -0
  66. data/lib/stimulus_reflex/cable_ready_channels.rb +5 -5
  67. data/lib/stimulus_reflex/callbacks.rb +17 -1
  68. data/lib/stimulus_reflex/concern_enhancer.rb +6 -4
  69. data/lib/stimulus_reflex/configuration.rb +12 -2
  70. data/lib/stimulus_reflex/dataset.rb +11 -1
  71. data/lib/stimulus_reflex/engine.rb +40 -0
  72. data/lib/stimulus_reflex/html/document.rb +59 -0
  73. data/lib/stimulus_reflex/html/document_fragment.rb +13 -0
  74. data/lib/stimulus_reflex/importmap.rb +7 -0
  75. data/lib/stimulus_reflex/installer.rb +274 -0
  76. data/lib/stimulus_reflex/open_struct_fix.rb +31 -0
  77. data/lib/stimulus_reflex/reflex.rb +32 -25
  78. data/lib/stimulus_reflex/reflex_data.rb +18 -2
  79. data/lib/stimulus_reflex/reflex_factory.rb +6 -3
  80. data/lib/stimulus_reflex/request_parameters.rb +2 -0
  81. data/lib/stimulus_reflex/utils/logger.rb +12 -0
  82. data/lib/stimulus_reflex/utils/sanity_checker.rb +8 -106
  83. data/lib/stimulus_reflex/version.rb +1 -1
  84. data/lib/stimulus_reflex.rb +4 -6
  85. data/lib/tasks/stimulus_reflex/stimulus_reflex.rake +252 -0
  86. data/package.json +73 -0
  87. data/rollup.config.mjs +86 -0
  88. data/stimulus_reflex.gemspec +60 -0
  89. data/web-test-runner.config.mjs +12 -0
  90. data/yarn.lock +5098 -0
  91. metadata +191 -78
  92. data/LATEST +0 -1
  93. data/lib/generators/USAGE +0 -14
  94. data/lib/generators/stimulus_reflex/initializer_generator.rb +0 -14
  95. data/lib/tasks/stimulus_reflex/install.rake +0 -116
  96. data/test/broadcasters/broadcaster_test.rb +0 -11
  97. data/test/broadcasters/broadcaster_test_case.rb +0 -39
  98. data/test/broadcasters/nothing_broadcaster_test.rb +0 -30
  99. data/test/broadcasters/page_broadcaster_test.rb +0 -77
  100. data/test/broadcasters/selector_broadcaster_test.rb +0 -167
  101. data/test/callbacks_test.rb +0 -652
  102. data/test/concern_enhancer_test.rb +0 -54
  103. data/test/element_test.rb +0 -254
  104. data/test/generators/stimulus_reflex_generator_test.rb +0 -58
  105. data/test/reflex_test.rb +0 -43
  106. data/test/test_helper.rb +0 -71
  107. data/test/tmp/app/reflexes/application_reflex.rb +0 -19
  108. data/test/tmp/app/reflexes/demo_reflex.rb +0 -35
@@ -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
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/engine"
4
+
5
+ module StimulusReflex
6
+ class Engine < ::Rails::Engine
7
+ initializer "stimulus_reflex.sanity_check" do
8
+ SanityChecker.check! unless Rails.env.production?
9
+ end
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
+
27
+ initializer "stimulus_reflex.assets" do |app|
28
+ if app.config.respond_to?(:assets) && StimulusReflex.config.precompile_assets
29
+ app.config.assets.precompile += PRECOMPILE_ASSETS
30
+ end
31
+ end
32
+
33
+ initializer "stimulus_reflex.importmap", before: "importmap" do |app|
34
+ if app.config.respond_to?(:importmap)
35
+ app.config.importmap.paths << Engine.root.join("lib/stimulus_reflex/importmap.rb")
36
+ app.config.importmap.cache_sweepers << Engine.root.join("app/assets/javascripts")
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StimulusReflex
4
+ module HTML
5
+ class Document
6
+ DEFAULT_HTML_WITHOUT_FORMAT = Nokogiri::XML::Node::SaveOptions::DEFAULT_HTML & ~Nokogiri::XML::Node::SaveOptions::FORMAT
7
+
8
+ delegate :element, to: :@document
9
+
10
+ def document_element
11
+ @document&.root
12
+ end
13
+
14
+ def outer_html
15
+ document_element ? document_element.to_html(save_with: DEFAULT_HTML_WITHOUT_FORMAT) : ""
16
+ end
17
+ alias_method :to_html, :outer_html
18
+
19
+ def inner_html
20
+ document_element ? document_element.inner_html(save_with: DEFAULT_HTML_WITHOUT_FORMAT) : ""
21
+ end
22
+
23
+ def initialize(html)
24
+ @document = parsing_class.parse(html.to_s)
25
+ @matches = {
26
+ "body" => Match.new(@document.at_css("body"))
27
+ }
28
+ end
29
+
30
+ def empty?
31
+ @document.content.empty?
32
+ end
33
+
34
+ def parsing_class
35
+ Nokogiri::HTML5::Document
36
+ end
37
+
38
+ def match(selector)
39
+ @matches[selector] ||= Match.new(@document.at_css(selector))
40
+ end
41
+
42
+ Match = Struct.new(:element) do
43
+ delegate :present?, to: :element
44
+
45
+ def outer_html
46
+ element&.to_html(save_with: DEFAULT_HTML_WITHOUT_FORMAT)
47
+ end
48
+
49
+ def to_html
50
+ element&.to_html(save_with: DEFAULT_HTML_WITHOUT_FORMAT)
51
+ end
52
+
53
+ def inner_html
54
+ element&.inner_html(save_with: DEFAULT_HTML_WITHOUT_FORMAT)
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ # forzen_string_literal: true
4
+
5
+ module StimulusReflex
6
+ module HTML
7
+ class DocumentFragment < Document
8
+ def parsing_class
9
+ Nokogiri
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ pin "@rails/actioncable", to: "actioncable.esm.js", preload: true
4
+ pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
5
+ pin "morphdom", to: "https://ga.jspm.io/npm:morphdom@2.6.1/dist/morphdom.js", preload: true
6
+ pin "cable_ready", to: "cable_ready.min.js", preload: true
7
+ pin "stimulus_reflex", to: "stimulus_reflex.min.js", preload: true
@@ -0,0 +1,274 @@
1
+ # frozen_string_literal: true
2
+
3
+ ### general utilities
4
+
5
+ def fetch(step_path, file)
6
+ relative_path = step_path + file
7
+ location = template_src + relative_path
8
+
9
+ Pathname.new(location)
10
+ end
11
+
12
+ def complete_step(step)
13
+ create_file "tmp/stimulus_reflex_installer/#{step}", verbose: false
14
+ end
15
+
16
+ def create_or_append(path, *args, &block)
17
+ FileUtils.touch(path)
18
+ append_file(path, *args, &block)
19
+ end
20
+
21
+ def current_template
22
+ ENV["LOCATION"].split("/").last.gsub(".rb", "")
23
+ end
24
+
25
+ def pack_path_missing?
26
+ return false unless pack_path.nil?
27
+ halt "#{friendly_pack_path} is missing. You need a valid application pack file to proceed."
28
+ end
29
+
30
+ def halt(message)
31
+ say "❌ #{message}", :red
32
+ create_file "tmp/stimulus_reflex_installer/halt", verbose: false
33
+ end
34
+
35
+ def backup(path, delete: false)
36
+ if !path.exist?
37
+ yield
38
+ return
39
+ end
40
+
41
+ backup_path = Pathname.new("#{path}.bak")
42
+ old_path = path.relative_path_from(Rails.root).to_s
43
+ filename = path.to_path.split("/").last
44
+
45
+ if backup_path.exist?
46
+ if backup_path.read == path.read
47
+ path.delete if delete
48
+ yield
49
+ return
50
+ end
51
+ backup_path.delete
52
+ end
53
+
54
+ copy_file(path, backup_path, verbose: false)
55
+ path.delete if delete
56
+
57
+ yield
58
+
59
+ if path.read != backup_path.read
60
+ create_or_append(backups_path, "#{old_path}\n", verbose: false)
61
+ end
62
+ say "📦 #{old_path} backed up as #{filename}.bak"
63
+ end
64
+
65
+ def add_gem(name)
66
+ create_or_append(add_gem_list, "#{name}\n", verbose: false)
67
+ say "☑️ Added #{name} to the Gemfile"
68
+ end
69
+
70
+ def remove_gem(name)
71
+ create_or_append(remove_gem_list, "#{name}\n", verbose: false)
72
+ say "❎ Removed #{name} from Gemfile"
73
+ end
74
+
75
+ def add_package(name)
76
+ create_or_append(package_list, "#{name}\n", verbose: false)
77
+ say "☑️ Enqueued #{name} to be added to dependencies"
78
+ end
79
+
80
+ def add_dev_package(name)
81
+ create_or_append(dev_package_list, "#{name}\n", verbose: false)
82
+ say "☑️ Enqueued #{name} to be added to dev dependencies"
83
+ end
84
+
85
+ def drop_package(name)
86
+ create_or_append(drop_package_list, "#{name}\n", verbose: false)
87
+ say "❎ Enqueued #{name} to be removed from dependencies"
88
+ end
89
+
90
+ def gemfile_hash
91
+ Digest::MD5.hexdigest(gemfile_path.read)
92
+ end
93
+
94
+ ### memoized values
95
+
96
+ def sr_npm_version
97
+ @sr_npm_version ||= StimulusReflex::VERSION.gsub(".pre", "-pre")
98
+ end
99
+
100
+ def cr_npm_version
101
+ @cr_npm_version ||= CableReady::VERSION.gsub(".pre", "-pre")
102
+ end
103
+
104
+ def package_json
105
+ @package_json ||= Rails.root.join("package.json")
106
+ end
107
+
108
+ def entrypoint
109
+ @entrypoint ||= File.read("tmp/stimulus_reflex_installer/entrypoint")
110
+ end
111
+
112
+ def bundler
113
+ @bundler ||= File.read("tmp/stimulus_reflex_installer/bundler")
114
+ end
115
+
116
+ def network_issue_path
117
+ @network_issue_path ||= Rails.root.join("tmp/stimulus_reflex_installer/network_issue")
118
+ end
119
+
120
+ def config_path
121
+ @config_path ||= Rails.root.join(entrypoint, "config")
122
+ end
123
+
124
+ def importmap_path
125
+ @importmap_path ||= Rails.root.join("config/importmap.rb")
126
+ end
127
+
128
+ def friendly_importmap_path
129
+ @friendly_importmap_path ||= importmap_path.relative_path_from(Rails.root).to_s
130
+ end
131
+
132
+ def pack
133
+ @pack ||= pack_path.read
134
+ end
135
+
136
+ def friendly_pack_path
137
+ @friendly_pack_path ||= pack_path.relative_path_from(Rails.root).to_s
138
+ end
139
+
140
+ def pack_path
141
+ @pack_path ||= [
142
+ Rails.root.join(entrypoint, "application.js"),
143
+ Rails.root.join(entrypoint, "packs/application.js"),
144
+ Rails.root.join(entrypoint, "entrypoints/application.js")
145
+ ].find(&:exist?)
146
+ end
147
+
148
+ def package_list
149
+ @package_list ||= Rails.root.join("tmp/stimulus_reflex_installer/npm_package_list")
150
+ end
151
+
152
+ def dev_package_list
153
+ @dev_package_list ||= Rails.root.join("tmp/stimulus_reflex_installer/npm_dev_package_list")
154
+ end
155
+
156
+ def drop_package_list
157
+ @drop_package_list ||= Rails.root.join("tmp/stimulus_reflex_installer/drop_npm_package_list")
158
+ end
159
+
160
+ def template_src
161
+ @template_src ||= File.read("tmp/stimulus_reflex_installer/template_src")
162
+ end
163
+
164
+ def controllers_path
165
+ @controllers_path ||= Rails.root.join(entrypoint, "controllers")
166
+ end
167
+
168
+ def gemfile_path
169
+ @gemfile_path ||= Rails.root.join("Gemfile")
170
+ end
171
+
172
+ def gemfile
173
+ @gemfile ||= gemfile_path.read
174
+ end
175
+
176
+ def prefix
177
+ # standard:disable Style/RedundantStringEscape
178
+ @prefix ||= {
179
+ "vite" => "..\/",
180
+ "webpacker" => "",
181
+ "shakapacker" => "",
182
+ "importmap" => "",
183
+ "esbuild" => ".\/"
184
+ }[bundler]
185
+ # standard:enable Style/RedundantStringEscape
186
+ end
187
+
188
+ def application_record_path
189
+ @application_record_path ||= Rails.root.join("app/models/application_record.rb")
190
+ end
191
+
192
+ def action_cable_initializer_path
193
+ @action_cable_initializer_path ||= Rails.root.join("config/initializers/action_cable.rb")
194
+ end
195
+
196
+ def action_cable_initializer_working_path
197
+ @action_cable_initializer_working_path ||= Rails.root.join(working, "action_cable.rb")
198
+ end
199
+
200
+ def development_path
201
+ @development_path ||= Rails.root.join("config/environments/development.rb")
202
+ end
203
+
204
+ def development_working_path
205
+ @development_working_path ||= Rails.root.join(working, "development.rb")
206
+ end
207
+
208
+ def backups_path
209
+ @backups_path ||= Rails.root.join("tmp/stimulus_reflex_installer/backups")
210
+ end
211
+
212
+ def add_gem_list
213
+ @add_gem_list ||= Rails.root.join("tmp/stimulus_reflex_installer/add_gem_list")
214
+ end
215
+
216
+ def remove_gem_list
217
+ @remove_gem_list ||= Rails.root.join("tmp/stimulus_reflex_installer/remove_gem_list")
218
+ end
219
+
220
+ def options_path
221
+ @options_path ||= Rails.root.join("tmp/stimulus_reflex_installer/options")
222
+ end
223
+
224
+ def options
225
+ @options ||= YAML.safe_load(File.read(options_path))
226
+ end
227
+
228
+ def working
229
+ @working ||= Rails.root.join("tmp/stimulus_reflex_installer/working")
230
+ end
231
+
232
+ ### support for development step
233
+
234
+ def write_redis_recommendation(development_working, lines, index, gemfile)
235
+ # provide a recommendation for using redis-session-store, including commented source code
236
+ if !lines.index { |line| line.include?("StimulusReflex does not support :cookie_store") }
237
+ lines.insert index + 1, <<RUBY
238
+
239
+ # StimulusReflex does not support :cookie_store, and we recommend switching to Redis.
240
+ # To use `redis-session-store`, make sure to add it to your Gemfile and run `bundle install`.
241
+
242
+ # config.session_store :redis_session_store,
243
+ # serializer: :json,
244
+ # on_redis_down: ->(*a) { Rails.logger.error("Redis down! \#{a.inspect}") },
245
+ # redis: {
246
+ # expire_after: 120.minutes,
247
+ # key_prefix: "session:",
248
+ # url: ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" }
249
+ # }
250
+ RUBY
251
+ development_working.write lines.join
252
+ # add redis-session-store to Gemfile, but comment it out
253
+ if !gemfile.match?(/gem ['"]redis-session-store['"]/)
254
+ append_file(gemfile_path, verbose: false) do
255
+ <<~RUBY
256
+
257
+ # StimulusReflex recommends using Redis for session storage
258
+ # gem "redis-session-store", "0.11.5"
259
+ RUBY
260
+ end
261
+ say "💡 Added redis-session-store 0.11.5 to the Gemfile, commented out"
262
+ end
263
+ end
264
+ end
265
+
266
+ def find_index(lines)
267
+ # accomodate devs who modify their development.rb file structure
268
+ if (index = lines.index { |line| line =~ /caching-dev/ })
269
+ index += 3
270
+ else
271
+ index = lines.index { |line| line =~ /^Rails.application.configure do/ } + 1
272
+ end
273
+ index
274
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Temporary fix until this PR gets merged:
5
+ # https://github.com/ruby/ostruct/pull/37
6
+ # Thanks to @Laykou
7
+ #
8
+ # Calling this in OpenStruct 0.5.2 fails:
9
+ # os = OpenStruct.new(class: "my-class", method: "post")
10
+
11
+ if defined?(OpenStruct::VERSION) && OpenStruct::VERSION == "0.5.2"
12
+ class OpenStruct
13
+ private def is_method_protected!(name)
14
+ if !respond_to?(name, true)
15
+ false
16
+ elsif name.match?(/!$/)
17
+ true
18
+ else
19
+ owner = method!(name).owner
20
+ if owner.instance_of?(::Class)
21
+ owner < ::OpenStruct
22
+ else
23
+ class!.ancestors.any? do |mod|
24
+ return false if mod == ::OpenStruct
25
+ mod == owner
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end