mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add :trailing_slash option to UrlWriter. Closes #9117 [juanjo.bazan]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8892 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
8bbabd43a9
commit
db4f421b0b
2 changed files with 37 additions and 2 deletions
|
@ -61,10 +61,11 @@ module ActionController
|
|||
# Delete the unused options to prevent their appearance in the query string.
|
||||
[:protocol, :host, :port, :skip_relative_url_root].each { |k| options.delete(k) }
|
||||
end
|
||||
|
||||
trailing_slash = options.delete(:trailing_slash) if options.key?(:trailing_slash)
|
||||
url << ActionController::AbstractRequest.relative_url_root.to_s unless options[:skip_relative_url_root]
|
||||
anchor = "##{CGI.escape options.delete(:anchor).to_param.to_s}" if options[:anchor]
|
||||
url << Routing::Routes.generate(options, {})
|
||||
generated = Routing::Routes.generate(options, {})
|
||||
url << (trailing_slash ? generated.sub(/\?|\z/) { "/" + $& } : generated)
|
||||
url << anchor if anchor
|
||||
|
||||
url
|
||||
|
|
|
@ -149,6 +149,40 @@ class UrlWriterTests < Test::Unit::TestCase
|
|||
)
|
||||
end
|
||||
|
||||
def test_trailing_slash
|
||||
add_host!
|
||||
options = {:controller => 'foo', :trailing_slash => true, :action => 'bar', :id => '33'}
|
||||
assert_equal('http://www.basecamphq.com/foo/bar/33/', W.new.url_for(options) )
|
||||
end
|
||||
|
||||
def test_trailing_slash_with_protocol
|
||||
add_host!
|
||||
options = { :trailing_slash => true,:protocol => 'https', :controller => 'foo', :action => 'bar', :id => '33'}
|
||||
assert_equal('https://www.basecamphq.com/foo/bar/33/', W.new.url_for(options) )
|
||||
assert_equal 'https://www.basecamphq.com/foo/bar/33/?query=string', W.new.url_for(options.merge({:query => 'string'}))
|
||||
end
|
||||
|
||||
def test_trailing_slash_with_only_path
|
||||
options = {:controller => 'foo', :trailing_slash => true}
|
||||
assert_equal '/foo/', W.new.url_for(options.merge({:only_path => true}))
|
||||
options.update({:action => 'bar', :id => '33'})
|
||||
assert_equal '/foo/bar/33/', W.new.url_for(options.merge({:only_path => true}))
|
||||
assert_equal '/foo/bar/33/?query=string', W.new.url_for(options.merge({:query => 'string',:only_path => true}))
|
||||
end
|
||||
|
||||
def test_trailing_slash_with_anchor
|
||||
options = {:trailing_slash => true, :controller => 'foo', :action => 'bar', :id => '33', :only_path => true, :anchor=> 'chapter7'}
|
||||
assert_equal '/foo/bar/33/#chapter7', W.new.url_for(options)
|
||||
assert_equal '/foo/bar/33/?query=string#chapter7', W.new.url_for(options.merge({:query => 'string'}))
|
||||
end
|
||||
|
||||
def test_trailing_slash_with_params
|
||||
url = W.new.url_for(:trailing_slash => true, :only_path => true, :controller => 'cont', :action => 'act', :p1 => 'cafe', :p2 => 'link')
|
||||
params = extract_params(url)
|
||||
assert_equal params[0], { :p1 => 'cafe' }.to_query
|
||||
assert_equal params[1], { :p2 => 'link' }.to_query
|
||||
end
|
||||
|
||||
def test_relative_url_root_is_respected
|
||||
orig_relative_url_root = ActionController::AbstractRequest.relative_url_root
|
||||
ActionController::AbstractRequest.relative_url_root = '/subdir'
|
||||
|
|
Loading…
Reference in a new issue