railties 6.1.7.8 → 7.0.8.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +219 -320
  3. data/RDOC_MAIN.rdoc +16 -16
  4. data/README.rdoc +1 -2
  5. data/lib/minitest/rails_plugin.rb +1 -1
  6. data/lib/rails/all.rb +0 -1
  7. data/lib/rails/app_updater.rb +9 -6
  8. data/lib/rails/application/bootstrap.rb +24 -5
  9. data/lib/rails/application/configuration.rb +95 -33
  10. data/lib/rails/application/default_middleware_stack.rb +22 -3
  11. data/lib/rails/application/finisher.rb +53 -95
  12. data/lib/rails/application/routes_reloader.rb +8 -0
  13. data/lib/rails/application.rb +74 -101
  14. data/lib/rails/application_controller.rb +3 -3
  15. data/lib/rails/autoloaders/inflector.rb +21 -0
  16. data/lib/rails/autoloaders.rb +42 -42
  17. data/lib/rails/code_statistics.rb +2 -2
  18. data/lib/rails/code_statistics_calculator.rb +10 -1
  19. data/lib/rails/command/base.rb +26 -12
  20. data/lib/rails/command/behavior.rb +1 -1
  21. data/lib/rails/command/environment_argument.rb +1 -1
  22. data/lib/rails/command.rb +9 -6
  23. data/lib/rails/commands/console/console_command.rb +4 -0
  24. data/lib/rails/commands/credentials/USAGE +4 -2
  25. data/lib/rails/commands/credentials/credentials_command/diffing.rb +26 -16
  26. data/lib/rails/commands/credentials/credentials_command.rb +7 -3
  27. data/lib/rails/commands/dbconsole/dbconsole_command.rb +10 -11
  28. data/lib/rails/commands/help/USAGE +3 -2
  29. data/lib/rails/commands/runner/runner_command.rb +3 -2
  30. data/lib/rails/commands/server/server_command.rb +7 -5
  31. data/lib/rails/configuration.rb +18 -23
  32. data/lib/rails/console/helpers.rb +2 -2
  33. data/lib/rails/engine/configuration.rb +3 -3
  34. data/lib/rails/engine.rb +31 -37
  35. data/lib/rails/gem_version.rb +5 -5
  36. data/lib/rails/generators/actions/create_migration.rb +2 -4
  37. data/lib/rails/generators/actions.rb +229 -62
  38. data/lib/rails/generators/app_base.rb +155 -130
  39. data/lib/rails/generators/app_name.rb +1 -1
  40. data/lib/rails/generators/base.rb +12 -16
  41. data/lib/rails/generators/erb/scaffold/scaffold_generator.rb +2 -0
  42. data/lib/rails/generators/erb/scaffold/templates/_form.html.erb.tt +8 -8
  43. data/lib/rails/generators/erb/scaffold/templates/edit.html.erb.tt +8 -4
  44. data/lib/rails/generators/erb/scaffold/templates/index.html.erb.tt +11 -28
  45. data/lib/rails/generators/erb/scaffold/templates/new.html.erb.tt +7 -3
  46. data/lib/rails/generators/erb/scaffold/templates/partial.html.erb.tt +17 -0
  47. data/lib/rails/generators/erb/scaffold/templates/show.html.erb.tt +8 -17
  48. data/lib/rails/generators/erb.rb +1 -1
  49. data/lib/rails/generators/generated_attribute.rb +45 -9
  50. data/lib/rails/generators/migration.rb +2 -6
  51. data/lib/rails/generators/model_helpers.rb +1 -1
  52. data/lib/rails/generators/named_base.rb +13 -13
  53. data/lib/rails/generators/rails/app/app_generator.rb +70 -91
  54. data/lib/rails/generators/rails/app/templates/Gemfile.tt +44 -52
  55. data/lib/rails/generators/rails/app/templates/app/assets/stylesheets/application.css.tt +2 -2
  56. data/lib/rails/generators/rails/app/templates/app/mailers/application_mailer.rb.tt +2 -2
  57. data/lib/rails/generators/rails/app/templates/app/models/application_record.rb.tt +1 -1
  58. data/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt +3 -9
  59. data/lib/rails/generators/rails/app/templates/bin/rails.tt +1 -4
  60. data/lib/rails/generators/rails/app/templates/bin/rake.tt +0 -3
  61. data/lib/rails/generators/rails/app/templates/bin/setup.tt +9 -14
  62. data/lib/rails/generators/rails/app/templates/config/application.rb.tt +0 -1
  63. data/lib/rails/generators/rails/app/templates/config/boot.rb.tt +1 -1
  64. data/lib/rails/generators/rails/app/templates/config/databases/jdbc.yml.tt +2 -2
  65. data/lib/rails/generators/rails/app/templates/config/databases/jdbcmysql.yml.tt +3 -3
  66. data/lib/rails/generators/rails/app/templates/config/databases/jdbcpostgresql.yml.tt +3 -3
  67. data/lib/rails/generators/rails/app/templates/config/databases/jdbcsqlite3.yml.tt +2 -2
  68. data/lib/rails/generators/rails/app/templates/config/databases/mysql.yml.tt +3 -3
  69. data/lib/rails/generators/rails/app/templates/config/databases/oracle.yml.tt +2 -2
  70. data/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml.tt +3 -3
  71. data/lib/rails/generators/rails/app/templates/config/databases/sqlite3.yml.tt +1 -1
  72. data/lib/rails/generators/rails/app/templates/config/databases/sqlserver.yml.tt +5 -5
  73. data/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt +7 -13
  74. data/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +12 -39
  75. data/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt +7 -12
  76. data/lib/rails/generators/rails/app/templates/config/initializers/assets.rb.tt +1 -5
  77. data/lib/rails/generators/rails/app/templates/config/initializers/content_security_policy.rb.tt +21 -28
  78. data/lib/rails/generators/rails/app/templates/config/initializers/cors.rb.tt +2 -2
  79. data/lib/rails/generators/rails/app/templates/config/initializers/filter_parameter_logging.rb.tt +3 -1
  80. data/lib/rails/generators/rails/app/templates/config/initializers/inflections.rb.tt +4 -4
  81. data/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_7_0.rb.tt +143 -0
  82. data/lib/rails/generators/rails/app/templates/config/locales/en.yml +3 -3
  83. data/lib/rails/generators/rails/app/templates/config/puma.rb.tt +1 -1
  84. data/lib/rails/generators/rails/app/templates/config/routes.rb.tt +4 -1
  85. data/lib/rails/generators/rails/app/templates/config/storage.yml.tt +5 -5
  86. data/lib/rails/generators/rails/app/templates/db/seeds.rb.tt +2 -2
  87. data/lib/rails/generators/rails/app/templates/gitattributes.tt +0 -5
  88. data/lib/rails/generators/rails/app/templates/gitignore.tt +3 -1
  89. data/lib/rails/generators/rails/app/templates/test/test_helper.rb.tt +1 -1
  90. data/lib/rails/generators/rails/controller/controller_generator.rb +1 -2
  91. data/lib/rails/generators/rails/controller/templates/controller.rb.tt +0 -4
  92. data/lib/rails/generators/rails/db/system/change/change_generator.rb +1 -1
  93. data/lib/rails/generators/rails/generator/templates/%file_name%_generator.rb.tt +1 -1
  94. data/lib/rails/generators/rails/plugin/plugin_generator.rb +76 -27
  95. data/lib/rails/generators/rails/plugin/templates/%name%.gemspec.tt +7 -3
  96. data/lib/rails/generators/rails/plugin/templates/Gemfile.tt +9 -31
  97. data/lib/rails/generators/rails/plugin/templates/README.md.tt +1 -1
  98. data/lib/rails/generators/rails/plugin/templates/app/mailers/%namespaced_name%/application_mailer.rb.tt +2 -2
  99. data/lib/rails/generators/rails/plugin/templates/app/views/layouts/%namespaced_name%/application.html.erb.tt +0 -3
  100. data/lib/rails/generators/rails/plugin/templates/bin/rails.tt +7 -6
  101. data/lib/rails/generators/rails/plugin/templates/gitignore.tt +0 -5
  102. data/lib/rails/generators/rails/plugin/templates/lib/%namespaced_name%/engine.rb.tt +2 -2
  103. data/lib/rails/generators/rails/plugin/templates/lib/%namespaced_name%/version.rb.tt +1 -1
  104. data/lib/rails/generators/rails/plugin/templates/rails/boot.rb.tt +2 -2
  105. data/lib/rails/generators/rails/plugin/templates/test/test_helper.rb.tt +2 -7
  106. data/lib/rails/generators/rails/scaffold/scaffold_generator.rb +0 -19
  107. data/lib/rails/generators/rails/scaffold_controller/templates/api_controller.rb.tt +1 -5
  108. data/lib/rails/generators/rails/scaffold_controller/templates/controller.rb.tt +6 -10
  109. data/lib/rails/generators/resource_helpers.rb +2 -2
  110. data/lib/rails/generators/test_unit/generator/templates/generator_test.rb.tt +1 -1
  111. data/lib/rails/generators/test_unit/model/templates/fixtures.yml.tt +2 -2
  112. data/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +3 -3
  113. data/lib/rails/generators/test_unit/scaffold/templates/api_functional_test.rb.tt +5 -5
  114. data/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb.tt +7 -7
  115. data/lib/rails/generators/test_unit/scaffold/templates/system_test.rb.tt +9 -11
  116. data/lib/rails/generators/testing/behaviour.rb +3 -4
  117. data/lib/rails/generators.rb +17 -24
  118. data/lib/rails/info.rb +1 -1
  119. data/lib/rails/info_controller.rb +1 -3
  120. data/lib/rails/initializable.rb +1 -1
  121. data/lib/rails/mailers_controller.rb +2 -4
  122. data/lib/rails/paths.rb +1 -1
  123. data/lib/rails/rack/logger.rb +7 -9
  124. data/lib/rails/railtie/configuration.rb +1 -2
  125. data/lib/rails/railtie.rb +36 -13
  126. data/lib/rails/ruby_version_check.rb +3 -3
  127. data/lib/rails/source_annotation_extractor.rb +1 -1
  128. data/lib/rails/tasks/framework.rake +7 -9
  129. data/lib/rails/tasks/statistics.rake +3 -1
  130. data/lib/rails/tasks/tmp.rake +8 -1
  131. data/lib/rails/tasks/yarn.rake +8 -9
  132. data/lib/rails/tasks/zeitwerk.rake +2 -10
  133. data/lib/rails/templates/layouts/application.html.erb +15 -0
  134. data/lib/rails/templates/rails/mailers/email.html.erb +13 -11
  135. data/lib/rails/templates/rails/welcome/index.html.erb +64 -48
  136. data/lib/rails/test_help.rb +4 -0
  137. data/lib/rails/test_unit/railtie.rb +0 -4
  138. data/lib/rails/test_unit/runner.rb +16 -9
  139. data/lib/rails/test_unit/testing.rake +4 -9
  140. data/lib/rails/version.rb +1 -1
  141. data/lib/rails/welcome_controller.rb +1 -0
  142. data/lib/rails.rb +13 -2
  143. metadata +33 -37
  144. data/lib/rails/command/spellchecker.rb +0 -57
  145. data/lib/rails/generators/css/assets/assets_generator.rb +0 -15
  146. data/lib/rails/generators/css/assets/templates/stylesheet.css +0 -4
  147. data/lib/rails/generators/css/scaffold/scaffold_generator.rb +0 -18
  148. data/lib/rails/generators/rails/app/templates/app/javascript/channels/consumer.js +0 -6
  149. data/lib/rails/generators/rails/app/templates/app/javascript/channels/index.js +0 -5
  150. data/lib/rails/generators/rails/app/templates/app/javascript/packs/application.js.tt +0 -23
  151. data/lib/rails/generators/rails/app/templates/bin/spring.tt +0 -13
  152. data/lib/rails/generators/rails/app/templates/bin/yarn.tt +0 -16
  153. data/lib/rails/generators/rails/app/templates/config/initializers/application_controller_renderer.rb.tt +0 -8
  154. data/lib/rails/generators/rails/app/templates/config/initializers/backtrace_silencers.rb.tt +0 -8
  155. data/lib/rails/generators/rails/app/templates/config/initializers/cookies_serializer.rb.tt +0 -5
  156. data/lib/rails/generators/rails/app/templates/config/initializers/mime_types.rb.tt +0 -4
  157. data/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_6_1.rb.tt +0 -67
  158. data/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt +0 -16
  159. data/lib/rails/generators/rails/app/templates/config/spring.rb.tt +0 -6
  160. data/lib/rails/generators/rails/app/templates/package.json.tt +0 -11
  161. data/lib/rails/generators/rails/assets/USAGE +0 -16
  162. data/lib/rails/generators/rails/assets/assets_generator.rb +0 -26
  163. data/lib/rails/generators/rails/assets/templates/stylesheet.css +0 -4
  164. data/lib/rails/generators/rails/scaffold/templates/scaffold.css +0 -80
@@ -2,6 +2,8 @@
2
2
 
3
3
  require "active_support/core_ext/string/inflections"
4
4
  require "active_support/core_ext/array/conversions"
5
+ require "active_support/descendants_tracker"
6
+ require "active_support/dependencies"
5
7
 
6
8
  module Rails
7
9
  class Application
@@ -12,88 +14,30 @@ module Rails
12
14
  config.generators.templates.unshift(*paths["lib/templates"].existent)
13
15
  end
14
16
 
15
- initializer :ensure_autoload_once_paths_as_subset do
16
- extra = ActiveSupport::Dependencies.autoload_once_paths -
17
- ActiveSupport::Dependencies.autoload_paths
17
+ initializer :setup_main_autoloader do
18
+ autoloader = Rails.autoloaders.main
18
19
 
19
- unless extra.empty?
20
- abort <<-end_error
21
- autoload_once_paths must be a subset of the autoload_paths.
22
- Extra items in autoload_once_paths: #{extra * ','}
23
- end_error
24
- end
25
- end
20
+ ActiveSupport::Dependencies.autoload_paths.freeze
21
+ ActiveSupport::Dependencies.autoload_paths.uniq.each do |path|
22
+ # Zeitwerk only accepts existing directories in `push_dir`.
23
+ next unless File.directory?(path)
26
24
 
27
- # This will become an error if/when we remove classic mode. The plan is
28
- # autoloaders won't be configured up to this point in the finisher, so
29
- # constants just won't be found, raising regular NameError exceptions.
30
- initializer :warn_if_autoloaded, before: :let_zeitwerk_take_over do
31
- next if config.cache_classes
32
- next if ActiveSupport::Dependencies.autoloaded_constants.empty?
33
-
34
- autoloaded = ActiveSupport::Dependencies.autoloaded_constants
35
- constants = "constant".pluralize(autoloaded.size)
36
- enum = autoloaded.to_sentence
37
- have = autoloaded.size == 1 ? "has" : "have"
38
- these = autoloaded.size == 1 ? "This" : "These"
39
- example = autoloaded.first
40
- example_klass = example.constantize.class
41
-
42
- if config.autoloader == :zeitwerk
43
- ActiveSupport::DescendantsTracker.clear
44
- ActiveSupport::Dependencies.clear
45
-
46
- unload_message = "#{these} autoloaded #{constants} #{have} been unloaded."
47
- else
48
- unload_message = "`config.autoloader` is set to `#{config.autoloader}`. #{these} autoloaded #{constants} would have been unloaded if `config.autoloader` had been set to `:zeitwerk`."
25
+ autoloader.push_dir(path)
26
+ autoloader.do_not_eager_load(path) unless ActiveSupport::Dependencies.eager_load?(path)
49
27
  end
50
28
 
51
- ActiveSupport::Deprecation.warn(<<~WARNING)
52
- Initialization autoloaded the #{constants} #{enum}.
53
-
54
- Being able to do this is deprecated. Autoloading during initialization is going
55
- to be an error condition in future versions of Rails.
56
-
57
- Reloading does not reboot the application, and therefore code executed during
58
- initialization does not run again. So, if you reload #{example}, for example,
59
- the expected changes won't be reflected in that stale #{example_klass} object.
60
-
61
- #{unload_message}
62
-
63
- In order to autoload safely at boot time, please wrap your code in a reloader
64
- callback this way:
65
-
66
- Rails.application.reloader.to_prepare do
67
- # Autoload classes and modules needed at boot time here.
68
- end
29
+ unless config.cache_classes
30
+ autoloader.enable_reloading
31
+ ActiveSupport::Dependencies.autoloader = autoloader
69
32
 
70
- That block runs when the application boots, and every time there is a reload.
71
- For historical reasons, it may run twice, so it has to be idempotent.
72
-
73
- Check the "Autoloading and Reloading Constants" guide to learn more about how
74
- Rails autoloads and reloads.
75
- WARNING
76
- end
77
-
78
- initializer :let_zeitwerk_take_over do
79
- if config.autoloader == :zeitwerk
80
- require "active_support/dependencies/zeitwerk_integration"
81
- ActiveSupport::Dependencies::ZeitwerkIntegration.take_over(enable_reloading: !config.cache_classes)
82
- end
83
- end
84
-
85
- initializer :add_builtin_route do |app|
86
- if Rails.env.development?
87
- app.routes.prepend do
88
- get "/rails/info/properties" => "rails/info#properties", internal: true
89
- get "/rails/info/routes" => "rails/info#routes", internal: true
90
- get "/rails/info" => "rails/info#index", internal: true
91
- end
92
-
93
- app.routes.append do
94
- get "/" => "rails/welcome#index", internal: true
33
+ autoloader.on_load do |_cpath, value, _abspath|
34
+ if value.is_a?(Class) && value.singleton_class < ActiveSupport::DescendantsTracker
35
+ ActiveSupport::Dependencies._autoloaded_tracked_classes << value
36
+ end
95
37
  end
96
38
  end
39
+
40
+ autoloader.setup
97
41
  end
98
42
 
99
43
  # Setup default session store if not already set in config/application.rb
@@ -124,14 +68,17 @@ module Rails
124
68
  app.reloader.prepare!
125
69
  end
126
70
 
127
- initializer :eager_load! do
71
+ initializer :eager_load! do |app|
128
72
  if config.eager_load
129
73
  ActiveSupport.run_load_hooks(:before_eager_load, self)
130
- # Checks defined?(Zeitwerk) instead of zeitwerk_enabled? because we
131
- # want to eager load any dependency managed by Zeitwerk regardless of
132
- # the autoloading mode of the application.
133
- Zeitwerk::Loader.eager_load_all if defined?(Zeitwerk)
74
+ Zeitwerk::Loader.eager_load_all
134
75
  config.eager_load_namespaces.each(&:eager_load!)
76
+
77
+ unless config.cache_classes
78
+ app.reloader.after_class_unload do
79
+ Rails.autoloaders.main.eager_load
80
+ end
81
+ end
135
82
  end
136
83
  end
137
84
 
@@ -140,21 +87,21 @@ module Rails
140
87
  ActiveSupport.run_load_hooks(:after_initialize, self)
141
88
  end
142
89
 
143
- class MutexHook
144
- def initialize(mutex = Mutex.new)
145
- @mutex = mutex
90
+ class MonitorHook # :nodoc:
91
+ def initialize(monitor = Monitor.new)
92
+ @monitor = monitor
146
93
  end
147
94
 
148
95
  def run
149
- @mutex.lock
96
+ @monitor.enter
150
97
  end
151
98
 
152
99
  def complete(_state)
153
- @mutex.unlock
100
+ @monitor.exit
154
101
  end
155
102
  end
156
103
 
157
- module InterlockHook
104
+ module InterlockHook # :nodoc:
158
105
  def self.run
159
106
  ActiveSupport::Dependencies.interlock.start_running
160
107
  end
@@ -169,7 +116,7 @@ module Rails
169
116
  # User has explicitly opted out of concurrent request
170
117
  # handling: presumably their code is not threadsafe
171
118
 
172
- app.executor.register_hook(MutexHook.new, outer: true)
119
+ app.executor.register_hook(MonitorHook.new, outer: true)
173
120
 
174
121
  elsif config.allow_concurrency == :unsafe
175
122
  # Do nothing, even if we know this is dangerous. This is the
@@ -187,6 +134,22 @@ module Rails
187
134
  end
188
135
  end
189
136
 
137
+ initializer :add_internal_routes do |app|
138
+ if Rails.env.development?
139
+ app.routes.prepend do
140
+ get "/rails/info/properties" => "rails/info#properties", internal: true
141
+ get "/rails/info/routes" => "rails/info#routes", internal: true
142
+ get "/rails/info" => "rails/info#index", internal: true
143
+ end
144
+
145
+ routes_reloader.run_after_load_paths = -> do
146
+ app.routes.append do
147
+ get "/" => "rails/welcome#index", internal: true
148
+ end
149
+ end
150
+ end
151
+ end
152
+
190
153
  # Set routes reload after the finisher hook to ensure routes added in
191
154
  # the hook are taken into account.
192
155
  initializer :set_routes_reloader_hook do |app|
@@ -213,7 +176,8 @@ module Rails
213
176
  # added in the hook are taken into account.
214
177
  initializer :set_clear_dependencies_hook, group: :all do |app|
215
178
  callback = lambda do
216
- ActiveSupport::DescendantsTracker.clear
179
+ # Order matters.
180
+ ActiveSupport::DescendantsTracker.clear(ActiveSupport::Dependencies._autoloaded_tracked_classes)
217
181
  ActiveSupport::Dependencies.clear
218
182
  end
219
183
 
@@ -229,6 +193,7 @@ module Rails
229
193
 
230
194
  if config.cache_classes
231
195
  # No reloader
196
+ ActiveSupport::DescendantsTracker.disable_clear!
232
197
  elsif config.reload_classes_only_on_change
233
198
  reloader = config.file_watcher.new(*watchable_args, &callback)
234
199
  reloaders << reloader
@@ -251,13 +216,6 @@ module Rails
251
216
  end
252
217
  end
253
218
  end
254
-
255
- # Disable dependency loading during request cycle
256
- initializer :disable_dependency_loading do
257
- if config.eager_load && config.cache_classes && !config.enable_dependency_loading
258
- ActiveSupport::Dependencies.unhook!
259
- end
260
- end
261
219
  end
262
220
  end
263
221
  end
@@ -5,8 +5,11 @@ require "active_support/core_ext/module/delegation"
5
5
  module Rails
6
6
  class Application
7
7
  class RoutesReloader
8
+ include ActiveSupport::Callbacks
9
+
8
10
  attr_reader :route_sets, :paths, :external_routes
9
11
  attr_accessor :eager_load
12
+ attr_writer :run_after_load_paths # :nodoc:
10
13
  delegate :execute_if_updated, :execute, :updated?, to: :updater
11
14
 
12
15
  def initialize
@@ -45,6 +48,11 @@ module Rails
45
48
 
46
49
  def load_paths
47
50
  paths.each { |path| load(path) }
51
+ run_after_load_paths.call
52
+ end
53
+
54
+ def run_after_load_paths
55
+ @run_after_load_paths ||= -> { }
48
56
  end
49
57
 
50
58
  def finalize!
@@ -10,6 +10,7 @@ require "active_support/hash_with_indifferent_access"
10
10
  require "active_support/configuration_file"
11
11
  require "rails/engine"
12
12
  require "rails/secrets"
13
+ require "rails/autoloaders"
13
14
 
14
15
  module Rails
15
16
  # An Engine with the responsibility of coordinating the whole boot process.
@@ -21,12 +22,12 @@ module Rails
21
22
  # Rails::Application::Bootstrap) and finishing initializers, after all the others
22
23
  # are executed (check Rails::Application::Finisher).
23
24
  #
24
- # == Configuration
25
+ # == \Configuration
25
26
  #
26
27
  # Besides providing the same configuration as Rails::Engine and Rails::Railtie,
27
28
  # the application object has several specific configurations, for example
28
- # "cache_classes", "consider_all_requests_local", "filter_parameters",
29
- # "logger" and so forth.
29
+ # +cache_classes+, +consider_all_requests_local+, +filter_parameters+,
30
+ # +logger+, and so forth.
30
31
  #
31
32
  # Check Rails::Application::Configuration to see them all.
32
33
  #
@@ -42,44 +43,21 @@ module Rails
42
43
  # == Booting process
43
44
  #
44
45
  # The application is also responsible for setting up and executing the booting
45
- # process. From the moment you require "config/application.rb" in your app,
46
+ # process. From the moment you require <tt>config/application.rb</tt> in your app,
46
47
  # the booting process goes like this:
47
48
  #
48
- # 1) require "config/boot.rb" to set up load paths
49
- # 2) require railties and engines
50
- # 3) Define Rails.application as "class MyApp::Application < Rails::Application"
51
- # 4) Run config.before_configuration callbacks
52
- # 5) Load config/environments/ENV.rb
53
- # 6) Run config.before_initialize callbacks
54
- # 7) Run Railtie#initializer defined by railties, engines and application.
55
- # One by one, each engine sets up its load paths, routes and runs its config/initializers/* files.
56
- # 8) Custom Railtie#initializers added by railties, engines and applications are executed
57
- # 9) Build the middleware stack and run to_prepare callbacks
58
- # 10) Run config.before_eager_load and eager_load! if eager_load is true
59
- # 11) Run config.after_initialize callbacks
60
- #
61
- # == Multiple Applications
62
- #
63
- # If you decide to define multiple applications, then the first application
64
- # that is initialized will be set to +Rails.application+, unless you override
65
- # it with a different application.
66
- #
67
- # To create a new application, you can instantiate a new instance of a class
68
- # that has already been created:
69
- #
70
- # class Application < Rails::Application
71
- # end
72
- #
73
- # first_application = Application.new
74
- # second_application = Application.new(config: first_application.config)
75
- #
76
- # In the above example, the configuration from the first application was used
77
- # to initialize the second application. You can also use the +initialize_copy+
78
- # on one of the applications to create a copy of the application which shares
79
- # the configuration.
80
- #
81
- # If you decide to define Rake tasks, runners, or initializers in an
82
- # application other than +Rails.application+, then you must run them manually.
49
+ # 1. <tt>require "config/boot.rb"</tt> to set up load paths.
50
+ # 2. +require+ railties and engines.
51
+ # 3. Define +Rails.application+ as <tt>class MyApp::Application < Rails::Application</tt>.
52
+ # 4. Run +config.before_configuration+ callbacks.
53
+ # 5. Load <tt>config/environments/ENV.rb</tt>.
54
+ # 6. Run +config.before_initialize+ callbacks.
55
+ # 7. Run <tt>Railtie#initializer</tt> defined by railties, engines, and application.
56
+ # One by one, each engine sets up its load paths and routes, and runs its <tt>config/initializers/*</tt> files.
57
+ # 8. Custom <tt>Railtie#initializers</tt> added by railties, engines, and applications are executed.
58
+ # 9. Build the middleware stack and run +to_prepare+ callbacks.
59
+ # 10. Run +config.before_eager_load+ and +eager_load!+ if +eager_load+ is +true+.
60
+ # 11. Run +config.after_initialize+ callbacks.
83
61
  class Application < Engine
84
62
  autoload :Bootstrap, "rails/application/bootstrap"
85
63
  autoload :Configuration, "rails/application/configuration"
@@ -118,7 +96,7 @@ module Rails
118
96
 
119
97
  attr_accessor :assets, :sandbox
120
98
  alias_method :sandbox?, :sandbox
121
- attr_reader :reloaders, :reloader, :executor
99
+ attr_reader :reloaders, :reloader, :executor, :autoloaders
122
100
 
123
101
  delegate :default_url_options, :default_url_options=, to: :routes
124
102
 
@@ -140,6 +118,8 @@ module Rails
140
118
  @reloader = Class.new(ActiveSupport::Reloader)
141
119
  @reloader.executor = @executor
142
120
 
121
+ @autoloaders = Rails::Autoloaders.new
122
+
143
123
  # are these actually used?
144
124
  @initial_variable_values = initial_variable_values
145
125
  @block = block
@@ -195,7 +175,7 @@ module Rails
195
175
  # Rails.application.message_verifier('sensitive_data').verify(message)
196
176
  # # => 'my sensible data'
197
177
  #
198
- # See the +ActiveSupport::MessageVerifier+ documentation for more information.
178
+ # See the ActiveSupport::MessageVerifier documentation for more information.
199
179
  def message_verifier(verifier_name)
200
180
  @message_verifiers[verifier_name] ||= begin
201
181
  secret = key_generator.generate_key(verifier_name.to_s)
@@ -247,9 +227,11 @@ module Rails
247
227
  config, shared = all_configs[env.to_sym], all_configs[:shared]
248
228
 
249
229
  if shared
250
- config = {} if config.nil?
251
- if config.is_a?(Hash)
230
+ config = {} if config.nil? && shared.is_a?(Hash)
231
+ if config.is_a?(Hash) && shared.is_a?(Hash)
252
232
  config = shared.deep_merge(config)
233
+ elsif config.nil?
234
+ config = shared
253
235
  end
254
236
  end
255
237
 
@@ -266,13 +248,13 @@ module Rails
266
248
  # Stores some of the Rails initial environment parameters which
267
249
  # will be used by middlewares and engines to configure themselves.
268
250
  def env_config
269
- @app_env_config ||= begin
270
- super.merge(
251
+ @app_env_config ||= super.merge(
271
252
  "action_dispatch.parameter_filter" => config.filter_parameters,
272
253
  "action_dispatch.redirect_filter" => config.filter_redirect,
273
254
  "action_dispatch.secret_key_base" => secret_key_base,
274
255
  "action_dispatch.show_exceptions" => config.action_dispatch.show_exceptions,
275
256
  "action_dispatch.show_detailed_exceptions" => config.consider_all_requests_local,
257
+ "action_dispatch.log_rescued_responses" => config.action_dispatch.log_rescued_responses,
276
258
  "action_dispatch.logger" => Rails.logger,
277
259
  "action_dispatch.backtrace_cleaner" => Rails.backtrace_cleaner,
278
260
  "action_dispatch.key_generator" => key_generator,
@@ -295,7 +277,6 @@ module Rails
295
277
  "action_dispatch.content_security_policy_nonce_directives" => config.content_security_policy_nonce_directives,
296
278
  "action_dispatch.permissions_policy" => config.permissions_policy,
297
279
  )
298
- end
299
280
  end
300
281
 
301
282
  # If you try to define a set of Rake tasks on the instance, these will get
@@ -355,26 +336,26 @@ module Rails
355
336
  # are changing config.root inside your application definition or having a custom
356
337
  # Rails application, you will need to add lib to $LOAD_PATH on your own in case
357
338
  # you need to load files in lib/ during the application configuration as well.
358
- def self.add_lib_to_load_path!(root) #:nodoc:
339
+ def self.add_lib_to_load_path!(root) # :nodoc:
359
340
  path = File.join root, "lib"
360
341
  if File.exist?(path) && !$LOAD_PATH.include?(path)
361
342
  $LOAD_PATH.unshift(path)
362
343
  end
363
344
  end
364
345
 
365
- def require_environment! #:nodoc:
346
+ def require_environment! # :nodoc:
366
347
  environment = paths["config/environment"].existent.first
367
348
  require environment if environment
368
349
  end
369
350
 
370
- def routes_reloader #:nodoc:
351
+ def routes_reloader # :nodoc:
371
352
  @routes_reloader ||= RoutesReloader.new
372
353
  end
373
354
 
374
355
  # Returns an array of file paths appended with a hash of
375
356
  # directories-extensions suitable for ActiveSupport::FileUpdateChecker
376
357
  # API.
377
- def watchable_args #:nodoc:
358
+ def watchable_args # :nodoc:
378
359
  files, dirs = config.watchable_files.dup, config.watchable_dirs.dup
379
360
 
380
361
  ActiveSupport::Dependencies.autoload_paths.each do |path|
@@ -386,20 +367,20 @@ module Rails
386
367
 
387
368
  # Initialize the application passing the given group. By default, the
388
369
  # group is :default
389
- def initialize!(group = :default) #:nodoc:
370
+ def initialize!(group = :default) # :nodoc:
390
371
  raise "Application has been already initialized." if @initialized
391
372
  run_initializers(group, self)
392
373
  @initialized = true
393
374
  self
394
375
  end
395
376
 
396
- def initializers #:nodoc:
377
+ def initializers # :nodoc:
397
378
  Bootstrap.initializers_for(self) +
398
379
  railties_initializers(super) +
399
380
  Finisher.initializers_for(self)
400
381
  end
401
382
 
402
- def config #:nodoc:
383
+ def config # :nodoc:
403
384
  @config ||= Application::Configuration.new(self.class.find_root(self.class.called_from))
404
385
  end
405
386
 
@@ -422,13 +403,14 @@ module Rails
422
403
  attr_writer :secrets, :credentials
423
404
 
424
405
  # The secret_key_base is used as the input secret to the application's key generator, which in turn
425
- # is used to create all MessageVerifiers/MessageEncryptors, including the ones that sign and encrypt cookies.
406
+ # is used to create all ActiveSupport::MessageVerifier and ActiveSupport::MessageEncryptor instances,
407
+ # including the ones that sign and encrypt cookies.
426
408
  #
427
409
  # In development and test, this is randomly generated and stored in a
428
410
  # temporary file in <tt>tmp/development_secret.txt</tt>.
429
411
  #
430
- # In all other environments, we look for it first in ENV["SECRET_KEY_BASE"],
431
- # then credentials.secret_key_base, and finally secrets.secret_key_base. For most applications,
412
+ # In all other environments, we look for it first in <tt>ENV["SECRET_KEY_BASE"]</tt>,
413
+ # then +credentials.secret_key_base+, and finally +secrets.secret_key_base+. For most applications,
432
414
  # the correct place to store it is in the encrypted credentials file.
433
415
  def secret_key_base
434
416
  if Rails.env.development? || Rails.env.test?
@@ -440,44 +422,39 @@ module Rails
440
422
  end
441
423
  end
442
424
 
443
- # Decrypts the credentials hash as kept in +config/credentials.yml.enc+. This file is encrypted with
444
- # the Rails master key, which is either taken from <tt>ENV["RAILS_MASTER_KEY"]</tt> or from loading
445
- # +config/master.key+.
446
- # If specific credentials file exists for current environment, it takes precedence, thus for +production+
447
- # environment look first for +config/credentials/production.yml.enc+ with master key taken
448
- # from <tt>ENV["RAILS_MASTER_KEY"]</tt> or from loading +config/credentials/production.key+.
449
- # Default behavior can be overwritten by setting +config.credentials.content_path+ and +config.credentials.key_path+.
425
+ # Returns an ActiveSupport::EncryptedConfiguration instance for the
426
+ # credentials file specified by +config.credentials.content_path+.
427
+ #
428
+ # By default, +config.credentials.content_path+ will point to either
429
+ # <tt>config/credentials/#{environment}.yml.enc</tt> for the current
430
+ # environment (for example, +config/credentials/production.yml.enc+ for the
431
+ # +production+ environment), or +config/credentials.yml.enc+ if that file
432
+ # does not exist.
433
+ #
434
+ # The encryption key is taken from either <tt>ENV["RAILS_MASTER_KEY"]</tt>,
435
+ # or from the file specified by +config.credentials.key_path+. By default,
436
+ # +config.credentials.key_path+ will point to either
437
+ # <tt>config/credentials/#{environment}.key</tt> for the current
438
+ # environment, or +config/master.key+ if that file does not exist.
450
439
  def credentials
451
440
  @credentials ||= encrypted(config.credentials.content_path, key_path: config.credentials.key_path)
452
441
  end
453
442
 
454
- # Shorthand to decrypt any encrypted configurations or files.
455
- #
456
- # For any file added with <tt>rails encrypted:edit</tt> call +read+ to decrypt
457
- # the file with the master key.
458
- # The master key is either stored in +config/master.key+ or <tt>ENV["RAILS_MASTER_KEY"]</tt>.
459
- #
460
- # Rails.application.encrypted("config/mystery_man.txt.enc").read
461
- # # => "We've met before, haven't we?"
462
- #
463
- # It's also possible to interpret encrypted YAML files with +config+.
464
- #
465
- # Rails.application.encrypted("config/credentials.yml.enc").config
466
- # # => { next_guys_line: "I don't think so. Where was it you think we met?" }
443
+ # Returns an ActiveSupport::EncryptedConfiguration instance for an encrypted
444
+ # file. By default, the encryption key is taken from either
445
+ # <tt>ENV["RAILS_MASTER_KEY"]</tt>, or from the +config/master.key+ file.
467
446
  #
468
- # Any top-level configs are also accessible directly on the return value:
447
+ # my_config = Rails.application.encrypted("config/my_config.enc")
469
448
  #
470
- # Rails.application.encrypted("config/credentials.yml.enc").next_guys_line
471
- # # => "I don't think so. Where was it you think we met?"
449
+ # my_config.read
450
+ # # => "foo:\n bar: 123\n"
472
451
  #
473
- # The files or configs can also be encrypted with a custom key. To decrypt with
474
- # a key in the +ENV+, use:
452
+ # my_config.foo.bar
453
+ # # => 123
475
454
  #
476
- # Rails.application.encrypted("config/special_tokens.yml.enc", env_key: "SPECIAL_TOKENS")
477
- #
478
- # Or to decrypt with a file, that should be version control ignored, relative to +Rails.root+:
479
- #
480
- # Rails.application.encrypted("config/special_tokens.yml.enc", key_path: "config/special_tokens.key")
455
+ # Encrypted files can be edited with the <tt>bin/rails encrypted:edit</tt>
456
+ # command. (See the output of <tt>bin/rails encrypted:edit --help</tt> for
457
+ # more information.)
481
458
  def encrypted(path, key_path: "config/master.key", env_key: "RAILS_MASTER_KEY")
482
459
  ActiveSupport::EncryptedConfiguration.new(
483
460
  config_path: Rails.root.join(path),
@@ -487,11 +464,11 @@ module Rails
487
464
  )
488
465
  end
489
466
 
490
- def to_app #:nodoc:
467
+ def to_app # :nodoc:
491
468
  self
492
469
  end
493
470
 
494
- def helpers_paths #:nodoc:
471
+ def helpers_paths # :nodoc:
495
472
  config.helpers_paths
496
473
  end
497
474
 
@@ -513,17 +490,13 @@ module Rails
513
490
 
514
491
  # Eager loads the application code.
515
492
  def eager_load!
516
- if Rails.autoloaders.zeitwerk_enabled?
517
- Rails.autoloaders.each(&:eager_load)
518
- else
519
- super
520
- end
493
+ Rails.autoloaders.each(&:eager_load)
521
494
  end
522
495
 
523
496
  protected
524
497
  alias :build_middleware_stack :app
525
498
 
526
- def run_tasks_blocks(app) #:nodoc:
499
+ def run_tasks_blocks(app) # :nodoc:
527
500
  railties.each { |r| r.run_tasks_blocks(app) }
528
501
  super
529
502
  load "rails/tasks.rb"
@@ -534,28 +507,28 @@ module Rails
534
507
  end
535
508
  end
536
509
 
537
- def run_generators_blocks(app) #:nodoc:
510
+ def run_generators_blocks(app) # :nodoc:
538
511
  railties.each { |r| r.run_generators_blocks(app) }
539
512
  super
540
513
  end
541
514
 
542
- def run_runner_blocks(app) #:nodoc:
515
+ def run_runner_blocks(app) # :nodoc:
543
516
  railties.each { |r| r.run_runner_blocks(app) }
544
517
  super
545
518
  end
546
519
 
547
- def run_console_blocks(app) #:nodoc:
520
+ def run_console_blocks(app) # :nodoc:
548
521
  railties.each { |r| r.run_console_blocks(app) }
549
522
  super
550
523
  end
551
524
 
552
- def run_server_blocks(app) #:nodoc:
525
+ def run_server_blocks(app) # :nodoc:
553
526
  railties.each { |r| r.run_server_blocks(app) }
554
527
  super
555
528
  end
556
529
 
557
530
  # Returns the ordered railties for this application considering railties_order.
558
- def ordered_railties #:nodoc:
531
+ def ordered_railties # :nodoc:
559
532
  @ordered_railties ||= begin
560
533
  order = config.railties_order.map do |railtie|
561
534
  if railtie == :main_app
@@ -577,7 +550,7 @@ module Rails
577
550
  end
578
551
  end
579
552
 
580
- def railties_initializers(current) #:nodoc:
553
+ def railties_initializers(current) # :nodoc:
581
554
  initializers = []
582
555
  ordered_railties.reverse.flatten.each do |r|
583
556
  if r == self
@@ -589,7 +562,7 @@ module Rails
589
562
  initializers
590
563
  end
591
564
 
592
- def default_middleware_stack #:nodoc:
565
+ def default_middleware_stack # :nodoc:
593
566
  default_stack = DefaultMiddlewareStack.new(self, config, paths)
594
567
  default_stack.build_stack
595
568
  end
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Rails::ApplicationController < ActionController::Base # :nodoc:
4
- self.view_paths = File.expand_path("templates", __dir__)
4
+ prepend_view_path File.expand_path("templates", __dir__)
5
5
  layout "application"
6
6
 
7
7
  before_action :disable_content_security_policy_nonce!
8
8
 
9
9
  content_security_policy do |policy|
10
- policy.script_src :unsafe_inline
11
- policy.style_src :unsafe_inline
10
+ policy.script_src :self, :unsafe_inline
11
+ policy.style_src :self, :unsafe_inline
12
12
  end
13
13
 
14
14
  private
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/inflector"
4
+
5
+ module Rails
6
+ class Autoloaders
7
+ module Inflector # :nodoc:
8
+ # Concurrent::Map is not needed. This is a private class, and overrides
9
+ # must be defined while the application boots.
10
+ @overrides = {}
11
+
12
+ def self.camelize(basename, _abspath)
13
+ @overrides[basename] || basename.camelize
14
+ end
15
+
16
+ def self.inflect(overrides)
17
+ @overrides.merge!(overrides)
18
+ end
19
+ end
20
+ end
21
+ end