diff options
author | Piotr Sarnacki <drogus@gmail.com> | 2010-07-09 10:25:10 +0200 |
---|---|---|
committer | Piotr Sarnacki <drogus@gmail.com> | 2010-09-03 22:59:06 +0200 |
commit | b697ba9fd72ac8701747863b42082e59f13ba678 (patch) | |
tree | b89e392dbfccd29a7fc2d6c0d877503a96a4f016 | |
parent | 8a077089d9b7aa89cb56ef754b4540f411453375 (diff) | |
download | rails-b697ba9fd72ac8701747863b42082e59f13ba678.tar.gz rails-b697ba9fd72ac8701747863b42082e59f13ba678.tar.bz2 rails-b697ba9fd72ac8701747863b42082e59f13ba678.zip |
Added some more tests for url generation between Engine and Application
-rw-r--r-- | actionpack/lib/action_controller/metal/url_for.rb | 8 | ||||
-rw-r--r-- | actionpack/test/dispatch/prefix_generation_test.rb | 38 | ||||
-rw-r--r-- | railties/test/railties/mounted_engine_routes_test.rb | 31 |
3 files changed, 59 insertions, 18 deletions
diff --git a/actionpack/lib/action_controller/metal/url_for.rb b/actionpack/lib/action_controller/metal/url_for.rb index ae628df81c..c1f1be3bef 100644 --- a/actionpack/lib/action_controller/metal/url_for.rb +++ b/actionpack/lib/action_controller/metal/url_for.rb @@ -5,10 +5,10 @@ module ActionController 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, diff --git a/actionpack/test/dispatch/prefix_generation_test.rb b/actionpack/test/dispatch/prefix_generation_test.rb index 6ceb07a4f1..af83ced5e9 100644 --- a/actionpack/test/dispatch/prefix_generation_test.rb +++ b/actionpack/test/dispatch/prefix_generation_test.rb @@ -11,7 +11,7 @@ module TestGenerationPrefix 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 @@ module TestGenerationPrefix 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 @@ module TestGenerationPrefix 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 @@ module TestGenerationPrefix 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 @@ module TestGenerationPrefix 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 diff --git a/railties/test/railties/mounted_engine_routes_test.rb b/railties/test/railties/mounted_engine_routes_test.rb index 18789ab9e3..1a0f5f2c21 100644 --- a/railties/test/railties/mounted_engine_routes_test.rb +++ b/railties/test/railties/mounted_engine_routes_test.rb @@ -70,7 +70,7 @@ module ApplicationTests 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 @@ module ApplicationTests 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 |