Permalink
Browse files

Merge pull request #8704 from senny/remove_regexp_global_from_url_for

replace regexp global in #url_for
  • Loading branch information...
2 parents cdbd64d + 19e9e67 commit 9d023c87de98f8c85574ecec019dd30a3dfc6f03 @tenderlove tenderlove committed Feb 14, 2013
Showing with 9 additions and 2 deletions.
  1. +6 −2 actionpack/lib/action_dispatch/http/url.rb
  2. +3 −0 actionpack/test/dispatch/request_test.rb
@@ -32,8 +32,12 @@ def url_for(options = {})
params.reject! { |_,v| v.to_param.nil? }
result = build_host_url(options)
- if options[:trailing_slash] && !path.ends_with?('/')
- result << path.sub(/(\?|\z)/) { "/" + $& }
+ if options[:trailing_slash]
+ if path.include?('?')
+ result << path.sub(/\?/, '/\&')
+ else
+ result << path.sub(/[^\/]\z|\A\z/, '\&/')
+ end
else
result << path
end
@@ -13,6 +13,9 @@ def url_for(options = {})
assert_equal '/books', url_for(:only_path => true, :path => '/books')
+ assert_equal 'http://www.example.com/books/?q=code', url_for(trailing_slash: true, path: '/books?q=code')
+ assert_equal 'http://www.example.com/books/?spareslashes=////', url_for(trailing_slash: true, path: '/books?spareslashes=////')
+
assert_equal 'http://www.example.com', url_for
assert_equal 'http://api.example.com', url_for(:subdomain => 'api')
assert_equal 'http://example.com', url_for(:subdomain => false)

0 comments on commit 9d023c8

Please sign in to comment.