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

Changeset 3861

Show
Ignore:
Timestamp:
03/13/06 15:54:33 (3 years ago)
Author:
ulysses
Message:

Simplify controller_path
Cache File.directory? calls to avoid filesystem calls when using layouts

Files:

Legend:

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

    r3856 r3861  
    11*SVN* 
     2 
     3* Avoid hitting the filesystem when using layouts by using a File.directory? cache. [Stefan Kaes, Nicholas Seckar] 
     4 
     5* Simplify ActionController::Base#controller_path [Nicholas Seckar] 
    26 
    37* Added simple alert() notifications for RJS exceptions when config.action_view.debug_rjs = true. [Sam Stephenson] 
  • trunk/actionpack/lib/action_controller/base.rb

    r3860 r3861  
    349349      # Converts the class name from something like "OneModule::TwoModule::NeatController" to "one_module/two_module/neat". 
    350350      def controller_path 
    351         unless @controller_path 
    352           components = self.name.to_s.split('::') 
    353           components[-1] = $1 if /^(.*)Controller$/ =~ components.last 
    354           @controller_path = components.map { |name| name.underscore }.join('/') 
    355         end 
    356  
    357         @controller_path 
     351        @controller_path ||= name.gsub(/Controller$/, '').underscore 
    358352      end 
    359353 
  • trunk/actionpack/lib/action_controller/layout.rb

    r3852 r3861  
    193193          conditions.inject({}) {|hash, (key, value)| hash.merge(key => [value].flatten.map {|action| action.to_s})} 
    194194        end 
     195         
     196        def layout_directory_exists_cache 
     197          @@layout_directory_exists_cache ||= Hash.new do |h, dirname| 
     198            h[dirname] = File.directory? dirname 
     199          end 
     200        end 
    195201    end 
    196202 
     
    203209 
    204210      active_layout = case layout 
     211        when String then layout 
    205212        when Symbol then send(layout) 
    206213        when Proc   then layout.call(self) 
    207         when String then layout 
    208214      end 
    209215       
     
    211217      # but auto-discovered layouts derived from a nested controller will contain a slash, though be relative 
    212218      # to the 'layouts' directory so we have to check the file system to infer which case the layout name came from. 
    213       nested_controller = File.directory?(File.dirname(File.join(self.class.template_root, 'layouts', active_layout))) if active_layout 
    214       active_layout.include?('/') && !nested_controller ? active_layout : "layouts/#{active_layout}" if active_layout 
     219      if active_layout 
     220        if active_layout.include?('/') && ! layout_directory?(active_layout) 
     221          active_layout 
     222        else 
     223          "layouts/#{active_layout}" 
     224        end 
     225      end 
    215226    end 
    216227 
     
    281292        end 
    282293      end 
     294       
     295      # Does a layout directory for this class exist? 
     296      # we cache this info in a class level hash 
     297      def layout_directory?(layout_name) 
     298        template_path = File.join(self.class.view_root, 'layouts', layout_name) 
     299        dirname = File.dirname(template_path) 
     300        self.class.send(:layout_directory_exists_cache)[dirname] 
     301      end 
    283302  end 
    284303end