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.
- checksums.yaml +4 -4
- data/Gemfile +0 -1
- data/Gemfile.lock +122 -127
- data/README.md +10 -16
- data/app/assets/javascripts/stimulus_reflex.js +710 -505
- data/app/assets/javascripts/stimulus_reflex.min.js +1 -1
- data/app/assets/javascripts/stimulus_reflex.min.js.map +1 -1
- data/app/assets/javascripts/stimulus_reflex.umd.js +660 -500
- data/app/assets/javascripts/stimulus_reflex.umd.min.js +660 -500
- data/app/assets/javascripts/stimulus_reflex.umd.min.js.map +1 -1
- data/app/channels/stimulus_reflex/channel.rb +9 -7
- data/bin/console +0 -2
- data/bin/standardize +2 -1
- data/lib/generators/stimulus_reflex/stimulus_reflex_generator.rb +72 -7
- data/lib/generators/stimulus_reflex/templates/app/controllers/examples_controller.rb.tt +9 -0
- data/lib/generators/stimulus_reflex/templates/app/javascript/channels/consumer.js.tt +6 -0
- data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.esbuild.tt +4 -0
- data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.importmap.tt +2 -0
- data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.shakapacker.tt +5 -0
- data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.vite.tt +1 -0
- data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.webpacker.tt +5 -0
- data/lib/generators/stimulus_reflex/templates/app/javascript/config/cable_ready.js.tt +4 -0
- data/lib/generators/stimulus_reflex/templates/app/javascript/config/index.js.tt +2 -0
- data/lib/generators/stimulus_reflex/templates/app/javascript/config/mrujs.js.tt +9 -0
- data/lib/generators/stimulus_reflex/templates/app/javascript/config/stimulus_reflex.js.tt +5 -0
- data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/%file_name%_controller.js.tt +141 -0
- data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/application.js.tt +11 -0
- data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/application_controller.js.tt +74 -0
- data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.esbuild.tt +7 -0
- data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.importmap.tt +5 -0
- data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.shakapacker.tt +5 -0
- data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.vite.tt +5 -0
- data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.webpacker.tt +5 -0
- data/{test/tmp/app/reflexes/user_reflex.rb → lib/generators/stimulus_reflex/templates/app/reflexes/%file_name%_reflex.rb.tt} +38 -9
- data/lib/generators/stimulus_reflex/templates/app/reflexes/application_reflex.rb.tt +27 -0
- data/lib/generators/stimulus_reflex/templates/app/views/examples/show.html.erb.tt +207 -0
- data/lib/generators/stimulus_reflex/templates/config/initializers/cable_ready.rb +22 -0
- data/lib/generators/stimulus_reflex/templates/config/initializers/stimulus_reflex.rb +18 -13
- data/lib/generators/stimulus_reflex/templates/esbuild.config.mjs.tt +94 -0
- data/lib/install/action_cable.rb +155 -0
- data/lib/install/broadcaster.rb +90 -0
- data/lib/install/bundle.rb +56 -0
- data/lib/install/compression.rb +41 -0
- data/lib/install/config.rb +87 -0
- data/lib/install/development.rb +110 -0
- data/lib/install/esbuild.rb +114 -0
- data/lib/install/example.rb +22 -0
- data/lib/install/importmap.rb +133 -0
- data/lib/install/initializers.rb +25 -0
- data/lib/install/mrujs.rb +133 -0
- data/lib/install/npm_packages.rb +25 -0
- data/lib/install/reflexes.rb +25 -0
- data/lib/install/shakapacker.rb +64 -0
- data/lib/install/spring.rb +54 -0
- data/lib/install/updatable.rb +34 -0
- data/lib/install/vite.rb +64 -0
- data/lib/install/webpacker.rb +90 -0
- data/lib/install/yarn.rb +55 -0
- data/lib/stimulus_reflex/broadcasters/broadcaster.rb +15 -8
- data/lib/stimulus_reflex/broadcasters/page_broadcaster.rb +7 -8
- data/lib/stimulus_reflex/broadcasters/selector_broadcaster.rb +10 -10
- data/lib/stimulus_reflex/broadcasters/update.rb +3 -0
- data/lib/stimulus_reflex/cable_readiness.rb +29 -0
- data/lib/stimulus_reflex/cable_ready_channels.rb +5 -5
- data/lib/stimulus_reflex/callbacks.rb +17 -1
- data/lib/stimulus_reflex/concern_enhancer.rb +6 -4
- data/lib/stimulus_reflex/configuration.rb +12 -2
- data/lib/stimulus_reflex/dataset.rb +11 -1
- data/lib/stimulus_reflex/engine.rb +20 -9
- data/lib/stimulus_reflex/html/document.rb +59 -0
- data/lib/stimulus_reflex/html/document_fragment.rb +13 -0
- data/lib/stimulus_reflex/importmap.rb +3 -0
- data/lib/stimulus_reflex/installer.rb +274 -0
- data/lib/stimulus_reflex/open_struct_fix.rb +2 -0
- data/lib/stimulus_reflex/reflex.rb +25 -25
- data/lib/stimulus_reflex/reflex_data.rb +15 -3
- data/lib/stimulus_reflex/reflex_factory.rb +4 -2
- data/lib/stimulus_reflex/request_parameters.rb +2 -0
- data/lib/stimulus_reflex/utils/logger.rb +10 -0
- data/lib/stimulus_reflex/utils/sanity_checker.rb +8 -48
- data/lib/stimulus_reflex/version.rb +1 -1
- data/lib/stimulus_reflex.rb +2 -0
- data/lib/tasks/stimulus_reflex/stimulus_reflex.rake +252 -0
- data/package.json +34 -28
- data/{rollup.config.js → rollup.config.mjs} +8 -7
- data/stimulus_reflex.gemspec +16 -19
- data/yarn.lock +1320 -742
- metadata +124 -77
- data/LATEST +0 -1
- data/lib/generators/stimulus_reflex/initializer_generator.rb +0 -14
- data/test/broadcasters/broadcaster_test.rb +0 -11
- data/test/broadcasters/broadcaster_test_case.rb +0 -39
- data/test/broadcasters/nothing_broadcaster_test.rb +0 -31
- data/test/broadcasters/page_broadcaster_test.rb +0 -79
- data/test/broadcasters/selector_broadcaster_test.rb +0 -173
- data/test/callbacks_test.rb +0 -652
- data/test/concern_enhancer_test.rb +0 -54
- data/test/element_test.rb +0 -254
- data/test/generators/stimulus_reflex_generator_test.rb +0 -58
- data/test/reflex_test.rb +0 -43
- data/test/test_helper.rb +0 -71
- data/test/tmp/app/reflexes/application_reflex.rb +0 -12
- data/yarn-error.log +0 -4964
@@ -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
|
@@ -1,4 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
pin "@rails/actioncable", to: "actioncable.esm.js", preload: true
|
2
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
|
3
6
|
pin "cable_ready", to: "cable_ready.min.js", preload: true
|
4
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
|
@@ -1,47 +1,40 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require "stimulus_reflex/cable_readiness"
|
4
|
+
|
5
|
+
# TODO remove xpath_controller and xpath_element for v4
|
6
|
+
ClientAttributes = Struct.new(:id, :tab_id, :reflex_controller, :xpath_controller, :xpath_element, :permanent_attribute_name, :version, :suppress_logging, keyword_init: true)
|
4
7
|
|
5
8
|
class StimulusReflex::Reflex
|
6
9
|
class VersionMismatchError < StandardError; end
|
7
10
|
|
11
|
+
prepend StimulusReflex::CableReadiness
|
8
12
|
include ActiveSupport::Rescuable
|
9
13
|
include StimulusReflex::Callbacks
|
10
14
|
include ActionView::Helpers::TagHelper
|
11
15
|
include CableReady::Identifiable
|
12
16
|
|
13
17
|
attr_accessor :payload, :headers
|
14
|
-
attr_reader :
|
18
|
+
attr_reader :channel, :url, :element, :selectors, :method_name, :broadcaster, :client_attributes, :logger
|
15
19
|
|
16
20
|
alias_method :action_name, :method_name # for compatibility with controller libraries like Pundit that expect an action name
|
17
21
|
|
18
22
|
delegate :connection, :stream_name, to: :channel
|
19
23
|
delegate :controller_class, :flash, :session, to: :request
|
20
|
-
delegate :broadcast, :
|
21
|
-
|
24
|
+
delegate :broadcast, :broadcast_halt, :broadcast_forbid, :broadcast_error, to: :broadcaster
|
25
|
+
# TODO remove xpath_controller and xpath_element for v4
|
26
|
+
delegate :id, :tab_id, :reflex_controller, :xpath_controller, :xpath_element, :permanent_attribute_name, :version, :suppress_logging, to: :client_attributes
|
22
27
|
|
23
28
|
def initialize(channel, url: nil, element: nil, selectors: [], method_name: nil, params: {}, client_attributes: {})
|
24
|
-
if is_a? CableReady::Broadcaster
|
25
|
-
message = <<~MSG
|
26
|
-
|
27
|
-
#{self.class.name} includes CableReady::Broadcaster, and you need to remove it.
|
28
|
-
Reflexes have their own CableReady interface. You can just assume that it's present.
|
29
|
-
See https://docs.stimulusreflex.com/rtfm/cableready#using-cableready-inside-a-reflex-action for more details.
|
30
|
-
|
31
|
-
MSG
|
32
|
-
raise TypeError.new(message.strip)
|
33
|
-
end
|
34
|
-
|
35
29
|
@channel = channel
|
36
30
|
@url = url
|
37
31
|
@element = element
|
38
32
|
@selectors = selectors
|
39
33
|
@method_name = method_name
|
40
34
|
@params = params
|
41
|
-
@broadcaster = StimulusReflex::PageBroadcaster.new(self)
|
42
35
|
@client_attributes = ClientAttributes.new(client_attributes)
|
36
|
+
@broadcaster = StimulusReflex::PageBroadcaster.new(self)
|
43
37
|
@logger = suppress_logging ? nil : StimulusReflex::Logger.new(self)
|
44
|
-
@cable_ready = StimulusReflex::CableReadyChannels.new(stream_name, reflex_id)
|
45
38
|
@payload = {}
|
46
39
|
@headers = {}
|
47
40
|
|
@@ -52,6 +45,13 @@ class StimulusReflex::Reflex
|
|
52
45
|
self.params
|
53
46
|
end
|
54
47
|
|
48
|
+
# TODO: remove this for v4
|
49
|
+
def reflex_id
|
50
|
+
warn "Deprecation warning: reflex_id will be removed in v4. Use id instead!" if Rails.env.development?
|
51
|
+
id
|
52
|
+
end
|
53
|
+
# END TODO: remove
|
54
|
+
|
55
55
|
def request
|
56
56
|
@request ||= begin
|
57
57
|
uri = URI.parse(url)
|
@@ -94,10 +94,10 @@ class StimulusReflex::Reflex
|
|
94
94
|
when :page
|
95
95
|
raise StandardError.new("Cannot call :page morph after :#{broadcaster.to_sym} morph") unless broadcaster.page?
|
96
96
|
when :nothing
|
97
|
-
raise StandardError.new("
|
97
|
+
raise StandardError.new("#{broadcaster.to_sym} morph type has already been set") if broadcaster.selector?
|
98
98
|
@broadcaster = StimulusReflex::NothingBroadcaster.new(self) unless broadcaster.nothing?
|
99
99
|
else
|
100
|
-
raise StandardError.new("
|
100
|
+
raise StandardError.new("#{broadcaster.to_sym} morph type has already been set") if broadcaster.nothing?
|
101
101
|
@broadcaster = StimulusReflex::SelectorBroadcaster.new(self) unless broadcaster.selector?
|
102
102
|
broadcaster.append_morph(selectors, html)
|
103
103
|
end
|
@@ -128,12 +128,7 @@ class StimulusReflex::Reflex
|
|
128
128
|
|
129
129
|
# Invoke the reflex action specified by `name` and run all callbacks
|
130
130
|
def process(name, *args)
|
131
|
-
|
132
|
-
result = run_callbacks(:process) {
|
133
|
-
public_send(name, *args).tap { reflex_invoked = true }
|
134
|
-
}
|
135
|
-
@halted ||= result == false && !reflex_invoked
|
136
|
-
result
|
131
|
+
run_callbacks(:process) { public_send(name, *args) }
|
137
132
|
end
|
138
133
|
|
139
134
|
# Indicates if the callback chain was halted via a throw(:abort) in a before_reflex callback.
|
@@ -143,6 +138,11 @@ class StimulusReflex::Reflex
|
|
143
138
|
!!@halted
|
144
139
|
end
|
145
140
|
|
141
|
+
# Indicates if the callback chain was halted via a throw(:forbidden) in a before_reflex callback.
|
142
|
+
def forbidden?
|
143
|
+
!!@forbidden
|
144
|
+
end
|
145
|
+
|
146
146
|
def default_reflex
|
147
147
|
# noop default reflex to force page reloads
|
148
148
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class StimulusReflex::ReflexData
|
2
4
|
attr_reader :data
|
3
5
|
|
@@ -49,18 +51,27 @@ class StimulusReflex::ReflexData
|
|
49
51
|
Rack::Utils.parse_nested_query(data["formData"])
|
50
52
|
end
|
51
53
|
|
54
|
+
def params
|
55
|
+
form_params.merge(url_params)
|
56
|
+
end
|
57
|
+
|
52
58
|
def form_params
|
53
59
|
form_data.deep_merge(data["params"] || {})
|
54
60
|
end
|
55
61
|
|
56
|
-
def
|
57
|
-
|
62
|
+
def url_params
|
63
|
+
Rack::Utils.parse_nested_query(URI.parse(url).query)
|
64
|
+
end
|
65
|
+
|
66
|
+
def id
|
67
|
+
data["id"]
|
58
68
|
end
|
59
69
|
|
60
70
|
def tab_id
|
61
71
|
data["tabId"]
|
62
72
|
end
|
63
73
|
|
74
|
+
# TODO: remove this in v4
|
64
75
|
def xpath_controller
|
65
76
|
data["xpathController"]
|
66
77
|
end
|
@@ -68,13 +79,14 @@ class StimulusReflex::ReflexData
|
|
68
79
|
def xpath_element
|
69
80
|
data["xpathElement"]
|
70
81
|
end
|
82
|
+
# END TODO remove
|
71
83
|
|
72
84
|
def reflex_controller
|
73
85
|
data["reflexController"]
|
74
86
|
end
|
75
87
|
|
76
88
|
def version
|
77
|
-
data["version"].gsub("-pre", ".pre")
|
89
|
+
data["version"].to_s.gsub("-pre", ".pre")
|
78
90
|
end
|
79
91
|
|
80
92
|
private
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class StimulusReflex::ReflexFactory
|
2
4
|
class << self
|
3
5
|
attr_reader :reflex_data
|
@@ -9,9 +11,9 @@ class StimulusReflex::ReflexFactory
|
|
9
11
|
element: reflex_data.element,
|
10
12
|
selectors: reflex_data.selectors,
|
11
13
|
method_name: reflex_data.method_name,
|
12
|
-
params: reflex_data.
|
14
|
+
params: reflex_data.params,
|
13
15
|
client_attributes: {
|
14
|
-
|
16
|
+
id: reflex_data.id,
|
15
17
|
tab_id: reflex_data.tab_id,
|
16
18
|
xpath_controller: reflex_data.xpath_controller,
|
17
19
|
xpath_element: reflex_data.xpath_element,
|
@@ -46,6 +46,15 @@ module StimulusReflex
|
|
46
46
|
reflex.class.to_s + "#" + reflex.method_name
|
47
47
|
end
|
48
48
|
|
49
|
+
def id_full
|
50
|
+
reflex.id
|
51
|
+
end
|
52
|
+
|
53
|
+
def id
|
54
|
+
id_full[0..7]
|
55
|
+
end
|
56
|
+
|
57
|
+
# TODO: remove for v4
|
49
58
|
def reflex_id_full
|
50
59
|
reflex.reflex_id
|
51
60
|
end
|
@@ -53,6 +62,7 @@ module StimulusReflex
|
|
53
62
|
def reflex_id
|
54
63
|
reflex_id_full[0..7]
|
55
64
|
end
|
65
|
+
# END TODO remove
|
56
66
|
|
57
67
|
def mode
|
58
68
|
reflex.broadcaster.to_s
|
@@ -1,8 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class StimulusReflex::SanityChecker
|
4
|
-
LATEST_VERSION_FORMAT = /^(\d+\.\d+\.\d+)$/
|
5
|
-
|
6
4
|
class << self
|
7
5
|
def check!
|
8
6
|
return if ENV["SKIP_SANITY_CHECK"]
|
@@ -14,7 +12,6 @@ class StimulusReflex::SanityChecker
|
|
14
12
|
instance = new
|
15
13
|
instance.check_caching_enabled
|
16
14
|
# instance.check_default_url_config
|
17
|
-
instance.check_new_version_available
|
18
15
|
end
|
19
16
|
|
20
17
|
private
|
@@ -48,7 +45,7 @@ class StimulusReflex::SanityChecker
|
|
48
45
|
if using_null_store?
|
49
46
|
warn_and_exit <<~WARN
|
50
47
|
👉 StimulusReflex requires caching to be enabled.
|
51
|
-
|
48
|
+
|
52
49
|
Caching allows the session to be modified during ActionCable requests. Your config.cache_store is set to :null_store, so it won't work.
|
53
50
|
WARN
|
54
51
|
end
|
@@ -56,7 +53,7 @@ class StimulusReflex::SanityChecker
|
|
56
53
|
|
57
54
|
def check_default_url_config
|
58
55
|
return if StimulusReflex.config.on_missing_default_urls == :ignore
|
59
|
-
if
|
56
|
+
if default_url_config_missing?
|
60
57
|
puts <<~WARN
|
61
58
|
👉 StimulusReflex strongly suggests that you set default_url_options in your environment files. Otherwise, ActionController #{"and ActionMailer " if defined?(ActionMailer)}will default to example.com when rendering route helpers.
|
62
59
|
|
@@ -70,29 +67,6 @@ class StimulusReflex::SanityChecker
|
|
70
67
|
end
|
71
68
|
end
|
72
69
|
|
73
|
-
def check_new_version_available
|
74
|
-
return if StimulusReflex.config.on_new_version_available == :ignore
|
75
|
-
return if Rails.env.development? == false
|
76
|
-
return if using_preview_release?
|
77
|
-
begin
|
78
|
-
latest_version = URI.open("https://raw.githubusercontent.com/stimulusreflex/stimulus_reflex/master/LATEST", open_timeout: 1, read_timeout: 1).read.strip
|
79
|
-
if latest_version != StimulusReflex::VERSION
|
80
|
-
puts <<~WARN
|
81
|
-
|
82
|
-
👉 There is a new version of StimulusReflex available!
|
83
|
-
Current: #{StimulusReflex::VERSION} Latest: #{latest_version}
|
84
|
-
|
85
|
-
If you upgrade, it is very important that you update BOTH Gemfile and package.json
|
86
|
-
Then, run `bundle install && yarn install` to update to #{latest_version}.
|
87
|
-
|
88
|
-
WARN
|
89
|
-
exit if StimulusReflex.config.on_new_version_available == :exit
|
90
|
-
end
|
91
|
-
rescue
|
92
|
-
puts "👉 StimulusReflex #{StimulusReflex::VERSION} update check skipped: connection timeout"
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
70
|
def caching_not_enabled?
|
97
71
|
Rails.application.config.action_controller.perform_caching == false
|
98
72
|
end
|
@@ -101,24 +75,18 @@ class StimulusReflex::SanityChecker
|
|
101
75
|
Rails.application.config.cache_store == :null_store
|
102
76
|
end
|
103
77
|
|
78
|
+
def initializer_missing?
|
79
|
+
File.exist?(Rails.root.join("config", "initializers", "stimulus_reflex.rb")) == false
|
80
|
+
end
|
81
|
+
|
104
82
|
def default_url_config_set?
|
105
83
|
if defined?(ActionMailer)
|
106
|
-
Rails.application.config.action_controller.default_url_options.blank?
|
84
|
+
Rails.application.config.action_controller.default_url_options.blank? || Rails.application.config.action_mailer.default_url_options.blank?
|
107
85
|
else
|
108
86
|
Rails.application.config.action_controller.default_url_options.blank?
|
109
87
|
end
|
110
88
|
end
|
111
89
|
|
112
|
-
def using_preview_release?
|
113
|
-
preview = StimulusReflex::VERSION.match?(LATEST_VERSION_FORMAT) == false
|
114
|
-
puts "👉 StimulusReflex #{StimulusReflex::VERSION} update check skipped: pre-release build" if preview
|
115
|
-
preview
|
116
|
-
end
|
117
|
-
|
118
|
-
def initializer_missing?
|
119
|
-
File.exist?(Rails.root.join("config", "initializers", "stimulus_reflex.rb")) == false
|
120
|
-
end
|
121
|
-
|
122
90
|
def warn_and_exit(text)
|
123
91
|
puts
|
124
92
|
puts "Heads up! 🔥"
|
@@ -138,15 +106,7 @@ class StimulusReflex::SanityChecker
|
|
138
106
|
end
|
139
107
|
|
140
108
|
INFO
|
141
|
-
|
142
|
-
puts <<~INFO
|
143
|
-
You can create a StimulusReflex initializer with the command:
|
144
|
-
|
145
|
-
bundle exec rails generate stimulus_reflex:initializer
|
146
|
-
|
147
|
-
INFO
|
148
|
-
end
|
149
|
-
exit false if Rails.env.test? == false
|
109
|
+
exit false unless Rails.env.test?
|
150
110
|
end
|
151
111
|
end
|
152
112
|
end
|