mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Get rid of the instance-level URL rewriter
This commit is contained in:
parent
c507e16dba
commit
de79525d04
5 changed files with 25 additions and 71 deletions
|
@ -339,9 +339,13 @@ module ActionController
|
||||||
def build_request_uri(action, parameters)
|
def build_request_uri(action, parameters)
|
||||||
unless @request.env["PATH_INFO"]
|
unless @request.env["PATH_INFO"]
|
||||||
options = @controller.__send__(:url_options).merge(parameters)
|
options = @controller.__send__(:url_options).merge(parameters)
|
||||||
options.update(:only_path => true, :action => action, :relative_url_root => nil)
|
options.update(
|
||||||
rewriter = ActionController::UrlRewriter.new(@request, parameters)
|
:only_path => true,
|
||||||
|
:action => action,
|
||||||
|
:relative_url_root => nil,
|
||||||
|
:_path_segments => @request.symbolized_path_parameters)
|
||||||
|
|
||||||
|
rewriter = ActionController::UrlRewriter
|
||||||
url, query_string = rewriter.rewrite(@router, options).split("?", 2)
|
url, query_string = rewriter.rewrite(@router, options).split("?", 2)
|
||||||
|
|
||||||
@request.env["SCRIPT_NAME"] = @controller.config.relative_url_root
|
@request.env["SCRIPT_NAME"] = @controller.config.relative_url_root
|
||||||
|
|
|
@ -2,37 +2,16 @@ require 'active_support/core_ext/hash/except'
|
||||||
|
|
||||||
module ActionController
|
module ActionController
|
||||||
# Rewrites URLs for Base.redirect_to and Base.url_for in the controller.
|
# Rewrites URLs for Base.redirect_to and Base.url_for in the controller.
|
||||||
class UrlRewriter #:nodoc:
|
module UrlRewriter #:nodoc:
|
||||||
RESERVED_OPTIONS = [:anchor, :params, :only_path, :host, :protocol, :port, :trailing_slash, :skip_relative_url_root]
|
RESERVED_OPTIONS = [:anchor, :params, :only_path, :host, :protocol, :port, :trailing_slash, :skip_relative_url_root]
|
||||||
|
|
||||||
def initialize(request, parameters)
|
|
||||||
@request, @parameters = request, parameters
|
|
||||||
end
|
|
||||||
|
|
||||||
def rewrite(router, options = {})
|
|
||||||
options[:host] ||= @request.host_with_port
|
|
||||||
options[:protocol] ||= @request.protocol
|
|
||||||
|
|
||||||
self.class.rewrite(router, options, @request.symbolized_path_parameters) do |options|
|
|
||||||
process_path_options(options)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_str
|
|
||||||
"#{@request.protocol}, #{@request.host_with_port}, #{@request.path}, #{@parameters[:controller]}, #{@parameters[:action]}, #{@request.parameters.inspect}"
|
|
||||||
end
|
|
||||||
|
|
||||||
alias_method :to_s, :to_str
|
|
||||||
|
|
||||||
# ROUTES TODO: Class method code smell
|
# ROUTES TODO: Class method code smell
|
||||||
def self.rewrite(router, options, path_segments=nil)
|
def self.rewrite(router, options)
|
||||||
handle_positional_args(options)
|
handle_positional_args(options)
|
||||||
|
|
||||||
rewritten_url = ""
|
rewritten_url = ""
|
||||||
|
|
||||||
# ROUTES TODO: Fix the tests
|
path_segments = options.delete(:_path_segments)
|
||||||
segments = options.delete(:_path_segments)
|
|
||||||
path_segments = path_segments ? path_segments.merge(segments || {}) : segments
|
|
||||||
|
|
||||||
unless options[:only_path]
|
unless options[:only_path]
|
||||||
rewritten_url << (options[:protocol] || "http")
|
rewritten_url << (options[:protocol] || "http")
|
||||||
|
@ -81,18 +60,5 @@ module ActionController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Given a Hash of options, generates a route
|
|
||||||
def process_path_options(options)
|
|
||||||
options = options.symbolize_keys
|
|
||||||
options.update(options[:params].symbolize_keys) if options[:params]
|
|
||||||
|
|
||||||
if (overwrite = options.delete(:overwrite_params))
|
|
||||||
options.update(@parameters.symbolize_keys)
|
|
||||||
options.update(overwrite.symbolize_keys)
|
|
||||||
end
|
|
||||||
|
|
||||||
options
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -64,7 +64,7 @@ class PageCachingTest < ActionController::TestCase
|
||||||
@controller.cache_store = :file_store, FILE_STORE_PATH
|
@controller.cache_store = :file_store, FILE_STORE_PATH
|
||||||
|
|
||||||
@params = {:controller => 'posts', :action => 'index', :only_path => true, :skip_relative_url_root => true}
|
@params = {:controller => 'posts', :action => 'index', :only_path => true, :skip_relative_url_root => true}
|
||||||
@rewriter = ActionController::UrlRewriter.new(@request, @params)
|
@rewriter = ActionController::UrlRewriter
|
||||||
|
|
||||||
FileUtils.rm_rf(File.dirname(FILE_STORE_PATH))
|
FileUtils.rm_rf(File.dirname(FILE_STORE_PATH))
|
||||||
FileUtils.mkdir_p(FILE_STORE_PATH)
|
FileUtils.mkdir_p(FILE_STORE_PATH)
|
||||||
|
|
|
@ -4,10 +4,24 @@ require 'controller/fake_controllers'
|
||||||
ActionController::UrlRewriter
|
ActionController::UrlRewriter
|
||||||
|
|
||||||
class UrlRewriterTests < ActionController::TestCase
|
class UrlRewriterTests < ActionController::TestCase
|
||||||
|
class Rewriter
|
||||||
|
def initialize(request)
|
||||||
|
@options = {
|
||||||
|
:host => request.host_with_port,
|
||||||
|
:protocol => request.protocol
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def rewrite(router, options)
|
||||||
|
options = @options.merge(options)
|
||||||
|
ActionController::UrlRewriter.rewrite(router, options)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@request = ActionController::TestRequest.new
|
@request = ActionController::TestRequest.new
|
||||||
@params = {}
|
@params = {}
|
||||||
@rewriter = ActionController::UrlRewriter.new(@request, @params)
|
@rewriter = Rewriter.new(@request) #.new(@request, @params)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_port
|
def test_port
|
||||||
|
@ -61,34 +75,6 @@ class UrlRewriterTests < ActionController::TestCase
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_overwrite_params
|
|
||||||
@params[:controller] = 'hi'
|
|
||||||
@params[:action] = 'bye'
|
|
||||||
@params[:id] = '2'
|
|
||||||
|
|
||||||
assert_equal '/hi/hi/2', @rewriter.rewrite(@router, :only_path => true, :overwrite_params => {:action => 'hi'})
|
|
||||||
u = @rewriter.rewrite(@router, :only_path => false, :overwrite_params => {:action => 'hi'})
|
|
||||||
assert_match %r(/hi/hi/2$), u
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_overwrite_removes_original
|
|
||||||
@params[:controller] = 'search'
|
|
||||||
@params[:action] = 'list'
|
|
||||||
@params[:list_page] = 1
|
|
||||||
|
|
||||||
assert_equal '/search/list?list_page=2', @rewriter.rewrite(@router, :only_path => true, :overwrite_params => {"list_page" => 2})
|
|
||||||
u = @rewriter.rewrite(@router, :only_path => false, :overwrite_params => {:list_page => 2})
|
|
||||||
assert_equal 'http://test.host/search/list?list_page=2', u
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_to_str
|
|
||||||
@params[:controller] = 'hi'
|
|
||||||
@params[:action] = 'bye'
|
|
||||||
@request.parameters[:id] = '2'
|
|
||||||
|
|
||||||
assert_equal 'http://, test.host, /, hi, bye, {"id"=>"2"}', @rewriter.to_str
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_trailing_slash
|
def test_trailing_slash
|
||||||
options = {:controller => 'foo', :action => 'bar', :id => '3', :only_path => true}
|
options = {:controller => 'foo', :action => 'bar', :id => '3', :only_path => true}
|
||||||
assert_equal '/foo/bar/3', @rewriter.rewrite(@router, options)
|
assert_equal '/foo/bar/3', @rewriter.rewrite(@router, options)
|
||||||
|
|
|
@ -122,7 +122,6 @@ class UrlHelperTest < ActionView::TestCase
|
||||||
url = {:controller => 'weblog', :action => 'show'}
|
url = {:controller => 'weblog', :action => 'show'}
|
||||||
@controller = ActionController::Base.new
|
@controller = ActionController::Base.new
|
||||||
@controller.request = ActionController::TestRequest.new
|
@controller.request = ActionController::TestRequest.new
|
||||||
@controller.url = ActionController::UrlRewriter.new(@controller.request, url)
|
|
||||||
assert_dom_equal(%q{<a href="/weblog/show">Test Link</a>}, link_to('Test Link', url))
|
assert_dom_equal(%q{<a href="/weblog/show">Test Link</a>}, link_to('Test Link', url))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -131,7 +130,6 @@ class UrlHelperTest < ActionView::TestCase
|
||||||
url = {:controller => 'weblog', :action => 'show', :host => 'www.example.com'}
|
url = {:controller => 'weblog', :action => 'show', :host => 'www.example.com'}
|
||||||
@controller = ActionController::Base.new
|
@controller = ActionController::Base.new
|
||||||
@controller.request = ActionController::TestRequest.new
|
@controller.request = ActionController::TestRequest.new
|
||||||
@controller.url = ActionController::UrlRewriter.new(@controller.request, url)
|
|
||||||
assert_dom_equal(%q{<a href="http://www.example.com/weblog/show">Test Link</a>}, link_to('Test Link', url))
|
assert_dom_equal(%q{<a href="http://www.example.com/weblog/show">Test Link</a>}, link_to('Test Link', url))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue