1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Use safe_constantize where possible.

This commit is contained in:
José Valim 2011-09-23 16:46:33 +02:00
parent b2f34d1e35
commit e8987c30d0
3 changed files with 5 additions and 12 deletions

View file

@ -141,19 +141,16 @@ module ActionController
# try to find Foo::Bar::User, Foo::User and finally User.
def _default_wrap_model #:nodoc:
return nil if self.anonymous?
model_name = self.name.sub(/Controller$/, '').singularize
begin
model_klass = model_name.constantize
rescue NameError, ArgumentError => e
if e.message =~ /is not missing constant|uninitialized constant #{model_name}/
if model_klass = model_name.safe_constantize
model_klass
else
namespaces = model_name.split("::")
namespaces.delete_at(-2)
break if namespaces.last == model_name
model_name = namespaces.join("::")
else
raise
end
end until model_klass

View file

@ -54,10 +54,8 @@ module ActionView
end
def determine_default_helper_class(name)
mod = name.sub(/Test$/, '').constantize
mod = name.sub(/Test$/, '').safe_constantize
mod.is_a?(Class) ? nil : mod
rescue NameError
nil
end
def helper_method(*methods)

View file

@ -3,8 +3,6 @@ require 'active_support/core_ext/string/inflections'
class Module
def reachable? #:nodoc:
!anonymous? && name.constantize.equal?(self)
rescue NameError
false
!anonymous? && name.safe_constantize.equal?(self)
end
end