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

Changeset 5126

Show
Ignore:
Timestamp:
09/16/06 01:31:17 (3 years ago)
Author:
bitsweat
Message:

Declare file extensions exempt from layouts. Closes #6219.

Files:

Legend:

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

    r5110 r5126  
    11*SVN* 
     2 
     3* Declare file extensions exempt from layouts.  #6219 [brandon] 
     4    Example: ActionController::Base.exempt_from_layout 'rpdf' 
    25 
    36* Add chained replace/update support for assert_select_rjs [Rick Olson] 
  • trunk/actionpack/lib/action_controller/base.rb

    r4988 r5126  
    316316    attr_accessor :action_name 
    317317 
     318    # Templates that are exempt from layouts 
     319    @@exempt_from_layout = Set.new([/\.rjs$/]) 
     320 
    318321    class << self 
    319322      # Factory for the standard create, process loop where the controller is discarded after processing. 
     
    395398          filtered_parameters 
    396399        end 
     400      end 
     401 
     402      # Don't render layouts for templates with the given extensions. 
     403      def exempt_from_layout(*extensions) 
     404        regexps = extensions.collect do |extension| 
     405          extension.is_a?(Regexp) ? extension : /\.#{Regexp.escape(extension.to_s)}$/ 
     406        end 
     407        @@exempt_from_layout.merge regexps 
    397408      end 
    398409    end 
     
    10951106 
    10961107      def template_exempt_from_layout?(template_name = default_template_name) 
    1097         template_name =~ /\.rjs$/ || (@template.pick_template_extension(template_name) == :rjs rescue false) 
     1108        @@exempt_from_layout.any? { |ext| template_name =~ ext } or 
     1109          @template.pick_template_extension(template_name) == :rjs 
     1110      rescue 
     1111        false 
    10981112      end 
    10991113 
  • trunk/actionpack/test/controller/layout_test.rb

    r4550 r5126  
    7373end 
    7474 
     75class ExemptFromLayoutTest < Test::Unit::TestCase 
     76  def setup 
     77    @controller = LayoutTest.new 
     78  end 
     79 
     80  def test_rjs_exempt_from_layout 
     81    assert @controller.send(:template_exempt_from_layout?, 'test.rjs') 
     82  end 
     83 
     84  def test_rhtml_and_rxml_not_exempt_from_layout 
     85    assert !@controller.send(:template_exempt_from_layout?, 'test.rhtml') 
     86    assert !@controller.send(:template_exempt_from_layout?, 'test.rxml') 
     87  end 
     88 
     89  def test_other_extension_not_exempt_from_layout 
     90    assert !@controller.send(:template_exempt_from_layout?, 'test.random') 
     91  end 
     92 
     93  def test_add_extension_to_exempt_from_layout 
     94    ['rpdf', :rpdf].each do |ext| 
     95      assert_nothing_raised do 
     96        ActionController::Base.exempt_from_layout ext 
     97      end 
     98      assert @controller.send(:template_exempt_from_layout?, "test.#{ext}") 
     99    end 
     100  end 
     101 
     102  def test_add_regexp_to_exempt_from_layout 
     103    ActionController::Base.exempt_from_layout /\.rdoc/ 
     104    assert @controller.send(:template_exempt_from_layout?, 'test.rdoc') 
     105  end 
     106end 
     107 
    75108 
    76109class DefaultLayoutController < LayoutTest