Permalink
Browse files

Added path collection syntax for Routes that will gobble up the rest …

…of the url and pass it on to the controller #830 [rayners]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@927 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent ce9a943 commit 37a46151ec14ae11f5306d997b822f30be7a69d3 @dhh dhh committed Mar 20, 2005
View
@@ -1,5 +1,12 @@
*SVN*
+* 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:
+
+ map.connect 'categories/*path_info', :controller => 'categories', :action => 'show'
+
+ A request for /categories/top-level-cat, would give @params[:path_info] with "top-level-cat".
+ 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.
+
* Fixed options_for_select on selected line issue #624 [Florian Weber]
* Added CaptureHelper with CaptureHelper#capture and CaptureHelper#content_for. See documentation in helper #837 [Tobias Luetke]
@@ -48,12 +48,28 @@ def generate(options, defaults={})
used_names = @requirements.inject({}) {|hash, (k, v)| hash[k] = true; hash} # Mark requirements as used so they don't get put in the query params
components = @items.collect do |item|
if item.kind_of? Symbol
+ collection = false
+
+ if /^\*/ =~ item.to_s
+ collection = true
+ item = item.to_s.sub(/^\*/,"").intern
+ end
+
used_names[item] = true
value = options[item] || defaults[item] || @defaults[item]
return nil, requirements_for(item) unless passes_requirements?(item, value)
+
defaults = {} unless defaults == {} || value == defaults[item] # Stop using defaults if this component isn't the same as the default.
- (value.nil? || item == :controller) ? value : CGI.escape(value.to_s)
- else item
+
+ if value.nil? || item == :controller
+ value
+ elsif collection
+ CGI.escape(value.to_s).gsub(/%2F/, "/")
+ else
+ CGI.escape(value.to_s)
+ end
+ else
+ item
end
end
@@ -96,6 +112,10 @@ def recognize(components, options={})
end
options[:controller] = controller_class.controller_path
return nil, requirements_for(:controller) unless passes_requirements?(:controller, options[:controller])
+ elsif /^\*/ =~ item.to_s
+ value = components.join("/") || @defaults[item]
+ components = []
+ options[item.to_s.sub(/^\*/,"").intern] = value.nil? ? value : CGI.unescape(value)
elsif item.kind_of? Symbol
value = components.shift || @defaults[item]
return nil, requirements_for(item) unless passes_requirements?(item, value)
@@ -142,7 +162,7 @@ def eat_path_to_controller(path)
end
def items=(path)
- items = path.split('/').collect {|c| (/^:(\w+)$/ =~ c) ? $1.intern : c} if path.kind_of?(String) # split and convert ':xyz' to symbols
+ items = path.split('/').collect {|c| (/^(:|\*)(\w+)$/ =~ c) ? (($1 == ':' ) ? $2.intern : "*#{$2}".intern) : c} if path.kind_of?(String) # split and convert ':xyz' to symbols
items.shift if items.first == ""
items.pop if items.last == ""
@items = items
@@ -172,6 +192,7 @@ def passes_requirements?(name, value)
end
end
def requirements_for(name)
+ name = name.to_s.sub(/^\*/,"").intern if (/^\*/ =~ name.inspect)
presence = (@defaults.key?(name) && @defaults[name].nil?)
requirement = case @requirements[name]
when nil then nil
@@ -325,6 +325,15 @@ def test_expand_controller_path_nested_no_leftover
assert_equal Controllers::Admin::UserController, controller
assert_equal %w{action id}, leftovers
end
+
+ def test_path_collection
+ route '*path_info', :controller => 'content', :action => 'fish'
+ verify_recognize'path/with/slashes',
+ :controller => 'content', :action => 'fish', :path_info => 'path/with/slashes'
+ verify_generate('path/with/slashes', {},
+ {:controller => 'content', :action => 'fish', :path_info => 'path/with/slashes'},
+ {})
+ end
def test_special_characters
route ':id', :controller => 'content', :action => 'fish'

0 comments on commit 37a4615

Please sign in to comment.