-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix safe_constantize to not raise a LoadError. #34892
Conversation
@@ -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 | |||
rause unless /Unable to autoload constant/.match?(e.message) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rause?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might also want to add #{const_regexp(camel_cased_word)}
to the end of this regex to ensure we're catching the right load error. Might be a good idea to test this actually raises now too seeing how CI is passing with a typo 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this makes sense. 👍 Please add a changelog entry to ActiveSupport.
8bd1dad
to
a0c1cfb
Compare
Thank you @gmcgibbon and @djberg96
|
👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still needs a changelog entry here. Other than that, LGTM! 👍
a0c1cfb
to
cfaa416
Compare
### 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 ```
cfaa416
to
a930f5b
Compare
fixed Changelog conflict... @gmcgibbon thank you so much for your help (and your patience for having to repeat that) |
odd collate error for mysql (that I haven't seen before) |
Fix safe_constantize to not raise a LoadError.
Thanks for all the help on this PR, and rails in general. It is hard work maintaining OSS. |
Summary
There was an issues when using
safe_constantize
on a string that has the wrong case.File
em.rb
definesEM
."Em".safe_constantize
causes a little confusion with the autoloader.The autoloader finds file "em.rb", expecting it to define
Em
, butEm
is not defined.The autoloader raises a
LoadError
, which is good, butsafe_constantize
is defined to returnnil
when a class is not found.Before
After