From 2fae37f0acbb4154fe75c22a892f79e158016866 Mon Sep 17 00:00:00 2001 From: Sammy Larbi Date: Sat, 30 Aug 2014 17:19:16 -0500 Subject: Allow polymorphic routes with nil when a route can still be drawn Suppose you have two resources routed in the following manner: ```ruby resources :blogs do resources :posts end resources :posts ``` When using polymorphic resource routing like `url_for([@blog, @post])`, and `@blog` is `nil` Rails should still try to match the route to the top-level posts resource. Fixes #16754 --- .../test/activerecord/polymorphic_routes_test.rb | 36 ++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'actionview/test/activerecord/polymorphic_routes_test.rb') diff --git a/actionview/test/activerecord/polymorphic_routes_test.rb b/actionview/test/activerecord/polymorphic_routes_test.rb index 0d97ddb2f6..a259c359f0 100644 --- a/actionview/test/activerecord/polymorphic_routes_test.rb +++ b/actionview/test/activerecord/polymorphic_routes_test.rb @@ -183,16 +183,33 @@ class PolymorphicRoutesTest < ActionController::TestCase end end - def test_with_nil_in_list + def test_with_entirely_nil_list with_test_routes do exception = assert_raise ArgumentError do @series.save - polymorphic_url([nil, @series]) + polymorphic_url([nil, nil]) end assert_equal "Nil location provided. Can't build URI.", exception.message end end + def test_with_nil_in_list_for_resource_that_could_be_top_level_or_nested + with_top_level_and_nested_routes do + @blog_post.save + assert_equal "http://example.com/posts/#{@blog_post.id}", polymorphic_url([nil, @blog_post]) + end + end + + def test_with_nil_in_list_does_not_generate_invalid_link + with_top_level_and_nested_routes do + exception = assert_raise NoMethodError do + @series.save + polymorphic_url([nil, @series]) + end + assert_match /undefined method `series_url' for/, exception.message + end + end + def test_with_record with_test_routes do @project.save @@ -626,6 +643,21 @@ class PolymorphicRoutesTest < ActionController::TestCase end end + def with_top_level_and_nested_routes(options = {}) + with_routing do |set| + set.draw do + resources :blogs do + resources :posts + resources :series + end + resources :posts + end + + extend @routes.url_helpers + yield + end + end + def with_admin_test_routes(options = {}) with_routing do |set| set.draw do -- cgit v1.2.3 From 5c057f925516e87b2bcd6701fab42c1454652cc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 1 Sep 2014 22:20:39 -0300 Subject: Remove warning --- actionview/test/activerecord/polymorphic_routes_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'actionview/test/activerecord/polymorphic_routes_test.rb') diff --git a/actionview/test/activerecord/polymorphic_routes_test.rb b/actionview/test/activerecord/polymorphic_routes_test.rb index a259c359f0..4e94304796 100644 --- a/actionview/test/activerecord/polymorphic_routes_test.rb +++ b/actionview/test/activerecord/polymorphic_routes_test.rb @@ -199,14 +199,14 @@ class PolymorphicRoutesTest < ActionController::TestCase assert_equal "http://example.com/posts/#{@blog_post.id}", polymorphic_url([nil, @blog_post]) end end - + def test_with_nil_in_list_does_not_generate_invalid_link with_top_level_and_nested_routes do exception = assert_raise NoMethodError do @series.save polymorphic_url([nil, @series]) end - assert_match /undefined method `series_url' for/, exception.message + assert_match(/undefined method `series_url' for/, exception.message) end end -- cgit v1.2.3