2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
2007-02-04 15:47:05 -05:00
|
|
|
|
|
|
|
class ViewLoadPathsTest < Test::Unit::TestCase
|
|
|
|
LOAD_PATH_ROOT = File.join(File.dirname(__FILE__), '..', 'fixtures')
|
|
|
|
|
2008-06-15 22:22:27 -04:00
|
|
|
ActionController::Base.view_paths = [LOAD_PATH_ROOT]
|
2007-10-26 01:45:41 -04:00
|
|
|
|
2007-02-04 15:47:05 -05:00
|
|
|
class TestController < ActionController::Base
|
|
|
|
def self.controller_path() "test" end
|
|
|
|
def rescue_action(e) raise end
|
2008-06-15 22:22:27 -04:00
|
|
|
|
2007-06-15 19:52:37 -04:00
|
|
|
before_filter :add_view_path, :only => :hello_world_at_request_time
|
2008-06-15 22:22:27 -04:00
|
|
|
|
2007-02-04 15:47:05 -05:00
|
|
|
def hello_world() end
|
2007-06-15 19:52:37 -04:00
|
|
|
def hello_world_at_request_time() render(:action => 'hello_world') end
|
2008-06-15 22:22:27 -04:00
|
|
|
|
2007-06-15 19:52:37 -04:00
|
|
|
private
|
2008-06-15 22:22:27 -04:00
|
|
|
def add_view_path
|
|
|
|
prepend_view_path "#{LOAD_PATH_ROOT}/override"
|
|
|
|
end
|
2007-02-04 15:47:05 -05:00
|
|
|
end
|
2008-06-15 22:22:27 -04:00
|
|
|
|
2007-08-15 14:59:37 -04:00
|
|
|
class Test::SubController < ActionController::Base
|
|
|
|
layout 'test/sub'
|
2007-09-02 20:18:30 -04:00
|
|
|
def hello_world; render(:template => 'test/hello_world'); end
|
2007-08-15 14:59:37 -04:00
|
|
|
end
|
2008-06-15 22:22:27 -04:00
|
|
|
|
2007-02-04 15:47:05 -05:00
|
|
|
def setup
|
2007-10-26 01:45:41 -04:00
|
|
|
TestController.view_paths = nil
|
2008-01-15 20:41:31 -05:00
|
|
|
|
2007-02-04 15:47:05 -05:00
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
2008-01-15 20:41:31 -05:00
|
|
|
|
|
|
|
@controller = TestController.new
|
|
|
|
# Following is needed in order to setup @controller.template object properly
|
|
|
|
@controller.send :initialize_template_class, @response
|
|
|
|
@controller.send :assign_shortcuts, @request, @response
|
|
|
|
|
2007-02-04 15:47:05 -05:00
|
|
|
# Track the last warning.
|
|
|
|
@old_behavior = ActiveSupport::Deprecation.behavior
|
|
|
|
@last_message = nil
|
2007-08-15 14:59:37 -04:00
|
|
|
ActiveSupport::Deprecation.behavior = Proc.new { |message, callback| @last_message = message }
|
2007-02-04 15:47:05 -05:00
|
|
|
end
|
2008-06-15 22:22:27 -04:00
|
|
|
|
2007-02-04 15:47:05 -05:00
|
|
|
def teardown
|
|
|
|
ActiveSupport::Deprecation.behavior = @old_behavior
|
|
|
|
end
|
2008-06-15 22:22:27 -04:00
|
|
|
|
2007-02-04 15:47:05 -05:00
|
|
|
def test_template_load_path_was_set_correctly
|
2008-06-25 06:47:01 -04:00
|
|
|
assert_equal [ LOAD_PATH_ROOT ], @controller.view_paths
|
2007-02-04 15:47:05 -05:00
|
|
|
end
|
2008-06-15 22:22:27 -04:00
|
|
|
|
2007-11-25 22:59:23 -05:00
|
|
|
def test_controller_appends_view_path_correctly
|
2008-01-15 20:41:31 -05:00
|
|
|
@controller.append_view_path 'foo'
|
2008-06-25 06:47:01 -04:00
|
|
|
assert_equal [LOAD_PATH_ROOT, 'foo'], @controller.view_paths
|
2008-06-15 22:22:27 -04:00
|
|
|
|
2008-01-15 20:41:31 -05:00
|
|
|
@controller.append_view_path(%w(bar baz))
|
2008-06-25 06:47:01 -04:00
|
|
|
assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths
|
2008-06-15 22:22:27 -04:00
|
|
|
|
|
|
|
@controller.append_view_path(LOAD_PATH_ROOT)
|
2008-06-25 06:47:01 -04:00
|
|
|
assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths
|
2008-06-15 22:22:27 -04:00
|
|
|
|
|
|
|
@controller.append_view_path([LOAD_PATH_ROOT])
|
2008-06-25 06:47:01 -04:00
|
|
|
assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths
|
2007-10-26 01:45:41 -04:00
|
|
|
end
|
2008-06-15 22:22:27 -04:00
|
|
|
|
2007-11-25 22:59:23 -05:00
|
|
|
def test_controller_prepends_view_path_correctly
|
2008-01-15 20:41:31 -05:00
|
|
|
@controller.prepend_view_path 'baz'
|
2008-06-25 06:47:01 -04:00
|
|
|
assert_equal ['baz', LOAD_PATH_ROOT], @controller.view_paths
|
2008-06-15 22:22:27 -04:00
|
|
|
|
2008-01-15 20:41:31 -05:00
|
|
|
@controller.prepend_view_path(%w(foo bar))
|
2008-06-25 06:47:01 -04:00
|
|
|
assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths
|
2008-06-15 22:22:27 -04:00
|
|
|
|
|
|
|
@controller.prepend_view_path(LOAD_PATH_ROOT)
|
2008-06-25 06:47:01 -04:00
|
|
|
assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths
|
2008-06-15 22:22:27 -04:00
|
|
|
|
|
|
|
@controller.prepend_view_path([LOAD_PATH_ROOT])
|
2008-06-25 06:47:01 -04:00
|
|
|
assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths
|
2007-10-26 01:45:41 -04:00
|
|
|
end
|
2008-06-15 22:22:27 -04:00
|
|
|
|
2007-11-25 22:59:23 -05:00
|
|
|
def test_template_appends_view_path_correctly
|
|
|
|
@controller.instance_variable_set :@template, ActionView::Base.new(TestController.view_paths, {}, @controller)
|
|
|
|
class_view_paths = TestController.view_paths
|
|
|
|
|
|
|
|
@controller.append_view_path 'foo'
|
2008-06-25 06:47:01 -04:00
|
|
|
assert_equal [LOAD_PATH_ROOT, 'foo'], @controller.view_paths
|
2008-06-15 22:22:27 -04:00
|
|
|
|
2007-11-25 22:59:23 -05:00
|
|
|
@controller.append_view_path(%w(bar baz))
|
2008-06-25 06:47:01 -04:00
|
|
|
assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths
|
2007-11-25 22:59:23 -05:00
|
|
|
assert_equal class_view_paths, TestController.view_paths
|
|
|
|
end
|
2008-06-15 22:22:27 -04:00
|
|
|
|
2007-11-25 22:59:23 -05:00
|
|
|
def test_template_prepends_view_path_correctly
|
|
|
|
@controller.instance_variable_set :@template, ActionView::Base.new(TestController.view_paths, {}, @controller)
|
|
|
|
class_view_paths = TestController.view_paths
|
2008-06-15 22:22:27 -04:00
|
|
|
|
2007-11-25 22:59:23 -05:00
|
|
|
@controller.prepend_view_path 'baz'
|
2008-06-25 06:47:01 -04:00
|
|
|
assert_equal ['baz', LOAD_PATH_ROOT], @controller.view_paths
|
2008-06-15 22:22:27 -04:00
|
|
|
|
2007-11-25 22:59:23 -05:00
|
|
|
@controller.prepend_view_path(%w(foo bar))
|
2008-06-25 06:47:01 -04:00
|
|
|
assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths
|
2007-11-25 22:59:23 -05:00
|
|
|
assert_equal class_view_paths, TestController.view_paths
|
|
|
|
end
|
2008-06-15 22:22:27 -04:00
|
|
|
|
2007-02-04 15:47:05 -05:00
|
|
|
def test_view_paths
|
|
|
|
get :hello_world
|
|
|
|
assert_response :success
|
|
|
|
assert_equal "Hello world!", @response.body
|
|
|
|
end
|
2008-06-15 22:22:27 -04:00
|
|
|
|
2007-02-04 15:47:05 -05:00
|
|
|
def test_view_paths_override
|
2008-01-21 15:45:04 -05:00
|
|
|
TestController.prepend_view_path "#{LOAD_PATH_ROOT}/override"
|
2007-02-04 15:47:05 -05:00
|
|
|
get :hello_world
|
|
|
|
assert_response :success
|
|
|
|
assert_equal "Hello overridden world!", @response.body
|
2007-06-15 19:52:37 -04:00
|
|
|
end
|
2008-06-15 22:22:27 -04:00
|
|
|
|
2007-08-15 14:59:37 -04:00
|
|
|
def test_view_paths_override_for_layouts_in_controllers_with_a_module
|
|
|
|
@controller = Test::SubController.new
|
|
|
|
Test::SubController.view_paths = [ "#{LOAD_PATH_ROOT}/override", LOAD_PATH_ROOT, "#{LOAD_PATH_ROOT}/override2" ]
|
|
|
|
get :hello_world
|
|
|
|
assert_response :success
|
|
|
|
assert_equal "layout: Hello overridden world!", @response.body
|
|
|
|
end
|
2008-06-15 22:22:27 -04:00
|
|
|
|
2007-06-15 19:52:37 -04:00
|
|
|
def test_view_paths_override_at_request_time
|
|
|
|
get :hello_world_at_request_time
|
|
|
|
assert_response :success
|
|
|
|
assert_equal "Hello overridden world!", @response.body
|
2007-02-04 15:47:05 -05:00
|
|
|
end
|
2008-06-15 22:22:27 -04:00
|
|
|
|
2007-02-04 15:47:05 -05:00
|
|
|
def test_inheritance
|
|
|
|
original_load_paths = ActionController::Base.view_paths
|
2008-06-15 22:22:27 -04:00
|
|
|
|
2007-02-04 15:47:05 -05:00
|
|
|
self.class.class_eval %{
|
|
|
|
class A < ActionController::Base; end
|
|
|
|
class B < A; end
|
|
|
|
class C < ActionController::Base; end
|
|
|
|
}
|
2008-06-15 22:22:27 -04:00
|
|
|
|
|
|
|
A.view_paths = ['a/path']
|
|
|
|
|
2008-06-25 06:47:01 -04:00
|
|
|
assert_equal ['a/path'], A.view_paths
|
2008-06-15 22:22:27 -04:00
|
|
|
assert_equal A.view_paths, B.view_paths
|
2007-02-04 15:47:05 -05:00
|
|
|
assert_equal original_load_paths, C.view_paths
|
2008-06-15 22:22:27 -04:00
|
|
|
|
2007-02-04 15:47:05 -05:00
|
|
|
C.view_paths = []
|
|
|
|
assert_nothing_raised { C.view_paths << 'c/path' }
|
2008-06-25 06:47:01 -04:00
|
|
|
assert_equal ['c/path'], C.view_paths
|
2008-06-15 22:22:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_template_file_for_path
|
|
|
|
assert_equal "test/hello_world.erb", @controller.view_paths.find_template_file_for_path("test/hello_world.erb").to_s
|
|
|
|
assert_equal "test/hello.builder", @controller.view_paths.find_template_file_for_path("test/hello.builder").to_s
|
|
|
|
assert_equal nil, @controller.view_paths.find_template_file_for_path("test/missing.erb")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_view_paths_find_template_file_for_path
|
|
|
|
assert_equal "test/formatted_html_erb.html.erb", @controller.view_paths.find_template_file_for_path("test/formatted_html_erb.html").to_s
|
|
|
|
assert_equal "test/formatted_xml_erb.xml.erb", @controller.view_paths.find_template_file_for_path("test/formatted_xml_erb.xml").to_s
|
|
|
|
assert_equal "test/hello_world.erb", @controller.view_paths.find_template_file_for_path("test/hello_world.html").to_s
|
|
|
|
assert_equal "test/hello_world.erb", @controller.view_paths.find_template_file_for_path("test/hello_world.xml").to_s
|
|
|
|
assert_equal nil, @controller.view_paths.find_template_file_for_path("test/missing.html")
|
2007-02-04 15:47:05 -05:00
|
|
|
end
|
2008-01-05 08:32:06 -05:00
|
|
|
end
|