diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2015-03-05 13:11:53 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2015-03-05 13:12:06 -0800 |
commit | 798a14a6388ac525109e550d72bba0b1118e5e2e (patch) | |
tree | cad588c08fd1a4aa2e7a67c3899d131146f14e05 /actionpack | |
parent | ad23aab7b30612d77bf746e6c0ae7a7140de0144 (diff) | |
download | rails-798a14a6388ac525109e550d72bba0b1118e5e2e.tar.gz rails-798a14a6388ac525109e550d72bba0b1118e5e2e.tar.bz2 rails-798a14a6388ac525109e550d72bba0b1118e5e2e.zip |
pass a config to the route set
This way we can get the relative_url_root from the application without
setting another global value
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_dispatch/railtie.rb | 2 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/routing/route_set.rb | 22 | ||||
-rw-r--r-- | actionpack/test/controller/url_for_test.rb | 16 |
3 files changed, 29 insertions, 11 deletions
diff --git a/actionpack/lib/action_dispatch/railtie.rb b/actionpack/lib/action_dispatch/railtie.rb index 8cde5eb6f7..ddeea24bb3 100644 --- a/actionpack/lib/action_dispatch/railtie.rb +++ b/actionpack/lib/action_dispatch/railtie.rb @@ -40,8 +40,6 @@ module ActionDispatch 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 diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 6d964c2cbf..0f3734dd74 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -20,8 +20,6 @@ module ActionDispatch # 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 @@ module ActionDispatch { :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 @@ module ActionDispatch @formatter = Journey::Formatter.new @set end + def relative_url_root + @config.relative_url_root + end + def request_class ActionDispatch::Request end diff --git a/actionpack/test/controller/url_for_test.rb b/actionpack/test/controller/url_for_test.rb index cd63493b32..31677f202d 100644 --- a/actionpack/test/controller/url_for_test.rb +++ b/actionpack/test/controller/url_for_test.rb @@ -30,8 +30,8 @@ module AbstractController 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 @@ module AbstractController 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 |