Skip to content

Commit

Permalink
polymorphic_url with an array generates a query string
Browse files Browse the repository at this point in the history
Generating an URL with an array of records is now able to build a query
string if the last item of the array is a hash.
  • Loading branch information
Sephi-Chan committed Aug 4, 2012
1 parent 5fe923c commit 61c8a4d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion actionpack/lib/action_dispatch/routing/polymorphic_routes.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ module PolymorphicRoutes
# #
# # calls post_url(post) # # calls post_url(post)
# polymorphic_url(post) # => "http://example.com/posts/1" # polymorphic_url(post) # => "http://example.com/posts/1"
# polymorphic_url([post, :foo => 'bar']) # => "http://example.com/posts/1?foo=bar"
# polymorphic_url([blog, post]) # => "http://example.com/blogs/1/posts/1" # 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([: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([user, :blog, post]) # => "http://example.com/users/1/blog/posts/1"
Expand Down Expand Up @@ -164,6 +165,7 @@ def routing_type(options)


def build_named_route_call(records, inflection, options = {}) def build_named_route_call(records, inflection, options = {})
if records.is_a?(Array) if records.is_a?(Array)
query_string = records.pop if records.last.is_a?(Hash)
record = records.pop record = records.pop
route = records.map do |parent| route = records.map do |parent|
if parent.is_a?(Symbol) || parent.is_a?(String) if parent.is_a?(Symbol) || parent.is_a?(String)
Expand Down Expand Up @@ -196,7 +198,8 @@ def build_named_route_call(records, inflection, options = {})


def extract_record(record_or_hash_or_array) def extract_record(record_or_hash_or_array)
case record_or_hash_or_array case record_or_hash_or_array
when Array; record_or_hash_or_array.last when Array
record_or_hash_or_array.last.is_a?(Hash) ? record_or_hash_or_array[-2] : record_or_hash_or_array.last
when Hash; record_or_hash_or_array[:id] when Hash; record_or_hash_or_array[:id]
else record_or_hash_or_array else record_or_hash_or_array
end end
Expand Down
14 changes: 14 additions & 0 deletions actionpack/test/activerecord/polymorphic_routes_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -309,6 +309,20 @@ def test_with_array_containing_single_object
end end
end end


def test_with_array_containing_simple_hash
with_test_routes do
@project.save
assert_equal "http://example.com/projects/#{@project.id}?foo=bar", polymorphic_url([@project, :foo => 'bar' ])
end
end

def test_with_array_containing_complex_hash
with_test_routes do
@project.save
assert_equal "http://example.com/projects/#{@project.id}?foo=bar&nested%5Bfoo%5D=bar", polymorphic_url([@project, :nested => { :foo => 'bar' }, :foo => 'bar'])
end
end

def test_with_array_containing_single_name def test_with_array_containing_single_name
with_test_routes do with_test_routes do
@project.save @project.save
Expand Down

0 comments on commit 61c8a4d

Please sign in to comment.