aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2014-08-31 13:06:13 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2014-08-31 13:06:13 -0700
commitba7d35c6b79eb9f1ca60462b822cbc08644e950a (patch)
treecebf276674305c090572c378d8a68fe93afdee06
parent0db179a99804de0c26eb4a0dfd91986ffaa2a4dc (diff)
parent2fae37f0acbb4154fe75c22a892f79e158016866 (diff)
downloadrails-ba7d35c6b79eb9f1ca60462b822cbc08644e950a.tar.gz
rails-ba7d35c6b79eb9f1ca60462b822cbc08644e950a.tar.bz2
rails-ba7d35c6b79eb9f1ca60462b822cbc08644e950a.zip
Merge pull request #16755 from codeodor/master
Allow polymorphic routes with nil when a route can still be drawn
-rw-r--r--actionpack/lib/action_dispatch/routing/polymorphic_routes.rb3
-rw-r--r--actionview/test/activerecord/polymorphic_routes_test.rb36
2 files changed, 36 insertions, 3 deletions
diff --git a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb
index 427a5674bd..f15868d37e 100644
--- a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb
+++ b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb
@@ -197,7 +197,8 @@ module ActionDispatch
case record_or_hash_or_array
when Array
- if record_or_hash_or_array.empty? || record_or_hash_or_array.include?(nil)
+ record_or_hash_or_array = record_or_hash_or_array.compact
+ if record_or_hash_or_array.empty?
raise ArgumentError, "Nil location provided. Can't build URI."
end
if record_or_hash_or_array.first.is_a?(ActionDispatch::Routing::RoutesProxy)
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