1 require 'abstract_unit'
6 def new_record
?; @id.nil? end
8 model
= self.class.name
.downcase
9 @id.nil? ? "new #{model}" : "#{model} ##{@id}"
13 class Response
< Article
18 def response_id
; 1 end
24 def new_record
?; @id.nil? end
26 model
= self.class.name
.downcase
27 @id.nil? ? "new #{model}" : "#{model} ##{@id}"
35 # TODO: test nested models
36 class Response
::Nested < Response
; end
38 class PolymorphicRoutesTest
< ActiveSupport
::TestCase
39 include ActionController
::PolymorphicRoutes
42 @article = Article
.new
43 @response = Response
.new
50 expects(:article_url).with(@article)
51 polymorphic_url(@article)
54 def test_with_new_record
55 expects(:articles_url).with()
56 @article.expects(:new_record?).returns(true)
57 polymorphic_url(@article)
60 def test_with_record_and_action
61 expects(:new_article_url).with()
62 @article.expects(:new_record?).never
63 polymorphic_url(@article, :action => 'new')
66 def test_url_helper_prefixed_with_new
67 expects(:new_article_url).with()
68 new_polymorphic_url(@article)
71 def test_url_helper_prefixed_with_edit
73 expects(:edit_article_url).with(@article)
74 edit_polymorphic_url(@article)
77 def test_url_helper_prefixed_with_edit_with_url_options
79 expects(:edit_article_url).with(@article, :param1 => '10')
80 edit_polymorphic_url(@article, :param1 => '10')
83 def test_url_helper_with_url_options
85 expects(:article_url).with(@article, :param1 => '10')
86 polymorphic_url(@article, :param1 => '10')
89 def test_formatted_url_helper_is_deprecated
90 expects(:articles_url).with(:format => :pdf)
92 formatted_polymorphic_url([@article, :pdf])
96 def test_format_option
98 expects(:article_url).with(@article, :format => :pdf)
99 polymorphic_url(@article, :format => :pdf)
102 def test_format_option_with_url_options
104 expects(:article_url).with(@article, :format => :pdf, :param1 => '10')
105 polymorphic_url(@article, :format => :pdf, :param1 => '10')
108 def test_id_and_format_option
110 expects(:article_url).with(:id => @article, :format => :pdf)
111 polymorphic_url(:id => @article, :format => :pdf)
116 expects(:article_response_url).with(@article, @response)
117 polymorphic_url([@article, @response])
120 def test_with_nested_unsaved
121 expects(:article_responses_url).with(@article)
122 polymorphic_url([@article, @response])
125 def test_new_with_array_and_namespace
126 expects(:new_admin_article_url).with()
127 polymorphic_url([:admin, @article], :action => 'new')
130 def test_unsaved_with_array_and_namespace
131 expects(:admin_articles_url).with()
132 polymorphic_url([:admin, @article])
135 def test_nested_unsaved_with_array_and_namespace
137 expects(:admin_article_url).with(@article)
138 polymorphic_url([:admin, @article])
139 expects(:admin_article_responses_url).with(@article)
140 polymorphic_url([:admin, @article, @response])
143 def test_nested_with_array_and_namespace
145 expects(:admin_article_response_url).with(@article, @response)
146 polymorphic_url([:admin, @article, @response])
148 # a ridiculously long named route tests correct ordering of namespaces and nesting:
151 expects(:site_admin_article_response_tag_url).with(@article, @response, @tag)
152 polymorphic_url([:site, :admin, @article, @response, @tag])
155 def test_nesting_with_array_ending_in_singleton_resource
156 expects(:article_response_url).with(@article)
157 polymorphic_url([@article, :response])
160 def test_nesting_with_array_containing_singleton_resource
163 expects(:article_response_tag_url).with(@article, @tag)
164 polymorphic_url([@article, :response, @tag])
167 def test_nesting_with_array_containing_namespace_and_singleton_resource
170 expects(:admin_article_response_tag_url).with(@article, @tag)
171 polymorphic_url([:admin, @article, :response, @tag])
174 def test_nesting_with_array_containing_singleton_resource_and_format
177 expects(:article_response_tag_url).with(@article, @tag, :format => :pdf)
178 polymorphic_url([@article, :response, @tag], :format => :pdf)
181 def test_nesting_with_array_containing_singleton_resource_and_format_option
184 expects(:article_response_tag_url).with(@article, @tag, :format => :pdf)
185 polymorphic_url([@article, :response, @tag], :format => :pdf)
188 def test_nesting_with_array_containing_nil
189 expects(:article_response_url).with(@article)
190 polymorphic_url([@article, nil, :response])
193 def test_with_array_containing_single_object
195 expects(:article_url).with(@article)
196 polymorphic_url([nil, @article])
199 def test_with_array_containing_single_name
201 expects(:articles_url)
202 polymorphic_url([:articles])
205 # TODO: Needs to be updated to correctly know about whether the object is in a hash or not
207 expects(:article_url).with(@article)
209 polymorphic_url(:id => @article)
212 def test_polymorphic_path_accepts_options
213 expects(:new_article_path).with()
214 polymorphic_path(@article, :action => :new)
217 def test_polymorphic_path_does_not_modify_arguments
218 expects(:admin_article_responses_url).with(@article)
219 path
= [:admin, @article, @response]
220 assert_no_difference
'path.size' do
221 polymorphic_url(path
)
225 # Tests for names where .plural.singular doesn't round-trip
226 def test_with_irregular_plural_record
228 expects(:taxis_url).with(@tax)
229 polymorphic_url(@tax)
232 def test_with_irregular_plural_new_record
233 expects(:taxes_url).with()
234 @tax.expects(:new_record?).returns(true)
235 polymorphic_url(@tax)
238 def test_with_irregular_plural_record_and_action
239 expects(:new_taxis_url).with()
240 @tax.expects(:new_record?).never
241 polymorphic_url(@tax, :action => 'new')
244 def test_irregular_plural_url_helper_prefixed_with_new
245 expects(:new_taxis_url).with()
246 new_polymorphic_url(@tax)
249 def test_irregular_plural_url_helper_prefixed_with_edit
251 expects(:edit_taxis_url).with(@tax)
252 edit_polymorphic_url(@tax)
255 def test_with_nested_irregular_plurals
257 expects(:taxis_faxis_url).with(@tax, @fax)
258 polymorphic_url([@tax, @fax])
261 def test_with_nested_unsaved_irregular_plurals
262 expects(:taxis_faxes_url).with(@tax)
263 polymorphic_url([@tax, @fax])
266 def test_new_with_irregular_plural_array_and_namespace
267 expects(:new_admin_taxis_url).with()
268 polymorphic_url([:admin, @tax], :action => 'new')
271 def test_unsaved_with_irregular_plural_array_and_namespace
272 expects(:admin_taxes_url).with()
273 polymorphic_url([:admin, @tax])
276 def test_nesting_with_irregular_plurals_and_array_ending_in_singleton_resource
277 expects(:taxis_faxis_url).with(@tax)
278 polymorphic_url([@tax, :faxis])
281 def test_with_array_containing_single_irregular_plural_object
283 expects(:taxis_url).with(@tax)
284 polymorphic_url([nil, @tax])
287 def test_with_array_containing_single_name_irregular_plural
290 polymorphic_url([:taxes])