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

Ticket #307: AP-default_layouts_set_automatically.diff

File AP-default_layouts_set_automatically.diff, 1.5 kB (added by marcel, 4 years ago)

Tries to automatically set a controller's layout

  • layout.rb

    old new  
    22  module Layout #:nodoc: 
    33    def self.append_features(base) 
    44      super 
    5       base.extend(ClassMethods) 
    65      base.class_eval do 
    76        alias_method :render_without_layout, :render 
    87        alias_method :render, :render_with_layout 
     8        class << self 
     9          alias_method :inherited_without_layout, :inherited 
     10        end 
    911      end 
     12      base.extend(ClassMethods) 
    1013    end 
    1114 
    1215    # Layouts reverse the common pattern of including shared headers and footers in many templates to isolate changes in 
     
    119122      def layout(template_name) 
    120123        write_inheritable_attribute "layout", template_name 
    121124      end 
     125 
     126      private 
     127        def inherited(child) 
     128          inherited_without_layout(child) 
     129          controller = child.controller_name 
     130          layout_name = controller.eql?('abstract_application') ? 'application' : controller 
     131          child.layout(layout_name) unless layout_list.grep(/^#{layout_name}\.r(?:xml|html)$/).empty? 
     132        end 
     133 
     134        def layout_list 
     135          Dir.glob("#{RAILS_ROOT}/app/views/layouts/*.r{xml,html}").map {|l| File.basename(l)} 
     136        end 
    122137    end 
    123138 
    124139    # Returns the name of the active layout. If the layout was specified as a method reference (through a symbol), this method 
     
    145160        render_without_layout(template_name, status) 
    146161      end 
    147162    end 
     163 
    148164  end 
    149165end