diff options
Diffstat (limited to 'actionpack/lib/action_controller/routing.rb')
-rw-r--r-- | actionpack/lib/action_controller/routing.rb | 36 |
1 files changed, 31 insertions, 5 deletions
diff --git a/actionpack/lib/action_controller/routing.rb b/actionpack/lib/action_controller/routing.rb index 70c864646d..14921c3ac0 100644 --- a/actionpack/lib/action_controller/routing.rb +++ b/actionpack/lib/action_controller/routing.rb @@ -57,28 +57,54 @@ module ActionController ensure use_controllers! nil end - + + def normalize_paths(paths=$LOAD_PATH) + # do the hokey-pokey of path normalization... + paths = paths.collect do |path| + path = path. + gsub("//", "/"). # replace double / chars with a single + gsub("\\\\", "\\"). # replace double \ chars with a single + gsub(%r{(.)[\\/]$}, '\1') # drop final / or \ if path ends with it + + # eliminate .. paths where possible + re = %r{\w+[/\\]\.\.[/\\]} + path.gsub!(%r{\w+[/\\]\.\.[/\\]}, "") while path.match(re) + path + end + + # start with longest path, first + paths = paths.uniq.sort_by { |path| - path.length } + end + def possible_controllers unless @possible_controllers @possible_controllers = [] paths = $LOAD_PATH.select { |path| File.directory? path } - paths = paths.sort_by { |path| - path.length } - + seen_paths = Hash.new {|h, k| h[k] = true; false} - paths.each do |load_path| + normalize_paths(paths).each do |load_path| Dir["#{load_path}/**/*_controller.rb"].collect do |path| - next if seen_paths[path] + next if seen_paths[path.gsub(%r{^\.[/\\]}, "")] controller_name = path[(load_path.length + 1)..-1] + next unless path_may_be_controller?(controller_name) + controller_name.gsub!(/_controller\.rb\Z/, '') @possible_controllers << controller_name end end + + # remove duplicates + @possible_controllers.uniq! end @possible_controllers end + def path_may_be_controller?(path) + path !~ /(?:rails\/.*\/(?:examples|test))|(?:actionpack\/lib\/action_controller.rb$)|(?:app\/controllers)/o + end + def use_controllers!(controller_names) @possible_controllers = controller_names end |