mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add possibility to explicitly call engine's routes through polymorphic_routes, for example: polymorphic_url([blog, @post])
This commit is contained in:
parent
16dcaf8368
commit
613cbe1f00
3 changed files with 58 additions and 1 deletions
|
@ -42,6 +42,18 @@ module ActionDispatch
|
||||||
#
|
#
|
||||||
# edit_polymorphic_path(@post) # => "/posts/1/edit"
|
# edit_polymorphic_path(@post) # => "/posts/1/edit"
|
||||||
# polymorphic_path(@post, :format => :pdf) # => "/posts/1.pdf"
|
# polymorphic_path(@post, :format => :pdf) # => "/posts/1.pdf"
|
||||||
|
#
|
||||||
|
# == Using with mounted engines
|
||||||
|
#
|
||||||
|
# If you use mounted engine, there is a possibility that you will need to use
|
||||||
|
# polymorphic_url pointing at engine's routes. To do that, just pass proxy used
|
||||||
|
# to reach engine's routes as a first argument:
|
||||||
|
#
|
||||||
|
# For example:
|
||||||
|
#
|
||||||
|
# polymorphic_url([blog, @post]) # it will call blog.post_path(@post)
|
||||||
|
# form_for([blog, @post]) # => "/blog/posts/1
|
||||||
|
#
|
||||||
module PolymorphicRoutes
|
module PolymorphicRoutes
|
||||||
# Constructs a call to a named RESTful route for the given record and returns the
|
# Constructs a call to a named RESTful route for the given record and returns the
|
||||||
# resulting URL string. For example:
|
# resulting URL string. For example:
|
||||||
|
@ -78,6 +90,9 @@ module ActionDispatch
|
||||||
def polymorphic_url(record_or_hash_or_array, options = {})
|
def polymorphic_url(record_or_hash_or_array, options = {})
|
||||||
if record_or_hash_or_array.kind_of?(Array)
|
if record_or_hash_or_array.kind_of?(Array)
|
||||||
record_or_hash_or_array = record_or_hash_or_array.compact
|
record_or_hash_or_array = record_or_hash_or_array.compact
|
||||||
|
if record_or_hash_or_array.first.is_a?(ActionDispatch::Routing::RoutesProxy)
|
||||||
|
proxy = record_or_hash_or_array.shift
|
||||||
|
end
|
||||||
record_or_hash_or_array = record_or_hash_or_array[0] if record_or_hash_or_array.size == 1
|
record_or_hash_or_array = record_or_hash_or_array[0] if record_or_hash_or_array.size == 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -111,7 +126,11 @@ module ActionDispatch
|
||||||
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
|
||||||
|
|
||||||
url_for _routes.url_helpers.__send__("hash_for_#{named_route}", *args)
|
if proxy
|
||||||
|
proxy.send(named_route, *args)
|
||||||
|
else
|
||||||
|
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
|
||||||
|
|
|
@ -57,6 +57,14 @@ class PolymorphicRoutesTest < ActionController::TestCase
|
||||||
@blog_blog = Blog::Blog.new
|
@blog_blog = Blog::Blog.new
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_passing_routes_proxy
|
||||||
|
with_namespaced_routes(:blog) do
|
||||||
|
proxy = ActionDispatch::Routing::RoutesProxy.new(_routes, self)
|
||||||
|
@blog_post.save
|
||||||
|
assert_equal "http://example.com/posts/#{@blog_post.id}", polymorphic_url([proxy, @blog_post])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_namespaced_model
|
def test_namespaced_model
|
||||||
with_namespaced_routes(:blog) do
|
with_namespaced_routes(:blog) do
|
||||||
@blog_post.save
|
@blog_post.save
|
||||||
|
|
|
@ -18,6 +18,7 @@ module ApplicationTests
|
||||||
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"
|
||||||
scope "/:user", :user => "anonymous" do
|
scope "/:user", :user => "anonymous" do
|
||||||
mount Blog::Engine => "/blog"
|
mount Blog::Engine => "/blog"
|
||||||
end
|
end
|
||||||
|
@ -25,6 +26,26 @@ module ApplicationTests
|
||||||
end
|
end
|
||||||
RUBY
|
RUBY
|
||||||
|
|
||||||
|
@plugin.write "app/models/blog/post.rb", <<-RUBY
|
||||||
|
module Blog
|
||||||
|
class Post
|
||||||
|
extend ActiveModel::Naming
|
||||||
|
|
||||||
|
def id
|
||||||
|
44
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_param
|
||||||
|
id.to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
def new_record?
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
|
||||||
@plugin.write "lib/blog.rb", <<-RUBY
|
@plugin.write "lib/blog.rb", <<-RUBY
|
||||||
module Blog
|
module Blog
|
||||||
class Engine < ::Rails::Engine
|
class Engine < ::Rails::Engine
|
||||||
|
@ -78,6 +99,10 @@ module ApplicationTests
|
||||||
def url_for_engine_route
|
def url_for_engine_route
|
||||||
render :text => blog.url_for(:controller => "blog/posts", :action => "index", :user => "john", :only_path => true)
|
render :text => blog.url_for(:controller => "blog/posts", :action => "index", :user => "john", :only_path => true)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def polymorphic_route
|
||||||
|
render :text => polymorphic_url([blog, Blog::Post.new])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
RUBY
|
RUBY
|
||||||
|
|
||||||
|
@ -141,6 +166,11 @@ module ApplicationTests
|
||||||
script_name "/foo"
|
script_name "/foo"
|
||||||
get "/someone/blog/generate_application_route", {}, 'SCRIPT_NAME' => '/foo'
|
get "/someone/blog/generate_application_route", {}, 'SCRIPT_NAME' => '/foo'
|
||||||
assert_equal "/foo/", last_response.body
|
assert_equal "/foo/", last_response.body
|
||||||
|
reset_script_name!
|
||||||
|
|
||||||
|
# test polymorphic routes
|
||||||
|
get "/polymorphic_route"
|
||||||
|
assert_equal "http://example.org/anonymous/blog/posts/44", last_response.body
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue