提交 036f7757 编写于 作者: T thedarkone

Make polymorphic_url calls go through application helpers again.

This brings back the ability to overwrite/extend url generating methods in application heleprs.
上级 02691d35
...@@ -124,14 +124,7 @@ def polymorphic_url(record_or_hash_or_array, options = {}) ...@@ -124,14 +124,7 @@ def polymorphic_url(record_or_hash_or_array, options = {})
args.last.kind_of?(Hash) ? args.last.merge!(url_options) : args << url_options args.last.kind_of?(Hash) ? args.last.merge!(url_options) : args << url_options
end end
if proxy (proxy || self).send(named_route, *args)
proxy.send(named_route, *args)
else
# we need to use url_for, because polymorphic_url can be used in context of other than
# current routes (e.g. engine's routes). As named routes from engine are not included
# calling engine's named route directly would fail.
url_for _routes.url_helpers.__send__("hash_for_#{named_route}", *args)
end
end end
# Returns the path component of a URL for the given record. It uses # Returns the path component of a URL for the given record. It uses
......
...@@ -50,7 +50,9 @@ def self.routes ...@@ -50,7 +50,9 @@ def self.routes
scope "/:omg", :omg => "awesome" do scope "/:omg", :omg => "awesome" do
mount BlogEngine => "/blog", :as => "blog_engine" mount BlogEngine => "/blog", :as => "blog_engine"
end end
match "/posts/:id", :to => "outside_engine_generating#post", :as => :post
match "/generate", :to => "outside_engine_generating#index" match "/generate", :to => "outside_engine_generating#index"
match "/polymorphic_path_for_app", :to => "outside_engine_generating#polymorphic_path_for_app"
match "/polymorphic_path_for_engine", :to => "outside_engine_generating#polymorphic_path_for_engine" match "/polymorphic_path_for_engine", :to => "outside_engine_generating#polymorphic_path_for_engine"
match "/polymorphic_with_url_for", :to => "outside_engine_generating#polymorphic_with_url_for" match "/polymorphic_with_url_for", :to => "outside_engine_generating#polymorphic_with_url_for"
match "/conflicting_url", :to => "outside_engine_generating#conflicting" match "/conflicting_url", :to => "outside_engine_generating#conflicting"
...@@ -101,6 +103,7 @@ def conflicting ...@@ -101,6 +103,7 @@ def conflicting
class ::OutsideEngineGeneratingController < ActionController::Base class ::OutsideEngineGeneratingController < ActionController::Base
include BlogEngine.routes.mounted_helpers include BlogEngine.routes.mounted_helpers
include RailsApplication.routes.url_helpers
def index def index
render :text => blog_engine.post_path(:id => 1) render :text => blog_engine.post_path(:id => 1)
...@@ -110,6 +113,10 @@ def polymorphic_path_for_engine ...@@ -110,6 +113,10 @@ def polymorphic_path_for_engine
render :text => blog_engine.polymorphic_path(Post.new) render :text => blog_engine.polymorphic_path(Post.new)
end end
def polymorphic_path_for_app
render :text => polymorphic_path(Post.new)
end
def polymorphic_with_url_for def polymorphic_with_url_for
render :text => blog_engine.url_for(Post.new) render :text => blog_engine.url_for(Post.new)
end end
...@@ -201,6 +208,11 @@ def setup ...@@ -201,6 +208,11 @@ def setup
assert_equal "/awesome/blog/posts/1", last_response.body assert_equal "/awesome/blog/posts/1", last_response.body
end end
test "polymorphic_path_for_app" do
get "/polymorphic_path_for_app"
assert_equal "/posts/1", last_response.body
end
test "[APP] generating engine's url with url_for(@post)" do test "[APP] generating engine's url with url_for(@post)" do
get "/polymorphic_with_url_for" get "/polymorphic_with_url_for"
assert_equal "http://example.org/awesome/blog/posts/1", last_response.body assert_equal "http://example.org/awesome/blog/posts/1", last_response.body
......
...@@ -39,7 +39,7 @@ def test_link_to_person ...@@ -39,7 +39,7 @@ def test_link_to_person
with_test_route_set do with_test_route_set do
person = mock(:name => "David") person = mock(:name => "David")
person.class.extend ActiveModel::Naming person.class.extend ActiveModel::Naming
_routes.url_helpers.expects(:hash_for_mocha_mock_path).with(person).returns("/people/1") expects(:mocha_mock_path).with(person).returns("/people/1")
assert_equal '<a href="/people/1">David</a>', link_to_person(person) assert_equal '<a href="/people/1">David</a>', link_to_person(person)
end end
end end
......
...@@ -15,10 +15,12 @@ def setup ...@@ -15,10 +15,12 @@ def setup
app_file 'config/routes.rb', <<-RUBY app_file 'config/routes.rb', <<-RUBY
AppTemplate::Application.routes.draw do AppTemplate::Application.routes.draw do
resources :posts
match "/engine_route" => "application_generating#engine_route" match "/engine_route" => "application_generating#engine_route"
match "/engine_route_in_view" => "application_generating#engine_route_in_view" match "/engine_route_in_view" => "application_generating#engine_route_in_view"
match "/url_for_engine_route" => "application_generating#url_for_engine_route" match "/url_for_engine_route" => "application_generating#url_for_engine_route"
match "/polymorphic_route" => "application_generating#polymorphic_route" match "/polymorphic_route" => "application_generating#polymorphic_route"
match "/application_polymorphic_path" => "application_generating#application_polymorphic_path"
scope "/:user", :user => "anonymous" do scope "/:user", :user => "anonymous" do
mount Blog::Engine => "/blog" mount Blog::Engine => "/blog"
end end
...@@ -59,6 +61,7 @@ class Engine < ::Rails::Engine ...@@ -59,6 +61,7 @@ class Engine < ::Rails::Engine
resources :posts resources :posts
match '/generate_application_route', :to => 'posts#generate_application_route' match '/generate_application_route', :to => 'posts#generate_application_route'
match '/application_route_in_view', :to => 'posts#application_route_in_view' match '/application_route_in_view', :to => 'posts#application_route_in_view'
match '/engine_polymorphic_path', :to => 'posts#engine_polymorphic_path'
end end
RUBY RUBY
...@@ -79,6 +82,10 @@ def generate_application_route ...@@ -79,6 +82,10 @@ def generate_application_route
def application_route_in_view def application_route_in_view
render :inline => "<%= main_app.root_path %>" render :inline => "<%= main_app.root_path %>"
end end
def engine_polymorphic_path
render :text => polymorphic_path(Post.new)
end
end end
end end
RUBY RUBY
...@@ -100,6 +107,10 @@ def url_for_engine_route ...@@ -100,6 +107,10 @@ def url_for_engine_route
def polymorphic_route def polymorphic_route
render :text => polymorphic_url([blog, Blog::Post.new]) render :text => polymorphic_url([blog, Blog::Post.new])
end end
def application_polymorphic_path
render :text => polymorphic_path(Blog::Post.new)
end
end end
RUBY RUBY
...@@ -172,6 +183,14 @@ def script_name(script_name) ...@@ -172,6 +183,14 @@ def script_name(script_name)
# test polymorphic routes # test polymorphic routes
get "/polymorphic_route" get "/polymorphic_route"
assert_equal "http://example.org/anonymous/blog/posts/44", last_response.body assert_equal "http://example.org/anonymous/blog/posts/44", last_response.body
# test that correct path is generated for the same polymorphic_path call in an engine
get "/somone/blog/engine_polymorphic_path"
assert_equal "/somone/blog/posts/44", last_response.body
# and in an application
get "/application_polymorphic_path"
assert_equal "/posts/44", last_response.body
end end
end end
end end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册