Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

New way of generating urls for Application from Engine.

It's based specifying application's script_name with:
Rails.application.default_url_options = {:script_name => "/foo"}

default_url_options method is delegated to routes. If router
used to generate url differs from the router passed via env
it always overwrites :script_name with this value.
  • Loading branch information...
commit eedbf87d15b99a7cae38b0d8894fc39f1e70a81e 1 parent 177a4bd
Piotr Sarnacki drogus authored
13 actionpack/lib/action_controller/metal/url_for.rb
View
@@ -5,11 +5,20 @@ module UrlFor
include ActionDispatch::Routing::UrlFor
def url_options
- super.reverse_merge(
+ options = {}
+ if respond_to?(:env) && env
+ if _routes.equal?(env["action_dispatch.routes"])
+ options[:skip_prefix] = true
+ elsif env["action_dispatch.routes"]
+ options[:script_name] = _routes.default_url_options[:script_name]
+ end
+ end
+
+ super.merge(options).reverse_merge(
:host => request.host_with_port,
:protocol => request.protocol,
:_path_segments => request.symbolized_path_parameters
- ).merge(:script_name => request.script_name)
+ ).reverse_merge(:script_name => request.script_name)
end
def _routes
4 actionpack/lib/action_dispatch/routing/route_set.rb
View
@@ -280,10 +280,10 @@ class << self
# Yes plz - JP
included do
routes.install_helpers(self)
- singleton_class.send(:define_method, :_routes) { routes }
+ singleton_class.send(:define_method, :_routes) { @_routes || routes }
end
- define_method(:_routes) { routes }
+ define_method(:_routes) { @_routes || routes }
end
helpers
15 actionpack/lib/action_dispatch/routing/url_for.rb
View
@@ -129,16 +129,21 @@ def url_for(options = nil)
options
when nil, Hash
routes = (options ? options.delete(:routes) : nil) || _routes
- if respond_to?(:env) && env
- options[:skip_prefix] = true if routes.equal?(env["action_dispatch.routes"])
- options[:script_name] = env["ORIGINAL_SCRIPT_NAME"] if routes.equal?(env["action_dispatch.parent_routes"])
- end
- routes.url_for((options || {}).reverse_merge!(url_options).symbolize_keys)
+ _with_routes(routes) do
+ routes.url_for((options || {}).reverse_merge!(url_options).symbolize_keys)
+ end
else
polymorphic_url(options)
end
end
+
+ def _with_routes(routes)
+ old_routes, @_routes = @_routes, routes
+ yield
+ ensure
+ @_routes = old_routes
+ end
end
end
end
2  actionpack/test/controller/routing_test.rb
View
@@ -251,7 +251,7 @@ def test_optimised_named_route_with_host
map.pages 'pages', :controller => 'content', :action => 'show_page', :host => 'foo.com'
end
x = setup_for_named_route
- x.expects(:url_for).with(:host => 'foo.com', :only_path => false, :controller => 'content', :action => 'show_page', :use_route => :pages, :router => rs).once
+ x.expects(:url_for).with(:host => 'foo.com', :only_path => false, :controller => 'content', :action => 'show_page', :use_route => :pages).once
x.send(:pages_url)
end
1  actionpack/test/dispatch/prefix_generation_test.rb
View
@@ -114,6 +114,7 @@ def foo
test "passing :routes to url_for to change current routes" do
env = Rack::MockRequest.env_for("/pure-awesomness/blog/bare_url_for")
env["SCRIPT_NAME"] = "/something"
+ RailsApplication.routes.default_url_options = {:script_name => "/something"}
response = ActionDispatch::Response.new(*RailsApplication.call(env))
assert_equal "/something/generate", response.body
end
7 railties/lib/rails/application.rb
View
@@ -50,6 +50,8 @@ def inherited(base)
end
end
+ delegate :default_url_options, :default_url_options=, :to => :routes
+
# This method is called just after an application inherits from Rails::Application,
# allowing the developer to load classes in lib and use them during application
# configuration.
@@ -121,11 +123,6 @@ 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
3  railties/test/railties/mounted_engine_routes_test.rb
View
@@ -99,6 +99,9 @@ def app
# 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"
assert_equal "/foo/", last_response.body
end
Please sign in to comment.
Something went wrong with that request. Please try again.