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

Changeset 927

Show
Ignore:
Timestamp:
03/20/05 14:04:33 (4 years ago)
Author:
david
Message:

Added path collection syntax for Routes that will gobble up the rest of the url and pass it on to the controller #830 [rayners]

Files:

Legend:

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

    r911 r927  
    11*SVN* 
     2 
     3* Added path collection syntax for Routes that will gobble up the rest of the url and pass it on to the controller #830 [rayners]. Example: 
     4 
     5    map.connect 'categories/*path_info', :controller => 'categories', :action => 'show' 
     6 
     7  A request for /categories/top-level-cat, would give @params[:path_info] with "top-level-cat". 
     8  A request for /categories/top-level-cat/level-1-cat, would give @params['path_info'] with "top-level-cat/level-1-cat" and so forth. 
    29 
    310* Fixed options_for_select on selected line issue #624 [Florian Weber] 
  • trunk/actionpack/lib/action_controller/routing.rb

    r870 r927  
    4949        components = @items.collect do |item| 
    5050          if item.kind_of? Symbol 
     51            collection = false 
     52 
     53            if /^\*/ =~ item.to_s 
     54              collection = true 
     55              item = item.to_s.sub(/^\*/,"").intern 
     56            end 
     57 
    5158            used_names[item] = true 
    5259            value = options[item] || defaults[item] || @defaults[item] 
    5360            return nil, requirements_for(item) unless passes_requirements?(item, value) 
     61 
    5462            defaults = {} unless defaults == {} || value == defaults[item] # Stop using defaults if this component isn't the same as the default. 
    55             (value.nil? || item == :controller) ? value : CGI.escape(value.to_s) 
    56           else item 
     63 
     64            if value.nil? || item == :controller 
     65              value 
     66            elsif collection 
     67                    CGI.escape(value.to_s).gsub(/%2F/, "/") 
     68            else 
     69              CGI.escape(value.to_s) 
     70            end 
     71          else 
     72            item 
    5773          end 
    5874        end 
     
    97113            options[:controller] = controller_class.controller_path 
    98114            return nil, requirements_for(:controller) unless passes_requirements?(:controller, options[:controller]) 
     115          elsif /^\*/ =~ item.to_s 
     116            value = components.join("/") || @defaults[item] 
     117            components = [] 
     118            options[item.to_s.sub(/^\*/,"").intern] = value.nil? ? value : CGI.unescape(value) 
    99119          elsif item.kind_of? Symbol 
    100120            value = components.shift || @defaults[item] 
     
    143163       
    144164        def items=(path) 
    145           items = path.split('/').collect {|c| (/^:(\w+)$/ =~ c) ? $1.intern : c} if path.kind_of?(String) # split and convert ':xyz' to symbols 
     165          items = path.split('/').collect {|c| (/^(:|\*)(\w+)$/ =~ c) ? (($1 == ':' ) ? $2.intern : "*#{$2}".intern) : c} if path.kind_of?(String) # split and convert ':xyz' to symbols 
    146166          items.shift if items.first == "" 
    147167          items.pop if items.last == "" 
     
    173193        end 
    174194        def requirements_for(name) 
     195          name = name.to_s.sub(/^\*/,"").intern if (/^\*/ =~ name.inspect) 
    175196          presence = (@defaults.key?(name) && @defaults[name].nil?) 
    176197          requirement = case @requirements[name] 
  • trunk/actionpack/test/controller/routing_tests.rb

    r855 r927  
    326326    assert_equal %w{action id}, leftovers 
    327327  end 
     328 
     329  def test_path_collection 
     330    route '*path_info', :controller => 'content', :action => 'fish' 
     331    verify_recognize'path/with/slashes', 
     332        :controller => 'content', :action => 'fish', :path_info => 'path/with/slashes' 
     333    verify_generate('path/with/slashes', {}, 
     334        {:controller => 'content', :action => 'fish', :path_info => 'path/with/slashes'}, 
     335        {}) 
     336  end 
    328337   
    329338  def test_special_characters