activesupport 5.2.7.1 → 6.1.4.6

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activesupport might be problematic. Click here for more details.

Files changed (187) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +399 -434
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +4 -3
  5. data/lib/active_support/actionable_error.rb +48 -0
  6. data/lib/active_support/array_inquirer.rb +4 -2
  7. data/lib/active_support/backtrace_cleaner.rb +29 -3
  8. data/lib/active_support/benchmarkable.rb +1 -1
  9. data/lib/active_support/cache/file_store.rb +34 -34
  10. data/lib/active_support/cache/mem_cache_store.rb +39 -24
  11. data/lib/active_support/cache/memory_store.rb +59 -33
  12. data/lib/active_support/cache/null_store.rb +8 -3
  13. data/lib/active_support/cache/redis_cache_store.rb +72 -45
  14. data/lib/active_support/cache/strategy/local_cache.rb +41 -26
  15. data/lib/active_support/cache.rb +148 -78
  16. data/lib/active_support/callbacks.rb +81 -64
  17. data/lib/active_support/concern.rb +70 -3
  18. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +18 -0
  19. data/lib/active_support/concurrency/share_lock.rb +0 -1
  20. data/lib/active_support/configurable.rb +10 -14
  21. data/lib/active_support/configuration_file.rb +51 -0
  22. data/lib/active_support/core_ext/array/access.rb +18 -6
  23. data/lib/active_support/core_ext/array/conversions.rb +5 -5
  24. data/lib/active_support/core_ext/array/extract.rb +21 -0
  25. data/lib/active_support/core_ext/array.rb +1 -1
  26. data/lib/active_support/core_ext/benchmark.rb +2 -2
  27. data/lib/active_support/core_ext/class/attribute.rb +32 -47
  28. data/lib/active_support/core_ext/class/subclasses.rb +17 -38
  29. data/lib/active_support/core_ext/date/calculations.rb +6 -5
  30. data/lib/active_support/core_ext/date/conversions.rb +2 -1
  31. data/lib/active_support/core_ext/date_and_time/calculations.rb +37 -47
  32. data/lib/active_support/core_ext/date_and_time/compatibility.rb +15 -0
  33. data/lib/active_support/core_ext/date_and_time/zones.rb +0 -1
  34. data/lib/active_support/core_ext/date_time/calculations.rb +1 -1
  35. data/lib/active_support/core_ext/date_time/conversions.rb +0 -1
  36. data/lib/active_support/core_ext/enumerable.rb +171 -75
  37. data/lib/active_support/core_ext/hash/conversions.rb +3 -3
  38. data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
  39. data/lib/active_support/core_ext/hash/except.rb +2 -2
  40. data/lib/active_support/core_ext/hash/keys.rb +1 -30
  41. data/lib/active_support/core_ext/hash/slice.rb +6 -27
  42. data/lib/active_support/core_ext/hash.rb +1 -2
  43. data/lib/active_support/core_ext/integer/multiple.rb +1 -1
  44. data/lib/active_support/core_ext/kernel.rb +0 -1
  45. data/lib/active_support/core_ext/load_error.rb +1 -1
  46. data/lib/active_support/core_ext/marshal.rb +2 -0
  47. data/lib/active_support/core_ext/module/attr_internal.rb +2 -2
  48. data/lib/active_support/core_ext/module/attribute_accessors.rb +30 -39
  49. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +17 -19
  50. data/lib/active_support/core_ext/module/concerning.rb +8 -2
  51. data/lib/active_support/core_ext/module/delegation.rb +76 -33
  52. data/lib/active_support/core_ext/module/introspection.rb +16 -15
  53. data/lib/active_support/core_ext/module/redefine_method.rb +8 -17
  54. data/lib/active_support/core_ext/module.rb +0 -1
  55. data/lib/active_support/core_ext/name_error.rb +29 -2
  56. data/lib/active_support/core_ext/numeric/conversions.rb +129 -129
  57. data/lib/active_support/core_ext/numeric.rb +0 -1
  58. data/lib/active_support/core_ext/object/blank.rb +1 -2
  59. data/lib/active_support/core_ext/object/deep_dup.rb +1 -1
  60. data/lib/active_support/core_ext/object/duplicable.rb +7 -114
  61. data/lib/active_support/core_ext/object/json.rb +14 -2
  62. data/lib/active_support/core_ext/object/try.rb +17 -7
  63. data/lib/active_support/core_ext/object/with_options.rb +1 -1
  64. data/lib/active_support/core_ext/range/compare_range.rb +34 -13
  65. data/lib/active_support/core_ext/range/conversions.rb +31 -29
  66. data/lib/active_support/core_ext/range/each.rb +0 -1
  67. data/lib/active_support/core_ext/range/include_time_with_zone.rb +8 -3
  68. data/lib/active_support/core_ext/regexp.rb +8 -5
  69. data/lib/active_support/core_ext/securerandom.rb +23 -3
  70. data/lib/active_support/core_ext/string/access.rb +5 -16
  71. data/lib/active_support/core_ext/string/conversions.rb +1 -0
  72. data/lib/active_support/core_ext/string/filters.rb +42 -1
  73. data/lib/active_support/core_ext/string/inflections.rb +45 -6
  74. data/lib/active_support/core_ext/string/inquiry.rb +1 -0
  75. data/lib/active_support/core_ext/string/multibyte.rb +6 -5
  76. data/lib/active_support/core_ext/string/output_safety.rb +70 -41
  77. data/lib/active_support/core_ext/string/starts_ends_with.rb +2 -2
  78. data/lib/active_support/core_ext/string/strip.rb +3 -1
  79. data/lib/active_support/core_ext/symbol/starts_ends_with.rb +14 -0
  80. data/lib/active_support/core_ext/symbol.rb +3 -0
  81. data/lib/active_support/core_ext/time/calculations.rb +51 -3
  82. data/lib/active_support/core_ext/time/conversions.rb +2 -0
  83. data/lib/active_support/core_ext/uri.rb +6 -1
  84. data/lib/active_support/core_ext.rb +1 -1
  85. data/lib/active_support/current_attributes/test_helper.rb +13 -0
  86. data/lib/active_support/current_attributes.rb +16 -2
  87. data/lib/active_support/dependencies/zeitwerk_integration.rb +117 -0
  88. data/lib/active_support/dependencies.rb +109 -34
  89. data/lib/active_support/deprecation/behaviors.rb +16 -3
  90. data/lib/active_support/deprecation/disallowed.rb +56 -0
  91. data/lib/active_support/deprecation/instance_delegator.rb +0 -1
  92. data/lib/active_support/deprecation/method_wrappers.rb +18 -23
  93. data/lib/active_support/deprecation/proxy_wrappers.rb +29 -6
  94. data/lib/active_support/deprecation/reporting.rb +50 -7
  95. data/lib/active_support/deprecation.rb +6 -1
  96. data/lib/active_support/descendants_tracker.rb +59 -9
  97. data/lib/active_support/duration/iso8601_parser.rb +2 -4
  98. data/lib/active_support/duration/iso8601_serializer.rb +18 -14
  99. data/lib/active_support/duration.rb +78 -30
  100. data/lib/active_support/encrypted_configuration.rb +0 -4
  101. data/lib/active_support/encrypted_file.rb +22 -4
  102. data/lib/active_support/environment_inquirer.rb +20 -0
  103. data/lib/active_support/evented_file_update_checker.rb +82 -117
  104. data/lib/active_support/execution_wrapper.rb +2 -1
  105. data/lib/active_support/file_update_checker.rb +0 -1
  106. data/lib/active_support/fork_tracker.rb +64 -0
  107. data/lib/active_support/gem_version.rb +4 -4
  108. data/lib/active_support/hash_with_indifferent_access.rb +70 -42
  109. data/lib/active_support/i18n.rb +1 -0
  110. data/lib/active_support/i18n_railtie.rb +15 -8
  111. data/lib/active_support/inflector/inflections.rb +2 -7
  112. data/lib/active_support/inflector/methods.rb +49 -58
  113. data/lib/active_support/inflector/transliterate.rb +47 -18
  114. data/lib/active_support/json/decoding.rb +25 -26
  115. data/lib/active_support/json/encoding.rb +11 -3
  116. data/lib/active_support/key_generator.rb +1 -33
  117. data/lib/active_support/lazy_load_hooks.rb +5 -2
  118. data/lib/active_support/locale/en.rb +33 -0
  119. data/lib/active_support/locale/en.yml +7 -3
  120. data/lib/active_support/log_subscriber.rb +39 -9
  121. data/lib/active_support/logger.rb +2 -17
  122. data/lib/active_support/logger_silence.rb +11 -19
  123. data/lib/active_support/logger_thread_safe_level.rb +50 -6
  124. data/lib/active_support/message_encryptor.rb +8 -13
  125. data/lib/active_support/message_verifier.rb +10 -10
  126. data/lib/active_support/messages/metadata.rb +11 -2
  127. data/lib/active_support/messages/rotation_configuration.rb +2 -1
  128. data/lib/active_support/messages/rotator.rb +10 -9
  129. data/lib/active_support/multibyte/chars.rb +10 -68
  130. data/lib/active_support/multibyte/unicode.rb +15 -327
  131. data/lib/active_support/notifications/fanout.rb +116 -16
  132. data/lib/active_support/notifications/instrumenter.rb +71 -9
  133. data/lib/active_support/notifications.rb +72 -8
  134. data/lib/active_support/number_helper/number_converter.rb +5 -6
  135. data/lib/active_support/number_helper/number_to_currency_converter.rb +4 -9
  136. data/lib/active_support/number_helper/number_to_delimited_converter.rb +3 -2
  137. data/lib/active_support/number_helper/number_to_human_converter.rb +4 -3
  138. data/lib/active_support/number_helper/number_to_human_size_converter.rb +4 -3
  139. data/lib/active_support/number_helper/number_to_percentage_converter.rb +3 -1
  140. data/lib/active_support/number_helper/number_to_phone_converter.rb +2 -1
  141. data/lib/active_support/number_helper/number_to_rounded_converter.rb +12 -7
  142. data/lib/active_support/number_helper/rounding_helper.rb +12 -28
  143. data/lib/active_support/number_helper.rb +38 -12
  144. data/lib/active_support/option_merger.rb +22 -3
  145. data/lib/active_support/ordered_hash.rb +1 -1
  146. data/lib/active_support/ordered_options.rb +13 -3
  147. data/lib/active_support/parameter_filter.rb +133 -0
  148. data/lib/active_support/per_thread_registry.rb +1 -1
  149. data/lib/active_support/rails.rb +1 -10
  150. data/lib/active_support/railtie.rb +23 -1
  151. data/lib/active_support/reloader.rb +4 -5
  152. data/lib/active_support/rescuable.rb +4 -4
  153. data/lib/active_support/secure_compare_rotator.rb +51 -0
  154. data/lib/active_support/security_utils.rb +19 -12
  155. data/lib/active_support/string_inquirer.rb +4 -3
  156. data/lib/active_support/subscriber.rb +72 -28
  157. data/lib/active_support/tagged_logging.rb +42 -8
  158. data/lib/active_support/test_case.rb +91 -0
  159. data/lib/active_support/testing/assertions.rb +30 -9
  160. data/lib/active_support/testing/deprecation.rb +0 -1
  161. data/lib/active_support/testing/file_fixtures.rb +2 -0
  162. data/lib/active_support/testing/isolation.rb +2 -2
  163. data/lib/active_support/testing/method_call_assertions.rb +28 -1
  164. data/lib/active_support/testing/parallelization/server.rb +78 -0
  165. data/lib/active_support/testing/parallelization/worker.rb +100 -0
  166. data/lib/active_support/testing/parallelization.rb +51 -0
  167. data/lib/active_support/testing/stream.rb +1 -2
  168. data/lib/active_support/testing/time_helpers.rb +47 -12
  169. data/lib/active_support/time_with_zone.rb +81 -47
  170. data/lib/active_support/values/time_zone.rb +32 -17
  171. data/lib/active_support/xml_mini/jdom.rb +2 -3
  172. data/lib/active_support/xml_mini/libxml.rb +2 -2
  173. data/lib/active_support/xml_mini/libxmlsax.rb +4 -4
  174. data/lib/active_support/xml_mini/nokogiri.rb +2 -2
  175. data/lib/active_support/xml_mini/nokogirisax.rb +3 -3
  176. data/lib/active_support/xml_mini/rexml.rb +10 -3
  177. data/lib/active_support/xml_mini.rb +2 -10
  178. data/lib/active_support.rb +14 -1
  179. metadata +55 -29
  180. data/lib/active_support/core_ext/array/prepend_and_append.rb +0 -9
  181. data/lib/active_support/core_ext/hash/compact.rb +0 -29
  182. data/lib/active_support/core_ext/hash/transform_values.rb +0 -32
  183. data/lib/active_support/core_ext/kernel/agnostics.rb +0 -13
  184. data/lib/active_support/core_ext/module/reachable.rb +0 -11
  185. data/lib/active_support/core_ext/numeric/inquiry.rb +0 -28
  186. data/lib/active_support/core_ext/range/include_range.rb +0 -3
  187. data/lib/active_support/values/unicode_tables.dat +0 -0
@@ -12,7 +12,6 @@ require "active_support/core_ext/object/blank"
12
12
  require "active_support/core_ext/kernel/reporting"
13
13
  require "active_support/core_ext/load_error"
14
14
  require "active_support/core_ext/name_error"
15
- require "active_support/core_ext/string/starts_ends_with"
16
15
  require "active_support/dependencies/interlock"
17
16
  require "active_support/inflector"
18
17
 
@@ -20,6 +19,9 @@ module ActiveSupport #:nodoc:
20
19
  module Dependencies #:nodoc:
21
20
  extend self
22
21
 
22
+ UNBOUND_METHOD_MODULE_NAME = Module.instance_method(:name)
23
+ private_constant :UNBOUND_METHOD_MODULE_NAME
24
+
23
25
  mattr_accessor :interlock, default: Interlock.new
24
26
 
25
27
  # :doc:
@@ -70,6 +72,11 @@ module ActiveSupport #:nodoc:
70
72
  # only once. All directories in this set must also be present in +autoload_paths+.
71
73
  mattr_accessor :autoload_once_paths, default: []
72
74
 
75
+ # This is a private set that collects all eager load paths during bootstrap.
76
+ # Useful for Zeitwerk integration. Its public interface is the config.* path
77
+ # accessors of each engine.
78
+ mattr_accessor :_eager_load_paths, default: Set.new
79
+
73
80
  # An array of qualified constant names that have been loaded. Adding a name
74
81
  # to this array will cause it to be unloaded the next time Dependencies are
75
82
  # cleared.
@@ -79,6 +86,12 @@ module ActiveSupport #:nodoc:
79
86
  # to allow arbitrary constants to be marked for unloading.
80
87
  mattr_accessor :explicitly_unloadable_constants, default: []
81
88
 
89
+ # The logger used when tracing autoloads.
90
+ mattr_accessor :logger
91
+
92
+ # If true, trace autoloads with +logger.debug+.
93
+ mattr_accessor :verbose, default: false
94
+
82
95
  # The WatchStack keeps a stack of the modules being watched as files are
83
96
  # loaded. If a file in the process of being loaded (parent.rb) triggers the
84
97
  # load of another file (child.rb) the stack will ensure that child.rb
@@ -140,7 +153,7 @@ module ActiveSupport #:nodoc:
140
153
 
141
154
  # Normalize the list of new constants, and add them to the list we will return
142
155
  new_constants.each do |suffix|
143
- constants << ([namespace, suffix] - ["Object"]).join("::".freeze)
156
+ constants << ([namespace, suffix] - ["Object"]).join("::")
144
157
  end
145
158
  end
146
159
  constants
@@ -190,6 +203,11 @@ module ActiveSupport #:nodoc:
190
203
  end
191
204
  end
192
205
 
206
+ def self.include_into(base)
207
+ base.include(self)
208
+ append_features(base)
209
+ end
210
+
193
211
  def const_missing(const_name)
194
212
  from_mod = anonymous? ? guess_for_anonymous(const_name) : self
195
213
  Dependencies.load_missing_constant(from_mod, const_name)
@@ -219,6 +237,21 @@ module ActiveSupport #:nodoc:
219
237
  base.class_eval do
220
238
  define_method(:load, Kernel.instance_method(:load))
221
239
  private :load
240
+
241
+ define_method(:require, Kernel.instance_method(:require))
242
+ private :require
243
+ end
244
+ end
245
+
246
+ def self.include_into(base)
247
+ base.include(self)
248
+
249
+ if base.instance_method(:load).owner == base
250
+ base.remove_method(:load)
251
+ end
252
+
253
+ if base.instance_method(:require).owner == base
254
+ base.remove_method(:require)
222
255
  end
223
256
  end
224
257
 
@@ -228,15 +261,24 @@ module ActiveSupport #:nodoc:
228
261
 
229
262
  # :doc:
230
263
 
231
- # Interprets a file using <tt>mechanism</tt> and marks its defined
232
- # constants as autoloaded. <tt>file_name</tt> can be either a string or
264
+ # <b>Warning:</b> This method is obsolete in +:zeitwerk+ mode. In
265
+ # +:zeitwerk+ mode semantics match Ruby's and you do not need to be
266
+ # defensive with load order. Just refer to classes and modules normally.
267
+ # If the constant name is dynamic, camelize if needed, and constantize.
268
+ #
269
+ # In +:classic+ mode, interprets a file using +mechanism+ and marks its
270
+ # defined constants as autoloaded. +file_name+ can be either a string or
233
271
  # respond to <tt>to_path</tt>.
234
272
  #
235
- # Use this method in code that absolutely needs a certain constant to be
236
- # defined at that point. A typical use case is to make constant name
237
- # resolution deterministic for constants with the same relative name in
238
- # different namespaces whose evaluation would depend on load order
239
- # otherwise.
273
+ # In +:classic+ mode, use this method in code that absolutely needs a
274
+ # certain constant to be defined at that point. A typical use case is to
275
+ # make constant name resolution deterministic for constants with the same
276
+ # relative name in different namespaces whose evaluation would depend on
277
+ # load order otherwise.
278
+ #
279
+ # Engines that do not control the mode in which their parent application
280
+ # runs should call +require_dependency+ where needed in case the runtime
281
+ # mode is +:classic+.
240
282
  def require_dependency(file_name, message = "No such file to load -- %s.rb")
241
283
  file_name = file_name.to_path if file_name.respond_to?(:to_path)
242
284
  unless file_name.is_a?(String)
@@ -279,7 +321,6 @@ module ActiveSupport #:nodoc:
279
321
  end
280
322
 
281
323
  private
282
-
283
324
  def load(file, wrap = false)
284
325
  result = false
285
326
  load_dependency(file) { result = super }
@@ -315,9 +356,9 @@ module ActiveSupport #:nodoc:
315
356
  end
316
357
 
317
358
  def hook!
318
- Object.class_eval { include Loadable }
319
- Module.class_eval { include ModuleConstMissing }
320
- Exception.class_eval { include Blamable }
359
+ Loadable.include_into(Object)
360
+ ModuleConstMissing.include_into(Module)
361
+ Exception.include(Blamable)
321
362
  end
322
363
 
323
364
  def unhook!
@@ -334,7 +375,12 @@ module ActiveSupport #:nodoc:
334
375
  require_or_load(path || file_name)
335
376
  rescue LoadError => load_error
336
377
  if file_name = load_error.message[/ -- (.*?)(\.rb)?$/, 1]
337
- load_error.message.replace(message % file_name)
378
+ load_error_message = if load_error.respond_to?(:original_message)
379
+ load_error.original_message
380
+ else
381
+ load_error.message
382
+ end
383
+ load_error_message.replace(message % file_name)
338
384
  load_error.copy_blame!(load_error)
339
385
  end
340
386
  raise
@@ -349,7 +395,7 @@ module ActiveSupport #:nodoc:
349
395
  end
350
396
 
351
397
  def require_or_load(file_name, const_path = nil)
352
- file_name = $` if file_name =~ /\.rb\z/
398
+ file_name = file_name.chomp(".rb")
353
399
  expanded = File.expand_path(file_name)
354
400
  return if loaded.include?(expanded)
355
401
 
@@ -399,7 +445,7 @@ module ActiveSupport #:nodoc:
399
445
  # constant paths which would cause Dependencies to attempt to load this
400
446
  # file.
401
447
  def loadable_constants_for_path(path, bases = autoload_paths)
402
- path = $` if path =~ /\.rb\z/
448
+ path = path.chomp(".rb")
403
449
  expanded_path = File.expand_path(path)
404
450
  paths = []
405
451
 
@@ -408,7 +454,7 @@ module ActiveSupport #:nodoc:
408
454
  next unless expanded_path.start_with?(expanded_root)
409
455
 
410
456
  root_size = expanded_root.size
411
- next if expanded_path[root_size] != ?/.freeze
457
+ next if expanded_path[root_size] != ?/
412
458
 
413
459
  nesting = expanded_path[(root_size + 1)..-1]
414
460
  paths << nesting.camelize unless nesting.blank?
@@ -420,7 +466,7 @@ module ActiveSupport #:nodoc:
420
466
 
421
467
  # Search for a file in autoload_paths matching the provided suffix.
422
468
  def search_for_file(path_suffix)
423
- path_suffix = path_suffix.sub(/(\.rb)?$/, ".rb".freeze)
469
+ path_suffix += ".rb" unless path_suffix.end_with?(".rb")
424
470
 
425
471
  autoload_paths.each do |root|
426
472
  path = File.join(root, path_suffix)
@@ -440,9 +486,9 @@ module ActiveSupport #:nodoc:
440
486
  end
441
487
 
442
488
  def load_once_path?(path)
443
- # to_s works around a ruby issue where String#starts_with?(Pathname)
489
+ # to_s works around a ruby issue where String#start_with?(Pathname)
444
490
  # will raise a TypeError: no implicit conversion of Pathname into String
445
- autoload_once_paths.any? { |base| path.starts_with? base.to_s }
491
+ autoload_once_paths.any? { |base| path.start_with?(base.to_s) }
446
492
  end
447
493
 
448
494
  # Attempt to autoload the provided module name by searching for a directory
@@ -454,6 +500,7 @@ module ActiveSupport #:nodoc:
454
500
  return nil unless base_path = autoloadable_module?(path_suffix)
455
501
  mod = Module.new
456
502
  into.const_set const_name, mod
503
+ log("constant #{qualified_name} autoloaded (module autovivified from #{File.join(base_path, path_suffix)})")
457
504
  autoloaded_constants << qualified_name unless autoload_once_paths.include?(base_path)
458
505
  autoloaded_constants.uniq!
459
506
  mod
@@ -491,30 +538,36 @@ module ActiveSupport #:nodoc:
491
538
  # it is not possible to load the constant into from_mod, try its parent
492
539
  # module using +const_missing+.
493
540
  def load_missing_constant(from_mod, const_name)
494
- unless qualified_const_defined?(from_mod.name) && Inflector.constantize(from_mod.name).equal?(from_mod)
541
+ from_mod_name = real_mod_name(from_mod)
542
+ unless qualified_const_defined?(from_mod_name) && Inflector.constantize(from_mod_name).equal?(from_mod)
495
543
  raise ArgumentError, "A copy of #{from_mod} has been removed from the module tree but is still active!"
496
544
  end
497
545
 
498
- qualified_name = qualified_name_for from_mod, const_name
546
+ qualified_name = qualified_name_for(from_mod, const_name)
499
547
  path_suffix = qualified_name.underscore
500
548
 
501
549
  file_path = search_for_file(path_suffix)
502
550
 
503
551
  if file_path
504
552
  expanded = File.expand_path(file_path)
505
- expanded.sub!(/\.rb\z/, "".freeze)
553
+ expanded.delete_suffix!(".rb")
506
554
 
507
555
  if loading.include?(expanded)
508
556
  raise "Circular dependency detected while autoloading constant #{qualified_name}"
509
557
  else
510
558
  require_or_load(expanded, qualified_name)
511
- raise LoadError, "Unable to autoload constant #{qualified_name}, expected #{file_path} to define it" unless from_mod.const_defined?(const_name, false)
512
- return from_mod.const_get(const_name)
559
+
560
+ if from_mod.const_defined?(const_name, false)
561
+ log("constant #{qualified_name} autoloaded from #{expanded}.rb")
562
+ return from_mod.const_get(const_name)
563
+ else
564
+ raise LoadError, "Unable to autoload constant #{qualified_name}, expected #{file_path} to define it"
565
+ end
513
566
  end
514
567
  elsif mod = autoload_module!(from_mod, const_name, qualified_name, path_suffix)
515
568
  return mod
516
- elsif (parent = from_mod.parent) && parent != from_mod &&
517
- ! from_mod.parents.any? { |p| p.const_defined?(const_name, false) }
569
+ elsif (parent = from_mod.module_parent) && parent != from_mod &&
570
+ ! from_mod.module_parents.any? { |p| p.const_defined?(const_name, false) }
518
571
  # If our parents do not have a constant named +const_name+ then we are free
519
572
  # to attempt to load upwards. If they do have such a constant, then this
520
573
  # const_missing must be due to from_mod::const_name, which should not
@@ -545,8 +598,8 @@ module ActiveSupport #:nodoc:
545
598
  end
546
599
  end
547
600
 
548
- name_error = NameError.new("uninitialized constant #{qualified_name}", const_name)
549
- name_error.set_backtrace(caller.reject { |l| l.starts_with? __FILE__ })
601
+ name_error = uninitialized_constant(qualified_name, const_name, receiver: from_mod)
602
+ name_error.set_backtrace(caller.reject { |l| l.start_with? __FILE__ })
550
603
  raise name_error
551
604
  end
552
605
 
@@ -558,6 +611,7 @@ module ActiveSupport #:nodoc:
558
611
  # as the environment will be in an inconsistent state, e.g. other constants
559
612
  # may have already been unloaded and not accessible.
560
613
  def remove_unloadable_constants!
614
+ log("removing unloadable constants")
561
615
  autoloaded_constants.each { |const| remove_constant const }
562
616
  autoloaded_constants.clear
563
617
  Reference.clear!
@@ -621,7 +675,7 @@ module ActiveSupport #:nodoc:
621
675
 
622
676
  # Determine if the given constant has been automatically loaded.
623
677
  def autoloaded?(desc)
624
- return false if desc.is_a?(Module) && desc.anonymous?
678
+ return false if desc.is_a?(Module) && real_mod_name(desc).nil?
625
679
  name = to_constant_name desc
626
680
  return false unless qualified_const_defined?(name)
627
681
  autoloaded_constants.include?(name)
@@ -674,10 +728,10 @@ module ActiveSupport #:nodoc:
674
728
  # A module, class, symbol, or string may be provided.
675
729
  def to_constant_name(desc) #:nodoc:
676
730
  case desc
677
- when String then desc.sub(/^::/, "")
731
+ when String then desc.delete_prefix("::")
678
732
  when Symbol then desc.to_s
679
733
  when Module
680
- desc.name ||
734
+ real_mod_name(desc) ||
681
735
  raise(ArgumentError, "Anonymous modules have no name to be referenced by")
682
736
  else raise TypeError, "Not a valid constant descriptor: #{desc.inspect}"
683
737
  end
@@ -685,7 +739,7 @@ module ActiveSupport #:nodoc:
685
739
 
686
740
  def remove_constant(const) #:nodoc:
687
741
  # Normalize ::Foo, ::Object::Foo, Object::Foo, Object::Object::Foo, etc. as Foo.
688
- normalized = const.to_s.sub(/\A::/, "")
742
+ normalized = const.to_s.delete_prefix("::")
689
743
  normalized.sub!(/\A(Object::)+/, "")
690
744
 
691
745
  constants = normalized.split("::")
@@ -695,7 +749,7 @@ module ActiveSupport #:nodoc:
695
749
  file_path = search_for_file(const.underscore)
696
750
  if file_path
697
751
  expanded = File.expand_path(file_path)
698
- expanded.sub!(/\.rb\z/, "")
752
+ expanded.delete_suffix!(".rb")
699
753
  loaded.delete(expanded)
700
754
  end
701
755
 
@@ -747,6 +801,27 @@ module ActiveSupport #:nodoc:
747
801
  # The constant is no longer reachable, just skip it.
748
802
  end
749
803
  end
804
+
805
+ def log(message)
806
+ logger.debug("autoloading: #{message}") if logger && verbose
807
+ end
808
+
809
+ private
810
+ if RUBY_VERSION < "2.6"
811
+ def uninitialized_constant(qualified_name, const_name, receiver:)
812
+ NameError.new("uninitialized constant #{qualified_name}", const_name)
813
+ end
814
+ else
815
+ def uninitialized_constant(qualified_name, const_name, receiver:)
816
+ NameError.new("uninitialized constant #{qualified_name}", const_name, receiver: receiver)
817
+ end
818
+ end
819
+
820
+ # Returns the original name of a class or module even if `name` has been
821
+ # overridden.
822
+ def real_mod_name(mod)
823
+ UNBOUND_METHOD_MODULE_NAME.bind(mod).call
824
+ end
750
825
  end
751
826
  end
752
827
 
@@ -43,7 +43,7 @@ module ActiveSupport
43
43
  deprecation_horizon: deprecation_horizon)
44
44
  },
45
45
 
46
- silence: ->(message, callstack, deprecation_horizon, gem_name) {},
46
+ silence: ->(message, callstack, deprecation_horizon, gem_name) { },
47
47
  }
48
48
 
49
49
  # Behavior module allows to determine how to display deprecation messages.
@@ -51,7 +51,7 @@ module ActiveSupport
51
51
  # constant. Available behaviors are:
52
52
  #
53
53
  # [+raise+] Raise <tt>ActiveSupport::DeprecationException</tt>.
54
- # [+stderr+] Log all deprecation warnings to +$stderr+.
54
+ # [+stderr+] Log all deprecation warnings to <tt>$stderr</tt>.
55
55
  # [+log+] Log all deprecation warnings to +Rails.logger+.
56
56
  # [+notify+] Use +ActiveSupport::Notifications+ to notify +deprecation.rails+.
57
57
  # [+silence+] Do nothing.
@@ -67,13 +67,18 @@ module ActiveSupport
67
67
  @behavior ||= [DEFAULT_BEHAVIORS[:stderr]]
68
68
  end
69
69
 
70
+ # Returns the current behavior for disallowed deprecations or if one isn't set, defaults to +:raise+.
71
+ def disallowed_behavior
72
+ @disallowed_behavior ||= [DEFAULT_BEHAVIORS[:raise]]
73
+ end
74
+
70
75
  # Sets the behavior to the specified value. Can be a single value, array,
71
76
  # or an object that responds to +call+.
72
77
  #
73
78
  # Available behaviors:
74
79
  #
75
80
  # [+raise+] Raise <tt>ActiveSupport::DeprecationException</tt>.
76
- # [+stderr+] Log all deprecation warnings to +$stderr+.
81
+ # [+stderr+] Log all deprecation warnings to <tt>$stderr</tt>.
77
82
  # [+log+] Log all deprecation warnings to +Rails.logger+.
78
83
  # [+notify+] Use +ActiveSupport::Notifications+ to notify +deprecation.rails+.
79
84
  # [+silence+] Do nothing.
@@ -92,6 +97,14 @@ module ActiveSupport
92
97
  @behavior = Array(behavior).map { |b| DEFAULT_BEHAVIORS[b] || arity_coerce(b) }
93
98
  end
94
99
 
100
+ # Sets the behavior for disallowed deprecations (those configured by
101
+ # ActiveSupport::Deprecation.disallowed_warnings=) to the specified
102
+ # value. As with +behavior=+, this can be a single value, array, or an
103
+ # object that responds to +call+.
104
+ def disallowed_behavior=(behavior)
105
+ @disallowed_behavior = Array(behavior).map { |b| DEFAULT_BEHAVIORS[b] || arity_coerce(b) }
106
+ end
107
+
95
108
  private
96
109
  def arity_coerce(behavior)
97
110
  unless behavior.respond_to?(:call)
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveSupport
4
+ class Deprecation
5
+ module Disallowed
6
+ # Sets the criteria used to identify deprecation messages which should be
7
+ # disallowed. Can be an array containing strings, symbols, or regular
8
+ # expressions. (Symbols are treated as strings). These are compared against
9
+ # the text of the generated deprecation warning.
10
+ #
11
+ # Additionally the scalar symbol +:all+ may be used to treat all
12
+ # deprecations as disallowed.
13
+ #
14
+ # Deprecations matching a substring or regular expression will be handled
15
+ # using the configured +ActiveSupport::Deprecation.disallowed_behavior+
16
+ # rather than +ActiveSupport::Deprecation.behavior+
17
+ attr_writer :disallowed_warnings
18
+
19
+ # Returns the configured criteria used to identify deprecation messages
20
+ # which should be treated as disallowed.
21
+ def disallowed_warnings
22
+ @disallowed_warnings ||= []
23
+ end
24
+
25
+ private
26
+ def deprecation_disallowed?(message)
27
+ disallowed = ActiveSupport::Deprecation.disallowed_warnings
28
+ return false if explicitly_allowed?(message)
29
+ return true if disallowed == :all
30
+ disallowed.any? do |rule|
31
+ case rule
32
+ when String, Symbol
33
+ message.include?(rule.to_s)
34
+ when Regexp
35
+ rule.match?(message)
36
+ end
37
+ end
38
+ end
39
+
40
+ def explicitly_allowed?(message)
41
+ allowances = @explicitly_allowed_warnings.value
42
+ return false unless allowances
43
+ return true if allowances == :all
44
+ allowances = [allowances] unless allowances.kind_of?(Array)
45
+ allowances.any? do |rule|
46
+ case rule
47
+ when String, Symbol
48
+ message.include?(rule.to_s)
49
+ when Regexp
50
+ rule.match?(message)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_support/core_ext/kernel/singleton_class"
4
3
  require "active_support/core_ext/module/delegation"
5
4
 
6
5
  module ActiveSupport
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_support/core_ext/module/aliasing"
4
3
  require "active_support/core_ext/array/extract_options"
4
+ require "active_support/core_ext/module/redefine_method"
5
5
 
6
6
  module ActiveSupport
7
7
  class Deprecation
@@ -53,37 +53,32 @@ module ActiveSupport
53
53
  options = method_names.extract_options!
54
54
  deprecator = options.delete(:deprecator) || self
55
55
  method_names += options.keys
56
- mod = Module.new
56
+ mod = nil
57
57
 
58
58
  method_names.each do |method_name|
59
+ message = options[method_name]
59
60
  if target_module.method_defined?(method_name) || target_module.private_method_defined?(method_name)
60
- aliased_method, punctuation = method_name.to_s.sub(/([?!=])$/, ""), $1
61
- with_method = "#{aliased_method}_with_deprecation#{punctuation}"
62
- without_method = "#{aliased_method}_without_deprecation#{punctuation}"
63
-
64
- target_module.send(:define_method, with_method) do |*args, &block|
65
- deprecator.deprecation_warning(method_name, options[method_name])
66
- send(without_method, *args, &block)
67
- end
68
-
69
- target_module.send(:alias_method, without_method, method_name)
70
- target_module.send(:alias_method, method_name, with_method)
71
-
72
- case
73
- when target_module.protected_method_defined?(without_method)
74
- target_module.send(:protected, method_name)
75
- when target_module.private_method_defined?(without_method)
76
- target_module.send(:private, method_name)
61
+ method = target_module.instance_method(method_name)
62
+ target_module.module_eval do
63
+ redefine_method(method_name) do |*args, &block|
64
+ deprecator.deprecation_warning(method_name, message)
65
+ method.bind(self).call(*args, &block)
66
+ end
67
+ ruby2_keywords(method_name) if respond_to?(:ruby2_keywords, true)
77
68
  end
78
69
  else
79
- mod.send(:define_method, method_name) do |*args, &block|
80
- deprecator.deprecation_warning(method_name, options[method_name])
81
- super(*args, &block)
70
+ mod ||= Module.new
71
+ mod.module_eval do
72
+ define_method(method_name) do |*args, &block|
73
+ deprecator.deprecation_warning(method_name, message)
74
+ super(*args, &block)
75
+ end
76
+ ruby2_keywords(method_name) if respond_to?(:ruby2_keywords, true)
82
77
  end
83
78
  end
84
79
  end
85
80
 
86
- target_module.prepend(mod) unless mod.instance_methods(false).empty?
81
+ target_module.prepend(mod) if mod
87
82
  end
88
83
  end
89
84
  end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_support/core_ext/regexp"
4
-
5
3
  module ActiveSupport
6
4
  class Deprecation
7
5
  class DeprecationProxy #:nodoc:
@@ -122,9 +120,16 @@ module ActiveSupport
122
120
  # # => DEPRECATION WARNING: PLANETS is deprecated! Use PLANETS_POST_2006 instead.
123
121
  # (Backtrace information…)
124
122
  # ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"]
125
- class DeprecatedConstantProxy < DeprecationProxy
123
+ class DeprecatedConstantProxy < Module
124
+ def self.new(*args, **options, &block)
125
+ object = args.first
126
+
127
+ return object unless object
128
+ super
129
+ end
130
+
126
131
  def initialize(old_const, new_const, deprecator = ActiveSupport::Deprecation.instance, message: "#{old_const} is deprecated! Use #{new_const} instead.")
127
- require "active_support/inflector/methods"
132
+ Kernel.require "active_support/inflector/methods"
128
133
 
129
134
  @old_const = old_const
130
135
  @new_const = new_const
@@ -132,6 +137,18 @@ module ActiveSupport
132
137
  @message = message
133
138
  end
134
139
 
140
+ instance_methods.each { |m| undef_method m unless /^__|^object_id$/.match?(m) }
141
+
142
+ # Don't give a deprecation warning on inspect since test/unit and error
143
+ # logs rely on it for diagnostics.
144
+ def inspect
145
+ target.inspect
146
+ end
147
+
148
+ # Don't give a deprecation warning on methods that IRB may invoke
149
+ # during tab-completion.
150
+ delegate :hash, :instance_methods, :name, :respond_to?, to: :target
151
+
135
152
  # Returns the class of the new constant.
136
153
  #
137
154
  # PLANETS_POST_2006 = %w(mercury venus earth mars jupiter saturn uranus neptune)
@@ -146,8 +163,14 @@ module ActiveSupport
146
163
  ActiveSupport::Inflector.constantize(@new_const.to_s)
147
164
  end
148
165
 
149
- def warn(callstack, called, args)
150
- @deprecator.warn(@message, callstack)
166
+ def const_missing(name)
167
+ @deprecator.warn(@message, caller_locations)
168
+ target.const_get(name)
169
+ end
170
+
171
+ def method_missing(called, *args, &block)
172
+ @deprecator.warn(@message, caller_locations)
173
+ target.__send__(called, *args, &block)
151
174
  end
152
175
  end
153
176
  end
@@ -6,7 +6,7 @@ module ActiveSupport
6
6
  class Deprecation
7
7
  module Reporting
8
8
  # Whether to print a message (silent mode)
9
- attr_accessor :silenced
9
+ attr_writer :silenced
10
10
  # Name of gem where method is deprecated
11
11
  attr_accessor :gem_name
12
12
 
@@ -20,7 +20,11 @@ module ActiveSupport
20
20
 
21
21
  callstack ||= caller_locations(2)
22
22
  deprecation_message(callstack, message).tap do |m|
23
- behavior.each { |b| b.call(m, callstack, deprecation_horizon, gem_name) }
23
+ if deprecation_disallowed?(message)
24
+ disallowed_behavior.each { |b| b.call(m, callstack, deprecation_horizon, gem_name) }
25
+ else
26
+ behavior.each { |b| b.call(m, callstack, deprecation_horizon, gem_name) }
27
+ end
24
28
  end
25
29
  end
26
30
 
@@ -33,11 +37,50 @@ module ActiveSupport
33
37
  # ActiveSupport::Deprecation.warn('something broke!')
34
38
  # end
35
39
  # # => nil
36
- def silence
37
- old_silenced, @silenced = @silenced, true
38
- yield
39
- ensure
40
- @silenced = old_silenced
40
+ def silence(&block)
41
+ @silenced_thread.bind(true, &block)
42
+ end
43
+
44
+ # Allow previously disallowed deprecation warnings within the block.
45
+ # <tt>allowed_warnings</tt> can be an array containing strings, symbols, or regular
46
+ # expressions. (Symbols are treated as strings). These are compared against
47
+ # the text of deprecation warning messages generated within the block.
48
+ # Matching warnings will be exempt from the rules set by
49
+ # +ActiveSupport::Deprecation.disallowed_warnings+
50
+ #
51
+ # The optional <tt>if:</tt> argument accepts a truthy/falsy value or an object that
52
+ # responds to <tt>.call</tt>. If truthy, then matching warnings will be allowed.
53
+ # If falsey then the method yields to the block without allowing the warning.
54
+ #
55
+ # ActiveSupport::Deprecation.disallowed_behavior = :raise
56
+ # ActiveSupport::Deprecation.disallowed_warnings = [
57
+ # "something broke"
58
+ # ]
59
+ #
60
+ # ActiveSupport::Deprecation.warn('something broke!')
61
+ # # => ActiveSupport::DeprecationException
62
+ #
63
+ # ActiveSupport::Deprecation.allow ['something broke'] do
64
+ # ActiveSupport::Deprecation.warn('something broke!')
65
+ # end
66
+ # # => nil
67
+ #
68
+ # ActiveSupport::Deprecation.allow ['something broke'], if: Rails.env.production? do
69
+ # ActiveSupport::Deprecation.warn('something broke!')
70
+ # end
71
+ # # => ActiveSupport::DeprecationException for dev/test, nil for production
72
+ def allow(allowed_warnings = :all, if: true, &block)
73
+ conditional = binding.local_variable_get(:if)
74
+ conditional = conditional.call if conditional.respond_to?(:call)
75
+ if conditional
76
+ @explicitly_allowed_warnings.bind(allowed_warnings, &block)
77
+ else
78
+ yield
79
+ end
80
+ end
81
+
82
+ def silenced
83
+ @silenced || @silenced_thread.value
41
84
  end
42
85
 
43
86
  def deprecation_warning(deprecated_method_name, message = nil, caller_backtrace = nil)