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

Ticket #2754: alternate_base_paths2.diff

File alternate_base_paths2.diff, 4.2 kB (added by duane.johnson@gmail.com, 4 years ago)

Gives ActionView::Base an alternate base_path list

  • test/controller/render_test.rb

    old new  
    9292  def render_to_string_test 
    9393    @foo = render_to_string :inline => "this is a test" 
    9494  end 
     95   
     96  def alternate_only 
     97    # See if the alternate_base_path_* options works 
     98  end 
    9599 
    96100  def rescue_action(e) raise end 
    97101     
     
    240244    @request.query_parameters[:local_name] = "Local David" 
    241245    assert_equal "Goodbye, Local David", process_request.body 
    242246  end 
     247   
     248  def test_alternate_base_paths_before 
     249    ActionView::Base.alternate_base_paths_before = 
     250      [File.dirname(__FILE__) + "/../fixtures/alternate_base_path/"] 
     251     
     252    @request.action = "hello_world" 
     253    assert_equal "Bonjour monde!", process_request.body 
    243254 
     255    @request.action = "greeting" 
     256    assert_equal "<p>This is grand!</p>\n", process_request.body 
     257 
     258    ActionView::Base.alternate_base_paths_before = [] 
     259  end 
     260 
     261  def test_alternate_base_paths_after 
     262    ActionView::Base.alternate_base_paths_after = 
     263      [File.dirname(__FILE__) + "/../fixtures/alternate_base_path/"] 
     264     
     265    @request.action = "hello_world" 
     266    assert_equal "Hello world!", process_request.body 
     267 
     268    @request.action = "alternate_only" 
     269    assert_equal "Only the alternate base path has this file.", process_request.body 
     270 
     271    ActionView::Base.alternate_base_paths_after = [] 
     272  end 
     273 
    244274  private 
    245275    def process_request 
    246276      TestController.process(@request, @response) 
  • test/fixtures/alternate_base_path/test/hello_world.rhtml

    old new  
  • test/fixtures/alternate_base_path/test/alternate_only.rhtml

    old new  
  • lib/action_view/base.rb

    old new  
    138138    # shortly. 
    139139    @@local_assigns_support_string_keys = true 
    140140    cattr_accessor :local_assigns_support_string_keys 
     141     
     142    # Specify alternate paths for view files, if any.  The _before_ list will 
     143    # be checked before the primary @base_path while the _after_ list will be 
     144    # checked after. 
     145    @@alternate_base_paths_before = [] 
     146    @@alternate_base_paths_after = [] 
     147    cattr_accessor :alternate_base_paths_before, :alternate_base_paths_after 
    141148 
    142149    @@template_handlers = {} 
    143150  
     
    306313 
    307314    private 
    308315      def full_template_path(template_path, extension) 
    309         "#{@base_path}/#{template_path}.#{extension}" 
     316        primary_location = "#{@base_path}/#{template_path}.#{extension}" 
     317        return primary_location if only_one_base_path? 
     318 
     319        # If there are alternate base paths to try, check them each one by one 
     320        all_base_paths.each do |try_path| 
     321          try_location = "#{try_path}/#{template_path}.#{extension}" 
     322          return try_location if template_is_cached_or_exists_on_disk?(try_location) 
     323        end 
     324         
     325        # If it cannot be found anywhere, return the default path, where the 
     326        # user *should* have put it.   
     327        return primary_location 
    310328      end 
     329       
     330      def all_base_paths 
     331        @@alternate_base_paths_before + [ @base_path ] + @@alternate_base_paths_after 
     332      end 
     333       
     334      def only_one_base_path? 
     335        alternate_base_paths_before.empty? and alternate_base_paths_after.empty? 
     336      end 
    311337 
    312338      def template_exists?(template_path, extension) 
    313339        file_path = full_template_path(template_path, extension) 
     340        template_is_cached_or_exists_on_disk?(file_path) 
     341      end 
     342       
     343      def template_is_cached_or_exists_on_disk?(file_path) 
    314344        @@method_names.has_key?(file_path) || FileTest.exists?(file_path) 
    315345      end 
    316346