From 69b0e5c44a05fddcac996c7aaaca61cb3188da5e Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Sun, 4 Feb 2007 20:47:05 +0000 Subject: Allow Controllers to have multiple view_paths instead of a single template_root. Closes #2754 [John Long] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6120 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../test/controller/action_pack_assertions_test.rb | 2 +- .../test/controller/addresses_render_test.rb | 2 +- actionpack/test/controller/capture_test.rb | 2 +- actionpack/test/controller/content_type_test.rb | 2 +- .../deprecation/deprecated_base_methods_test.rb | 2 +- actionpack/test/controller/layout_test.rb | 12 ++-- actionpack/test/controller/mime_responds_test.rb | 2 +- actionpack/test/controller/new_render_test.rb | 4 +- actionpack/test/controller/render_test.rb | 4 +- actionpack/test/controller/send_file_test.rb | 2 +- actionpack/test/controller/view_paths_test.rb | 78 ++++++++++++++++++++++ 11 files changed, 96 insertions(+), 16 deletions(-) create mode 100644 actionpack/test/controller/view_paths_test.rb (limited to 'actionpack/test/controller') diff --git a/actionpack/test/controller/action_pack_assertions_test.rb b/actionpack/test/controller/action_pack_assertions_test.rb index d9cfe18ae5..f9a641a1ec 100644 --- a/actionpack/test/controller/action_pack_assertions_test.rb +++ b/actionpack/test/controller/action_pack_assertions_test.rb @@ -152,7 +152,7 @@ end # tell the controller where to find its templates but start from parent # directory of test_request_response to simulate the behaviour of a # production environment -ActionPackAssertionsController.template_root = File.dirname(__FILE__) + "/../fixtures/" +ActionPackAssertionsController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] # a test case to exercise the new capabilities TestRequest & TestResponse class ActionPackAssertionsControllerTest < Test::Unit::TestCase diff --git a/actionpack/test/controller/addresses_render_test.rb b/actionpack/test/controller/addresses_render_test.rb index 4de1c80505..6c5424a5b6 100644 --- a/actionpack/test/controller/addresses_render_test.rb +++ b/actionpack/test/controller/addresses_render_test.rb @@ -22,7 +22,7 @@ class AddressesTestController < ActionController::Base def self.controller_path; "addresses"; end end -AddressesTestController.template_root = File.dirname(__FILE__) + "/../fixtures/" +AddressesTestController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] class AddressesTest < Test::Unit::TestCase def setup diff --git a/actionpack/test/controller/capture_test.rb b/actionpack/test/controller/capture_test.rb index 43bf346eef..300bdc3a84 100644 --- a/actionpack/test/controller/capture_test.rb +++ b/actionpack/test/controller/capture_test.rb @@ -23,7 +23,7 @@ class CaptureController < ActionController::Base def rescue_action(e) raise end end -CaptureController.template_root = File.dirname(__FILE__) + "/../fixtures/" +CaptureController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] class CaptureTest < Test::Unit::TestCase def setup diff --git a/actionpack/test/controller/content_type_test.rb b/actionpack/test/controller/content_type_test.rb index 6f0618da0e..1841d37c0b 100644 --- a/actionpack/test/controller/content_type_test.rb +++ b/actionpack/test/controller/content_type_test.rb @@ -45,7 +45,7 @@ class ContentTypeController < ActionController::Base def rescue_action(e) raise end end -ContentTypeController.template_root = File.dirname(__FILE__) + "/../fixtures/" +ContentTypeController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] class ContentTypeTest < Test::Unit::TestCase def setup diff --git a/actionpack/test/controller/deprecation/deprecated_base_methods_test.rb b/actionpack/test/controller/deprecation/deprecated_base_methods_test.rb index d8da676f96..3256b77477 100644 --- a/actionpack/test/controller/deprecation/deprecated_base_methods_test.rb +++ b/actionpack/test/controller/deprecation/deprecated_base_methods_test.rb @@ -21,7 +21,7 @@ class DeprecatedBaseMethodsTest < Test::Unit::TestCase def rescue_action(e) raise e end end - Target.template_root = File.dirname(__FILE__) + "/../../fixtures" + Target.view_paths = [ File.dirname(__FILE__) + "/../../fixtures" ] def setup @request = ActionController::TestRequest.new diff --git a/actionpack/test/controller/layout_test.rb b/actionpack/test/controller/layout_test.rb index 9338bfa6f9..cade6738a7 100644 --- a/actionpack/test/controller/layout_test.rb +++ b/actionpack/test/controller/layout_test.rb @@ -1,15 +1,17 @@ require File.dirname(__FILE__) + '/../abstract_unit' -# The template_root must be set on Base and not LayoutTest so that LayoutTest's inherited method has access to -# the template_root when looking for a layout -ActionController::Base.template_root = File.dirname(__FILE__) + '/../fixtures/layout_tests/' +# The view_paths array must be set on Base and not LayoutTest so that LayoutTest's inherited +# method has access to the view_paths array when looking for a layout to automatically assign. +old_load_paths = ActionController::Base.view_paths +ActionController::Base.view_paths = [ File.dirname(__FILE__) + '/../fixtures/layout_tests/' ] class LayoutTest < ActionController::Base def self.controller_path; 'views' end + self.view_paths = ActionController::Base.view_paths.dup end -# Restore template root to be unset -ActionController::Base.template_root = nil +# Restore view_paths to previous value +ActionController::Base.view_paths = old_load_paths class ProductController < LayoutTest end diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index 5692d26232..3a1df02a7f 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -118,7 +118,7 @@ class RespondToController < ActionController::Base end end -RespondToController.template_root = File.dirname(__FILE__) + "/../fixtures/" +RespondToController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] class MimeControllerTest < Test::Unit::TestCase def setup diff --git a/actionpack/test/controller/new_render_test.rb b/actionpack/test/controller/new_render_test.rb index 9b328e2c0a..70bb18cadc 100644 --- a/actionpack/test/controller/new_render_test.rb +++ b/actionpack/test/controller/new_render_test.rb @@ -350,8 +350,8 @@ class NewRenderTestController < ActionController::Base end end -NewRenderTestController.template_root = File.dirname(__FILE__) + "/../fixtures/" -Fun::GamesController.template_root = File.dirname(__FILE__) + "/../fixtures/" +NewRenderTestController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] +Fun::GamesController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] class NewRenderTest < Test::Unit::TestCase def setup diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index d364dd6c93..bf9042ec7a 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -134,8 +134,8 @@ class TestController < ActionController::Base end end -TestController.template_root = File.dirname(__FILE__) + "/../fixtures/" -Fun::GamesController.template_root = File.dirname(__FILE__) + "/../fixtures/" +TestController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] +Fun::GamesController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] class RenderTest < Test::Unit::TestCase def setup diff --git a/actionpack/test/controller/send_file_test.rb b/actionpack/test/controller/send_file_test.rb index 83590fd73e..e7ebc91676 100644 --- a/actionpack/test/controller/send_file_test.rb +++ b/actionpack/test/controller/send_file_test.rb @@ -21,7 +21,7 @@ class SendFileController < ActionController::Base def rescue_action(e) raise end end -SendFileController.template_root = File.dirname(__FILE__) + "/../fixtures/" +SendFileController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] class SendFileTest < Test::Unit::TestCase include TestFileUtils diff --git a/actionpack/test/controller/view_paths_test.rb b/actionpack/test/controller/view_paths_test.rb new file mode 100644 index 0000000000..9184ab1793 --- /dev/null +++ b/actionpack/test/controller/view_paths_test.rb @@ -0,0 +1,78 @@ +require File.dirname(__FILE__) + '/../abstract_unit' + +class ViewLoadPathsTest < Test::Unit::TestCase + + LOAD_PATH_ROOT = File.join(File.dirname(__FILE__), '..', 'fixtures') + + class TestController < ActionController::Base + def self.controller_path() "test" end + def rescue_action(e) raise end + + def hello_world() end + end + + def setup + TestController.view_paths = [ LOAD_PATH_ROOT ] + @controller = TestController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + + # Track the last warning. + @old_behavior = ActiveSupport::Deprecation.behavior + @last_message = nil + ActiveSupport::Deprecation.behavior = Proc.new { |message| @last_message = message } + end + + def teardown + ActiveSupport::Deprecation.behavior = @old_behavior + end + + def test_template_load_path_was_set_correctly + assert_equal [ LOAD_PATH_ROOT ], @controller.view_paths + end + + def test_view_paths + get :hello_world + assert_response :success + assert_equal "Hello world!", @response.body + end + + def test_view_paths_override + TestController.view_paths.unshift "#{LOAD_PATH_ROOT}/override" + get :hello_world + assert_response :success + assert_equal "Hello overridden world!", @response.body + end + + def test_template_root_deprecated + assert_deprecated(/template_root.*view_paths/) do + TestController.template_root = LOAD_PATH_ROOT + end + assert_deprecated(/template_root.*view_paths/) do + assert_equal LOAD_PATH_ROOT, TestController.template_root + end + end + + def test_inheritance + original_load_paths = ActionController::Base.view_paths + + self.class.class_eval %{ + class A < ActionController::Base; end + class B < A; end + class C < ActionController::Base; end + } + + A.view_paths = [ 'a/path' ] + + assert_equal [ 'a/path' ], A.view_paths + assert_equal A.view_paths, B.view_paths + assert_equal original_load_paths, C.view_paths + + e = assert_raises(TypeError) { C.view_paths << 'c/path' } + assert_equal "can't modify frozen array", e.message + + C.view_paths = [] + assert_nothing_raised { C.view_paths << 'c/path' } + end + +end \ No newline at end of file -- cgit v1.2.3