2004-11-23 20:04:44 -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.
|
2005-02-14 20:45:35 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
class UrlRewriter #:nodoc:
|
2005-04-17 10:10:24 -04:00
|
|
|
RESERVED_OPTIONS = [:anchor, :params, :only_path, :host, :protocol, :trailing_slash, :skip_relative_url_root]
|
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
|
|
|
|
|
2005-03-20 17:02:22 -05:00
|
|
|
def rewrite(options = {})
|
2005-02-14 20:45:35 -05:00
|
|
|
rewrite_url(rewrite_path(options), options)
|
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
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
private
|
2005-03-26 08:31:01 -05:00
|
|
|
def rewrite_url(path, options)
|
2004-11-23 20:04:44 -05:00
|
|
|
rewritten_url = ""
|
2005-07-23 05:00:05 -04:00
|
|
|
unless options[:only_path]
|
|
|
|
rewritten_url << (options[:protocol] || @request.protocol)
|
|
|
|
rewritten_url << (options[:host] || @request.host_with_port)
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2005-04-17 10:10:24 -04:00
|
|
|
rewritten_url << @request.relative_url_root.to_s unless options[:skip_relative_url_root]
|
2004-11-23 20:04:44 -05:00
|
|
|
rewritten_url << path
|
2005-03-26 08:38:21 -05:00
|
|
|
rewritten_url << '/' if options[:trailing_slash]
|
2004-11-23 20:04:44 -05:00
|
|
|
rewritten_url << "##{options[:anchor]}" if options[:anchor]
|
2005-02-14 20:45:35 -05:00
|
|
|
|
2005-06-24 12:40:01 -04:00
|
|
|
rewritten_url
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2005-07-17 23:25:53 -04:00
|
|
|
def rewrite_path(options)
|
|
|
|
options = options.symbolize_keys
|
|
|
|
options.update(options[:params].symbolize_keys) if options[:params]
|
2005-09-06 18:06:11 -04:00
|
|
|
if (overwrite = options.delete(:overwrite_params))
|
2005-09-07 04:59:33 -04:00
|
|
|
options.update(@parameters.symbolize_keys)
|
2005-09-06 18:06:11 -04:00
|
|
|
options.update(overwrite)
|
|
|
|
end
|
2005-02-14 20:45:35 -05:00
|
|
|
RESERVED_OPTIONS.each {|k| options.delete k}
|
2005-07-17 23:25:53 -04:00
|
|
|
path, extra_keys = Routing::Routes.generate(options.dup, @request) # Warning: Routes will mutate and violate the options hash
|
2005-03-26 19:01:56 -05:00
|
|
|
|
2005-07-17 23:25:53 -04:00
|
|
|
path << build_query_string(options, extra_keys) unless extra_keys.empty?
|
2005-02-14 20:45:35 -05:00
|
|
|
|
2005-06-24 12:40:01 -04:00
|
|
|
path
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a query string with escaped keys and values from the passed hash. If the passed hash contains an "id" it'll
|
|
|
|
# be added as a path element instead of a regular parameter pair.
|
2005-07-17 23:12:45 -04:00
|
|
|
def build_query_string(hash, only_keys = nil)
|
2004-11-23 20:04:44 -05:00
|
|
|
elements = []
|
|
|
|
query_string = ""
|
2005-07-17 23:12:45 -04:00
|
|
|
|
|
|
|
only_keys ||= hash.keys
|
2005-02-14 20:45:35 -05:00
|
|
|
|
2005-07-17 23:12:45 -04:00
|
|
|
only_keys.each do |key|
|
|
|
|
value = hash[key]
|
2005-06-24 12:40:01 -04:00
|
|
|
key = CGI.escape key.to_s
|
2005-07-23 05:00:05 -04:00
|
|
|
if value.class == Array
|
|
|
|
key << '[]'
|
|
|
|
else
|
|
|
|
value = [ value ]
|
|
|
|
end
|
2005-03-20 10:42:40 -05:00
|
|
|
value.each { |val| elements << "#{key}=#{Routing.extract_parameter_value(val)}" }
|
2005-02-14 20:45:35 -05:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2005-02-14 20:45:35 -05:00
|
|
|
query_string << ("?" + elements.join("&")) unless elements.empty?
|
2005-06-24 12:40:01 -04:00
|
|
|
query_string
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
end
|
2005-02-20 15:54:40 -05:00
|
|
|
end
|