提交 177a4bd5 编写于 作者: P Piotr Sarnacki

Fix url generation for mounted Engine

I added integration tests for generating urls in Engine and application
and tweaked Engines to fully cooparate with new router's behavior:
* Rails.application now sets ORIGINAL_SCRIPT_NAME
* Rails.application also sets its routes as env['action_dispatch.parent_routes']
* Engine implements responds_to? class method to respond to all the
  instance methods, like #routes
上级 451c9942
......@@ -121,14 +121,20 @@ def load_console(sandbox=false)
alias :build_middleware_stack :app
def call(env)
if Rails.application == self
env["ORIGINAL_SCRIPT_NAME"] = env["SCRIPT_NAME"]
env["action_dispatch.parent_routes"] = routes
end
env["action_dispatch.routes"] = routes
app.call(env.reverse_merge!(env_defaults))
end
def env_defaults
@env_defaults ||= super.merge({
@env_defaults ||= {
"action_dispatch.parameter_filter" => config.filter_parameters,
"action_dispatch.secret_token" => config.secret_token
})
}
end
def initializers
......
......@@ -159,13 +159,8 @@ def endpoint
end
def call(env)
app.call(env.reverse_merge!(env_defaults))
end
def env_defaults
@env_defaults ||= {
"action_dispatch.routes" => routes
}
env["action_dispatch.routes"] = routes
app.call(env)
end
def routes
......
require 'isolation/abstract_unit'
module ApplicationTests
class ApplicationRoutingTest < Test::Unit::TestCase
require 'rack/test'
include Rack::Test::Methods
include ActiveSupport::Testing::Isolation
def setup
build_app
add_to_config("config.action_dispatch.show_exceptions = false")
@plugin = engine "blog"
app_file 'config/routes.rb', <<-RUBY
AppTemplate::Application.routes.draw do |map|
match "/engine_route" => "application_generating#engine_route"
match "/url_for_engine_route" => "application_generating#url_for_engine_route"
scope "/:user", :user => "anonymous" do
mount Blog::Engine => "/blog"
end
root :to => 'main#index'
end
RUBY
@plugin.write "lib/blog.rb", <<-RUBY
module Blog
class Engine < ::Rails::Engine
end
end
RUBY
app_file "config/initializers/bla.rb", <<-RUBY
Blog::Engine.eager_load!
RUBY
@plugin.write "config/routes.rb", <<-RUBY
Blog::Engine.routes.draw do
resources :posts do
get :generate_application_route
end
end
RUBY
@plugin.write "app/controllers/posts_controller.rb", <<-RUBY
class PostsController < ActionController::Base
include Blog::Engine.routes.url_helpers
def index
render :text => post_path(1)
end
def generate_application_route
path = url_for( :routes => Rails.application.routes,
:controller => "main",
:action => "index",
:only_path => true)
render :text => path
end
end
RUBY
app_file "app/controllers/application_generating_controller.rb", <<-RUBY
class ApplicationGeneratingController < ActionController::Base
include Blog::Engine.routes.url_helpers
def engine_route
render :text => posts_path
end
def url_for_engine_route
render :text => url_for(:controller => "posts", :action => "index", :user => "john", :only_path => true)
end
end
RUBY
boot_rails
end
def app
@app ||= begin
require "#{app_path}/config/environment"
Rails.application
end
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 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 application's route from engine
get "/someone/blog/generate_application_route"
assert_equal "/", last_response.body
get "/someone/blog/generate_application_route", {}, "SCRIPT_NAME" => "/foo"
assert_equal "/foo/", last_response.body
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册