Skip to content

Commit

Permalink
Polymorphic routes generates collection URL from model class [#1089 s…
Browse files Browse the repository at this point in the history
…tate:resolved]

Signed-off-by: Dan Pickett <dpickett@enlightsolutions.com>
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
Niklas Holmgren authored and lifo committed Aug 8, 2009
1 parent ad98827 commit c284412
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Expand Up @@ -50,6 +50,7 @@ module PolymorphicRoutes
# polymorphic_url([blog, post]) # => "http://example.com/blogs/1/posts/1"
# polymorphic_url([:admin, blog, post]) # => "http://example.com/admin/blogs/1/posts/1"
# polymorphic_url([user, :blog, post]) # => "http://example.com/users/1/blog/posts/1"
# polymorphic_url(Comment) # => "http://example.com/comments"
#
# ==== Options
#
Expand All @@ -70,6 +71,9 @@ module PolymorphicRoutes
# record = Comment.new
# polymorphic_url(record) # same as comments_url()
#
# # the class of a record will also map to the collection
# polymorphic_url(Comment) # same as comments_url()
#
def polymorphic_url(record_or_hash_or_array, options = {})
if record_or_hash_or_array.kind_of?(Array)
record_or_hash_or_array = record_or_hash_or_array.compact
Expand All @@ -93,6 +97,9 @@ def polymorphic_url(record_or_hash_or_array, options = {})
(record.respond_to?(:destroyed?) && record.destroyed?)
args.pop
:plural
elsif record.is_a?(Class)
args.pop
:plural
else
:singular
end
Expand Down
31 changes: 31 additions & 0 deletions actionpack/test/activerecord/polymorphic_routes_test.rb
Expand Up @@ -45,6 +45,12 @@ def test_with_record
assert_equal "http://example.com/projects/#{@project.id}", polymorphic_url(@project)
end
end

def test_with_class
with_test_routes do
assert_equal "http://example.com/projects", polymorphic_url(@project.class)
end
end

def test_with_new_record
with_test_routes do
Expand Down Expand Up @@ -144,6 +150,19 @@ def test_with_nested_destroyed
end
end

def test_with_nested_class
with_test_routes do
@project.save
assert_equal "http://example.com/projects/#{@project.id}/tasks", polymorphic_url([@project, @task.class])
end
end

def test_class_with_array_and_namespace
with_admin_test_routes do
assert_equal "http://example.com/admin/projects", polymorphic_url([:admin, @project.class])
end
end

def test_new_with_array_and_namespace
with_admin_test_routes do
assert_equal "http://example.com/admin/projects/new", polymorphic_url([:admin, @project], :action => 'new')
Expand Down Expand Up @@ -267,6 +286,12 @@ def test_with_irregular_plural_record
end
end

def test_with_irregular_plural_class
with_test_routes do
assert_equal "http://example.com/taxes", polymorphic_url(@tax.class)
end
end

def test_with_irregular_plural_new_record
with_test_routes do
assert_equal "http://example.com/taxes", polymorphic_url(@tax)
Expand Down Expand Up @@ -320,6 +345,12 @@ def test_new_with_irregular_plural_array_and_namespace
end
end

def test_class_with_irregular_plural_array_and_namespace
with_admin_test_routes do
assert_equal "http://example.com/admin/taxes", polymorphic_url([:admin, @tax.class])
end
end

def test_unsaved_with_irregular_plural_array_and_namespace
with_admin_test_routes do
assert_equal "http://example.com/admin/taxes", polymorphic_url([:admin, @tax])
Expand Down

0 comments on commit c284412

Please sign in to comment.