提交 a930f5ba 编写于 作者: K Keenan Brock

Fix safe_constantize to not raise a LoadError.

### Summary

There was an issues when using `safe_constantize` on a string that has
the wrong case.

File `em.rb` defines `EM`.
`"Em".safe_constantize` causes a little confusion with the autoloader.
The autoloader finds file "em.rb",
expecting it to define `Em`, but `Em` is not defined.
The autoloader raises a `LoadError`, which is good,
But `safe_constantize` is defined to return `nil` when a class is not found.

### Before

```
"Em".safe_constantize
LoadError: Unable to autoload constant Em, \
expected rails/activesupport/test/autoloading_fixtures/em.rb to define it
```

### After

```
"Em".safe_constantize
# => nil
```
上级 9f1a07af
* Fix `String#safe_constantize` throwing a `LoadError` for incorrectly cased constant references.
*Keenan Brock*
* Preserve key order passed to `ActiveSupport::CacheStore#fetch_multi`.
`fetch_multi(*names)` now returns its results in the same order as the `*names` requested, rather than returning cache hits followed by cache misses.
......
......@@ -328,6 +328,8 @@ def safe_constantize(camel_cased_word)
e.name.to_s == camel_cased_word.to_s)
rescue ArgumentError => e
raise unless /not missing constant #{const_regexp(camel_cased_word)}!$/.match?(e.message)
rescue LoadError => e
raise unless /Unable to autoload constant #{const_regexp(camel_cased_word)}/.match?(e.message)
end
# Returns the suffix that should be added to a number to denote the position
......
# frozen_string_literal: true
# raises a load error typical of the dynamic code that manually raises load errors
raise LoadError, "required gem not present kind of error"
......@@ -112,6 +112,16 @@ def run_safe_constantize_tests_on
assert_nil yield("A::Object::B")
assert_nil yield("A::Object::Object::Object::B")
with_autoloading_fixtures do
assert_nil yield("Em")
end
assert_raises(LoadError) do
with_autoloading_fixtures do
yield("RaisesLoadError")
end
end
assert_raises(NameError) do
with_autoloading_fixtures do
yield("RaisesNameError")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册