diff options
author | Marcel Molina <marcel@vernix.org> | 2006-01-15 11:28:55 +0000 |
---|---|---|
committer | Marcel Molina <marcel@vernix.org> | 2006-01-15 11:28:55 +0000 |
commit | 4793a2f5cdc07a5926e8399fd82a835c0dc0a023 (patch) | |
tree | 422a9724779a3ce0be13774eb8598b7a7e70a805 | |
parent | a471e6b4d741c498439767237efad9838df44834 (diff) | |
download | rails-4793a2f5cdc07a5926e8399fd82a835c0dc0a023.tar.gz rails-4793a2f5cdc07a5926e8399fd82a835c0dc0a023.tar.bz2 rails-4793a2f5cdc07a5926e8399fd82a835c0dc0a023.zip |
Automatically discover layouts when a controller is namespaced. Closes #2199, #3424.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3423 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | actionpack/CHANGELOG | 2 | ||||
-rw-r--r-- | actionpack/lib/action_controller/layout.rb | 13 | ||||
-rw-r--r-- | actionpack/test/controller/layout_test.rb | 13 | ||||
-rw-r--r-- | actionpack/test/fixtures/layout_tests/layouts/controller_name_space/nested.rhtml | 1 |
4 files changed, 25 insertions, 4 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 3345998626..62b6d65ec9 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Automatically discover layouts when a controller is namespaced. #2199, #3424 [me@jonnii.com rails@jeffcole.net Marcel Molina Jr.] + * Add support for multiple proxy servers to CgiRequest#host [gaetanot@comcast.net] * Documentation typo fix. #2367 [Blair Zajac] diff --git a/actionpack/lib/action_controller/layout.rb b/actionpack/lib/action_controller/layout.rb index 4b396bb0d4..baa035507d 100644 --- a/actionpack/lib/action_controller/layout.rb +++ b/actionpack/lib/action_controller/layout.rb @@ -174,11 +174,12 @@ module ActionController #:nodoc: private def inherited(child) inherited_without_layout(child) - child.layout(child.controller_name) unless layout_list.grep(/^#{child.controller_name}\.[a-z][0-9a-z]*$/).empty? + layout_match = child.name.underscore.sub(/_controller$/, '') + child.layout(layout_match) unless layout_list.grep(%r{layouts/#{layout_match}\.[a-z][0-9a-z]*$}).empty? end def layout_list - Dir.glob("#{template_root}/layouts/*.*").map { |layout| File.basename(layout) } + Dir.glob("#{template_root}/layouts/**/*") end def add_layout_conditions(conditions) @@ -202,8 +203,12 @@ module ActionController #:nodoc: when Proc then layout.call(self) when String then layout end - - active_layout.include?("/") ? active_layout : "layouts/#{active_layout}" if active_layout + + # Explicitly passed layout names with slashes are looked up relative to the template root, + # but auto-discovered layouts derived from a nested controller will contain a slash, though be relative + # to the 'layouts' directory so we have to check the file system to infer which case the layout name came from. + nested_controller = File.directory?(File.dirname(File.join(self.class.template_root, 'layouts', active_layout))) + active_layout.include?('/') && !nested_controller ? active_layout : "layouts/#{active_layout}" if active_layout end def render_with_a_layout(options = nil, deprecated_status = nil, deprecated_layout = nil) #:nodoc: diff --git a/actionpack/test/controller/layout_test.rb b/actionpack/test/controller/layout_test.rb index 57eb1f73b1..cffd4d388e 100644 --- a/actionpack/test/controller/layout_test.rb +++ b/actionpack/test/controller/layout_test.rb @@ -20,6 +20,12 @@ end class ThirdPartyTemplateLibraryController < LayoutTest end +module ControllerNameSpace +end + +class ControllerNameSpace::NestedController < LayoutTest +end + class MabView def initialize(view) end @@ -57,5 +63,12 @@ class LayoutAutoDiscoveryTest < Test::Unit::TestCase assert_equal 'layouts/third_party_template_library', @controller.active_layout assert_equal 'Mab', @response.body end + + def test_namespaced_controllers_auto_detect_layouts + @controller = ControllerNameSpace::NestedController.new + get :hello + assert_equal 'layouts/controller_name_space/nested', @controller.active_layout + assert_equal 'controller_name_space/nested.rhtml hello.rhtml', @response.body + end end
\ No newline at end of file diff --git a/actionpack/test/fixtures/layout_tests/layouts/controller_name_space/nested.rhtml b/actionpack/test/fixtures/layout_tests/layouts/controller_name_space/nested.rhtml new file mode 100644 index 0000000000..5f86a7de4d --- /dev/null +++ b/actionpack/test/fixtures/layout_tests/layouts/controller_name_space/nested.rhtml @@ -0,0 +1 @@ +controller_name_space/nested.rhtml <%= yield %>
\ No newline at end of file |