提交 798a14a6 编写于 作者: A Aaron Patterson

pass a config to the route set

This way we can get the relative_url_root from the application without
setting another global value
上级 ad23aab7
......@@ -40,8 +40,6 @@ class Railtie < Rails::Railtie # :nodoc:
ActionDispatch::Cookies::CookieJar.always_write_cookie = config.action_dispatch.always_write_cookie
ActionDispatch.test_app = app
ActionDispatch::Routing::RouteSet.relative_url_root = app.config.relative_url_root
end
end
end
......@@ -20,8 +20,6 @@ class RouteSet
# alias inspect to to_s.
alias inspect to_s
mattr_accessor :relative_url_root
class Dispatcher < Routing::Endpoint
def initialize(defaults)
@defaults = defaults
......@@ -320,11 +318,25 @@ def self.default_resources_path_names
{ :new => 'new', :edit => 'edit' }
end
def initialize
def self.new_with_config(config)
if config.respond_to? :relative_url_root
new Config.new config.relative_url_root
else
# engines apparently don't have this set
new
end
end
Config = Struct.new :relative_url_root
DEFAULT_CONFIG = Config.new(nil)
def initialize(config = DEFAULT_CONFIG)
self.named_routes = NamedRouteCollection.new
self.resources_path_names = self.class.default_resources_path_names
self.default_url_options = {}
@config = config
@append = []
@prepend = []
@disable_clear_and_finalize = false
......@@ -336,6 +348,10 @@ def initialize
@formatter = Journey::Formatter.new @set
end
def relative_url_root
@config.relative_url_root
end
def request_class
ActionDispatch::Request
end
......
......@@ -30,8 +30,8 @@ def test_nested_optional
assert_equal '/foo/zot', path
end
def add_host!
W.default_url_options[:host] = 'www.basecamphq.com'
def add_host!(app = W)
app.default_url_options[:host] = 'www.basecamphq.com'
end
def add_port!
......@@ -257,12 +257,16 @@ def test_relative_url_root_is_respected
def test_relative_url_root_is_respected_with_environment_variable
# `config.relative_url_root` is set by ENV['RAILS_RELATIVE_URL_ROOT']
ActionDispatch::Routing::RouteSet.relative_url_root = '/subdir'
add_host!
w = Class.new {
config = ActionDispatch::Routing::RouteSet::Config.new '/subdir'
r = ActionDispatch::Routing::RouteSet.new(config)
r.draw { get ':controller(/:action(/:id(.:format)))' }
include r.url_helpers
}
add_host!(w)
assert_equal('https://www.basecamphq.com/subdir/c/a/i',
W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https')
w.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https')
)
ActionDispatch::Routing::RouteSet.relative_url_root = nil
end
def test_named_routes
......
......@@ -528,7 +528,7 @@ def env_config
# Defines the routes for this engine. If a block is given to
# routes, it is appended to the engine.
def routes
@routes ||= ActionDispatch::Routing::RouteSet.new
@routes ||= ActionDispatch::Routing::RouteSet.new_with_config(config)
@routes.append(&Proc.new) if block_given?
@routes
end
......
......@@ -42,5 +42,18 @@ def index
get "/"
assert_equal "/", last_response.body
end
def test_routes_know_the_relative_root
boot_rails
require "rails"
require "action_controller/railtie"
require "action_view/railtie"
relative_url = '/hello'
ENV["RAILS_RELATIVE_URL_ROOT"] = relative_url
app = Class.new(Rails::Application)
assert_equal relative_url, app.routes.relative_url_root
ENV["RAILS_RELATIVE_URL_ROOT"] = nil
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册