From 33225c6db365407b63b5da324576c7580a488b59 Mon Sep 17 00:00:00 2001 From: Tammer Saleh Date: Fri, 20 Jun 2008 15:21:04 -0400 Subject: [PATCH] Fixed polymorphic_url to be able to handle singleton resources. Example usage: polymorphic_url([:admin, @user, :blog, @post]) # => admin_user_blog_post_url(@user, @post) --- .../lib/action_controller/polymorphic_routes.rb | 30 +++++++++++++------ .../test/controller/polymorphic_routes_test.rb | 28 ++++++++++++++++++ 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/actionpack/lib/action_controller/polymorphic_routes.rb b/actionpack/lib/action_controller/polymorphic_routes.rb index 509fa6a..bba7f6b 100644 --- a/actionpack/lib/action_controller/polymorphic_routes.rb +++ b/actionpack/lib/action_controller/polymorphic_routes.rb @@ -83,7 +83,6 @@ module ActionController else [ record_or_hash_or_array ] end - args << format if format inflection = case @@ -96,8 +95,14 @@ module ActionController else :singular end + + args.delete_if {|arg| arg.is_a?(Symbol) || arg.is_a?(String)} + args << format if format named_route = build_named_route_call(record_or_hash_or_array, namespace, inflection, options) + # we need to remove symbols from *args + # puts([named_route, *record_or_hash_or_array].inspect) + # puts([named_route, *args].inspect) send!(named_route, *args) end @@ -136,7 +141,11 @@ module ActionController else record = records.pop route = records.inject("") do |string, parent| - string << "#{RecordIdentifier.send!("singular_class_name", parent)}_" + if parent.is_a?(Symbol) || parent.is_a?(String) + string << "#{parent}_" + else + string << "#{RecordIdentifier.send!("singular_class_name", parent)}_" + end end end @@ -163,16 +172,17 @@ module ActionController end end + # [:one, :two, @three, :four, @five] + # one_two : [@three, :four, @five] def extract_namespace(record_or_hash_or_array) - returning "" do |namespace| - if record_or_hash_or_array.is_a?(Array) - record_or_hash_or_array.delete_if do |record_or_namespace| - if record_or_namespace.is_a?(String) || record_or_namespace.is_a?(Symbol) - namespace << "#{record_or_namespace}_" - end - end - end + return "" unless record_or_hash_or_array.is_a?(Array) + + namespace_keys = [] + while (key = record_or_hash_or_array.first) && key.is_a?(String) || key.is_a?(Symbol) + namespace_keys << record_or_hash_or_array.shift end + + namespace_keys.map {|k| "#{k}_"}.join end end end diff --git a/actionpack/test/controller/polymorphic_routes_test.rb b/actionpack/test/controller/polymorphic_routes_test.rb index 4ec0d3c..7558393 100644 --- a/actionpack/test/controller/polymorphic_routes_test.rb +++ b/actionpack/test/controller/polymorphic_routes_test.rb @@ -118,6 +118,34 @@ uses_mocha 'polymorphic URL helpers' do polymorphic_url([:site, :admin, @article, @response, @tag]) end + def test_nesting_with_array_containing_singleton_resource + @tag = Tag.new + @tag.save + expects(:article_response_tag_url).with(@article, @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) + polymorphic_url([:admin, @article, :response, @tag]) + end + + def test_nesting_with_array_containing_singleton_resource_and_format + @tag = Tag.new + @tag.save + expects(:formatted_article_response_tag_url).with(@article, @tag, :pdf) + formatted_polymorphic_url([@article, :response, @tag, :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, :pdf) + polymorphic_url([@article, :response, @tag], :format => :pdf) + end + # 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) -- 1.5.5 From d421061d5e4135b5b6e9e6d4619470bb88443d83 Mon Sep 17 00:00:00 2001 From: Tammer Saleh Date: Fri, 20 Jun 2008 16:14:03 -0400 Subject: [PATCH] documentation improvements --- .../lib/action_controller/polymorphic_routes.rb | 11 +++++------ 1 files changed, 5 insertions(+), 6 deletions(-) diff --git a/actionpack/lib/action_controller/polymorphic_routes.rb b/actionpack/lib/action_controller/polymorphic_routes.rb index bba7f6b..5b4b338 100644 --- a/actionpack/lib/action_controller/polymorphic_routes.rb +++ b/actionpack/lib/action_controller/polymorphic_routes.rb @@ -48,6 +48,9 @@ module ActionController # # # calls post_url(post) # polymorphic_url(post) # => "http://example.com/posts/1" + # 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" # # ==== Options # @@ -83,7 +86,6 @@ module ActionController else [ record_or_hash_or_array ] end - inflection = case when options[:action].to_s == "new" @@ -100,9 +102,6 @@ module ActionController args << format if format named_route = build_named_route_call(record_or_hash_or_array, namespace, inflection, options) - # we need to remove symbols from *args - # puts([named_route, *record_or_hash_or_array].inspect) - # puts([named_route, *args].inspect) send!(named_route, *args) end @@ -172,8 +171,8 @@ module ActionController end end - # [:one, :two, @three, :four, @five] - # one_two : [@three, :four, @five] + # Remove the first symbols from the array and return the url prefix + # implied by those symbols. def extract_namespace(record_or_hash_or_array) return "" unless record_or_hash_or_array.is_a?(Array) -- 1.5.5