mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Added more tests for polymorphic_url with namespaced models and implemented missing use cases
This commit is contained in:
@@ -111,10 +111,6 @@ module ActionDispatch
|
||||
args.last.kind_of?(Hash) ? args.last.merge!(url_options) : args << url_options
|
||||
end
|
||||
|
||||
if namespace = record.class.parents.detect { |n| n.respond_to?(:_railtie) }
|
||||
named_route.sub!(/#{namespace._railtie.railtie_name}_/, '')
|
||||
end
|
||||
|
||||
url_for _routes.url_helpers.__send__("hash_for_#{named_route}", *args)
|
||||
end
|
||||
|
||||
@@ -159,7 +155,8 @@ module ActionDispatch
|
||||
if parent.is_a?(Symbol) || parent.is_a?(String)
|
||||
parent
|
||||
else
|
||||
ActiveModel::Naming.plural(parent).singularize
|
||||
str = ActiveModel::Naming.plural(parent).singularize
|
||||
remove_namespace(str, parent)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -168,6 +165,7 @@ module ActionDispatch
|
||||
route << record
|
||||
else
|
||||
route << ActiveModel::Naming.plural(record)
|
||||
remove_namespace(route, record)
|
||||
route = [route.join("_").singularize] if inflection == :singular
|
||||
route << "index" if ActiveModel::Naming.uncountable?(record) && inflection == :plural
|
||||
end
|
||||
@@ -177,6 +175,13 @@ module ActionDispatch
|
||||
action_prefix(options) + route.join("_")
|
||||
end
|
||||
|
||||
def remove_namespace(string, parent)
|
||||
if namespace = parent.class.parents.detect { |n| n.respond_to?(:_railtie) }
|
||||
string.sub!(/#{namespace._railtie.railtie_name}_/, '')
|
||||
end
|
||||
string
|
||||
end
|
||||
|
||||
def extract_record(record_or_hash_or_array)
|
||||
case record_or_hash_or_array
|
||||
when Array; record_or_hash_or_array.last
|
||||
|
||||
@@ -25,6 +25,22 @@ class Series < ActiveRecord::Base
|
||||
set_table_name 'projects'
|
||||
end
|
||||
|
||||
module Blog
|
||||
class Post < ActiveRecord::Base
|
||||
set_table_name 'projects'
|
||||
end
|
||||
|
||||
class Blog < ActiveRecord::Base
|
||||
set_table_name 'projects'
|
||||
end
|
||||
|
||||
def self._railtie
|
||||
o = Object.new
|
||||
def o.railtie_name; "blog" end
|
||||
o
|
||||
end
|
||||
end
|
||||
|
||||
class PolymorphicRoutesTest < ActionController::TestCase
|
||||
include SharedTestRoutes.url_helpers
|
||||
self.default_url_options[:host] = 'example.com'
|
||||
@@ -37,6 +53,30 @@ class PolymorphicRoutesTest < ActionController::TestCase
|
||||
@tax = Tax.new
|
||||
@fax = Fax.new
|
||||
@series = Series.new
|
||||
@blog_post = Blog::Post.new
|
||||
@blog_blog = Blog::Blog.new
|
||||
end
|
||||
|
||||
def test_namespaced_model
|
||||
with_namespaced_routes(:blog) do
|
||||
@blog_post.save
|
||||
assert_equal "http://example.com/posts/#{@blog_post.id}", polymorphic_url(@blog_post)
|
||||
end
|
||||
end
|
||||
|
||||
def test_namespaced_model_with_name_the_same_as_namespace
|
||||
with_namespaced_routes(:blog) do
|
||||
@blog_blog.save
|
||||
assert_equal "http://example.com/blogs/#{@blog_blog.id}", polymorphic_url(@blog_blog)
|
||||
end
|
||||
end
|
||||
|
||||
def test_namespaced_model_with_nested_resources
|
||||
with_namespaced_routes(:blog) do
|
||||
@blog_post.save
|
||||
@blog_blog.save
|
||||
assert_equal "http://example.com/blogs/#{@blog_blog.id}/posts/#{@blog_post.id}", polymorphic_url([@blog_blog, @blog_post])
|
||||
end
|
||||
end
|
||||
|
||||
def test_with_record
|
||||
@@ -385,6 +425,22 @@ class PolymorphicRoutesTest < ActionController::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
def with_namespaced_routes(name)
|
||||
with_routing do |set|
|
||||
set.draw do
|
||||
namespace(name, :shallow_path => nil, :path => nil, :as => nil) do
|
||||
resources :blogs do
|
||||
resources :posts
|
||||
end
|
||||
resources :posts
|
||||
end
|
||||
end
|
||||
|
||||
self.class.send(:include, @routes.url_helpers)
|
||||
yield
|
||||
end
|
||||
end
|
||||
|
||||
def with_test_routes(options = {})
|
||||
with_routing do |set|
|
||||
set.draw do |map|
|
||||
|
||||
Reference in New Issue
Block a user