1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
require 'active_record_unit'
require 'fixtures/project'
class Task < ActiveRecord::Base
set_table_name 'projects'
end
class Step < ActiveRecord::Base
set_table_name 'projects'
end
class Bid < ActiveRecord::Base
set_table_name 'projects'
end
class Tax < ActiveRecord::Base
set_table_name 'projects'
end
class Fax < ActiveRecord::Base
set_table_name 'projects'
end
class Series < ActiveRecord::Base
set_table_name 'projects'
end
class PolymorphicRoutesTest < ActionController::TestCase
include ActionController::UrlWriter
self.default_url_options[:host] = 'example.com'
def setup
@project = Project.new
@task = Task.new
@step = Step.new
@bid = Bid.new
@tax = Tax.new
@fax = Fax.new
@series = Series.new
end
def test_with_record
with_test_routes do
@project.save
assert_equal "http://example.com/projects/#{@project.id}", polymorphic_url(@project)
end
end
def test_with_new_record
with_test_routes do
assert_equal "http://example.com/projects", polymorphic_url(@project)
end
end
def test_with_record_and_action
with_test_routes do
assert_equal "http://example.com/projects/new", polymorphic_url(@project, :action => 'new')
end
end
def test_url_helper_prefixed_with_new
with_test_routes do
assert_equal "http://example.com/projects/new", new_polymorphic_url(@project)
end
end
def test_url_helper_prefixed_with_edit
with_test_routes do
@project.save
assert_equal "http://example.com/projects/#{@project.id}/edit", edit_polymorphic_url(@project)
end
end
def test_url_helper_prefixed_with_edit_with_url_options
with_test_routes do
@project.save
assert_equal "http://example.com/projects/#{@project.id}/edit?param1=10", edit_polymorphic_url(@project, :param1 => '10')
end
end
def test_url_helper_with_url_options
with_test_routes do
@project.save
assert_equal "http://example.com/projects/#{@project.id}?param1=10", polymorphic_url(@project, :param1 => '10')
end
end
def test_formatted_url_helper_is_deprecated
with_test_routes do
assert_deprecated do
formatted_polymorphic_url([@project, :pdf])
end
end
end
def test_format_option
with_test_routes do
@project.save
assert_equal "http://example.com/projects/#{@project.id}.pdf", polymorphic_url(@project, :format => :pdf)
end
end
def test_format_option_with_url_options
with_test_routes do
@project.save
assert_equal "http://example.com/projects/#{@project.id}.pdf?param1=10", polymorphic_url(@project, :format => :pdf, :param1 => '10')
end
end
def test_id_and_format_option
with_test_routes do
@project.save
assert_equal "http://example.com/projects/#{@project.id}.pdf", polymorphic_url(:id => @project, :format => :pdf)
end
end
def test_with_nested
with_test_routes do
@project.save
@task.save
assert_equal "http://example.com/projects/#{@project.id}/tasks/#{@task.id}", polymorphic_url([@project, @task])
end
end
def test_with_nested_unsaved
with_test_routes do
@project.save
assert_equal "http://example.com/projects/#{@project.id}/tasks", polymorphic_url([@project, @task])
end
end
def test_new_with_array_and_namespace
with_admin_test_routes do
assert_equal "http://example.com/admin/projects/new", polymorphic_url([:admin, @project], :action => 'new')
end
end
def test_unsaved_with_array_and_namespace
with_admin_test_routes do
assert_equal "http://example.com/admin/projects", polymorphic_url([:admin, @project])
end
end
def test_nested_unsaved_with_array_and_namespace
with_admin_test_routes do
@project.save
assert_equal "http://example.com/admin/projects/#{@project.id}/tasks", polymorphic_url([:admin, @project, @task])
end
end
def test_nested_with_array_and_namespace
with_admin_test_routes do
@project.save
@task.save
assert_equal "http://example.com/admin/projects/#{@project.id}/tasks/#{@task.id}", polymorphic_url([:admin, @project, @task])
end
end
def test_ordering_of_nesting_and_namespace
with_admin_and_site_test_routes do
@project.save
@task.save
@step.save
assert_equal "http://example.com/admin/projects/#{@project.id}/site/tasks/#{@task.id}/steps/#{@step.id}", polymorphic_url([:admin, @project, :site, @task, @step])
end
end
def test_nesting_with_array_ending_in_singleton_resource
with_test_routes do
@project.save
assert_equal "http://example.com/projects/#{@project.id}/bid", polymorphic_url([@project, :bid])
end
end
def test_nesting_with_array_containing_singleton_resource
with_test_routes do
@project.save
@task.save
assert_equal "http://example.com/projects/#{@project.id}/bid/tasks/#{@task.id}", polymorphic_url([@project, :bid, @task])
end
end
def test_nesting_with_array_containing_singleton_resource_and_format
with_test_routes do
@project.save
@task.save
assert_equal "http://example.com/projects/#{@project.id}/bid/tasks/#{@task.id}.pdf", polymorphic_url([@project, :bid, @task], :format => :pdf)
end
end
def test_nesting_with_array_containing_namespace_and_singleton_resource
with_admin_test_routes do
@project.save
@task.save
assert_equal "http://example.com/admin/projects/#{@project.id}/bid/tasks/#{@task.id}", polymorphic_url([:admin, @project, :bid, @task])
end
end
def test_nesting_with_array_containing_nil
with_test_routes do
@project.save
assert_equal "http://example.com/projects/#{@project.id}/bid", polymorphic_url([@project, nil, :bid])
end
end
def test_with_array_containing_single_object
with_test_routes do
@project.save
assert_equal "http://example.com/projects/#{@project.id}", polymorphic_url([nil, @project])
end
end
def test_with_array_containing_single_name
with_test_routes do
@project.save
assert_equal "http://example.com/projects", polymorphic_url([:projects])
end
end
def test_with_hash
with_test_routes do
@project.save
assert_equal "http://example.com/projects/#{@project.id}", polymorphic_url(:id => @project)
end
end
def test_polymorphic_path_accepts_options
with_test_routes do
assert_equal "/projects/new", polymorphic_path(@project, :action => 'new')
end
end
def test_polymorphic_path_does_not_modify_arguments
with_admin_test_routes do
@project.save
@task.save
object_array = [:admin, @project, @task]
assert_no_difference 'object_array.size' do
polymorphic_url(object_array)
end
end
end
# Tests for names where .plural.singular doesn't round-trip
def test_with_irregular_plural_record
with_test_routes do
@tax.save
assert_equal "http://example.com/taxes/#{@tax.id}", polymorphic_url(@tax)
end
end
def test_with_irregular_plural_new_record
with_test_routes do
assert_equal "http://example.com/taxes", polymorphic_url(@tax)
end
end
def test_with_irregular_plural_record_and_action
with_test_routes do
assert_equal "http://example.com/taxes/new", polymorphic_url(@tax, :action => 'new')
end
end
def test_irregular_plural_url_helper_prefixed_with_new
with_test_routes do
assert_equal "http://example.com/taxes/new", new_polymorphic_url(@tax)
end
end
def test_irregular_plural_url_helper_prefixed_with_edit
with_test_routes do
@tax.save
assert_equal "http://example.com/taxes/#{@tax.id}/edit", edit_polymorphic_url(@tax)
end
end
def test_with_nested_irregular_plurals
with_test_routes do
@tax.save
@fax.save
assert_equal "http://example.com/taxes/#{@tax.id}/faxes/#{@fax.id}", polymorphic_url([@tax, @fax])
end
end
def test_with_nested_unsaved_irregular_plurals
with_test_routes do
@tax.save
assert_equal "http://example.com/taxes/#{@tax.id}/faxes", polymorphic_url([@tax, @fax])
end
end
def test_new_with_irregular_plural_array_and_namespace
with_admin_test_routes do
assert_equal "http://example.com/admin/taxes/new", polymorphic_url([:admin, @tax], :action => 'new')
end
end
def test_unsaved_with_irregular_plural_array_and_namespace
with_admin_test_routes do
assert_equal "http://example.com/admin/taxes", polymorphic_url([:admin, @tax])
end
end
def test_nesting_with_irregular_plurals_and_array_ending_in_singleton_resource
with_test_routes do
@tax.save
assert_equal "http://example.com/taxes/#{@tax.id}/bid", polymorphic_url([@tax, :bid])
end
end
def test_with_array_containing_single_irregular_plural_object
with_test_routes do
@tax.save
assert_equal "http://example.com/taxes/#{@tax.id}", polymorphic_url([nil, @tax])
end
end
def test_with_array_containing_single_name_irregular_plural
with_test_routes do
@tax.save
assert_equal "http://example.com/taxes", polymorphic_url([:taxes])
end
end
# Tests for uncountable names
def test_uncountable_resource
with_test_routes do
@series.save
assert_equal "http://example.com/series/#{@series.id}", polymorphic_url(@series)
end
end
def with_test_routes(options = {})
with_routing do |set|
set.draw do |map|
map.resources :projects do |projects|
projects.resources :tasks
projects.resource :bid do |bid|
bid.resources :tasks
end
end
map.resources :taxes do |taxes|
taxes.resources :faxes
taxes.resource :bid
end
map.resources :series
end
ActionController::Routing::Routes.install_helpers(self.class)
yield
end
end
def with_admin_test_routes(options = {})
with_routing do |set|
set.draw do |map|
map.namespace :admin do |admin|
admin.resources :projects do |projects|
projects.resources :tasks
projects.resource :bid do |bid|
bid.resources :tasks
end
end
admin.resources :taxes do |taxes|
taxes.resources :faxes
end
admin.resources :series
end
end
ActionController::Routing::Routes.install_helpers(self.class)
yield
end
end
def with_admin_and_site_test_routes(options = {})
with_routing do |set|
set.draw do |map|
map.namespace :admin do |admin|
admin.resources :projects do |projects|
projects.namespace :site do |site|
site.resources :tasks do |tasks|
tasks.resources :steps
end
end
end
end
end
ActionController::Routing::Routes.install_helpers(self.class)
yield
end
end
end
|