Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8035

Show
Ignore:
Timestamp:
10/26/07 05:45:41 (10 months ago)
Author:
rick
Message:

Simplfy #view_paths implementation. ActionView templates get the exact object, not a dup. [Rick]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r8034 r8035  
    11*SVN* 
     2 
     3* Simplfy #view_paths implementation.  ActionView templates get the exact object, not a dup.  [Rick] 
    24 
    35* Update tests for ActiveSupport's JSON escaping change.  [rick] 
  • trunk/actionpack/lib/action_controller/base.rb

    r8011 r8035  
    409409      end 
    410410 
    411  
    412       @@view_paths = {} 
    413  
    414       # View load paths determine the bases from which template references can be made. So a call to 
    415       # render("test/template") will be looked up in the view load paths array and the closest match will be 
    416       # returned. 
     411      ## View load paths determine the bases from which template references can be made. So a call to 
     412      ## render("test/template") will be looked up in the view load paths array and the closest match will be 
     413      ## returned. 
     414      def view_paths 
     415        @view_paths || superclass.view_paths 
     416      end 
     417 
    417418      def view_paths=(value) 
    418         @@view_paths[name] = value 
    419       end 
    420  
    421       # View load paths for controller. 
    422       def view_paths 
    423         if paths = @@view_paths[name] 
    424           paths 
    425         else 
    426           if superclass.respond_to?(:view_paths) 
    427             superclass.view_paths.dup.freeze 
    428           else 
    429             @@view_paths[name] = [] 
    430           end 
    431         end 
    432       end 
    433        
     419        @view_paths = value 
     420      end 
     421 
    434422      # Adds a view_path to the front of the view_paths array. 
    435423      # If the current class has no view paths, copy them from  
    436424      # the superclass 
     425      # 
     426      #   ArticleController.prepend_view_path("views/default") 
     427      #   ArticleController.prepend_view_path(["views/default", "views/custom"]) 
     428      # 
    437429      def prepend_view_path(path) 
    438         self.view_paths = view_paths.dup if view_paths.frozen
    439         view_paths.unshift(path) 
     430        @view_paths = superclass.view_paths.dup if @view_paths.nil
     431        view_paths.unshift(*path) 
    440432      end 
    441433       
     
    443435      # If the current class has no view paths, copy them from  
    444436      # the superclass 
     437      # 
     438      #   ArticleController.append_view_path("views/default") 
     439      #   ArticleController.append_view_path(["views/default", "views/custom"]) 
     440      # 
    445441      def append_view_path(path) 
    446         self.view_paths = view_paths.dup if view_paths.frozen
    447         view_paths << path 
     442        @view_paths = superclass.view_paths.dup if @view_paths.nil
     443        view_paths.push(*path) 
    448444      end 
    449445       
     
    632628      end 
    633629 
     630       
     631      self.view_paths = [] 
     632       
    634633      # View load paths for controller. 
    635634      def view_paths 
    636         self.class.view_paths 
     635        (@template || self.class).view_paths 
     636      end 
     637     
     638      def view_paths=(value) 
     639        (@template || self.class).view_paths = value 
    637640      end 
    638641       
  • trunk/actionpack/test/controller/view_paths_test.rb

    r7426 r8035  
    44   
    55  LOAD_PATH_ROOT = File.join(File.dirname(__FILE__), '..', 'fixtures') 
     6 
     7  ActionController::Base.view_paths = [ LOAD_PATH_ROOT ] 
    68 
    79  class TestController < ActionController::Base 
     
    2527   
    2628  def setup 
    27     TestController.view_paths = [ LOAD_PATH_ROOT ] 
     29    TestController.view_paths = nil 
    2830    @controller = TestController.new 
    2931    @request  = ActionController::TestRequest.new 
     
    4244  def test_template_load_path_was_set_correctly 
    4345    assert_equal [ LOAD_PATH_ROOT ], @controller.view_paths 
     46  end 
     47   
     48  def test_template_appends_path_correctly 
     49    TestController.append_view_path 'foo' 
     50    assert_equal [LOAD_PATH_ROOT, 'foo'], @controller.view_paths 
     51     
     52    TestController.append_view_path(%w(bar baz)) 
     53    assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths 
     54  end 
     55   
     56  def test_template_prepends_path_correctly 
     57    TestController.prepend_view_path 'baz' 
     58    assert_equal ['baz', LOAD_PATH_ROOT], @controller.view_paths 
     59     
     60    TestController.prepend_view_path(%w(foo bar)) 
     61    assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths 
    4462  end 
    4563   
     
    86104    assert_equal original_load_paths, C.view_paths 
    87105     
    88     e = assert_raises(TypeError) { C.view_paths << 'c/path' } 
    89     assert_equal "can't modify frozen array", e.message 
    90      
    91106    C.view_paths = [] 
    92107    assert_nothing_raised { C.view_paths << 'c/path' } 
     108    assert_equal ['c/path'], C.view_paths 
    93109  end 
    94110