Permalink
Browse files

Merge branch 'polymorphic_url_builds_query_string_with_array'

  • Loading branch information...
2 parents 3d3fa16 + 6be564c commit 067efad8af6e84e45518120a77dfbb0b49c94469 @pixeltrix pixeltrix committed Aug 4, 2012
View
2 actionpack/CHANGELOG.md
@@ -1,5 +1,7 @@
## Rails 4.0.0 (unreleased) ##
+* Add the ability to pass options as part of the array notation for `polymorphic_url` *Romain Tribes*
+
* Fixed issue with where Digest authentication would not work behind a proxy. *Arthur Smith*
* Added ActionController::Live. Mix it in to your controller and you can
View
5 actionpack/lib/action_dispatch/routing/polymorphic_routes.rb
@@ -62,6 +62,7 @@ module PolymorphicRoutes
#
# # calls post_url(post)
# 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([:admin, blog, post]) # => "http://example.com/admin/blogs/1/posts/1"
# polymorphic_url([user, :blog, post]) # => "http://example.com/users/1/blog/posts/1"
@@ -92,6 +93,7 @@ module PolymorphicRoutes
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
+ options.reverse_merge!(record_or_hash_or_array.extract_options!)
if record_or_hash_or_array.first.is_a?(ActionDispatch::Routing::RoutesProxy)
proxy = record_or_hash_or_array.shift
end
@@ -196,7 +198,8 @@ def build_named_route_call(records, inflection, options = {})
def extract_record(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
when Hash; record_or_hash_or_array[:id]
else record_or_hash_or_array
end
View
21 actionpack/test/activerecord/polymorphic_routes_test.rb
@@ -309,6 +309,27 @@ def test_with_array_containing_single_object
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_simple_hash_options_take_precedence
+ with_test_routes do
+ @project.save
+ assert_equal "http://example.com/projects/#{@project.id}?foo=baz", polymorphic_url([@project, :foo => 'bar' ], :foo => 'baz')
+ end
+ end
+
def test_with_array_containing_single_name
with_test_routes do
@project.save

0 comments on commit 067efad

Please sign in to comment.