Skip to content
This repository
Browse code

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

0 notes on commit 98fb161

Please sign in to comment.
Something went wrong with that request. Please try again.