Fix generating route from engine to other engine

A regression was introduced in 5b3bb6, generating route from within an
engine to an another engine resulted in prefixing a path with the
SCRIPT_NAME value.

The regression was caused by the fact that SCRIPT_NAME should be
appended only if it's the SCRIPT_NAME for the application, not if it's
SCRIPT_NAME from the current engine.

closes #10409
This commit is contained in:
Piotr Sarnacki 2013-05-03 16:42:31 +02:00
parent f1af9f8375
commit 9a4268db99
3 changed files with 48 additions and 1 deletions

@ -32,7 +32,8 @@ def url_options
if (same_origin = _routes.equal?(env["action_dispatch.routes"])) ||
(script_name = env["ROUTES_#{_routes.object_id}_SCRIPT_NAME"]) ||
(original_script_name = env['SCRIPT_NAME'])
(original_script_name = env['ORIGINAL_SCRIPT_NAME'])
@_url_options.dup.tap do |options|
if original_script_name
options[:original_script_name] = original_script_name

@ -93,6 +93,7 @@ def initialized?
# dispatches the request to the underlying middleware stack.
def call(env)
env["ORIGINAL_FULLPATH"] = build_original_fullpath(env)
env["ORIGINAL_SCRIPT_NAME"] = env["SCRIPT_NAME"]
super(env)
end

@ -13,6 +13,7 @@ def setup
@simple_plugin = engine "weblog"
@plugin = engine "blog"
@metrics_plugin = engine "metrics"
app_file 'config/routes.rb', <<-RUBY
AppTemplate::Application.routes.draw do
@ -28,6 +29,7 @@ def setup
scope "/:user", :user => "anonymous" do
mount Blog::Engine => "/blog"
end
mount Metrics::Engine => "/metrics"
root :to => 'main#index'
end
RUBY
@ -54,6 +56,34 @@ def index
end
RUBY
@metrics_plugin.write "lib/metrics.rb", <<-RUBY
module Metrics
class Engine < ::Rails::Engine
isolate_namespace(Metrics)
end
end
RUBY
@metrics_plugin.write "config/routes.rb", <<-RUBY
Metrics::Engine.routes.draw do
get '/generate_blog_route', to: 'generating#generate_blog_route'
get '/generate_blog_route_in_view', to: 'generating#generate_blog_route_in_view'
end
RUBY
@metrics_plugin.write "app/controllers/metrics/generating_controller.rb", <<-RUBY
module Metrics
class GeneratingController < ActionController::Base
def generate_blog_route
render text: blog.post_path(1)
end
def generate_blog_route_in_view
render inline: "<%= blog.post_path(1) -%>"
end
end
end
RUBY
@plugin.write "app/models/blog/post.rb", <<-RUBY
module Blog
@ -201,6 +231,21 @@ def app
get "/somone/blog/application_route_in_view"
assert_equal "/", last_response.body
# test generating engine's route from other engine
get "/metrics/generate_blog_route"
assert_equal '/anonymous/blog/posts/1', last_response.body
get "/metrics/generate_blog_route_in_view"
assert_equal '/anonymous/blog/posts/1', last_response.body
# test generating engine's route from other engine with default_url_options
get "/metrics/generate_blog_route", {}, 'SCRIPT_NAME' => '/foo'
assert_equal '/foo/anonymous/blog/posts/1', last_response.body
get "/metrics/generate_blog_route_in_view", {}, 'SCRIPT_NAME' => '/foo'
assert_equal '/foo/anonymous/blog/posts/1', last_response.body
# test generating application's route from engine with default_url_options
get "/someone/blog/generate_application_route", {}, 'SCRIPT_NAME' => '/foo'
assert_equal "/foo/", last_response.body