Skip to content

Commit

Permalink
UrlRewriter#rewrite_url should call #to_param on the value given in :…
Browse files Browse the repository at this point in the history
…anchor option, just as #url_for does

[#2746 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information
packagethief authored and jeremy committed Aug 26, 2009
1 parent e46e67c commit 4240890
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/url_rewriter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def rewrite_url(options)
path = rewrite_path(options)
rewritten_url << ActionController::Base.relative_url_root.to_s unless options[:skip_relative_url_root]
rewritten_url << (options[:trailing_slash] ? path.sub(/\?|\z/) { "/" + $& } : path)
rewritten_url << "##{options[:anchor]}" if options[:anchor]
rewritten_url << "##{CGI.escape(options[:anchor].to_param.to_s)}" if options[:anchor]

rewritten_url
end
Expand Down
26 changes: 26 additions & 0 deletions actionpack/test/controller/url_rewriter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ def test_anchor
)
end

def test_anchor_should_call_to_param
assert_equal(
'http://test.host/c/a/i#anchor',
@rewriter.rewrite(:controller => 'c', :action => 'a', :id => 'i', :anchor => Struct.new(:to_param).new('anchor'))
)
end

def test_anchor_should_be_cgi_escaped
assert_equal(
'http://test.host/c/a/i#anc%2Fhor',
@rewriter.rewrite(:controller => 'c', :action => 'a', :id => 'i', :anchor => Struct.new(:to_param).new('anc/hor'))
)
end

def test_overwrite_params
@params[:controller] = 'hi'
@params[:action] = 'bye'
Expand Down Expand Up @@ -110,6 +124,18 @@ def test_anchor
)
end

def test_anchor_should_call_to_param
assert_equal('/c/a#anchor',
W.new.url_for(:only_path => true, :controller => 'c', :action => 'a', :anchor => Struct.new(:to_param).new('anchor'))
)
end

def test_anchor_should_be_cgi_escaped
assert_equal('/c/a#anc%2Fhor',
W.new.url_for(:only_path => true, :controller => 'c', :action => 'a', :anchor => Struct.new(:to_param).new('anc/hor'))
)
end

def test_default_host
add_host!
assert_equal('http://www.basecamphq.com/c/a/i',
Expand Down

0 comments on commit 4240890

Please sign in to comment.