From 26f53f0beacfa0bb79d5132b04a3baf58f533645 Mon Sep 17 00:00:00 2001 From: Johannes Barre Date: Sat, 16 Jan 2010 14:51:30 +0100 Subject: [PATCH] Fix polymorphic_routes with variable in path_prefix --- .../lib/action_controller/polymorphic_routes.rb | 10 +++- .../test/controller/polymorphic_routes_test.rb | 60 +++++++++++--------- actionpack/test/template/form_helper_test.rb | 17 +++++- actionpack/test/template/prototype_helper_test.rb | 8 ++- actionpack/test/template/test_test.rb | 2 +- 5 files changed, 64 insertions(+), 33 deletions(-) diff --git a/actionpack/lib/action_controller/polymorphic_routes.rb b/actionpack/lib/action_controller/polymorphic_routes.rb index b30d441..812b68a 100644 --- a/actionpack/lib/action_controller/polymorphic_routes.rb +++ b/actionpack/lib/action_controller/polymorphic_routes.rb @@ -104,7 +104,15 @@ module ActionController args.last.kind_of?(Hash) ? args.last.merge!(url_options) : args << url_options end - __send__(named_route, *args) + hsh = args.last.is_a?(Hash) ? args.pop : {} + args.each do |itm| + if itm == record + hsh[:id] = itm + else + hsh[(itm.class.to_s.underscore.pluralize.singularize + "_id").gsub("/", "_").to_sym] = itm + end + end + hsh.empty? ? __send__(named_route) : __send__(named_route, hsh) end # Returns the path component of a URL for the given record. It uses diff --git a/actionpack/test/controller/polymorphic_routes_test.rb b/actionpack/test/controller/polymorphic_routes_test.rb index a075fc4..0ce333c 100644 --- a/actionpack/test/controller/polymorphic_routes_test.rb +++ b/actionpack/test/controller/polymorphic_routes_test.rb @@ -47,7 +47,7 @@ class PolymorphicRoutesTest < ActiveSupport::TestCase def test_with_record @article.save - expects(:article_url).with(@article) + expects(:article_url).with(:id => @article) polymorphic_url(@article) end @@ -70,19 +70,19 @@ class PolymorphicRoutesTest < ActiveSupport::TestCase def test_url_helper_prefixed_with_edit @article.save - expects(:edit_article_url).with(@article) + expects(:edit_article_url).with(:id => @article) 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') + expects(:edit_article_url).with(:id => @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') + expects(:article_url).with(:id => @article, :param1 => '10') polymorphic_url(@article, :param1 => '10') end @@ -95,13 +95,13 @@ class PolymorphicRoutesTest < ActiveSupport::TestCase def test_format_option @article.save - expects(:article_url).with(@article, :format => :pdf) + expects(:article_url).with(:id => @article, :format => :pdf) polymorphic_url(@article, :format => :pdf) end def test_format_option_with_url_options @article.save - expects(:article_url).with(@article, :format => :pdf, :param1 => '10') + expects(:article_url).with(:id => @article, :format => :pdf, :param1 => '10') polymorphic_url(@article, :format => :pdf, :param1 => '10') end @@ -113,12 +113,12 @@ class PolymorphicRoutesTest < ActiveSupport::TestCase def test_with_nested @response.save - expects(:article_response_url).with(@article, @response) + expects(:article_response_url).with(:article_id => @article, :id => @response) polymorphic_url([@article, @response]) end def test_with_nested_unsaved - expects(:article_responses_url).with(@article) + expects(:article_responses_url).with(:article_id => @article) polymorphic_url([@article, @response]) end @@ -134,65 +134,65 @@ class PolymorphicRoutesTest < ActiveSupport::TestCase def test_nested_unsaved_with_array_and_namespace @article.save - expects(:admin_article_url).with(@article) + expects(:admin_article_url).with(:id => @article) polymorphic_url([:admin, @article]) - expects(:admin_article_responses_url).with(@article) + expects(:admin_article_responses_url).with(:article_id => @article) polymorphic_url([:admin, @article, @response]) end def test_nested_with_array_and_namespace @response.save - expects(:admin_article_response_url).with(@article, @response) + expects(:admin_article_response_url).with(:article_id => @article, :id => @response) polymorphic_url([:admin, @article, @response]) # a ridiculously long named route tests correct ordering of namespaces and nesting: @tag = Tag.new @tag.save - expects(:site_admin_article_response_tag_url).with(@article, @response, @tag) + expects(:site_admin_article_response_tag_url).with(:article_id => @article, :response_id => @response, :id => @tag) polymorphic_url([:site, :admin, @article, @response, @tag]) end def test_nesting_with_array_ending_in_singleton_resource - expects(:article_response_url).with(@article) + expects(:article_response_url).with(:article_id => @article) polymorphic_url([@article, :response]) end def test_nesting_with_array_containing_singleton_resource @tag = Tag.new @tag.save - expects(:article_response_tag_url).with(@article, @tag) + expects(:article_response_tag_url).with(:article_id => @article, :id => @tag) polymorphic_url([@article, :response, @tag]) end def test_nesting_with_array_containing_namespace_and_singleton_resource @tag = Tag.new @tag.save - expects(:admin_article_response_tag_url).with(@article, @tag) + expects(:admin_article_response_tag_url).with(:article_id => @article, :id => @tag) polymorphic_url([:admin, @article, :response, @tag]) end def test_nesting_with_array_containing_singleton_resource_and_format @tag = Tag.new @tag.save - expects(:article_response_tag_url).with(@article, @tag, :format => :pdf) + expects(:article_response_tag_url).with(:article_id => @article, :id => @tag, :format => :pdf) polymorphic_url([@article, :response, @tag], :format => :pdf) end def test_nesting_with_array_containing_singleton_resource_and_format_option @tag = Tag.new @tag.save - expects(:article_response_tag_url).with(@article, @tag, :format => :pdf) + expects(:article_response_tag_url).with(:article_id => @article, :id => @tag, :format => :pdf) polymorphic_url([@article, :response, @tag], :format => :pdf) end def test_nesting_with_array_containing_nil - expects(:article_response_url).with(@article) + expects(:article_response_url).with(:article_id => @article) polymorphic_url([@article, nil, :response]) end def test_with_array_containing_single_object @article.save - expects(:article_url).with(@article) + expects(:article_url).with(:id => @article) polymorphic_url([nil, @article]) end @@ -204,7 +204,7 @@ class PolymorphicRoutesTest < ActiveSupport::TestCase # TODO: Needs to be updated to correctly know about whether the object is in a hash or not def xtest_with_hash - expects(:article_url).with(@article) + expects(:article_url).with(:id => @article) @article.save polymorphic_url(:id => @article) end @@ -215,7 +215,7 @@ class PolymorphicRoutesTest < ActiveSupport::TestCase end def test_polymorphic_path_does_not_modify_arguments - expects(:admin_article_responses_url).with(@article) + expects(:admin_article_responses_url).with(:article_id => @article) path = [:admin, @article, @response] assert_no_difference 'path.size' do polymorphic_url(path) @@ -225,7 +225,7 @@ class PolymorphicRoutesTest < ActiveSupport::TestCase # Tests for names where .plural.singular doesn't round-trip def test_with_irregular_plural_record @tax.save - expects(:taxis_url).with(@tax) + expects(:taxis_url).with(:id => @tax) polymorphic_url(@tax) end @@ -248,18 +248,18 @@ class PolymorphicRoutesTest < ActiveSupport::TestCase def test_irregular_plural_url_helper_prefixed_with_edit @tax.save - expects(:edit_taxis_url).with(@tax) + expects(:edit_taxis_url).with(:id => @tax) edit_polymorphic_url(@tax) end def test_with_nested_irregular_plurals @fax.save - expects(:taxis_faxis_url).with(@tax, @fax) + expects(:taxis_faxis_url).with(:taxis_id => @tax, :id => @fax) polymorphic_url([@tax, @fax]) end def test_with_nested_unsaved_irregular_plurals - expects(:taxis_faxes_url).with(@tax) + expects(:taxis_faxes_url).with(:taxis_id => @tax) polymorphic_url([@tax, @fax]) end @@ -274,13 +274,13 @@ class PolymorphicRoutesTest < ActiveSupport::TestCase end def test_nesting_with_irregular_plurals_and_array_ending_in_singleton_resource - expects(:taxis_faxis_url).with(@tax) + expects(:taxis_faxis_url).with(:taxis_id => @tax) polymorphic_url([@tax, :faxis]) end def test_with_array_containing_single_irregular_plural_object @tax.save - expects(:taxis_url).with(@tax) + expects(:taxis_url).with(:id => @tax) polymorphic_url([nil, @tax]) end @@ -294,4 +294,10 @@ class PolymorphicRoutesTest < ActiveSupport::TestCase expects(:new_article_url).with() polymorphic_url([:new, :article]) end + + def test_with_path_prefix + @article.save + expects(:article_url).with(:id => @article, :locale => "de") + polymorphic_url(@article, :locale => "de") + end end diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index fff0262..052eeab 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -1398,22 +1398,32 @@ class FormHelperTest < ActionView::TestCase protected def comments_path(post) + post = post[:post_id] if post.is_a?(Hash) "/posts/#{post.id}/comments" end alias_method :post_comments_path, :comments_path - def comment_path(post, comment) + def comment_path(post, comment = nil) + if post.is_a?(Hash) + comment = post[:id] + post = post[:post_id] + end "/posts/#{post.id}/comments/#{comment.id}" end alias_method :post_comment_path, :comment_path def admin_comments_path(post) + post = post[:post_id] if post.is_a?(Hash) "/admin/posts/#{post.id}/comments" end alias_method :admin_post_comments_path, :admin_comments_path - def admin_comment_path(post, comment) - "/admin/posts/#{post.id}/comments/#{comment.id}" + def admin_comment_path(post, comment = nil) + if post.is_a?(Hash) + comment = post[:id] + post = post[:post_id] + end + "/admin/posts/#{post.id}/comments/#{comment.id}" end alias_method :admin_post_comment_path, :admin_comment_path @@ -1422,6 +1432,7 @@ class FormHelperTest < ActionView::TestCase end def post_path(post) + post = post[:id] if post.is_a?(Hash) "/posts/#{post.id}" end diff --git a/actionpack/test/template/prototype_helper_test.rb b/actionpack/test/template/prototype_helper_test.rb index 3bcf532..5a9ad23 100644 --- a/actionpack/test/template/prototype_helper_test.rb +++ b/actionpack/test/template/prototype_helper_test.rb @@ -281,6 +281,7 @@ class PrototypeHelperTest < PrototypeHelperBaseTest protected def author_path(record) + record = record[:id] if record.is_a?(Hash) "/authors/#{record.id}" end @@ -289,10 +290,15 @@ class PrototypeHelperTest < PrototypeHelperBaseTest end def author_articles_path(author) + author = author[:author_id] if author.is_a?(Hash) "/authors/#{author.id}/articles" end - def author_article_path(author, article) + def author_article_path(author, article = nil) + if author.is_a?(Hash) + article = author[:id] + author = author[:author_id] + end "/authors/#{author.id}/articles/#{article.id}" end end diff --git a/actionpack/test/template/test_test.rb b/actionpack/test/template/test_test.rb index ccd299f..d2ff6a9 100644 --- a/actionpack/test/template/test_test.rb +++ b/actionpack/test/template/test_test.rb @@ -40,7 +40,7 @@ class PeopleHelperTest < ActionView::TestCase def test_link_to_person person = mock(:name => "David") - expects(:mocha_mock_path).with(person).returns("/people/1") + expects(:mocha_mock_path).with(:id => person).returns("/people/1") assert_equal 'David', link_to_person(person) end end -- 1.6.3.3