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

Get around weird missing constant error caused by AS instead of simply raising NameError, closes #477.

This commit is contained in:
José Valim 2011-05-11 00:08:18 +02:00
parent 9a7dbe2c05
commit a87894ae57
2 changed files with 36 additions and 25 deletions

View file

@ -136,15 +136,23 @@ module ActionController
# 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

View file

@ -170,28 +170,36 @@ end
class NamespacedParamsWrapperTest < ActionController::TestCase
module Admin
class UsersController < ActionController::Base
class << self
attr_accessor :last_parameters
end
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
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 @@ class NamespacedParamsWrapperTest < ActionController::TestCase
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 @@ class NamespacedParamsWrapperTest < ActionController::TestCase
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 @@ class NamespacedParamsWrapperTest < ActionController::TestCase
end
def assert_parameters(expected)
assert_equal expected, Admin::UsersController.last_parameters
assert_equal expected, Admin::Users::UsersController.last_parameters
end
end
end