From 84bacf99d67617421edfbbd787c845afb34a9d06 Mon Sep 17 00:00:00 2001 From: Nicholas Seckar Date: Sun, 13 Aug 2006 04:15:22 +0000 Subject: Invoke method_missing directly for hidden actions. Closes #3030. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4755 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_controller/base.rb | 7 ++-- actionpack/test/controller/base_test.rb | 58 ++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 762a3bd7a5..f4827f65f5 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Invoke method_missing directly on hidden actions. Closes #3030. [Nicholas Seckar] + * Require Tempfile explicitly for TestUploadedFile due to changes in class auto loading. [Rick Olson] * Add RoutingError exception when RouteSet fails to generate a path from a Named Route. [Rick Olson] diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 8c03f86609..2c91266f6f 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -974,11 +974,14 @@ module ActionController #:nodoc: logger.info " Parameters: #{respond_to?(:filter_parameters) ? filter_parameters(params).inspect : params.inspect}" end end - + def perform_action - if self.class.action_methods.include?(action_name) || self.class.action_methods.include?('method_missing') + if self.class.action_methods.include?(action_name) send(action_name) render unless performed? + elsif respond_to? :method_missing + send(:method_missing, action_name) + render unless performed? elsif template_exists? && template_public? render else diff --git a/actionpack/test/controller/base_test.rb b/actionpack/test/controller/base_test.rb index 588b9f60af..9cf035d1cb 100644 --- a/actionpack/test/controller/base_test.rb +++ b/actionpack/test/controller/base_test.rb @@ -12,6 +12,7 @@ module Submodule hide_action :hidden_action def hidden_action + raise "Noooo!" end def another_hidden_action @@ -33,6 +34,21 @@ class NonEmptyController < ActionController::Base end end +class MethodMissingController < ActionController::Base + + hide_action :shouldnt_be_called + def shouldnt_be_called + raise "NO WAY!" + end + +protected + + def method_missing(selector) + render :text => selector.to_s + end + +end + class ControllerClassTests < Test::Unit::TestCase def test_controller_path assert_equal 'empty', EmptyController.controller_path @@ -64,4 +80,46 @@ class ControllerInstanceTests < Test::Unit::TestCase assert_equal Set.new('public_action'), c.send(:action_methods), "#{c.controller_path} should not be empty!" end end + end + + +class PerformActionTest < Test::Unit::TestCase + def use_controller(controller_class) + @controller = controller_class.new + + # enable a logger so that (e.g.) the benchmarking stuff runs, so we can get + # a more accurate simulation of what happens in "real life". + @controller.logger = Logger.new(nil) + + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + + @request.host = "www.nextangle.com" + end + + def test_get_on_priv_should_show_selector + use_controller MethodMissingController + get :shouldnt_be_called + assert_response :success + assert_equal 'shouldnt_be_called', @response.body + end + + def test_method_missing_is_not_an_action_name + use_controller MethodMissingController + assert ! @controller.send(:action_methods).include?('method_missing') + + get :method_missing + assert_response :success + assert_equal 'method_missing', @response.body + end + + def test_get_on_hidden_should_fail + use_controller NonEmptyController + get :hidden_action + assert_response 404 + + get :another_hidden_action + assert_response 404 + end +end \ No newline at end of file -- cgit v1.2.3