Skip to content

Commit 38e82da

Browse files
committed
Delete AS::Dependencies.safe_constantize
1 parent 3e3504b commit 38e82da

File tree

9 files changed

+40
-52
lines changed

9 files changed

+40
-52
lines changed

activerecord/lib/active_record/inheritance.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def compute_type(type_name)
239239
type_name.constantize
240240
else
241241
type_candidate = @_type_candidates_cache[type_name]
242-
if type_candidate && type_constant = ActiveSupport::Dependencies.safe_constantize(type_candidate)
242+
if type_candidate && type_constant = type_candidate.safe_constantize
243243
return type_constant
244244
end
245245

@@ -249,7 +249,7 @@ def compute_type(type_name)
249249
candidates << type_name
250250

251251
candidates.each do |candidate|
252-
constant = ActiveSupport::Dependencies.safe_constantize(candidate)
252+
constant = candidate.safe_constantize
253253
if candidate == constant.to_s
254254
@_type_candidates_cache[type_name] = candidate
255255
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 & 7 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,12 +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+ if one exists.
86-
# Otherwise returns +nil+.
87-
def safe_constantize(name)
88-
Inflector.safe_constantize(name)
89-
end
90-
9184
# Determine if the given constant has been automatically loaded.
9285
def autoloaded?(desc)
9386
return false if desc.is_a?(Module) && real_mod_name(desc).nil?

activesupport/lib/active_support/dependencies/zeitwerk_integration.rb

Lines changed: 0 additions & 5 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,10 +15,6 @@ def clear
1615
end
1716
end
1817

19-
def safe_constantize(cpath)
20-
ActiveSupport::Inflector.safe_constantize(cpath)
21-
end
22-
2318
def autoloaded_constants
2419
Rails.autoloaders.main.unloadable_cpaths
2520
end

railties/test/application/zeitwerk_integration_test.rb

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

64-
test "safe_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.safe_constantize("Admin::User")
69-
end
70-
71-
test "safe_constantize returns nil for unknown constants" do
72-
boot
73-
74-
assert_nil deps.safe_constantize("Admin")
75-
end
76-
7764
test "autoloaded? and overridden class names" do
7865
invalid_constant_name = Module.new do
7966
def self.name

0 commit comments

Comments
 (0)