Permalink
Browse files

Allow to generate Application routes inside Engine

This requires knowledge about original SCRIPT_NAME and
the parent router. It should be pass through the env
as ORIGIAL_SCRIPT_NAME and action_dispatch.parent_routes
  • Loading branch information...
1 parent 628b94f commit 451c9942bb493190d5673c1b55be7506056db13b @drogus drogus committed Jul 7, 2010
@@ -289,7 +289,7 @@ def define_generate_prefix(app, name)
app.routes.class_eval do
define_method :_generate_prefix do |options|
keys = _route.segment_keys + ActionDispatch::Routing::RouteSet::RESERVED_OPTIONS
- prefix_options = options.reject { |k, v| !(keys).include?(k) }
+ prefix_options = options.slice(*keys)
# we must actually delete prefix segment keys to avoid passing them to next url_for
_route.segment_keys.each { |k| options.delete(k) }
_router.url_helpers.send("#{name}_path", prefix_options)
@@ -129,9 +129,9 @@ def url_for(options = nil)
options
when nil, Hash
routes = (options ? options.delete(:routes) : nil) || _routes
-
- if respond_to?(:env) && env && routes.equal?(env["action_dispatch.routes"])
- options[:skip_prefix] = true
+ 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)
@@ -8,6 +8,7 @@ def self.routes
routes = ActionDispatch::Routing::RouteSet.new
routes.draw do
match "/posts/:id", :to => "inside_engine_generating#index", :as => :post
+ match "/bare_url_for", :to => "inside_engine_generating#bare_url_for", :as => :bare_url_for
end
routes
@@ -37,6 +38,10 @@ def self.routes
def self.call(env)
env['action_dispatch.routes'] = routes
+
+ # the next to values should be set only in application
+ env['ORIGINAL_SCRIPT_NAME'] = env['SCRIPT_NAME']
+ env['action_dispatch.parent_routes'] = routes
routes.call(env)
end
end
@@ -46,6 +51,14 @@ class ::InsideEngineGeneratingController < ActionController::Base
def index
render :text => post_path(:id => params[:id])
end
+
+ def bare_url_for
+ path = url_for( :routes => RailsApplication.routes,
+ :controller => "outside_engine_generating",
+ :action => "index",
+ :only_path => true)
+ render :text => path
+ end
end
class ::OutsideEngineGeneratingController < ActionController::Base
@@ -73,9 +86,8 @@ def foo
end
test "use SCRIPT_NAME inside the engine" do
- env = Rack::MockRequest.env_for("/posts/1")
- env["SCRIPT_NAME"] = "/pure-awesomness/blog"
- response = ActionDispatch::Response.new(*BlogEngine.call(env))
+ env = Rack::MockRequest.env_for("/pure-awesomness/blog/posts/1")
+ response = ActionDispatch::Response.new(*RailsApplication.call(env))
assert_equal "/pure-awesomness/blog/posts/1", response.body
end
@@ -98,5 +110,12 @@ def foo
test "generating urls from a regular class" do
assert_equal "/awesome/blog/posts/42", Foo.new.foo
end
+
+ 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"
+ response = ActionDispatch::Response.new(*RailsApplication.call(env))
+ assert_equal "/something/generate", response.body
+ end
end
end

0 comments on commit 451c994

Please sign in to comment.