Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added some more tests for url generation between Engine and Application

  • Loading branch information...
commit b697ba9fd72ac8701747863b42082e59f13ba678 1 parent 8a07708
Piotr Sarnacki drogus authored
8 actionpack/lib/action_controller/metal/url_for.rb
View
@@ -5,10 +5,10 @@ module UrlFor
include ActionDispatch::Routing::UrlFor
def url_options
- options = {}
- if respond_to?(:env) && env && _routes.equal?(env["action_dispatch.routes"])
- options[:script_name] = request.script_name
- end
+ options = {}
+ if respond_to?(:env) && env && _routes.equal?(env["action_dispatch.routes"])
+ options[:script_name] = request.script_name
+ end
super.merge(options).reverse_merge(
:host => request.host_with_port,
38 actionpack/test/dispatch/prefix_generation_test.rb
View
@@ -11,7 +11,7 @@ def self.routes
routes = ActionDispatch::Routing::RouteSet.new
routes.draw do
match "/posts/:id", :to => "inside_engine_generating#index", :as => :post
- match "/bare_url_for", :to => "inside_engine_generating#bare_url_for", :as => :bare_url_for
+ match "/url_to_application", :to => "inside_engine_generating#url_to_application"
end
routes
@@ -42,10 +42,6 @@ def self.routes
def self.call(env)
env['action_dispatch.routes'] = routes
-
- # the next to values should be set only in application
- env['ORIGINAL_SCRIPT_NAME'] = env['SCRIPT_NAME']
- env['action_dispatch.parent_routes'] = routes
routes.call(env)
end
end
@@ -56,7 +52,7 @@ def index
render :text => post_path(:id => params[:id])
end
- def bare_url_for
+ def url_to_application
path = url_for( :routes => RailsApplication.routes,
:controller => "outside_engine_generating",
:action => "index",
@@ -111,12 +107,23 @@ def setup
end
test "prepend prefix outside the engine" do
+ get "/generate"
+ assert_equal "/awesome/blog/posts/1", last_response.body
+ end
+
+ test "prepend prefix outside the engine and use default_url_options[:script_name]" do
+ RailsApplication.routes.default_url_options = {:script_name => "/something"}
+ get "/generate"
+ assert_equal "/something/awesome/blog/posts/1", last_response.body
+ end
+
+ test "give higher priority to default_url_options[:script_name]" do
RailsApplication.routes.default_url_options = {:script_name => "/something"}
- get "/generate", {}, 'SCRIPT_NAME' => "/something"
+ get "/generate", {}, 'SCRIPT_NAME' => "/foo"
assert_equal "/something/awesome/blog/posts/1", last_response.body
end
- test "generating urls with options for both prefix and named_route" do
+ test "generating urls with options for prefix and named_route" do
assert_equal "/pure-awesomness/blog/posts/3", post_path(:id => 3, :omg => "pure-awesomness")
end
@@ -129,9 +136,20 @@ def setup
assert_equal "/awesome/blog/posts/42", Foo.new.foo
end
- test "passing :routes to url_for to change current routes" do
+ test "generating application's url from engine" do
+ get "/pure-awesomness/blog/url_to_application"
+ assert_equal "/generate", last_response.body
+ end
+
+ test "generating application's url from engine with default_url_options[:script_name]" do
+ RailsApplication.routes.default_url_options = {:script_name => "/something"}
+ get "/pure-awesomness/blog/url_to_application"
+ assert_equal "/something/generate", last_response.body
+ end
+
+ test "generating application's url from engine should give higher priority to default_url_options[:script_name]" do
RailsApplication.routes.default_url_options = {:script_name => "/something"}
- get "/pure-awesomness/blog/bare_url_for", {}, 'SCRIPT_NAME' => "/something"
+ get "/pure-awesomness/blog/url_to_application", {}, 'SCRIPT_NAME' => '/foo'
assert_equal "/something/generate", last_response.body
end
31 railties/test/railties/mounted_engine_routes_test.rb
View
@@ -70,7 +70,7 @@ def engine_route
end
def url_for_engine_route
- render :text => url_for(:controller => "posts", :action => "index", :user => "john", :only_path => true)
+ render :text => url_for(:controller => "posts", :action => "index", :user => "john", :only_path => true, :routes => Blog::Engine.routes)
end
end
RUBY
@@ -85,24 +85,47 @@ def app
end
end
+ def reset_script_name!
+ Rails.application.routes.default_url_options = {}
+ end
+
+ def script_name(script_name)
+ Rails.application.routes.default_url_options = {:script_name => script_name}
+ end
+
test "routes generation in engine and application" do
# test generating engine's route from engine
get "/john/blog/posts"
assert_equal "/john/blog/posts/1", last_response.body
+ # test generating engine's route from engine with default_url_options
+ script_name "/foo"
+ get "/john/blog/posts", {}, 'SCRIPT_NAME' => "/foo"
+ assert_equal "/foo/john/blog/posts/1", last_response.body
+ reset_script_name!
+
# test generating engine's route from application
get "/engine_route"
assert_equal "/anonymous/blog/posts", last_response.body
get "/url_for_engine_route"
assert_equal "/john/blog/posts", last_response.body
+ # test generating engine's route from application with default_url_options
+ script_name "/foo"
+ get "/engine_route", {}, 'SCRIPT_NAME' => "/foo"
+ assert_equal "/foo/anonymous/blog/posts", last_response.body
+ script_name "/foo"
+ get "/url_for_engine_route", {}, 'SCRIPT_NAME' => "/foo"
+ assert_equal "/foo/john/blog/posts", last_response.body
+ reset_script_name!
+
# test generating application's route from engine
get "/someone/blog/generate_application_route"
assert_equal "/", last_response.body
- # with script_name
- Rails.application.default_url_options = {:script_name => "/foo"}
- get "/someone/blog/generate_application_route", {}, "SCRIPT_NAME" => "/foo"
+ # test generating application's route from engine with default_url_options
+ script_name "/foo"
+ get "/someone/blog/generate_application_route", {}, 'SCRIPT_NAME' => '/foo'
assert_equal "/foo/", last_response.body
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.