public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Allow polymorphic_url helper to take url options. [#880 state:resolved]

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.

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Tarmo Tänav (author)
Thu Aug 21 10:26:05 -0700 2008
lifo (committer)
Thu Aug 21 12:26:00 -0700 2008
commit  98fb161dbb13feb18165468aedf1581d5c2305f4
tree    100a3192923dea387b59ef1b9364d3d940753b33
parent  654c41255d22b2767b943dbe970d8b24f2a1387b
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *Edge*
0
 
0
+* Allow polymorphic_url helper to take url options. #880 [Tarmo Tänav]
0
+
0
 * Switched integration test runner to use Rack processor instead of CGI [Josh Peek]
0
 
0
 * Made AbstractRequest.if_modified_sense return nil if the header could not be parsed [Jamis Buck]
...
102
103
104
 
 
 
 
 
 
105
106
107
...
114
115
116
117
118
 
 
119
120
121
122
 
 
123
124
125
126
127
128
129
 
130
131
132
...
102
103
104
105
106
107
108
109
110
111
112
113
...
120
121
122
 
 
123
124
125
126
 
 
127
128
129
130
131
132
133
134
 
135
136
137
138
0
@@ -102,6 +102,12 @@ module ActionController
0
       args << format if format
0
       
0
       named_route = build_named_route_call(record_or_hash_or_array, namespace, inflection, options)
0
+
0
+      url_options = options.except(:action, :routing_type, :format)
0
+      unless url_options.empty?
0
+        args.last.kind_of?(Hash) ? args.last.merge!(url_options) : args << url_options
0
+      end
0
+
0
       send!(named_route, *args)
0
     end
0
 
0
@@ -114,19 +120,19 @@ module ActionController
0
 
0
     %w(edit new formatted).each do |action|
0
       module_eval <<-EOT, __FILE__, __LINE__
0
-        def #{action}_polymorphic_url(record_or_hash)
0
-          polymorphic_url(record_or_hash, :action => "#{action}")
0
+        def #{action}_polymorphic_url(record_or_hash, options = {})
0
+          polymorphic_url(record_or_hash, options.merge(:action => "#{action}"))
0
         end
0
 
0
-        def #{action}_polymorphic_path(record_or_hash)
0
-          polymorphic_url(record_or_hash, :action => "#{action}", :routing_type => :path)
0
+        def #{action}_polymorphic_path(record_or_hash, options = {})
0
+          polymorphic_url(record_or_hash, options.merge(:action => "#{action}", :routing_type => :path))
0
         end
0
       EOT
0
     end
0
 
0
     private
0
       def action_prefix(options)
0
-        options[:action] ? "#{options[:action]}_" : ""
0
+        options[:action] ? "#{options[:action]}_" : options[:format] ? "formatted_" : ""
0
       end
0
 
0
       def routing_type(options)
...
60
61
62
 
 
 
 
 
 
 
 
 
 
 
 
63
64
65
...
67
68
69
70
 
71
72
73
 
 
 
 
 
 
74
75
76
...
147
148
149
150
 
151
152
153
...
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
...
79
80
81
 
82
83
84
85
86
87
88
89
90
91
92
93
94
...
165
166
167
 
168
169
170
171
0
@@ -60,6 +60,18 @@ uses_mocha 'polymorphic URL helpers' do
0
       edit_polymorphic_url(@article)
0
     end
0
 
0
+    def test_url_helper_prefixed_with_edit_with_url_options
0
+      @article.save
0
+      expects(:edit_article_url).with(@article, :param1 => '10')
0
+      edit_polymorphic_url(@article, :param1 => '10')
0
+    end
0
+
0
+    def test_url_helper_with_url_options
0
+      @article.save
0
+      expects(:article_url).with(@article, :param1 => '10')
0
+      polymorphic_url(@article, :param1 => '10')
0
+    end
0
+
0
     def test_formatted_url_helper
0
       expects(:formatted_article_url).with(@article, :pdf)
0
       formatted_polymorphic_url([@article, :pdf])
0
@@ -67,10 +79,16 @@ uses_mocha 'polymorphic URL helpers' do
0
 
0
     def test_format_option
0
       @article.save
0
-      expects(:article_url).with(@article, :pdf)
0
+      expects(:formatted_article_url).with(@article, :pdf)
0
       polymorphic_url(@article, :format => :pdf)
0
     end
0
 
0
+    def test_format_option_with_url_options
0
+      @article.save
0
+      expects(:formatted_article_url).with(@article, :pdf, :param1 => '10')
0
+      polymorphic_url(@article, :format => :pdf, :param1 => '10')
0
+    end
0
+
0
     def test_id_and_format_option
0
       @article.save
0
       expects(:article_url).with(:id => @article, :format => :pdf)
0
@@ -147,7 +165,7 @@ uses_mocha 'polymorphic URL helpers' do
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
+      expects(:formatted_article_response_tag_url).with(@article, @tag, :pdf)
0
       polymorphic_url([@article, :response, @tag], :format => :pdf)
0
     end
0
 

Comments