From 09b48472c73d6a15289a22d5b7cd8f245e22d3c0 Mon Sep 17 00:00:00 2001 From: =?utf-8?q?Tarmo=20T=C3=A4nav?= Date: Thu, 21 Aug 2008 18:26:05 +0100 Subject: [PATCH] polymorphic_url helper can now take url options. All *_polymorphic_url, *_polymorphic_path helpers can now accept an options hash which will be passed on to the named route making it possible to generate polymorphic routes with additional url parameters. --- .../lib/action_controller/polymorphic_routes.rb | 20 +++++++++++++++----- .../test/controller/polymorphic_routes_test.rb | 20 +++++++++++++++++++- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/actionpack/lib/action_controller/polymorphic_routes.rb b/actionpack/lib/action_controller/polymorphic_routes.rb index 7c30bf0..634fe7c 100644 --- a/actionpack/lib/action_controller/polymorphic_routes.rb +++ b/actionpack/lib/action_controller/polymorphic_routes.rb @@ -102,6 +102,16 @@ module ActionController args << format if format named_route = build_named_route_call(record_or_hash_or_array, namespace, inflection, options) + + url_options = options.except(:action, :routing_type, :format) + unless url_options.empty? + if args.last.kind_of?(Hash) + args.last.merge!(url_options) + else + args << url_options + end + end + send!(named_route, *args) end @@ -114,19 +124,19 @@ module ActionController %w(edit new formatted).each do |action| module_eval <<-EOT, __FILE__, __LINE__ - def #{action}_polymorphic_url(record_or_hash) - polymorphic_url(record_or_hash, :action => "#{action}") + def #{action}_polymorphic_url(record_or_hash, options = {}) + polymorphic_url(record_or_hash, options.merge(:action => "#{action}")) end - def #{action}_polymorphic_path(record_or_hash) - polymorphic_url(record_or_hash, :action => "#{action}", :routing_type => :path) + def #{action}_polymorphic_path(record_or_hash, options = {}) + polymorphic_url(record_or_hash, options.merge(:action => "#{action}", :routing_type => :path)) end EOT end private def action_prefix(options) - options[:action] ? "#{options[:action]}_" : "" + options[:action] ? "#{options[:action]}_" : options[:format] ? "formatted_" : "" end def routing_type(options) diff --git a/actionpack/test/controller/polymorphic_routes_test.rb b/actionpack/test/controller/polymorphic_routes_test.rb index 3f52526..d885dbc 100644 --- a/actionpack/test/controller/polymorphic_routes_test.rb +++ b/actionpack/test/controller/polymorphic_routes_test.rb @@ -60,6 +60,18 @@ uses_mocha 'polymorphic URL helpers' do edit_polymorphic_url(@article) end + def test_url_helper_prefixed_with_edit_with_url_options + @article.save + expects(:edit_article_url).with(@article, :param1 => '10') + edit_polymorphic_url(@article, :param1 => '10') + end + + def test_url_helper_with_url_options + @article.save + expects(:article_url).with(@article, :param1 => '10') + polymorphic_url(@article, :param1 => '10') + end + def test_formatted_url_helper expects(:formatted_article_url).with(@article, :pdf) formatted_polymorphic_url([@article, :pdf]) @@ -67,10 +79,16 @@ uses_mocha 'polymorphic URL helpers' do def test_format_option @article.save - expects(:article_url).with(@article, :pdf) + expects(:formatted_article_url).with(@article, :pdf) polymorphic_url(@article, :format => :pdf) end + def test_format_option_with_url_options + @article.save + expects(:formatted_article_url).with(@article, :pdf, :param1 => '10') + polymorphic_url(@article, :format => :pdf, :param1 => '10') + end + def test_id_and_format_option @article.save expects(:article_url).with(:id => @article, :format => :pdf) -- 1.5.4.5