Skip to content

Commit

Permalink
Added the option of passing false to :module or :controller_prefix in…
Browse files Browse the repository at this point in the history
… order to "break out" of a module or prefix

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@142 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Dec 13, 2004
1 parent 0cce17f commit 922f817
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
4 changes: 3 additions & 1 deletion actionpack/lib/action_controller/base.rb
Expand Up @@ -271,10 +271,11 @@ def process(request, response, method = :perform_action, *arguments) #:nodoc:
# .---> controller .--> action
# /library/books/ISBN/0743536703/show
# '------> '--------------> action_prefix
# controller_prefix
# controller_prefix (or module)
#
# * <tt>:controller_prefix</tt> - specifies the string before the controller name, which would be "/library" for the example.
# Called with "/shop" gives "/shop/books/ISBN/0743536703/show".
# * <tt>:module</tt> - serves as a alias to :controller_prefix (overwrites :controller_prefix unless its nil)
# * <tt>:controller</tt> - specifies a new controller and clears out everything after the controller name (including the action,
# the pre- and suffix, and all params), so called with "settings" gives "/library/settings/".
# * <tt>:action_prefix</tt> - specifies the string between the controller name and the action name, which would
Expand All @@ -297,6 +298,7 @@ def process(request, response, method = :perform_action, *arguments) #:nodoc:
# Naturally, you can combine multiple options in a single redirect. Examples:
#
# redirect_to(:controller_prefix => "/shop", :controller => "settings")
# redirect_to(:controller_prefix => false, :controller => "settings") # breaks out of the current controller_prefix
# redirect_to(:action => "edit", :id => 3425)
# redirect_to(:action => "edit", :path_params => { "type" => "XTC" }, :params => { "temp" => 1})
# redirect_to(:action => "publish", :action_prefix => "/published", :anchor => "x14")
Expand Down
18 changes: 14 additions & 4 deletions actionpack/lib/action_controller/url_rewriter.rb
Expand Up @@ -12,7 +12,7 @@ def rewrite(options = {})
validate_options(VALID_OPTIONS, options.keys)

rewrite_url(
rewrite_path(@rewritten_path, options),
rewrite_path(@rewritten_path, resolve_aliases(options)),
options
)
end
Expand All @@ -30,8 +30,13 @@ 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)
def rewrite_url(path, options)
rewritten_url = ""
rewritten_url << @request.protocol unless options[:only_path]
rewritten_url << @request.host_with_port unless options[:only_path]
Expand Down Expand Up @@ -122,9 +127,14 @@ def action_name(options, action_prefix = nil, action_suffix = nil)
end

def controller_name(options, controller_prefix)
options[:controller_prefix] = "#{options[:module]}/#{options[:controller_prefix]}" if options[:module]
ensure_slash_suffix(options, :controller_prefix)
controller_name = options[:controller_prefix] || controller_prefix || ""

controller_name = case options[:controller_prefix]
when String: options[:controller_prefix]
when false : ""
when nil : controller_prefix || ""
end

controller_name << (options[:controller] + "/") if options[:controller]
return controller_name
end
Expand Down
12 changes: 12 additions & 0 deletions actionpack/test/controller/url_test.rb
Expand Up @@ -22,6 +22,14 @@ def setup
{ "type" => "ISBN", "code" => "0743536703" }
), "books", "show")

@library_url_using_module = ActionController::UrlRewriter.new(MockRequest.new(
"http://",
"www.singlefile.com",
80,
"/library/books/ISBN/0743536703/show",
{ "type" => "ISBN", "code" => "0743536703", "module" => "library" }
), "books", "show")

@library_url_on_index = ActionController::UrlRewriter.new(MockRequest.new(
"http://",
"www.singlefile.com",
Expand Down Expand Up @@ -102,6 +110,10 @@ def test_clean_controller_prefix
def test_clean_controller_with_module
assert_equal "http://www.singlefile.com/shop/purchases/", @library_url.rewrite(:module => "shop", :controller => "purchases")
end

def test_getting_out_of_a_module
assert_equal "http://www.singlefile.com/purchases/", @library_url_using_module.rewrite(:module => false, :controller => "purchases")
end

def test_controller_and_action
assert_equal "http://www.singlefile.com/library/settings/show", @library_url.rewrite(:controller => "settings", :action => "show")
Expand Down

0 comments on commit 922f817

Please sign in to comment.