2010-01-07 08:20:31 -05:00
|
|
|
require 'active_support/core_ext/hash/except'
|
2008-01-09 21:51:09 -05:00
|
|
|
|
2010-01-07 08:20:31 -05:00
|
|
|
module ActionController
|
2005-02-07 09:15:53 -05:00
|
|
|
# Rewrites URLs for Base.redirect_to and Base.url_for in the controller.
|
2004-11-23 20:04:44 -05:00
|
|
|
class UrlRewriter #:nodoc:
|
2007-02-25 15:22:09 -05:00
|
|
|
RESERVED_OPTIONS = [:anchor, :params, :only_path, :host, :protocol, :port, :trailing_slash, :skip_relative_url_root]
|
2010-01-07 08:20:31 -05:00
|
|
|
|
2005-02-14 20:45:35 -05:00
|
|
|
def initialize(request, parameters)
|
|
|
|
@request, @parameters = request, parameters
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2008-01-09 21:51:09 -05:00
|
|
|
|
2010-02-24 19:01:03 -05:00
|
|
|
def rewrite(router, options = {})
|
2010-01-07 08:20:31 -05:00
|
|
|
options[:host] ||= @request.host_with_port
|
|
|
|
options[:protocol] ||= @request.protocol
|
|
|
|
|
2010-02-24 19:01:03 -05:00
|
|
|
self.class.rewrite(router, options, @request.symbolized_path_parameters) do |options|
|
2010-01-07 08:20:31 -05:00
|
|
|
process_path_options(options)
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_str
|
2005-06-24 12:40:01 -04:00
|
|
|
"#{@request.protocol}, #{@request.host_with_port}, #{@request.path}, #{@parameters[:controller]}, #{@parameters[:action]}, #{@request.parameters.inspect}"
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2005-02-14 20:45:35 -05:00
|
|
|
alias_method :to_s, :to_str
|
|
|
|
|
2010-02-24 19:01:03 -05:00
|
|
|
# ROUTES TODO: Class method code smell
|
|
|
|
def self.rewrite(router, options, path_segments=nil)
|
2010-02-25 18:05:10 -05:00
|
|
|
handle_positional_args(options)
|
|
|
|
|
2010-01-07 08:20:31 -05:00
|
|
|
rewritten_url = ""
|
|
|
|
|
2010-02-25 18:05:10 -05:00
|
|
|
# ROUTES TODO: Fix the tests
|
|
|
|
segments = options.delete(:_path_segments)
|
|
|
|
path_segments = path_segments ? path_segments.merge(segments || {}) : segments
|
|
|
|
|
2010-01-07 08:20:31 -05:00
|
|
|
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)
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2010-01-07 08:20:31 -05:00
|
|
|
path_options = options.except(*RESERVED_OPTIONS)
|
|
|
|
path_options = yield(path_options) if block_given?
|
2010-02-24 19:01:03 -05:00
|
|
|
path = router.generate(path_options, path_segments || {})
|
2010-01-07 08:20:31 -05:00
|
|
|
|
|
|
|
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 << "##{Rack::Utils.escape(options[:anchor].to_param.to_s)}" if options[:anchor]
|
2007-03-03 17:19:54 -05:00
|
|
|
|
2010-01-07 08:20:31 -05:00
|
|
|
rewritten_url
|
|
|
|
end
|
2007-03-03 17:19:54 -05:00
|
|
|
|
2010-01-07 08:20:31 -05:00
|
|
|
protected
|
2005-03-26 19:01:56 -05:00
|
|
|
|
2010-02-25 18:05:10 -05:00
|
|
|
def self.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
|
|
|
|
|
2010-01-07 08:20:31 -05:00
|
|
|
def self.rewrite_authentication(options)
|
|
|
|
if options[:user] && options[:password]
|
|
|
|
"#{Rack::Utils.escape(options.delete(:user))}:#{Rack::Utils.escape(options.delete(:password))}@"
|
|
|
|
else
|
|
|
|
""
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2010-01-07 08:20:31 -05:00
|
|
|
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]
|
2008-01-09 21:51:09 -05:00
|
|
|
|
2010-01-07 08:20:31 -05:00
|
|
|
if (overwrite = options.delete(:overwrite_params))
|
|
|
|
options.update(@parameters.symbolize_keys)
|
|
|
|
options.update(overwrite.symbolize_keys)
|
2007-03-03 17:19:54 -05:00
|
|
|
end
|
2010-01-07 08:20:31 -05:00
|
|
|
|
|
|
|
options
|
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2007-07-14 05:28:56 -04:00
|
|
|
end
|