aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/controller/view_paths_test.rb
blob: c84ced539458a6297744973808a71524d488be49 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
    
    before_filter :add_view_path, :only => :hello_world_at_request_time
    
    def hello_world() end
    def hello_world_at_request_time() render(:action => 'hello_world') end
    private
    def add_view_path
      self.class.view_paths.unshift "#{LOAD_PATH_ROOT}/override"
    end
  end
  
  class Test::SubController < ActionController::Base
    layout 'test/sub'
    def hello_world; render 'test/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, callback| @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_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
  
  def test_view_paths_override_at_request_time
    get :hello_world_at_request_time
    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 = 'foo/bar'
    end
    assert_deprecated(/template_root.*view_paths/) do
      assert_equal 'foo/bar', TestController.template_root
      assert_equal ['foo/bar', LOAD_PATH_ROOT], TestController.view_paths
    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