aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actionpack/CHANGELOG7
-rw-r--r--actionpack/lib/action_controller/routing.rb27
-rw-r--r--actionpack/test/controller/routing_tests.rb9
3 files changed, 40 insertions, 3 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index 9488d7847d..0a7435e799 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -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]
diff --git a/actionpack/lib/action_controller/routing.rb b/actionpack/lib/action_controller/routing.rb
index 1aaa599b9e..c021d88ca9 100644
--- a/actionpack/lib/action_controller/routing.rb
+++ b/actionpack/lib/action_controller/routing.rb
@@ -48,12 +48,28 @@ module ActionController
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 @@ module ActionController
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 @@ module ActionController
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 @@ module ActionController
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
diff --git a/actionpack/test/controller/routing_tests.rb b/actionpack/test/controller/routing_tests.rb
index d4f954e590..744ccb1093 100644
--- a/actionpack/test/controller/routing_tests.rb
+++ b/actionpack/test/controller/routing_tests.rb
@@ -325,6 +325,15 @@ class RouteTests < Test::Unit::TestCase
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'