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

Changeset 7661

Show
Ignore:
Timestamp:
09/28/07 01:23:20 (1 year ago)
Author:
david
Message:

Fixed the layout defaults (closes #9564) [lifo]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/lib/action_controller/layout.rb

    r7629 r7661  
    168168      # when the layout<tt>yield</tt>'s. This layout can itself depend on instance variables assigned during action 
    169169      # performance and have access to them as any normal template would. 
    170       def layout(template_name, conditions = {}
     170      def layout(template_name, conditions = {}, auto = false
    171171        add_layout_conditions(conditions) 
    172172        write_inheritable_attribute "layout", template_name 
     173        write_inheritable_attribute "auto_layout", auto 
    173174      end 
    174175 
     
    177178      end 
    178179       
    179       def default_layout #:nodoc: 
    180         @default_layout ||= read_inheritable_attribute("layout") 
     180      def default_layout(format) #:nodoc: 
     181        layout = read_inheritable_attribute("layout")                  
     182        return layout unless read_inheritable_attribute("auto_layout") 
     183        @default_layout ||= {} 
     184        @default_layout[format] ||= default_layout_with_format(format, layout) 
     185        @default_layout[format] 
    181186      end 
    182187       
     
    191196          inherited_without_layout(child) 
    192197          layout_match = child.name.underscore.sub(/_controller$/, '').sub(/^controllers\//, '') 
    193           child.layout(layout_match) unless child.layout_list.grep(%r{layouts/#{layout_match}(\.[a-z][0-9a-z]*)+$}).empty? 
     198          child.layout(layout_match, {}, true) unless child.layout_list.grep(%r{layouts/#{layout_match}(\.[a-z][0-9a-z]*)+$}).empty? 
    194199        end 
    195200 
     
    207212          end 
    208213        end 
     214         
     215        def default_layout_with_format(format, layout) 
     216          list = layout_list 
     217          if list.grep(%r{layouts/#{layout}\.#{format}(\.[a-z][0-9a-z]*)+$}).empty? 
     218            (!list.grep(%r{layouts/#{layout}\.([a-z][0-9a-z]*)+$}).empty? && format == :html) ? layout : nil 
     219          else 
     220            layout 
     221          end 
     222        end 
    209223    end 
    210224 
     
    214228    # weblog/standard, but <tt>layout "standard"</tt> will return layouts/standard. 
    215229    def active_layout(passed_layout = nil) 
    216       layout = passed_layout || self.class.default_layout 
     230      layout = passed_layout || self.class.default_layout(response.template.template_format) 
    217231      active_layout = case layout 
    218232        when String then layout 
     
    236250      def render_with_a_layout(options = nil, &block) #:nodoc: 
    237251        template_with_options = options.is_a?(Hash) 
    238         set_template_format(options) 
    239252         
    240253        if apply_layout?(template_with_options, options) && (layout = pick_layout(template_with_options, options)) 
     
    307320        end 
    308321      end 
    309        
    310       def set_template_format(options) 
    311         if options.is_a?(Hash) && options[:content_type] 
    312           response.template.template_format = options[:content_type].to_sym  
    313         elsif params[:format] 
    314           response.template.template_format = Mime::Type.lookup(Mime::Type.lookup_by_extension(params[:format]).to_s).to_sym 
    315         end 
    316       end 
    317322  end 
    318323end 
  • trunk/actionpack/test/controller/mime_responds_test.rb

    r7632 r7661  
    430430 
    431431    @request.env["HTTP_ACCEPT"] = "text/iphone" 
    432     get :iphone_with_html_response_type_without_layout 
    433     assert_equal 'Hello iPhone future from iPhone!', @response.body 
    434     assert_equal "text/html", @response.content_type 
     432    assert_raises(ActionController::MissingTemplate) { get :iphone_with_html_response_type_without_layout } 
    435433  end  
    436434end 
     435 
     436class AbstractPostController < ActionController::Base 
     437  class << self 
     438    def view_paths 
     439      [ File.dirname(__FILE__) + "/../fixtures/post_test/" ] 
     440    end 
     441  end 
     442end 
     443 
     444# For testing layouts which are set automatically 
     445class PostController < AbstractPostController 
     446  around_filter :with_iphone 
     447   
     448  def index 
     449    respond_to do |type| 
     450      type.html 
     451      type.iphone 
     452    end 
     453  end 
     454   
     455  protected 
     456   
     457  def with_iphone 
     458    Mime::Type.register_alias("text/html", :iphone) 
     459    request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone" 
     460    yield 
     461    Mime.send :remove_const, :IPHONE     
     462  end 
     463   
     464end                                                
     465 
     466class SuperPostController < PostController   
     467  def index 
     468    respond_to do |type| 
     469      type.html 
     470      type.iphone 
     471    end 
     472  end 
     473end 
     474 
     475class MimeControllerLayoutsTest < Test::Unit::TestCase 
     476  def setup 
     477    @request    = ActionController::TestRequest.new 
     478    @response   = ActionController::TestResponse.new 
     479 
     480    @controller   = PostController.new 
     481    @request.host = "www.example.com" 
     482  end 
     483   
     484  def test_missing_layout_renders_properly 
     485    get :index 
     486    assert_equal '<html><div id="html">Hello Firefox</div></html>', @response.body  
     487 
     488    @request.env["HTTP_ACCEPT"] = "text/iphone" 
     489    get :index 
     490    assert_equal 'Hello iPhone', @response.body 
     491  end 
     492   
     493  def test_format_with_inherited_layouts 
     494    @controller = SuperPostController.new 
     495     
     496    get :index 
     497    assert_equal 'Super Firefox', @response.body 
     498     
     499    @request.env["HTTP_ACCEPT"] = "text/iphone" 
     500    get :index 
     501    assert_equal '<html><div id="super_iphone">Super iPhone</div></html>', @response.body 
     502  end 
     503end 
     504