aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-03-22 21:03:43 +0100
committerJosé Valim <jose.valim@gmail.com>2010-03-22 21:07:37 +0100
commit4998e097cc597f26cbe292552bcf5608b87cb1d2 (patch)
tree0fe132fe3ec973ff601794adf6b3cc8dddaf2efe /actionpack
parentfb89aba8b45bc5adbd3181ebb2e92ec99e63f821 (diff)
downloadrails-4998e097cc597f26cbe292552bcf5608b87cb1d2.tar.gz
rails-4998e097cc597f26cbe292552bcf5608b87cb1d2.tar.bz2
rails-4998e097cc597f26cbe292552bcf5608b87cb1d2.zip
Make router shortcuts more polite to URLs starting with a leading slash.
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_dispatch/routing/mapper.rb32
-rw-r--r--actionpack/test/dispatch/routing_test.rb18
2 files changed, 35 insertions, 15 deletions
diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb
index 668abb5fdf..ddee742021 100644
--- a/actionpack/lib/action_dispatch/routing/mapper.rb
+++ b/actionpack/lib/action_dispatch/routing/mapper.rb
@@ -45,20 +45,23 @@ module ActionDispatch
def extract_path_and_options(args)
options = args.extract_options!
- case
- when using_to_shorthand?(args, options)
+ if using_to_shorthand?(args, options)
path, to = options.find { |name, value| name.is_a?(String) }
options.merge!(:to => to).delete(path) if path
- when using_match_shorthand?(args, options)
- path = args.first
- options = { :to => path.gsub("/", "#"),
- :as => path.gsub("/", "_")
- }.merge(options || {})
else
path = args.first
end
- [ normalize_path(path), options ]
+ path = normalize_path(path)
+
+ if using_match_shorthand?(path, options)
+ options = {
+ :to => path[1..-1].sub(%r{/([^/]*)$}, '#\1'),
+ :as => path[1..-1].gsub("/", "_")
+ }.merge!(options)
+ end
+
+ [ path, options ]
end
# match "account" => "account#index"
@@ -67,14 +70,13 @@ module ActionDispatch
end
# match "account/overview"
- def using_match_shorthand?(args, options)
- args.present? && options.except(:via, :anchor).empty? && !args.first.include?(':')
+ def using_match_shorthand?(path, options)
+ path && options.except(:via, :anchor).empty? && !path.include?(':')
end
def normalize_path(path)
- path = "#{@scope[:path]}/#{path}"
- raise ArgumentError, "path is required" if path.empty?
- Mapper.normalize_path(path)
+ raise ArgumentError, "path is required" if @scope[:path].blank? && path.blank?
+ Mapper.normalize_path("#{@scope[:path]}/#{path}")
end
def app
@@ -146,8 +148,8 @@ module ActionDispatch
def segment_keys
@segment_keys ||= Rack::Mount::RegexpWithNamedGroups.new(
- Rack::Mount::Strexp.compile(@path, requirements, SEPARATORS)
- ).names
+ Rack::Mount::Strexp.compile(@path, requirements, SEPARATORS)
+ ).names
end
def to
diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb
index e4d83fa0a4..e0500af29d 100644
--- a/actionpack/test/dispatch/routing_test.rb
+++ b/actionpack/test/dispatch/routing_test.rb
@@ -34,6 +34,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
match 'account/login', :to => redirect("/login")
match 'account/overview'
+ match '/account/nested/overview'
match 'account/modulo/:name', :to => redirect("/%{name}s")
match 'account/proc/:name', :to => redirect {|params| "/#{params[:name].pluralize}" }
@@ -121,6 +122,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
match 'articles/:year/:month/:day/:title', :to => "articles#show", :as => :article
namespace :account do
+ match 'shorthand'
match 'description', :to => "account#description", :as => "description"
resource :subscription, :credit, :credit_card
@@ -655,6 +657,22 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
end
end
+ def test_convention_match_inside_namespace
+ with_test_routes do
+ assert_equal '/account/shorthand', account_shorthand_path
+ get '/account/shorthand'
+ assert_equal 'account#shorthand', @response.body
+ end
+ end
+
+ def test_convention_match_nested_and_with_leading_slash
+ with_test_routes do
+ assert_equal '/account/nested/overview', account_nested_overview_path
+ get '/account/nested/overview'
+ assert_equal 'account/nested#overview', @response.body
+ end
+ end
+
def test_redirect_with_complete_url
with_test_routes do
get '/account/google'