From 61c8a4d926343903593a27080216af7e4ed81268 Mon Sep 17 00:00:00 2001 From: Romain Tribes Date: Sat, 4 Aug 2012 10:00:33 +0200 Subject: polymorphic_url with an array generates a query string 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. --- actionpack/lib/action_dispatch/routing/polymorphic_routes.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'actionpack/lib/action_dispatch') diff --git a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb index 3d7b8878b8..7bdd9244d1 100644 --- a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb +++ b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb @@ -62,6 +62,7 @@ module ActionDispatch # # # 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" @@ -164,6 +165,7 @@ module ActionDispatch 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) @@ -196,7 +198,8 @@ module ActionDispatch 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.is_a?(Hash) ? record_or_hash_or_array[-2] : record_or_hash_or_array.last when Hash; record_or_hash_or_array[:id] else record_or_hash_or_array end -- cgit v1.2.3 From 6be564c7a087773cb0b51c54396cc190e4f5c983 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Sat, 4 Aug 2012 11:24:53 +0100 Subject: 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 --- actionpack/lib/action_dispatch/routing/polymorphic_routes.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'actionpack/lib/action_dispatch') diff --git a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb index 7bdd9244d1..93e8418d9c 100644 --- a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb +++ b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb @@ -93,6 +93,7 @@ module ActionDispatch 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 @@ module ActionDispatch 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 @@ module ActionDispatch 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 -- cgit v1.2.3