Permalink
Browse files

Refactor passing url options via array for polymorphic_url

Rather than keep the url options in record_or_hash_or_array, extract it
and reverse merge with options as it may contain important private keys
like `:routing_type`.

Closes #7259
  • Loading branch information...
1 parent 61c8a4d commit 6be564c7a087773cb0b51c54396cc190e4f5c983 @pixeltrix pixeltrix committed Aug 4, 2012
@@ -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
@@ -93,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
@@ -165,7 +166,6 @@ def routing_type(options)
def build_named_route_call(records, inflection, options = {})
if records.is_a?(Array)
- query_string = records.pop if records.last.is_a?(Hash)
record = records.pop
route = records.map do |parent|
if parent.is_a?(Symbol) || parent.is_a?(String)
@@ -199,7 +199,7 @@ 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.is_a?(Hash) ? record_or_hash_or_array[-2] : record_or_hash_or_array.last
+ record_or_hash_or_array.last
when Hash; record_or_hash_or_array[:id]
else record_or_hash_or_array
end
@@ -323,6 +323,13 @@ def test_with_array_containing_complex_hash
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 6be564c

Please sign in to comment.