From d04c4fac3bb6f75ba15704dd03faeb5f2d7033f7 Mon Sep 17 00:00:00 2001 From: Andrey Ognevsky Date: Wed, 13 Nov 2013 00:18:36 +0400 Subject: Take Hash with options inside Array in #url_for --- actionpack/CHANGELOG.md | 9 +++++++++ actionpack/lib/action_dispatch/routing/url_for.rb | 2 ++ actionpack/test/controller/url_for_test.rb | 18 ++++++++++++++++++ actionview/lib/action_view/routing_url_for.rb | 2 ++ 4 files changed, 31 insertions(+) diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index dea80abfcd..93aaa9ab57 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,3 +1,12 @@ +* Take a hash with options inside array in #url_for + + Example: + + url_for [:new, :admin, :post, { param: 'value' }] + # => http://example.com/admin/posts/new?params=value + + *Andrey Ognevsky* + * Add `session#fetch` method fetch behaves similarly to [Hash#fetch](http://www.ruby-doc.org/core-1.9.3/Hash.html#method-i-fetch), diff --git a/actionpack/lib/action_dispatch/routing/url_for.rb b/actionpack/lib/action_dispatch/routing/url_for.rb index bcebe532bf..4a0ef40873 100644 --- a/actionpack/lib/action_dispatch/routing/url_for.rb +++ b/actionpack/lib/action_dispatch/routing/url_for.rb @@ -155,6 +155,8 @@ module ActionDispatch _routes.url_for(options.symbolize_keys.reverse_merge!(url_options)) when String options + when Array + polymorphic_url(options, options.extract_options!) else polymorphic_url(options) end diff --git a/actionpack/test/controller/url_for_test.rb b/actionpack/test/controller/url_for_test.rb index 088ad73f2f..d2b4952759 100644 --- a/actionpack/test/controller/url_for_test.rb +++ b/actionpack/test/controller/url_for_test.rb @@ -370,6 +370,24 @@ module AbstractController assert_equal("/c/a?show=false", W.new.url_for(:only_path => true, :controller => 'c', :action => 'a', :show => false)) end + def test_url_generation_with_array_and_hash + with_routing do |set| + set.draw do + namespace :admin do + resources :posts + end + end + + kls = Class.new { include set.url_helpers } + kls.default_url_options[:host] = 'www.basecamphq.com' + + controller = kls.new + assert_equal("http://www.basecamphq.com/admin/posts/new?param=value", + controller.send(:url_for, [:new, :admin, :post, { param: 'value' }]) + ) + end + end + private def extract_params(url) url.split('?', 2).last.split('&').sort diff --git a/actionview/lib/action_view/routing_url_for.rb b/actionview/lib/action_view/routing_url_for.rb index f10e7e88ba..33be06cbf7 100644 --- a/actionview/lib/action_view/routing_url_for.rb +++ b/actionview/lib/action_view/routing_url_for.rb @@ -83,6 +83,8 @@ module ActionView super when :back _back_url + when Array + polymorphic_path(options, options.extract_options!) else polymorphic_path(options) end -- cgit v1.2.3