aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller/url_rewriter.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-02-15 01:45:35 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-02-15 01:45:35 +0000
commitb1999be5a7efd67e2602c37ed898aa8433661863 (patch)
tree03bc833276075d802d0ce0ad261baed3d7232533 /actionpack/lib/action_controller/url_rewriter.rb
parent88a3343ed57c01ca358da8473d15fc4d2b4a5bff (diff)
downloadrails-b1999be5a7efd67e2602c37ed898aa8433661863.tar.gz
rails-b1999be5a7efd67e2602c37ed898aa8433661863.tar.bz2
rails-b1999be5a7efd67e2602c37ed898aa8433661863.zip
A hopefully more successful attempt at the Routing branch merge
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@617 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_controller/url_rewriter.rb')
-rw-r--r--actionpack/lib/action_controller/url_rewriter.rb96
1 files changed, 22 insertions, 74 deletions
diff --git a/actionpack/lib/action_controller/url_rewriter.rb b/actionpack/lib/action_controller/url_rewriter.rb
index 3451e6acc9..a1b1a2a57b 100644
--- a/actionpack/lib/action_controller/url_rewriter.rb
+++ b/actionpack/lib/action_controller/url_rewriter.rb
@@ -1,41 +1,29 @@
module ActionController
# Rewrites URLs for Base.redirect_to and Base.url_for in the controller.
+
class UrlRewriter #:nodoc:
- VALID_OPTIONS = [:action, :action_prefix, :action_suffix, :application_prefix, :module, :controller, :controller_prefix, :anchor, :params, :path_params, :id, :only_path, :overwrite_params, :host, :protocol ]
-
- def initialize(request, controller, action)
- @request, @controller, @action = request, controller, action
+ RESERVED_OPTIONS = [:anchor, :params, :path_params, :only_path, :host, :protocol]
+ def initialize(request, parameters)
+ @request, @parameters = request, parameters
@rewritten_path = @request.path ? @request.path.dup : ""
end
def rewrite(options = {})
- validate_options(VALID_OPTIONS, options.keys)
-
- rewrite_url(
- rewrite_path(@rewritten_path, resolve_aliases(options)),
- options
- )
- end
-
- def to_s
- to_str
+ rewrite_url(rewrite_path(options), options)
end
def to_str
- "#{@request.protocol}, #{@request.host_with_port}, #{@request.path}, #{@controller}, #{@action}, #{@request.parameters.inspect}"
+ "#{@request.protocol}, #{@request.host_with_port}, #{@request.path}, #{@parameters[:controller]}, #{@parameters[:action]}, #{@request.parameters.inspect}"
end
+ alias_method :to_s, :to_str
+
private
def validate_options(valid_option_keys, supplied_option_keys)
unknown_option_keys = supplied_option_keys - valid_option_keys
raise(ActionController::ActionControllerError, "Unknown options: #{unknown_option_keys}") unless unknown_option_keys.empty?
end
-
- def resolve_aliases(options)
- options[:controller_prefix] = options[:module] unless options[:module].nil?
- options
- end
-
+
def rewrite_url(path, options)
rewritten_url = ""
rewritten_url << (options[:protocol] || @request.protocol) unless options[:only_path]
@@ -45,15 +33,18 @@ module ActionController
rewritten_url << path
rewritten_url << build_query_string(new_parameters(options)) if options[:params] || options[:overwrite_params]
rewritten_url << "##{options[:anchor]}" if options[:anchor]
+
return rewritten_url
end
- def rewrite_path(path, options)
- include_id_in_path_params(options)
-
- path = rewrite_action(path, options) if options[:action] || options[:action_prefix]
- path = rewrite_path_params(path, options) if options[:path_params]
- path = rewrite_controller(path, options) if options[:controller] || options[:controller_prefix]
+ def rewrite_path(options)
+ options = options.symbolize_keys
+ RESERVED_OPTIONS.each {|k| options.delete k}
+
+ path, extras = Routing::Routes.generate(options, @request)
+ path = "/#{path.join('/')}"
+ path += build_query_string(extras)
+
return path
end
@@ -76,49 +67,6 @@ module ActionController
end
end
- def rewrite_action(path, options)
- # This regex assumes that "index" actions won't be included in the URL
- all, controller_prefix, action_prefix, action_suffix =
- /^\/(.*)#{@controller}\/(.*)#{@action == "index" ? "" : @action}(.*)/.match(path).to_a
-
- if @action == "index"
- if action_prefix == "index"
- # we broke the parsing assumption that this would be excluded, so
- # don't tell action_name about our little boo-boo
- path = path.sub(action_prefix, action_name(options, nil))
- elsif action_prefix && !action_prefix.empty?
- path = path.sub(%r(/#{action_prefix}/?), "/" + action_name(options, action_prefix))
- else
- path = path.sub(%r(#{@controller}/?$), @controller + "/" + action_name(options)) # " ruby-mode
- end
- else
- path = path.sub(
- @controller + "/" + (action_prefix || "") + @action + (action_suffix || ""),
- @controller + "/" + action_name(options, action_prefix)
- )
- end
-
- if options[:controller_prefix] && !options[:controller]
- ensure_slash_suffix(options, :controller_prefix)
- if controller_prefix
- path = path.sub(controller_prefix, options[:controller_prefix])
- else
- path = options[:controller_prefix] + path
- end
- end
-
- return path
- end
-
- def rewrite_controller(path, options)
- all, controller_prefix = /^\/(.*?)#{@controller}/.match(path).to_a
- path = "/"
- path << controller_name(options, controller_prefix)
- path << action_name(options) if options[:action]
- path << path_params_in_list(options) if options[:path_params]
- return path
- end
-
def action_name(options, action_prefix = nil, action_suffix = nil)
ensure_slash_suffix(options, :action_prefix)
ensure_slash_prefix(options, :action_suffix)
@@ -174,16 +122,16 @@ module ActionController
def build_query_string(hash)
elements = []
query_string = ""
-
+
hash.each do |key, value|
+ key = key.to_s
key = CGI.escape key
key += '[]' if value.class == Array
value = [ value ] unless value.class == Array
value.each { |val| elements << "#{key}=#{CGI.escape(val.to_s)}" }
- end
-
- unless elements.empty? then query_string << ("?" + elements.join("&")) end
+ end
+ query_string << ("?" + elements.join("&")) unless elements.empty?
return query_string
end
end