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

Changeset 8214

Show
Ignore:
Timestamp:
11/26/07 03:59:23 (11 months ago)
Author:
rick
Message:

Add #prepend_view_path and #append_view_path instance methods on ActionController::Base for consistency with the class methods. [rick]

Files:

Legend:

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

    r8213 r8214  
    11*SVN* 
     2 
     3* Add #prepend_view_path and #append_view_path instance methods on ActionController::Base for consistency with the class methods.  [rick] 
    24 
    35* Refactor sanitizer helpers into HTML classes and make it easy to swap them out with custom implementations.  Closes #10129.  [rick] 
  • trunk/actionpack/lib/action_controller/base.rb

    r8189 r8214  
    431431      # Adds a view_path to the front of the view_paths array. 
    432432      # If the current class has no view paths, copy them from  
    433       # the superclass 
     433      # the superclass.  This change will be visible for all future requests. 
    434434      # 
    435435      #   ArticleController.prepend_view_path("views/default") 
     
    443443      # Adds a view_path to the end of the view_paths array. 
    444444      # If the current class has no view paths, copy them from  
    445       # the superclass 
     445      # the superclass. This change will be visible for all future requests. 
    446446      # 
    447447      #   ArticleController.append_view_path("views/default") 
     
    637637      end 
    638638 
    639        
    640639      self.view_paths = [] 
    641640       
     
    648647        (@template || self.class).view_paths = value 
    649648      end 
     649 
     650      # Adds a view_path to the front of the view_paths array. 
     651      # This change affects the current request only. 
     652      # 
     653      #   self.prepend_view_path("views/default") 
     654      #   self.prepend_view_path(["views/default", "views/custom"]) 
     655      # 
     656      def prepend_view_path(path) 
     657        (@template || self.class).prepend_view_path(path) 
     658      end 
    650659       
     660      # Adds a view_path to the end of the view_paths array. 
     661      # This change affects the current request only. 
     662      # 
     663      #   self.append_view_path("views/default") 
     664      #   self.append_view_path(["views/default", "views/custom"]) 
     665      # 
     666      def append_view_path(path) 
     667        (@template || self.class).append_view_path(path) 
     668      end 
     669 
    651670    protected 
    652671      # Renders the content that will be returned to the browser as the response body. 
  • trunk/actionpack/lib/action_view/base.rb

    r8211 r8214  
    266266 
    267267    def initialize(view_paths = [], assigns_for_first_render = {}, controller = nil)#:nodoc: 
    268       @view_paths = view_paths.respond_to?(:find) ? view_paths : [*view_paths].compact 
     268      @view_paths = view_paths.respond_to?(:find) ? view_paths.dup : [*view_paths].compact 
    269269      @assigns = assigns_for_first_render 
    270270      @assigns_added = nil 
     
    471471    def template_handler_preferences 
    472472      TEMPLATE_HANDLER_PREFERENCES[template_format] || DEFAULT_TEMPLATE_HANDLER_PREFERENCE 
     473    end 
     474 
     475    # Adds a view_path to the front of the view_paths array. 
     476    # This change affects the current request only. 
     477    # 
     478    #   @template.prepend_view_path("views/default") 
     479    #   @template.prepend_view_path(["views/default", "views/custom"]) 
     480    # 
     481    def prepend_view_path(path) 
     482      @view_paths.unshift(*path) 
     483    end 
     484     
     485    # Adds a view_path to the end of the view_paths array. 
     486    # This change affects the current request only. 
     487    # 
     488    #   @template.append_view_path("views/default") 
     489    #   @template.append_view_path(["views/default", "views/custom"]) 
     490    # 
     491    def append_view_path(path) 
     492      @view_paths.push(*path) 
    473493    end 
    474494 
  • trunk/actionpack/test/controller/view_paths_test.rb

    r8135 r8214  
    4848  end 
    4949   
    50   def test_template_appends_path_correctly 
     50  def test_controller_appends_view_path_correctly 
    5151    TestController.append_view_path 'foo' 
    5252    assert_equal [LOAD_PATH_ROOT, 'foo'], @controller.view_paths 
     
    5656  end 
    5757   
    58   def test_template_prepends_path_correctly 
     58  def test_controller_prepends_view_path_correctly 
    5959    TestController.prepend_view_path 'baz' 
    6060    assert_equal ['baz', LOAD_PATH_ROOT], @controller.view_paths 
     
    6262    TestController.prepend_view_path(%w(foo bar)) 
    6363    assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths 
     64  end 
     65   
     66  def test_template_appends_view_path_correctly 
     67    @controller.instance_variable_set :@template, ActionView::Base.new(TestController.view_paths, {}, @controller) 
     68    class_view_paths = TestController.view_paths 
     69 
     70    @controller.append_view_path 'foo' 
     71    assert_equal [LOAD_PATH_ROOT, 'foo'], @controller.view_paths 
     72     
     73    @controller.append_view_path(%w(bar baz)) 
     74    assert_equal [LOAD_PATH_ROOT, 'foo', 'bar', 'baz'], @controller.view_paths 
     75    assert_equal class_view_paths, TestController.view_paths 
     76  end 
     77   
     78  def test_template_prepends_view_path_correctly 
     79    @controller.instance_variable_set :@template, ActionView::Base.new(TestController.view_paths, {}, @controller) 
     80    class_view_paths = TestController.view_paths 
     81     
     82    @controller.prepend_view_path 'baz' 
     83    assert_equal ['baz', LOAD_PATH_ROOT], @controller.view_paths 
     84     
     85    @controller.prepend_view_path(%w(foo bar)) 
     86    assert_equal ['foo', 'bar', 'baz', LOAD_PATH_ROOT], @controller.view_paths 
     87    assert_equal class_view_paths, TestController.view_paths 
    6488  end 
    6589