aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actionpack/lib/action_controller/metal/params_wrapper.rb18
-rw-r--r--actionpack/test/controller/params_wrapper_test.rb45
2 files changed, 37 insertions, 26 deletions
diff --git a/actionpack/lib/action_controller/metal/params_wrapper.rb b/actionpack/lib/action_controller/metal/params_wrapper.rb
index 881af74147..9b27bb8b91 100644
--- a/actionpack/lib/action_controller/metal/params_wrapper.rb
+++ b/actionpack/lib/action_controller/metal/params_wrapper.rb
@@ -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
diff --git a/actionpack/test/controller/params_wrapper_test.rb b/actionpack/test/controller/params_wrapper_test.rb
index 548cd02dc0..85464fc780 100644
--- a/actionpack/test/controller/params_wrapper_test.rb
+++ b/actionpack/test/controller/params_wrapper_test.rb
@@ -170,28 +170,36 @@ end
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 @@ 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 \ No newline at end of file