Skip to content

Commit

Permalink
Move AC::UrlRewriter onto route set
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Mar 10, 2010
1 parent a87683f commit 7db80f8
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 90 deletions.
1 change: 0 additions & 1 deletion actionpack/lib/action_controller.rb
Expand Up @@ -46,7 +46,6 @@ module ActionController


eager_autoload do eager_autoload do
autoload :RecordIdentifier autoload :RecordIdentifier
autoload :UrlRewriter


# TODO: Don't autoload exceptions, setup explicit # TODO: Don't autoload exceptions, setup explicit
# requires for files that need them # requires for files that need them
Expand Down
33 changes: 16 additions & 17 deletions actionpack/lib/action_controller/test_case.rb
Expand Up @@ -335,23 +335,22 @@ def rescue_action_in_public!
@request.remote_addr = '208.77.188.166' # example.com @request.remote_addr = '208.77.188.166' # example.com
end end


private private
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( options.update(
:only_path => true, :only_path => true,
:action => action, :action => action,
:relative_url_root => nil, :relative_url_root => nil,
:_path_segments => @request.symbolized_path_parameters) :_path_segments => @request.symbolized_path_parameters)


rewriter = ActionController::UrlRewriter url, query_string = @router.rewrite(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 @request.env["PATH_INFO"] = url
@request.env["PATH_INFO"] = url @request.env["QUERY_STRING"] = query_string || ""
@request.env["QUERY_STRING"] = query_string || "" end
end end
end end
end
end end
64 changes: 0 additions & 64 deletions actionpack/lib/action_controller/url_rewriter.rb

This file was deleted.

56 changes: 56 additions & 0 deletions actionpack/lib/action_dispatch/routing/route_set.rb
Expand Up @@ -410,6 +410,38 @@ def generate(options, recall = {}, extras = false)
Generator.new(options, recall, @set, extras).generate Generator.new(options, recall, @set, extras).generate
end end


RESERVED_OPTIONS = [:anchor, :params, :only_path, :host, :protocol, :port, :trailing_slash, :skip_relative_url_root]

# TODO: Terrible method name
def rewrite(options)
handle_positional_args(options)

rewritten_url = ""

path_segments = options.delete(:_path_segments)

unless options[:only_path]
rewritten_url << (options[:protocol] || "http")
rewritten_url << "://" unless rewritten_url.match("://")
rewritten_url << rewrite_authentication(options)

raise "Missing host to link to! Please provide :host parameter or set default_url_options[:host]" unless options[:host]

rewritten_url << options[:host]
rewritten_url << ":#{options.delete(:port)}" if options.key?(:port)
end

path_options = options.except(*RESERVED_OPTIONS)
path_options = yield(path_options) if block_given?
path = generate(path_options, path_segments || {})

# ROUTES TODO: This can be called directly, so script_name should probably be set in the router
rewritten_url << (options[:trailing_slash] ? path.sub(/\?|\z/) { "/" + $& } : path)
rewritten_url << "##{Rack::Utils.escape(options[:anchor].to_param.to_s)}" if options[:anchor]

rewritten_url
end

def call(env) def call(env)
@set.call(env) @set.call(env)
end end
Expand All @@ -435,6 +467,30 @@ def recognize_path(path, environment = {})


raise ActionController::RoutingError, "No route matches #{path.inspect}" raise ActionController::RoutingError, "No route matches #{path.inspect}"
end end

private
def handle_positional_args(options)
return unless args = options.delete(:_positional_args)

keys = options.delete(:_positional_keys)
keys -= options.keys if args.size < keys.size - 1 # take format into account

args = args.zip(keys).inject({}) do |h, (v, k)|
h[k] = v
h
end

# Tell url_for to skip default_url_options
options.merge!(args)
end

def rewrite_authentication(options)
if options[:user] && options[:password]
"#{Rack::Utils.escape(options.delete(:user))}:#{Rack::Utils.escape(options.delete(:password))}@"
else
""
end
end
end end
end end
end end
2 changes: 1 addition & 1 deletion actionpack/lib/action_dispatch/routing/url_for.rb
Expand Up @@ -131,7 +131,7 @@ def url_for(options = nil)
when String when String
options options
when nil, Hash when nil, Hash
ActionController::UrlRewriter.rewrite(_router, url_options.merge(options || {})) _router.rewrite(url_options.merge(options || {}))
else else
polymorphic_url(options) polymorphic_url(options)
end end
Expand Down
5 changes: 2 additions & 3 deletions actionpack/test/controller/caching_test.rb
Expand Up @@ -64,7 +64,6 @@ def setup
@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


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)
Expand All @@ -82,9 +81,9 @@ def test_page_caching_resources_saves_to_correct_path_with_extension_even_if_def
match '/', :to => 'posts#index', :as => :main match '/', :to => 'posts#index', :as => :main
end end
@params[:format] = 'rss' @params[:format] = 'rss'
assert_equal '/posts.rss', @rewriter.rewrite(@router, @params) assert_equal '/posts.rss', @router.rewrite(@params)
@params[:format] = nil @params[:format] = nil
assert_equal '/', @rewriter.rewrite(@router, @params) assert_equal '/', @router.rewrite(@params)
end end
end end


Expand Down
5 changes: 1 addition & 4 deletions actionpack/test/controller/url_rewriter_test.rb
@@ -1,8 +1,6 @@
require 'abstract_unit' require 'abstract_unit'
require 'controller/fake_controllers' require 'controller/fake_controllers'


ActionController::UrlRewriter

class UrlRewriterTests < ActionController::TestCase class UrlRewriterTests < ActionController::TestCase
class Rewriter class Rewriter
def initialize(request) def initialize(request)
Expand All @@ -13,8 +11,7 @@ def initialize(request)
end end


def rewrite(router, options) def rewrite(router, options)
options = @options.merge(options) router.rewrite(@options.merge(options))
ActionController::UrlRewriter.rewrite(router, options)
end end
end end


Expand Down

0 comments on commit 7db80f8

Please sign in to comment.