Permalink
Browse files

Get around weird missing constant error caused by AS instead of simpl…

…y raising NameError, closes #477.
  • Loading branch information...
1 parent 9a7dbe2 commit a87894ae57dcabbb299886959ec5646e356a7b56 @josevalim josevalim committed May 10, 2011
@@ -136,15 +136,23 @@ def inherited(klass)
# this could be done by trying to find the defined model that has the
# same singularize name as the controller. For example, +UsersController+
# will try to find if the +User+ model exists.
- def _default_wrap_model
+ #
+ # This method also does namespace lookup. Foo::Bar::UsersController will
+ # try to find Foo::Bar::User, Foo::User and finally User.
+ def _default_wrap_model #:nodoc:
model_name = self.name.sub(/Controller$/, '').singularize
begin
model_klass = model_name.constantize
- rescue NameError => e
- unscoped_model_name = model_name.split("::", 2).last
- break if unscoped_model_name == model_name
- model_name = unscoped_model_name
+ rescue NameError, ArgumentError => e
+ if e.message =~ /is not missing constant|uninitialized constant #{model_name}/
+ 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
model_klass
@@ -170,28 +170,36 @@ def assert_parameters(expected)
class NamespacedParamsWrapperTest < ActionController::TestCase
module Admin
- class UsersController < ActionController::Base
- class << self
- attr_accessor :last_parameters
- end
-
- def parse
- self.class.last_parameters = request.params.except(:controller, :action)
- head :ok
+ module Users
+ class UsersController < ActionController::Base;
+ class << self
+ attr_accessor :last_parameters
+ end
+
+ def parse
+ self.class.last_parameters = request.params.except(:controller, :action)
+ head :ok
+ end
end
end
end
- class Sample
+ class SampleOne
def self.column_names
["username"]
end
end
- tests Admin::UsersController
+ class SampleTwo
+ def self.column_names
+ ["title"]
+ end
+ end
+
+ tests Admin::Users::UsersController
def teardown
- Admin::UsersController.last_parameters = nil
+ Admin::Users::UsersController.last_parameters = nil
end
def test_derived_name_from_controller
@@ -203,7 +211,7 @@ def test_derived_name_from_controller
end
def test_namespace_lookup_from_model
- Admin.const_set(:User, Class.new(Sample))
+ Admin.const_set(:User, Class.new(SampleOne))
begin
with_default_wrapper_options do
@request.env['CONTENT_TYPE'] = 'application/json'
@@ -216,20 +224,15 @@ def test_namespace_lookup_from_model
end
def test_hierarchy_namespace_lookup_from_model
- # Make sure that we cleanup ::Admin::User
- admin_user_constant = ::Admin::User
- ::Admin.send :remove_const, :User
-
- Object.const_set(:User, Class.new(Sample))
+ Object.const_set(:User, Class.new(SampleTwo))
begin
with_default_wrapper_options do
@request.env['CONTENT_TYPE'] = 'application/json'
post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
- assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu' }})
+ assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'title' => 'Developer' }})
end
ensure
Object.send :remove_const, :User
- ::Admin.const_set(:User, admin_user_constant)
end
end
@@ -241,6 +244,6 @@ def with_default_wrapper_options(&block)
end
def assert_parameters(expected)
- assert_equal expected, Admin::UsersController.last_parameters
+ assert_equal expected, Admin::Users::UsersController.last_parameters
end
-end
+end

0 comments on commit a87894a

Please sign in to comment.