From 28016d33b0f2e668ca59a912c7fb2d777e3cf0a3 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Thu, 1 Jul 2010 09:04:30 +0200 Subject: Use env['action_dispatch.routes'] to determine if we should generate prefix or not. This technique is here to allow using routes from Engine in Application and vice versa. When using Engine routes inside Application it should generate prefix based on mount point. When using Engine routes inside Engine it should use env['SCRIPT_NAME']. In any other case it should generate prefix as env should not be even available. --- actionpack/test/controller/routing_test.rb | 2 +- actionpack/test/dispatch/prefix_generation_test.rb | 102 +++++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 actionpack/test/dispatch/prefix_generation_test.rb (limited to 'actionpack/test') diff --git a/actionpack/test/controller/routing_test.rb b/actionpack/test/controller/routing_test.rb index a8c74a6064..1f14607c31 100644 --- a/actionpack/test/controller/routing_test.rb +++ b/actionpack/test/controller/routing_test.rb @@ -251,7 +251,7 @@ class LegacyRouteSetTests < Test::Unit::TestCase 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).once + x.expects(:url_for).with(:host => 'foo.com', :only_path => false, :controller => 'content', :action => 'show_page', :use_route => :pages, :router => rs).once x.send(:pages_url) end diff --git a/actionpack/test/dispatch/prefix_generation_test.rb b/actionpack/test/dispatch/prefix_generation_test.rb new file mode 100644 index 0000000000..95d5c1c366 --- /dev/null +++ b/actionpack/test/dispatch/prefix_generation_test.rb @@ -0,0 +1,102 @@ +require 'abstract_unit' + +module TestGenerationPrefix + class WithMountedEngine < ActionDispatch::IntegrationTest + class BlogEngine + def self.routes + @routes ||= begin + routes = ActionDispatch::Routing::RouteSet.new + routes.draw do + match "/posts/:id", :to => "inside_engine_generating#index", :as => :post + end + + routes + end + end + + def self.call(env) + env['action_dispatch.routes'] = routes + routes.call(env) + end + end + + class RailsApplication + def self.routes + @routes ||= begin + routes = ActionDispatch::Routing::RouteSet.new + routes.draw do + scope "/:omg", :omg => "awesome" do + mount BlogEngine => "/blog" + end + match "/generate", :to => "outside_engine_generating#index" + end + + routes + end + end + + def self.call(env) + env['action_dispatch.routes'] = routes + routes.call(env) + end + end + + class ::InsideEngineGeneratingController < ActionController::Base + include BlogEngine.routes.url_helpers + def index + render :text => post_path(:id => params[:id]) + end + end + + class ::OutsideEngineGeneratingController < ActionController::Base + include BlogEngine.routes.url_helpers + def index + render :text => post_path(:id => 1) + end + end + + class Foo + include ActionDispatch::Routing::UrlFor + include BlogEngine.routes.url_helpers + + def foo + post_path(42) + end + end + + + RailsApplication.routes # force draw + include BlogEngine.routes.url_helpers + + test "generating URL with prefix" do + assert_equal "/awesome/blog/posts/1", post_path(:id => 1) + 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)) + assert_equal "/pure-awesomness/blog/posts/1", response.body + end + + test "prepend prefix outside the engine" do + env = Rack::MockRequest.env_for("/generate") + env["SCRIPT_NAME"] = "/something" # it could be set by passenger + response = ActionDispatch::Response.new(*RailsApplication.call(env)) + assert_equal "/something/awesome/blog/posts/1", response.body + end + + test "generating urls with options for both prefix and named_route" do + assert_equal "/pure-awesomness/blog/posts/3", post_path(:id => 3, :omg => "pure-awesomness") + end + + test "generating urls with url_for should prepend the prefix" do + path = BlogEngine.routes.url_for(:omg => 'omg', :controller => "inside_engine_generating", :action => "index", :id => 1, :only_path => true) + assert_equal "/omg/blog/posts/1", path + end + + test "generating urls from a regular class" do + assert_equal "/awesome/blog/posts/42", Foo.new.foo + end + end +end -- cgit v1.2.3