Skip to content

Commit e9f38a1

Browse files
authored
Merge pull request rails#43058 from rails/del-private-constantize
Delete AS::Dependencies.(safe_)constantize
2 parents 074c7f5 + 38e82da commit e9f38a1

File tree

10 files changed

+45
-79
lines changed

10 files changed

+45
-79
lines changed

actionpack/lib/action_dispatch/http/request.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def controller_class_for(name)
8787
controller_param = name.underscore
8888
const_name = controller_param.camelize << "Controller"
8989
begin
90-
ActiveSupport::Dependencies.constantize(const_name)
90+
const_name.constantize
9191
rescue NameError => error
9292
if error.missing_name == const_name || const_name.start_with?("#{error.missing_name}::")
9393
raise MissingController.new(error.message, error.name)

activerecord/lib/active_record/inheritance.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22

3+
require "active_support/inflector"
34
require "active_support/core_ext/hash/indifferent_access"
45

56
module ActiveRecord
@@ -181,7 +182,7 @@ def sti_name
181182
# It is used to find the class correspondent to the value stored in the inheritance column.
182183
def sti_class_for(type_name)
183184
if store_full_sti_class && store_full_class_name
184-
ActiveSupport::Dependencies.constantize(type_name)
185+
type_name.constantize
185186
else
186187
compute_type(type_name)
187188
end
@@ -203,7 +204,7 @@ def polymorphic_name
203204
# It is used to find the class correspondent to the value stored in the polymorphic type column.
204205
def polymorphic_class_for(name)
205206
if store_full_class_name
206-
ActiveSupport::Dependencies.constantize(name)
207+
name.constantize
207208
else
208209
compute_type(name)
209210
end
@@ -235,10 +236,10 @@ def compute_type(type_name)
235236
if type_name.start_with?("::")
236237
# If the type is prefixed with a scope operator then we assume that
237238
# the type_name is an absolute reference.
238-
ActiveSupport::Dependencies.constantize(type_name)
239+
type_name.constantize
239240
else
240241
type_candidate = @_type_candidates_cache[type_name]
241-
if type_candidate && type_constant = ActiveSupport::Dependencies.safe_constantize(type_candidate)
242+
if type_candidate && type_constant = type_candidate.safe_constantize
242243
return type_constant
243244
end
244245

@@ -248,7 +249,7 @@ def compute_type(type_name)
248249
candidates << type_name
249250

250251
candidates.each do |candidate|
251-
constant = ActiveSupport::Dependencies.safe_constantize(candidate)
252+
constant = candidate.safe_constantize
252253
if candidate == constant.to_s
253254
@_type_candidates_cache[type_name] = candidate
254255
return constant

activerecord/test/cases/inheritance_test.rb

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22

3+
require "active_support/inflector"
34
require "cases/helper"
45
require "models/author"
56
require "models/company"
@@ -68,37 +69,33 @@ def test_compute_type_nonexistent_constant
6869
end
6970

7071
def test_compute_type_no_method_error
71-
ActiveSupport::Dependencies.stub(:safe_constantize, proc { raise NoMethodError }) do
72-
assert_raises NoMethodError do
73-
Company.send :compute_type, "InvalidModel"
74-
end
75-
end
72+
# Done via autoload because you need the exception to happen when the file
73+
# is required.
74+
model = "RaisesNoMethodError"
75+
Object.autoload(model, "#{MODELS_ROOT}/#{model.underscore}")
76+
assert_raises(NoMethodError) { Company.send(:compute_type, model) }
77+
ensure
78+
Object.send(:remove_const, model)
7679
end
7780

7881
def test_compute_type_on_undefined_method
79-
error = nil
80-
begin
81-
Class.new(Author) do
82-
alias_method :foo, :bar
83-
end
84-
rescue => e
85-
error = e
86-
end
87-
88-
ActiveSupport::Dependencies.stub(:safe_constantize, proc { raise e }) do
89-
exception = assert_raises NameError do
90-
Company.send :compute_type, "InvalidModel"
91-
end
92-
assert_equal error.message, exception.message
93-
end
82+
# Done via autoload because you need the exception to happen when the file
83+
# is required.
84+
model = "InvokesAnUndefinedMethod"
85+
Object.autoload(model, "#{MODELS_ROOT}/#{model.underscore}")
86+
assert_raises(NameError) { Company.send(:compute_type, model) }
87+
ensure
88+
Object.send(:remove_const, model)
9489
end
9590

9691
def test_compute_type_argument_error
97-
ActiveSupport::Dependencies.stub(:safe_constantize, proc { raise ArgumentError }) do
98-
assert_raises ArgumentError do
99-
Company.send :compute_type, "InvalidModel"
100-
end
101-
end
92+
# Done via autoload because you need the exception to happen when the file
93+
# is required.
94+
model = "RaisesArgumentError"
95+
Object.autoload(model, "#{MODELS_ROOT}/#{model.underscore}")
96+
assert_raises(ArgumentError) { Company.send(:compute_type, model) }
97+
ensure
98+
Object.send(:remove_const, model)
10299
end
103100

104101
def test_should_store_demodulized_class_name_with_store_full_sti_class_option_disabled

activerecord/test/config.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
TEST_ROOT = __dir__
44
ASSETS_ROOT = TEST_ROOT + "/assets"
55
FIXTURES_ROOT = TEST_ROOT + "/fixtures"
6+
MODELS_ROOT = TEST_ROOT + "/models"
67
MIGRATIONS_ROOT = TEST_ROOT + "/migrations"
78
SCHEMA_ROOT = TEST_ROOT + "/schema"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
class InvokesAnUndefinedMethod
4+
this_method_is_undefined
5+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
class RaisesArgumentError
4+
raise ArgumentError
5+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
class RaisesNoMethodError
4+
Object.new.calling_a_non_existing_method
5+
end

activesupport/lib/active_support/dependencies.rb

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
require "set"
44
require "active_support/core_ext/module/attribute_accessors"
55
require "active_support/dependencies/interlock"
6-
require "active_support/inflector"
76

87
module ActiveSupport # :nodoc:
98
module Dependencies # :nodoc:
@@ -82,18 +81,6 @@ def search_for_file(path_suffix)
8281
nil # Gee, I sure wish we had first_match ;-)
8382
end
8483

85-
# Get the reference for class named +name+.
86-
# Raises an exception if referenced class does not exist.
87-
def constantize(name)
88-
Inflector.constantize(name)
89-
end
90-
91-
# Get the reference for class named +name+ if one exists.
92-
# Otherwise returns +nil+.
93-
def safe_constantize(name)
94-
Inflector.safe_constantize(name)
95-
end
96-
9784
# Determine if the given constant has been automatically loaded.
9885
def autoloaded?(desc)
9986
return false if desc.is_a?(Module) && real_mod_name(desc).nil?

activesupport/lib/active_support/dependencies/zeitwerk_integration.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# frozen_string_literal: true
22

33
require "set"
4-
require "active_support/core_ext/string/inflections"
54
require "zeitwerk"
65

76
module ActiveSupport
@@ -16,14 +15,6 @@ def clear
1615
end
1716
end
1817

19-
def constantize(cpath)
20-
ActiveSupport::Inflector.constantize(cpath)
21-
end
22-
23-
def safe_constantize(cpath)
24-
ActiveSupport::Inflector.safe_constantize(cpath)
25-
end
26-
2718
def autoloaded_constants
2819
Rails.autoloaders.main.unloadable_cpaths
2920
end

railties/test/application/zeitwerk_integration_test.rb

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,6 @@ class RESTfulController < ApplicationController
6161
assert RESTfulController
6262
end
6363

64-
test "constantize returns the value stored in the constant" do
65-
app_file "app/models/admin/user.rb", "class Admin::User; end"
66-
boot
67-
68-
assert_same Admin::User, deps.constantize("Admin::User")
69-
end
70-
71-
test "constantize raises if the constant is unknown" do
72-
boot
73-
74-
assert_raises(NameError) { deps.constantize("Admin") }
75-
end
76-
77-
test "safe_constantize returns the value stored in the constant" do
78-
app_file "app/models/admin/user.rb", "class Admin::User; end"
79-
boot
80-
81-
assert_same Admin::User, deps.safe_constantize("Admin::User")
82-
end
83-
84-
test "safe_constantize returns nil for unknown constants" do
85-
boot
86-
87-
assert_nil deps.safe_constantize("Admin")
88-
end
89-
9064
test "autoloaded? and overridden class names" do
9165
invalid_constant_name = Module.new do
9266
def self.name

0 commit comments

Comments
 (0)