Skip to content

Commit

Permalink
Use safe_constantize where possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Sep 23, 2011
1 parent b2f34d1 commit e8987c3
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 12 deletions.
9 changes: 3 additions & 6 deletions actionpack/lib/action_controller/metal/params_wrapper.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -141,19 +141,16 @@ def inherited(klass)
# try to find Foo::Bar::User, Foo::User and finally User. # try to find Foo::Bar::User, Foo::User and finally User.
def _default_wrap_model #:nodoc: def _default_wrap_model #:nodoc:
return nil if self.anonymous? return nil if self.anonymous?

model_name = self.name.sub(/Controller$/, '').singularize model_name = self.name.sub(/Controller$/, '').singularize


begin begin
model_klass = model_name.constantize if model_klass = model_name.safe_constantize
rescue NameError, ArgumentError => e model_klass
if e.message =~ /is not missing constant|uninitialized constant #{model_name}/ else
namespaces = model_name.split("::") namespaces = model_name.split("::")
namespaces.delete_at(-2) namespaces.delete_at(-2)
break if namespaces.last == model_name break if namespaces.last == model_name
model_name = namespaces.join("::") model_name = namespaces.join("::")
else
raise
end end
end until model_klass end until model_klass


Expand Down
4 changes: 1 addition & 3 deletions actionpack/lib/action_view/test_case.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@ def tests(helper_class)
end end


def determine_default_helper_class(name) def determine_default_helper_class(name)
mod = name.sub(/Test$/, '').constantize mod = name.sub(/Test$/, '').safe_constantize
mod.is_a?(Class) ? nil : mod mod.is_a?(Class) ? nil : mod
rescue NameError
nil
end end


def helper_method(*methods) def helper_method(*methods)
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@


class Module class Module
def reachable? #:nodoc: def reachable? #:nodoc:
!anonymous? && name.constantize.equal?(self) !anonymous? && name.safe_constantize.equal?(self)
rescue NameError
false
end end
end end

0 comments on commit e8987c3

Please sign in to comment.