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

Changeset 3423

Show
Ignore:
Timestamp:
01/15/06 11:28:55 (3 years ago)
Author:
marcel
Message:

Automatically discover layouts when a controller is namespaced. Closes #2199, #3424.

Files:

Legend:

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

    r3412 r3423  
    11*SVN* 
     2 
     3* Automatically discover layouts when a controller is namespaced. #2199, #3424 [me@jonnii.com rails@jeffcole.net Marcel Molina Jr.] 
    24 
    35* Add support for multiple proxy servers to CgiRequest#host [gaetanot@comcast.net] 
  • trunk/actionpack/lib/action_controller/layout.rb

    r3397 r3423  
    175175        def inherited(child) 
    176176          inherited_without_layout(child) 
    177           child.layout(child.controller_name) unless layout_list.grep(/^#{child.controller_name}\.[a-z][0-9a-z]*$/).empty? 
     177          layout_match = child.name.underscore.sub(/_controller$/, '') 
     178          child.layout(layout_match) unless layout_list.grep(%r{layouts/#{layout_match}\.[a-z][0-9a-z]*$}).empty? 
    178179        end 
    179180 
    180181        def layout_list 
    181           Dir.glob("#{template_root}/layouts/*.*").map { |layout| File.basename(layout) } 
     182          Dir.glob("#{template_root}/layouts/**/*") 
    182183        end 
    183184 
     
    203204        when String then layout 
    204205      end 
    205  
    206       active_layout.include?("/") ? active_layout : "layouts/#{active_layout}" if active_layout 
     206       
     207      # Explicitly passed layout names with slashes are looked up relative to the template root, 
     208      # but auto-discovered layouts derived from a nested controller will contain a slash, though be relative 
     209      # to the 'layouts' directory so we have to check the file system to infer which case the layout name came from. 
     210      nested_controller = File.directory?(File.dirname(File.join(self.class.template_root, 'layouts', active_layout))) 
     211      active_layout.include?('/') && !nested_controller ? active_layout : "layouts/#{active_layout}" if active_layout 
    207212    end 
    208213 
  • trunk/actionpack/test/controller/layout_test.rb

    r3397 r3423  
    1919 
    2020class ThirdPartyTemplateLibraryController < LayoutTest 
     21end 
     22 
     23module ControllerNameSpace 
     24end 
     25 
     26class ControllerNameSpace::NestedController < LayoutTest 
    2127end 
    2228 
     
    5864    assert_equal 'Mab', @response.body 
    5965  end 
     66   
     67  def test_namespaced_controllers_auto_detect_layouts 
     68    @controller = ControllerNameSpace::NestedController.new 
     69    get :hello 
     70    assert_equal 'layouts/controller_name_space/nested', @controller.active_layout 
     71    assert_equal 'controller_name_space/nested.rhtml hello.rhtml', @response.body 
     72  end 
    6073 
    6174end