polymorphic_routes_test.rb 19.4 KB
Newer Older
1 2
require "active_record_unit"
require "fixtures/project"
3 4

class Task < ActiveRecord::Base
5
  self.table_name = "projects"
6 7 8
end

class Step < ActiveRecord::Base
9
  self.table_name = "projects"
10 11 12
end

class Bid < ActiveRecord::Base
13
  self.table_name = "projects"
14 15 16
end

class Tax < ActiveRecord::Base
17
  self.table_name = "projects"
18 19 20
end

class Fax < ActiveRecord::Base
21
  self.table_name = "projects"
22 23 24
end

class Series < ActiveRecord::Base
25
  self.table_name = "projects"
26 27
end

28
class ModelDelegator
29 30 31 32 33 34
  def to_model
    ModelDelegate.new
  end
end

class ModelDelegate
35 36 37 38
  def persisted?
    true
  end

39 40
  def model_name
    ActiveModel::Name.new(self.class)
41 42 43
  end

  def to_param
44
    "overridden"
45 46 47
  end
end

48
module Weblog
49
  class Post < ActiveRecord::Base
50
    self.table_name = "projects"
51 52 53
  end

  class Blog < ActiveRecord::Base
54
    self.table_name = "projects"
55 56
  end

57 58
  def self.use_relative_model_naming?
    true
59 60 61
  end
end

62
class PolymorphicRoutesTest < ActionController::TestCase
63
  include SharedTestRoutes.url_helpers
64
  default_url_options[:host] = "example.com"
65 66 67 68 69 70 71 72

  def setup
    @project = Project.new
    @task = Task.new
    @step = Step.new
    @bid = Bid.new
    @tax = Tax.new
    @fax = Fax.new
73
    @delegator = ModelDelegator.new
74
    @series = Series.new
75 76
    @blog_post = Weblog::Post.new
    @blog_blog = Weblog::Blog.new
77 78
  end

79
  def assert_url(url, args)
80 81
    host = self.class.default_url_options[:host]

82
    assert_equal url.sub(/http:\/\/#{host}/, ""), polymorphic_path(args)
83 84 85 86
    assert_equal url, polymorphic_url(args)
    assert_equal url, url_for(args)
  end

87 88
  def test_string
    with_test_routes do
89
      assert_equal "/projects", polymorphic_path("projects")
90
      assert_equal "http://example.com/projects", polymorphic_url("projects")
91
      assert_equal "projects", url_for("projects")
92 93 94 95 96
    end
  end

  def test_string_with_options
    with_test_routes do
97
      assert_equal "http://example.com/projects?id=10", polymorphic_url("projects", id: 10)
98 99 100
    end
  end

101 102
  def test_symbol
    with_test_routes do
103
      assert_url "http://example.com/projects", :projects
104 105 106 107 108
    end
  end

  def test_symbol_with_options
    with_test_routes do
109
      assert_equal "http://example.com/projects?id=10", polymorphic_url(:projects, id: 10)
110 111 112
    end
  end

113 114
  def test_passing_routes_proxy
    with_namespaced_routes(:blog) do
115
      proxy = ActionDispatch::Routing::RoutesProxy.new(_routes, self, _routes.url_helpers)
116
      @blog_post.save
117
      assert_url "http://example.com/posts/#{@blog_post.id}", [proxy, @blog_post]
118 119 120
    end
  end

121 122 123
  def test_namespaced_model
    with_namespaced_routes(:blog) do
      @blog_post.save
124
      assert_url "http://example.com/posts/#{@blog_post.id}", @blog_post
125 126 127 128 129 130
    end
  end

  def test_namespaced_model_with_name_the_same_as_namespace
    with_namespaced_routes(:blog) do
      @blog_blog.save
131
      assert_url "http://example.com/blogs/#{@blog_blog.id}", @blog_blog
132 133 134
    end
  end

A
Aaron Patterson 已提交
135
  def test_polymorphic_url_with_2_objects
136 137 138 139 140 141 142
    with_namespaced_routes(:blog) do
      @blog_blog.save
      @blog_post.save
      assert_equal "http://example.com/blogs/#{@blog_blog.id}/posts/#{@blog_post.id}", polymorphic_url([@blog_blog, @blog_post])
    end
  end

A
Aaron Patterson 已提交
143 144 145 146 147 148 149 150 151
  def test_polymorphic_url_with_3_objects
    with_namespaced_routes(:blog) do
      @blog_blog.save
      @blog_post.save
      @fax.save
      assert_equal "http://example.com/blogs/#{@blog_blog.id}/posts/#{@blog_post.id}/faxes/#{@fax.id}", polymorphic_url([@blog_blog, @blog_post, @fax])
    end
  end

152 153 154 155
  def test_namespaced_model_with_nested_resources
    with_namespaced_routes(:blog) do
      @blog_post.save
      @blog_blog.save
156
      assert_url "http://example.com/blogs/#{@blog_blog.id}/posts/#{@blog_post.id}", [@blog_blog, @blog_post]
157
    end
158 159
  end

160 161
  def test_with_nil
    with_test_routes do
162
      exception = assert_raise ArgumentError do
163 164
        polymorphic_url(nil)
      end
165
      assert_equal "Nil location provided. Can't build URI.", exception.message
166 167 168
    end
  end

169 170
  def test_with_empty_list
    with_test_routes do
171
      exception = assert_raise ArgumentError do
172 173
        polymorphic_url([])
      end
174
      assert_equal "Nil location provided. Can't build URI.", exception.message
175 176 177 178 179
    end
  end

  def test_with_nil_id
    with_test_routes do
180
      exception = assert_raise ArgumentError do
181
        polymorphic_url(id: nil)
182
      end
183
      assert_equal "Nil location provided. Can't build URI.", exception.message
184 185 186
    end
  end

187
  def test_with_entirely_nil_list
188
    with_test_routes do
189
      exception = assert_raise ArgumentError do
190
        @series.save
191
        polymorphic_url([nil, nil])
192
      end
193
      assert_equal "Nil location provided. Can't build URI.", exception.message
194 195 196
    end
  end

197 198 199 200 201 202
  def test_with_nil_in_list_for_resource_that_could_be_top_level_or_nested
    with_top_level_and_nested_routes do
      @blog_post.save
      assert_equal "http://example.com/posts/#{@blog_post.id}", polymorphic_url([nil, @blog_post])
    end
  end
R
Rafael Mendonça França 已提交
203

204 205 206 207 208 209
  def test_with_nil_in_list_does_not_generate_invalid_link
    with_top_level_and_nested_routes do
      exception = assert_raise NoMethodError do
        @series.save
        polymorphic_url([nil, @series])
      end
210
      assert_match(/undefined method `series_url'/, exception.message)
211 212 213
    end
  end

214
  def test_with_record
J
Joshua Peek 已提交
215
    with_test_routes do
216
      @project.save
217
      assert_url "http://example.com/projects/#{@project.id}", @project
218 219
    end
  end
J
Joshua Peek 已提交
220

221 222
  def test_with_class
    with_test_routes do
223
      assert_url "http://example.com/projects", @project.class
224 225
    end
  end
226

227 228 229 230 231 232
  def test_with_class_list_of_one
    with_test_routes do
      assert_url "http://example.com/projects", [@project.class]
    end
  end

233 234
  def test_class_with_options
    with_test_routes do
235 236
      assert_equal "http://example.com/projects?foo=bar", polymorphic_url(@project.class, foo: :bar)
      assert_equal "/projects?foo=bar", polymorphic_path(@project.class, foo: :bar)
237 238 239
    end
  end

240
  def test_with_new_record
J
Joshua Peek 已提交
241
    with_test_routes do
242
      assert_url "http://example.com/projects", @project
243 244 245
    end
  end

246 247 248 249
  def test_new_record_arguments
    params = nil

    with_test_routes do
250 251 252 253 254
      extend Module.new {
        define_method("projects_url") { |*args|
          params = args
          super(*args)
        }
255 256 257 258 259

        define_method("projects_path") { |*args|
          params = args
          super(*args)
        }
260 261
      }

262
      assert_url "http://example.com/projects", @project
263 264 265 266
      assert_equal [], params
    end
  end

267
  def test_with_destroyed_record
J
Joshua Peek 已提交
268
    with_test_routes do
269
      @project.destroy
270
      assert_url "http://example.com/projects", @project
271 272 273
    end
  end

274
  def test_with_record_and_action
J
Joshua Peek 已提交
275
    with_test_routes do
276
      assert_equal "http://example.com/projects/new", polymorphic_url(@project, action: "new")
277 278 279 280
    end
  end

  def test_url_helper_prefixed_with_new
J
Joshua Peek 已提交
281
    with_test_routes do
282 283 284 285
      assert_equal "http://example.com/projects/new", new_polymorphic_url(@project)
    end
  end

286 287 288 289 290 291 292 293 294
  def test_regression_path_helper_prefixed_with_new_and_edit
    with_test_routes do
      assert_equal "/projects/new", new_polymorphic_path(@project)

      @project.save
      assert_equal "/projects/#{@project.id}/edit", edit_polymorphic_path(@project)
    end
  end

295
  def test_url_helper_prefixed_with_edit
J
Joshua Peek 已提交
296
    with_test_routes do
297 298 299 300
      @project.save
      assert_equal "http://example.com/projects/#{@project.id}/edit", edit_polymorphic_url(@project)
    end
  end
J
Joshua Peek 已提交
301

302
  def test_url_helper_prefixed_with_edit_with_url_options
J
Joshua Peek 已提交
303
    with_test_routes do
304
      @project.save
305
      assert_equal "http://example.com/projects/#{@project.id}/edit?param1=10", edit_polymorphic_url(@project, param1: "10")
306 307
    end
  end
J
Joshua Peek 已提交
308

309
  def test_url_helper_with_url_options
J
Joshua Peek 已提交
310
    with_test_routes do
311
      @project.save
312
      assert_equal "http://example.com/projects/#{@project.id}?param1=10", polymorphic_url(@project, param1: "10")
313 314 315 316
    end
  end

  def test_format_option
J
Joshua Peek 已提交
317
    with_test_routes do
318
      @project.save
319
      assert_equal "http://example.com/projects/#{@project.id}.pdf", polymorphic_url(@project, format: :pdf)
320 321
    end
  end
J
Joshua Peek 已提交
322

323
  def test_format_option_with_url_options
J
Joshua Peek 已提交
324
    with_test_routes do
325
      @project.save
326
      assert_equal "http://example.com/projects/#{@project.id}.pdf?param1=10", polymorphic_url(@project, format: :pdf, param1: "10")
327 328
    end
  end
J
Joshua Peek 已提交
329

330
  def test_id_and_format_option
J
Joshua Peek 已提交
331
    with_test_routes do
332
      @project.save
333
      assert_equal "http://example.com/projects/#{@project.id}.pdf", polymorphic_url(id: @project, format: :pdf)
334 335 336 337 338 339 340
    end
  end

  def test_with_nested
    with_test_routes do
      @project.save
      @task.save
341
      assert_url "http://example.com/projects/#{@project.id}/tasks/#{@task.id}", [@project, @task]
342 343
    end
  end
J
Joshua Peek 已提交
344

345 346 347
  def test_with_nested_unsaved
    with_test_routes do
      @project.save
348
      assert_url "http://example.com/projects/#{@project.id}/tasks", [@project, @task]
349 350
    end
  end
J
Joshua Peek 已提交
351

352 353 354 355
  def test_with_nested_destroyed
    with_test_routes do
      @project.save
      @task.destroy
356
      assert_url "http://example.com/projects/#{@project.id}/tasks", [@project, @task]
357 358
    end
  end
J
Joshua Peek 已提交
359

360 361 362
  def test_with_nested_class
    with_test_routes do
      @project.save
363
      assert_url "http://example.com/projects/#{@project.id}/tasks", [@project, @task.class]
364 365
    end
  end
J
Joshua Peek 已提交
366

367
  def test_class_with_array_and_namespace
J
Joshua Peek 已提交
368
    with_admin_test_routes do
369
      assert_url "http://example.com/admin/projects", [:admin, @project.class]
370 371
    end
  end
J
Joshua Peek 已提交
372

373
  def test_new_with_array_and_namespace
J
Joshua Peek 已提交
374
    with_admin_test_routes do
375
      assert_equal "http://example.com/admin/projects/new", polymorphic_url([:admin, @project], action: "new")
376 377
    end
  end
J
Joshua Peek 已提交
378

379
  def test_unsaved_with_array_and_namespace
J
Joshua Peek 已提交
380
    with_admin_test_routes do
381
      assert_url "http://example.com/admin/projects", [:admin, @project]
382 383
    end
  end
J
Joshua Peek 已提交
384

385
  def test_nested_unsaved_with_array_and_namespace
J
Joshua Peek 已提交
386
    with_admin_test_routes do
387
      @project.save
388
      assert_url "http://example.com/admin/projects/#{@project.id}/tasks", [:admin, @project, @task]
389 390
    end
  end
J
Joshua Peek 已提交
391

392
  def test_nested_with_array_and_namespace
J
Joshua Peek 已提交
393
    with_admin_test_routes do
394 395
      @project.save
      @task.save
396
      assert_url "http://example.com/admin/projects/#{@project.id}/tasks/#{@task.id}", [:admin, @project, @task]
397 398
    end
  end
J
Joshua Peek 已提交
399

400
  def test_ordering_of_nesting_and_namespace
J
Joshua Peek 已提交
401
    with_admin_and_site_test_routes do
402 403 404
      @project.save
      @task.save
      @step.save
405
      assert_url "http://example.com/admin/projects/#{@project.id}/site/tasks/#{@task.id}/steps/#{@step.id}", [:admin, @project, :site, @task, @step]
406 407
    end
  end
J
Joshua Peek 已提交
408

409 410 411
  def test_nesting_with_array_ending_in_singleton_resource
    with_test_routes do
      @project.save
412
      assert_url "http://example.com/projects/#{@project.id}/bid", [@project, :bid]
413 414
    end
  end
J
Joshua Peek 已提交
415

416 417 418 419
  def test_nesting_with_array_containing_singleton_resource
    with_test_routes do
      @project.save
      @task.save
420
      assert_url "http://example.com/projects/#{@project.id}/bid/tasks/#{@task.id}", [@project, :bid, @task]
421 422
    end
  end
J
Joshua Peek 已提交
423

424 425 426 427
  def test_nesting_with_array_containing_singleton_resource_and_format
    with_test_routes do
      @project.save
      @task.save
428
      assert_equal "http://example.com/projects/#{@project.id}/bid/tasks/#{@task.id}.pdf", polymorphic_url([@project, :bid, @task], format: :pdf)
429 430
    end
  end
J
Joshua Peek 已提交
431

432 433 434 435
  def test_nesting_with_array_containing_namespace_and_singleton_resource
    with_admin_test_routes do
      @project.save
      @task.save
436
      assert_url "http://example.com/admin/projects/#{@project.id}/bid/tasks/#{@task.id}", [:admin, @project, :bid, @task]
437 438
    end
  end
J
Joshua Peek 已提交
439

440
  def test_nesting_with_array
441 442
    with_test_routes do
      @project.save
443
      assert_url "http://example.com/projects/#{@project.id}/bid", [@project, :bid]
444 445
    end
  end
J
Joshua Peek 已提交
446

447
  def test_with_array_containing_single_object
J
Joshua Peek 已提交
448
    with_test_routes do
449
      @project.save
450
      assert_url "http://example.com/projects/#{@project.id}", [@project]
451 452
    end
  end
J
Joshua Peek 已提交
453

454
  def test_with_array_containing_single_name
J
Joshua Peek 已提交
455
    with_test_routes do
456
      @project.save
457
      assert_url "http://example.com/projects", [:projects]
458 459
    end
  end
J
Joshua Peek 已提交
460

461 462 463 464 465 466
  def test_with_array_containing_single_string_name
    with_test_routes do
      assert_url "http://example.com/projects", ["projects"]
    end
  end

467 468
  def test_with_array_containing_symbols
    with_test_routes do
469
      assert_url "http://example.com/series/new", [:new, :series]
470 471
    end
  end
J
Joshua Peek 已提交
472

473
  def test_with_hash
J
Joshua Peek 已提交
474
    with_test_routes do
475
      @project.save
476
      assert_equal "http://example.com/projects/#{@project.id}", polymorphic_url(id: @project)
477 478
    end
  end
J
Joshua Peek 已提交
479

480
  def test_polymorphic_path_accepts_options
J
Joshua Peek 已提交
481
    with_test_routes do
482
      assert_equal "/projects/new", polymorphic_path(@project, action: "new")
483 484
    end
  end
J
Joshua Peek 已提交
485

486 487 488 489
  def test_polymorphic_path_does_not_modify_arguments
    with_admin_test_routes do
      @project.save
      @task.save
490 491

      options = {}
492
      object_array = [:admin, @project, @task]
493 494
      original_args = [object_array.dup, options.dup]

495
      assert_no_difference("object_array.size") { polymorphic_path(object_array, options) }
496
      assert_equal original_args, [object_array, options]
497 498
    end
  end
J
Joshua Peek 已提交
499

500 501
  # Tests for names where .plural.singular doesn't round-trip
  def test_with_irregular_plural_record
J
Joshua Peek 已提交
502
    with_test_routes do
503
      @tax.save
504
      assert_url "http://example.com/taxes/#{@tax.id}", @tax
505 506
    end
  end
J
Joshua Peek 已提交
507

508
  def test_with_irregular_plural_class
J
Joshua Peek 已提交
509
    with_test_routes do
510
      assert_url "http://example.com/taxes", @tax.class
511 512
    end
  end
J
Joshua Peek 已提交
513

514
  def test_with_irregular_plural_new_record
J
Joshua Peek 已提交
515
    with_test_routes do
516
      assert_url "http://example.com/taxes", @tax
517 518
    end
  end
519 520 521

  def test_with_irregular_plural_destroyed_record
    with_test_routes do
J
Joshua Peek 已提交
522
      @tax.destroy
523
      assert_url "http://example.com/taxes", @tax
524 525
    end
  end
J
Joshua Peek 已提交
526

527
  def test_with_irregular_plural_record_and_action
J
Joshua Peek 已提交
528
    with_test_routes do
529
      assert_equal "http://example.com/taxes/new", polymorphic_url(@tax, action: "new")
530 531
    end
  end
J
Joshua Peek 已提交
532

533
  def test_irregular_plural_url_helper_prefixed_with_new
J
Joshua Peek 已提交
534
    with_test_routes do
535 536 537
      assert_equal "http://example.com/taxes/new", new_polymorphic_url(@tax)
    end
  end
J
Joshua Peek 已提交
538

539
  def test_irregular_plural_url_helper_prefixed_with_edit
J
Joshua Peek 已提交
540
    with_test_routes do
541 542 543 544
      @tax.save
      assert_equal "http://example.com/taxes/#{@tax.id}/edit", edit_polymorphic_url(@tax)
    end
  end
J
Joshua Peek 已提交
545

546
  def test_with_nested_irregular_plurals
J
Joshua Peek 已提交
547
    with_test_routes do
548 549 550 551 552
      @tax.save
      @fax.save
      assert_equal "http://example.com/taxes/#{@tax.id}/faxes/#{@fax.id}", polymorphic_url([@tax, @fax])
    end
  end
J
Joshua Peek 已提交
553

554
  def test_with_nested_unsaved_irregular_plurals
J
Joshua Peek 已提交
555
    with_test_routes do
556
      @tax.save
557
      assert_url "http://example.com/taxes/#{@tax.id}/faxes", [@tax, @fax]
558 559
    end
  end
J
Joshua Peek 已提交
560

561
  def test_new_with_irregular_plural_array_and_namespace
J
Joshua Peek 已提交
562
    with_admin_test_routes do
563
      assert_equal "http://example.com/admin/taxes/new", polymorphic_url([:admin, @tax], action: "new")
564 565
    end
  end
J
Joshua Peek 已提交
566

567
  def test_class_with_irregular_plural_array_and_namespace
J
Joshua Peek 已提交
568
    with_admin_test_routes do
569
      assert_url "http://example.com/admin/taxes", [:admin, @tax.class]
570 571
    end
  end
J
Joshua Peek 已提交
572

573
  def test_unsaved_with_irregular_plural_array_and_namespace
J
Joshua Peek 已提交
574
    with_admin_test_routes do
575
      assert_url "http://example.com/admin/taxes", [:admin, @tax]
576 577
    end
  end
J
Joshua Peek 已提交
578

579
  def test_nesting_with_irregular_plurals_and_array_ending_in_singleton_resource
J
Joshua Peek 已提交
580
    with_test_routes do
581
      @tax.save
582
      assert_url "http://example.com/taxes/#{@tax.id}/bid", [@tax, :bid]
583 584
    end
  end
J
Joshua Peek 已提交
585

586
  def test_with_array_containing_single_irregular_plural_object
J
Joshua Peek 已提交
587
    with_test_routes do
588
      @tax.save
589
      assert_url "http://example.com/taxes/#{@tax.id}", [@tax]
590 591
    end
  end
J
Joshua Peek 已提交
592

593
  def test_with_array_containing_single_name_irregular_plural
J
Joshua Peek 已提交
594
    with_test_routes do
595
      @tax.save
596
      assert_url "http://example.com/taxes", [:taxes]
597 598
    end
  end
J
Joshua Peek 已提交
599

600
  # Tests for uncountable names
601 602 603
  def test_uncountable_resource
    with_test_routes do
      @series.save
604 605
      assert_url "http://example.com/series/#{@series.id}", @series
      assert_url "http://example.com/series", Series.new
606 607 608
    end
  end

609
  def test_routing_to_a_model_delegate
610
    with_test_routes do
611
      assert_url "http://example.com/model_delegates/overridden", @delegator
612 613 614
    end
  end

615 616 617 618 619 620
  def test_nested_routing_to_a_model_delegate
    with_test_routes do
      assert_url "http://example.com/foo/model_delegates/overridden", [:foo, @delegator]
    end
  end

621 622 623
  def with_namespaced_routes(name)
    with_routing do |set|
      set.draw do
624
        scope(module: name) do
625
          resources :blogs do
A
Aaron Patterson 已提交
626 627 628
            resources :posts do
              resources :faxes
            end
629 630 631 632 633
          end
          resources :posts
        end
      end

634
      extend @routes.url_helpers
635 636 637 638
      yield
    end
  end

639 640
  def with_test_routes(options = {})
    with_routing do |set|
641 642 643 644 645
      set.draw do
        resources :projects do
          resources :tasks
          resource :bid do
            resources :tasks
646 647
          end
        end
648 649 650
        resources :taxes do
          resources :faxes
          resource :bid
651
        end
652
        resources :series
653
        resources :model_delegates
654 655 656
        namespace :foo do
          resources :model_delegates
        end
657 658
      end

659
      extend @routes.url_helpers
660 661 662
      yield
    end
  end
J
Joshua Peek 已提交
663

664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
  def with_top_level_and_nested_routes(options = {})
    with_routing do |set|
      set.draw do
        resources :blogs do
          resources :posts
          resources :series
        end
        resources :posts
      end

      extend @routes.url_helpers
      yield
    end
  end

679 680
  def with_admin_test_routes(options = {})
    with_routing do |set|
681 682 683 684 685 686
      set.draw do
        namespace :admin do
          resources :projects do
            resources :tasks
            resource :bid do
              resources :tasks
687 688
            end
          end
689 690
          resources :taxes do
            resources :faxes
691
          end
692
          resources :series
693 694 695
        end
      end

696
      extend @routes.url_helpers
697 698 699
      yield
    end
  end
J
Joshua Peek 已提交
700

701 702
  def with_admin_and_site_test_routes(options = {})
    with_routing do |set|
703 704 705 706 707 708
      set.draw do
        namespace :admin do
          resources :projects do
            namespace :site do
              resources :tasks do
                resources :steps
709 710 711 712 713 714
              end
            end
          end
        end
      end

715
      extend @routes.url_helpers
716 717 718
      yield
    end
  end
719
end
720 721 722 723 724 725 726 727 728 729

class PolymorphicPathRoutesTest < PolymorphicRoutesTest
  include ActionView::RoutingUrlFor
  include ActionView::Context

  attr_accessor :controller

  def assert_url(url, args)
    host = self.class.default_url_options[:host]

730
    assert_equal url.sub(/http:\/\/#{host}/, ""), url_for(args)
731 732
  end
end
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778

class DirectRoutesTest < ActionView::TestCase
  class Linkable
    attr_reader :id

    def self.name
      super.demodulize
    end

    def initialize(id)
      @id = id
    end

    def linkable_type
      self.class.name.underscore
    end
  end

  class Category < Linkable; end
  class Collection < Linkable; end
  class Product < Linkable; end

  Routes = ActionDispatch::Routing::RouteSet.new
  Routes.draw do
    resources :categories, :collections, :products
    direct(:linkable) { |linkable| [:"#{linkable.linkable_type}", { id: linkable.id }] }
  end

  include Routes.url_helpers

  def setup
    @category = Category.new("1")
    @collection = Collection.new("2")
    @product = Product.new("3")
  end

  def test_direct_routes
    assert_equal "/categories/1", linkable_path(@category)
    assert_equal "/collections/2", linkable_path(@collection)
    assert_equal "/products/3", linkable_path(@product)

    assert_equal "http://test.host/categories/1", linkable_url(@category)
    assert_equal "http://test.host/collections/2", linkable_url(@collection)
    assert_equal "http://test.host/products/3", linkable_url(@product)
  end
end