public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
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)

[#461 state:resolved]
tsaleh (author)
Fri Jun 20 12:21:04 -0700 2008
jeremy (committer)
Sun Jun 22 18:58:47 -0700 2008
commit  bb6e8eea5a8190aaab67da0a7efedb3bb3d9fccb
tree    099fea2c0fd42c87044889deae59e652e9a09f00
parent  a210f503619dd27dc9575bcae1df596fd2387563
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *Edge*
0
 
0
+* Fix polymorphic_url with singleton resources.  #461 [Tammer Saleh]
0
+
0
 * Replaced TemplateFinder abstraction with ViewLoadPaths [Josh Peek]
0
 
0
 * Added block-call style to link_to [Sam Stephenson/DHH]. Example:
...
48
49
50
 
 
 
51
52
53
...
83
84
85
86
87
88
89
90
...
96
97
98
 
 
 
99
100
101
...
136
137
138
139
 
 
 
 
 
140
141
142
143
 
 
 
 
 
144
145
146
...
163
164
165
 
 
166
167
168
169
170
171
172
173
174
 
 
 
 
 
175
 
 
176
177
178
...
48
49
50
51
52
53
54
55
56
...
86
87
88
 
 
89
90
91
...
97
98
99
100
101
102
103
104
105
...
140
141
142
 
143
144
145
146
147
148
149
150
 
151
152
153
154
155
156
157
158
...
175
176
177
178
179
180
 
 
 
 
 
 
 
 
181
182
183
184
185
186
187
188
189
190
191
0
@@ -48,6 +48,9 @@ module ActionController
0
     #
0
     #   # calls post_url(post)
0
     #   polymorphic_url(post) # => "http://example.com/posts/1"
0
+    #   polymorphic_url([blog, post]) # => "http://example.com/blogs/1/posts/1"
0
+    #   polymorphic_url([:admin, blog, post]) # => "http://example.com/admin/blogs/1/posts/1"
0
+    #   polymorphic_url([user, :blog, post]) # => "http://example.com/users/1/blog/posts/1"
0
     #
0
     # ==== Options
0
     #
0
@@ -83,8 +86,6 @@ module ActionController
0
         else        [ record_or_hash_or_array ]
0
       end
0
 
0
-      args << format if format
0
-
0
       inflection =
0
         case
0
         when options[:action].to_s == "new"
0
@@ -96,6 +97,9 @@ module ActionController
0
         else
0
           :singular
0
         end
0
+
0
+      args.delete_if {|arg| arg.is_a?(Symbol) || arg.is_a?(String)}
0
+      args << format if format
0
       
0
       named_route = build_named_route_call(record_or_hash_or_array, namespace, inflection, options)
0
       send!(named_route, *args)
0
@@ -136,11 +140,19 @@ module ActionController
0
         else
0
           record = records.pop
0
           route = records.inject("") do |string, parent|
0
-            string << "#{RecordIdentifier.send!("singular_class_name", parent)}_"
0
+            if parent.is_a?(Symbol) || parent.is_a?(String)
0
+              string << "#{parent}_"
0
+            else
0
+              string << "#{RecordIdentifier.send!("singular_class_name", parent)}_"
0
+            end
0
           end
0
         end
0
 
0
-        route << "#{RecordIdentifier.send!("#{inflection}_class_name", record)}_"
0
+        if record.is_a?(Symbol) || record.is_a?(String)
0
+          route << "#{record}_"
0
+        else
0
+          route << "#{RecordIdentifier.send!("#{inflection}_class_name", record)}_"
0
+        end
0
 
0
         action_prefix(options) + namespace + route + routing_type(options).to_s
0
       end
0
@@ -163,16 +175,17 @@ module ActionController
0
         end
0
       end
0
       
0
+      # Remove the first symbols from the array and return the url prefix
0
+      # implied by those symbols.
0
       def extract_namespace(record_or_hash_or_array)
0
-        returning "" do |namespace|
0
-          if record_or_hash_or_array.is_a?(Array)
0
-            record_or_hash_or_array.delete_if do |record_or_namespace|
0
-              if record_or_namespace.is_a?(String) || record_or_namespace.is_a?(Symbol)
0
-                namespace << "#{record_or_namespace}_"
0
-              end
0
-            end
0
-          end  
0
+        return "" unless record_or_hash_or_array.is_a?(Array)
0
+
0
+        namespace_keys = []
0
+        while (key = record_or_hash_or_array.first) && key.is_a?(String) || key.is_a?(Symbol)
0
+          namespace_keys << record_or_hash_or_array.shift
0
         end
0
+
0
+        namespace_keys.map {|k| "#{k}_"}.join
0
       end
0
   end
0
 end
...
118
119
120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
122
123
...
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
0
@@ -118,6 +118,39 @@ uses_mocha 'polymorphic URL helpers' do
0
       polymorphic_url([:site, :admin, @article, @response, @tag])
0
     end
0
 
0
+    def test_nesting_with_array_ending_in_singleton_resource
0
+      expects(:article_response_url).with(@article)
0
+      polymorphic_url([@article, :response])
0
+    end
0
+
0
+    def test_nesting_with_array_containing_singleton_resource
0
+      @tag = Tag.new
0
+      @tag.save
0
+      expects(:article_response_tag_url).with(@article, @tag)
0
+      polymorphic_url([@article, :response, @tag])
0
+    end
0
+
0
+    def test_nesting_with_array_containing_namespace_and_singleton_resource
0
+      @tag = Tag.new
0
+      @tag.save
0
+      expects(:admin_article_response_tag_url).with(@article, @tag)
0
+      polymorphic_url([:admin, @article, :response, @tag])
0
+    end
0
+
0
+    def test_nesting_with_array_containing_singleton_resource_and_format
0
+      @tag = Tag.new
0
+      @tag.save
0
+      expects(:formatted_article_response_tag_url).with(@article, @tag, :pdf)
0
+      formatted_polymorphic_url([@article, :response, @tag, :pdf])
0
+    end
0
+
0
+    def test_nesting_with_array_containing_singleton_resource_and_format_option
0
+      @tag = Tag.new
0
+      @tag.save
0
+      expects(:article_response_tag_url).with(@article, @tag, :pdf)
0
+      polymorphic_url([@article, :response, @tag], :format => :pdf)
0
+    end
0
+
0
     # TODO: Needs to be updated to correctly know about whether the object is in a hash or not
0
     def xtest_with_hash
0
       expects(:article_url).with(@article)

Comments