Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed the layout defaults (closes #9564) [lifo]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7661 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Sep 28, 2007
1 parent 5f4d121 commit 3c69535
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 17 deletions.
33 changes: 19 additions & 14 deletions actionpack/lib/action_controller/layout.rb
Expand Up @@ -167,17 +167,22 @@ module ClassMethods
# If a layout is specified, all rendered actions will have their result rendered
# when the layout<tt>yield</tt>'s. This layout can itself depend on instance variables assigned during action
# performance and have access to them as any normal template would.
def layout(template_name, conditions = {})
def layout(template_name, conditions = {}, auto = false)
add_layout_conditions(conditions)
write_inheritable_attribute "layout", template_name
write_inheritable_attribute "auto_layout", auto
end

def layout_conditions #:nodoc:
@layout_conditions ||= read_inheritable_attribute("layout_conditions")
end

def default_layout #:nodoc:
@default_layout ||= read_inheritable_attribute("layout")
def default_layout(format) #:nodoc:
layout = read_inheritable_attribute("layout")
return layout unless read_inheritable_attribute("auto_layout")
@default_layout ||= {}
@default_layout[format] ||= default_layout_with_format(format, layout)
@default_layout[format]
end

def layout_list #:nodoc:
Expand All @@ -190,7 +195,7 @@ def layout_list #:nodoc:
def inherited_with_layout(child)
inherited_without_layout(child)
layout_match = child.name.underscore.sub(/_controller$/, '').sub(/^controllers\//, '')
child.layout(layout_match) unless child.layout_list.grep(%r{layouts/#{layout_match}(\.[a-z][0-9a-z]*)+$}).empty?
child.layout(layout_match, {}, true) unless child.layout_list.grep(%r{layouts/#{layout_match}(\.[a-z][0-9a-z]*)+$}).empty?
end

def add_layout_conditions(conditions)
Expand All @@ -206,14 +211,23 @@ def layout_directory_exists_cache
h[dirname] = File.directory? dirname
end
end

def default_layout_with_format(format, layout)
list = layout_list
if list.grep(%r{layouts/#{layout}\.#{format}(\.[a-z][0-9a-z]*)+$}).empty?
(!list.grep(%r{layouts/#{layout}\.([a-z][0-9a-z]*)+$}).empty? && format == :html) ? layout : nil
else
layout
end
end
end

# Returns the name of the active layout. If the layout was specified as a method reference (through a symbol), this method
# is called and the return value is used. Likewise if the layout was specified as an inline method (through a proc or method
# object). If the layout was defined without a directory, layouts is assumed. So <tt>layout "weblog/standard"</tt> will return
# weblog/standard, but <tt>layout "standard"</tt> will return layouts/standard.
def active_layout(passed_layout = nil)
layout = passed_layout || self.class.default_layout
layout = passed_layout || self.class.default_layout(response.template.template_format)
active_layout = case layout
when String then layout
when Symbol then send(layout)
Expand All @@ -235,7 +249,6 @@ def active_layout(passed_layout = nil)
protected
def render_with_a_layout(options = nil, &block) #:nodoc:
template_with_options = options.is_a?(Hash)
set_template_format(options)

if apply_layout?(template_with_options, options) && (layout = pick_layout(template_with_options, options))
assert_existence_of_template_file(layout)
Expand Down Expand Up @@ -306,13 +319,5 @@ def layout_directory?(layout_name)
self.class.send(:layout_directory_exists_cache)[File.dirname(template_path)]
end
end

def set_template_format(options)
if options.is_a?(Hash) && options[:content_type]
response.template.template_format = options[:content_type].to_sym
elsif params[:format]
response.template.template_format = Mime::Type.lookup(Mime::Type.lookup_by_extension(params[:format]).to_s).to_sym
end
end
end
end
74 changes: 71 additions & 3 deletions actionpack/test/controller/mime_responds_test.rb
Expand Up @@ -429,8 +429,76 @@ def test_format_with_custom_response_type_and_request_headers_with_only_one_layo
assert_equal '<html><div id="html_missing">Hello future from Firefox!</div></html>', @response.body

@request.env["HTTP_ACCEPT"] = "text/iphone"
get :iphone_with_html_response_type_without_layout
assert_equal 'Hello iPhone future from iPhone!', @response.body
assert_equal "text/html", @response.content_type
assert_raises(ActionController::MissingTemplate) { get :iphone_with_html_response_type_without_layout }
end
end

class AbstractPostController < ActionController::Base
class << self
def view_paths
[ File.dirname(__FILE__) + "/../fixtures/post_test/" ]
end
end
end

# For testing layouts which are set automatically
class PostController < AbstractPostController
around_filter :with_iphone

def index
respond_to do |type|
type.html
type.iphone
end
end

protected

def with_iphone
Mime::Type.register_alias("text/html", :iphone)
request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone"
yield
Mime.send :remove_const, :IPHONE
end

end

class SuperPostController < PostController
def index
respond_to do |type|
type.html
type.iphone
end
end
end

class MimeControllerLayoutsTest < Test::Unit::TestCase
def setup
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new

@controller = PostController.new
@request.host = "www.example.com"
end

def test_missing_layout_renders_properly
get :index
assert_equal '<html><div id="html">Hello Firefox</div></html>', @response.body

@request.env["HTTP_ACCEPT"] = "text/iphone"
get :index
assert_equal 'Hello iPhone', @response.body
end

def test_format_with_inherited_layouts
@controller = SuperPostController.new

get :index
assert_equal 'Super Firefox', @response.body

@request.env["HTTP_ACCEPT"] = "text/iphone"
get :index
assert_equal '<html><div id="super_iphone">Super iPhone</div></html>', @response.body
end
end

1 change: 1 addition & 0 deletions actionpack/test/fixtures/post_test/layouts/post.html.erb
@@ -0,0 +1 @@
<html><div id="html"><%= yield %></div></html>
@@ -0,0 +1 @@
<html><div id="super_iphone"><%= yield %></div></html>
1 change: 1 addition & 0 deletions actionpack/test/fixtures/post_test/post/index.html.erb
@@ -0,0 +1 @@
Hello Firefox
1 change: 1 addition & 0 deletions actionpack/test/fixtures/post_test/post/index.iphone.erb
@@ -0,0 +1 @@
Hello iPhone
@@ -0,0 +1 @@
Super Firefox
@@ -0,0 +1 @@
Super iPhone

0 comments on commit 3c69535

Please sign in to comment.