Skip to content

Commit 6983a89

Browse files
committed
Deletes the classic implementation of require_dependency
1 parent 5422751 commit 6983a89

File tree

4 files changed

+40
-35
lines changed

4 files changed

+40
-35
lines changed

activesupport/lib/active_support/dependencies.rb

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
module ActiveSupport # :nodoc:
1919
module Dependencies # :nodoc:
20+
require_relative "dependencies/require_dependency"
21+
2022
extend self
2123

2224
UNBOUND_METHOD_MODULE_NAME = Module.instance_method(:name)
@@ -250,37 +252,6 @@ def require_or_load(file_name)
250252
Dependencies.require_or_load(file_name)
251253
end
252254

253-
# :doc:
254-
255-
# <b>Warning:</b> This method is obsolete in +:zeitwerk+ mode. In
256-
# +:zeitwerk+ mode semantics match Ruby's and you do not need to be
257-
# defensive with load order. Just refer to classes and modules normally.
258-
# If the constant name is dynamic, camelize if needed, and constantize.
259-
#
260-
# In +:classic+ mode, interprets a file using +mechanism+ and marks its
261-
# defined constants as autoloaded. +file_name+ can be either a string or
262-
# respond to <tt>to_path</tt>.
263-
#
264-
# In +:classic+ mode, use this method in code that absolutely needs a
265-
# certain constant to be defined at that point. A typical use case is to
266-
# make constant name resolution deterministic for constants with the same
267-
# relative name in different namespaces whose evaluation would depend on
268-
# load order otherwise.
269-
#
270-
# Engines that do not control the mode in which their parent application
271-
# runs should call +require_dependency+ where needed in case the runtime
272-
# mode is +:classic+.
273-
def require_dependency(file_name, message = "No such file to load -- %s.rb")
274-
file_name = file_name.to_path if file_name.respond_to?(:to_path)
275-
unless file_name.is_a?(String)
276-
raise ArgumentError, "the file name must either be a String or implement #to_path -- you passed #{file_name.inspect}"
277-
end
278-
279-
Dependencies.depend_on(file_name, message)
280-
end
281-
282-
# :nodoc:
283-
284255
def load_dependency(file)
285256
if Dependencies.load? && Dependencies.constant_watch_stack.watching?
286257
descs = Dependencies.constant_watch_stack.watching.flatten.uniq
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
module ActiveSupport::Dependencies::RequireDependency
4+
# <b>Warning:</b> This method is obsolete. The semantics of the autoloader
5+
# match Ruby's and you do not need to be defensive with load order anymore.
6+
# Just refer to classes and modules normally.
7+
#
8+
# Engines that do not control the mode in which their parent application runs
9+
# should call +require_dependency+ where needed in case the runtime mode is
10+
# +:classic+.
11+
def require_dependency(filename)
12+
filename = filename.to_path if filename.respond_to?(:to_path)
13+
14+
unless filename.is_a?(String)
15+
raise ArgumentError, "the file name must be either a String or implement #to_path -- you passed #{filename.inspect}"
16+
end
17+
18+
if abspath = ActiveSupport::Dependencies.search_for_file(filename)
19+
require abspath
20+
else
21+
require filename
22+
end
23+
end
24+
25+
# We could define require_dependency in Object directly, but a module makes
26+
# the extension apparent if you list ancestors.
27+
Object.prepend(self)
28+
end

activesupport/test/dependencies_test.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ def test_depend_on_message
4646
assert_equal "No such file to load -- omgwtfbbq.rb", e.message
4747
end
4848

49-
def test_missing_dependency_raises_missing_source_file
50-
assert_raise(LoadError) { require_dependency("missing_service") }
51-
end
52-
5349
def test_smart_name_error_strings
5450
e = assert_raise NameError do
5551
Object.module_eval "ImaginaryObject"
@@ -118,4 +114,6 @@ def test_load_and_require_stay_private
118114
ensure
119115
ActiveSupport::Dependencies.hook!
120116
end
117+
118+
# Coverage for require_dependency can be found in the railties test suite.
121119
end

railties/test/application/zeitwerk_integration_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,14 @@ def user.to_path; "user"; end
168168
end
169169
end
170170

171+
test "require_dependency raises ArgumentError if the argument is not a String and does not respond to #to_path" do
172+
assert_raises(ArgumentError) { require_dependency(Object.new) }
173+
end
174+
175+
test "require_dependency raises LoadError if the given argument is not found" do
176+
assert_raise(LoadError) { require_dependency("nonexistent_filename") }
177+
end
178+
171179
test "eager loading loads the application code" do
172180
$zeitwerk_integration_test_user = false
173181
$zeitwerk_integration_test_post = false

0 commit comments

Comments
 (0)