diff options
Diffstat (limited to 'actionview/test/activerecord/polymorphic_routes_test.rb')
-rw-r--r-- | actionview/test/activerecord/polymorphic_routes_test.rb | 133 |
1 files changed, 93 insertions, 40 deletions
diff --git a/actionview/test/activerecord/polymorphic_routes_test.rb b/actionview/test/activerecord/polymorphic_routes_test.rb index 34b2698c7f..724129a7d9 100644 --- a/actionview/test/activerecord/polymorphic_routes_test.rb +++ b/actionview/test/activerecord/polymorphic_routes_test.rb @@ -1,28 +1,30 @@ -require 'active_record_unit' -require 'fixtures/project' +# frozen_string_literal: true + +require "active_record_unit" +require "fixtures/project" class Task < ActiveRecord::Base - self.table_name = 'projects' + self.table_name = "projects" end class Step < ActiveRecord::Base - self.table_name = 'projects' + self.table_name = "projects" end class Bid < ActiveRecord::Base - self.table_name = 'projects' + self.table_name = "projects" end class Tax < ActiveRecord::Base - self.table_name = 'projects' + self.table_name = "projects" end class Fax < ActiveRecord::Base - self.table_name = 'projects' + self.table_name = "projects" end class Series < ActiveRecord::Base - self.table_name = 'projects' + self.table_name = "projects" end class ModelDelegator @@ -41,17 +43,17 @@ class ModelDelegate end def to_param - 'overridden' + "overridden" end end -module Blog +module Weblog class Post < ActiveRecord::Base - self.table_name = 'projects' + self.table_name = "projects" end class Blog < ActiveRecord::Base - self.table_name = 'projects' + self.table_name = "projects" end def self.use_relative_model_naming? @@ -60,10 +62,14 @@ module Blog end class PolymorphicRoutesTest < ActionController::TestCase - include SharedTestRoutes.url_helpers - self.default_url_options[:host] = 'example.com' + Routes = ActionDispatch::Routing::RouteSet.new + Routes.draw { } + include Routes.url_helpers + + default_url_options[:host] = "example.com" def setup + super @project = Project.new @task = Task.new @step = Step.new @@ -72,22 +78,21 @@ class PolymorphicRoutesTest < ActionController::TestCase @fax = Fax.new @delegator = ModelDelegator.new @series = Series.new - @blog_post = Blog::Post.new - @blog_blog = Blog::Blog.new + @blog_post = Weblog::Post.new + @blog_blog = Weblog::Blog.new end def assert_url(url, args) host = self.class.default_url_options[:host] - assert_equal url.sub(/http:\/\/#{host}/, ''), polymorphic_path(args) + assert_equal url.sub(/http:\/\/#{host}/, ""), polymorphic_path(args) assert_equal url, polymorphic_url(args) assert_equal url, url_for(args) end def test_string with_test_routes do - # FIXME: why are these different? Symbol case passes through to - # `polymorphic_url`, but the String case doesn't. + assert_equal "/projects", polymorphic_path("projects") assert_equal "http://example.com/projects", polymorphic_url("projects") assert_equal "projects", url_for("projects") end @@ -95,7 +100,7 @@ class PolymorphicRoutesTest < ActionController::TestCase def test_string_with_options with_test_routes do - assert_equal "http://example.com/projects?id=10", polymorphic_url("projects", :id => 10) + assert_equal "http://example.com/projects?id=10", polymorphic_url("projects", id: 10) end end @@ -107,7 +112,7 @@ class PolymorphicRoutesTest < ActionController::TestCase def test_symbol_with_options with_test_routes do - assert_equal "http://example.com/projects?id=10", polymorphic_url(:projects, :id => 10) + assert_equal "http://example.com/projects?id=10", polymorphic_url(:projects, id: 10) end end @@ -179,7 +184,7 @@ class PolymorphicRoutesTest < ActionController::TestCase def test_with_nil_id with_test_routes do exception = assert_raise ArgumentError do - polymorphic_url({ :id => nil }) + polymorphic_url(id: nil) end assert_equal "Nil location provided. Can't build URI.", exception.message end @@ -233,8 +238,8 @@ class PolymorphicRoutesTest < ActionController::TestCase def test_class_with_options with_test_routes do - 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 }) + 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) end end @@ -274,7 +279,7 @@ class PolymorphicRoutesTest < ActionController::TestCase def test_with_record_and_action with_test_routes do - assert_equal "http://example.com/projects/new", polymorphic_url(@project, :action => 'new') + assert_equal "http://example.com/projects/new", polymorphic_url(@project, action: "new") end end @@ -303,35 +308,35 @@ class PolymorphicRoutesTest < ActionController::TestCase 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') + 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') + assert_equal "http://example.com/projects/#{@project.id}?param1=10", polymorphic_url(@project, param1: "10") 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) + 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') + 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) + assert_equal "http://example.com/projects/#{@project.id}.pdf", polymorphic_url(id: @project, format: :pdf) end end @@ -373,7 +378,7 @@ class PolymorphicRoutesTest < ActionController::TestCase 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') + assert_equal "http://example.com/admin/projects/new", polymorphic_url([:admin, @project], action: "new") end end @@ -426,7 +431,7 @@ class PolymorphicRoutesTest < ActionController::TestCase 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) + assert_equal "http://example.com/projects/#{@project.id}/bid/tasks/#{@task.id}.pdf", polymorphic_url([@project, :bid, @task], format: :pdf) end end @@ -474,13 +479,13 @@ class PolymorphicRoutesTest < ActionController::TestCase def test_with_hash with_test_routes do @project.save - assert_equal "http://example.com/projects/#{@project.id}", polymorphic_url(:id => @project) + 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') + assert_equal "/projects/new", polymorphic_path(@project, action: "new") end end @@ -493,7 +498,7 @@ class PolymorphicRoutesTest < ActionController::TestCase object_array = [:admin, @project, @task] original_args = [object_array.dup, options.dup] - assert_no_difference('object_array.size') { polymorphic_path(object_array, options) } + assert_no_difference("object_array.size") { polymorphic_path(object_array, options) } assert_equal original_args, [object_array, options] end end @@ -527,7 +532,7 @@ class PolymorphicRoutesTest < ActionController::TestCase def test_with_irregular_plural_record_and_action with_test_routes do - assert_equal "http://example.com/taxes/new", polymorphic_url(@tax, :action => 'new') + assert_equal "http://example.com/taxes/new", polymorphic_url(@tax, action: "new") end end @@ -561,7 +566,7 @@ class PolymorphicRoutesTest < ActionController::TestCase 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') + assert_equal "http://example.com/admin/taxes/new", polymorphic_url([:admin, @tax], action: "new") end end @@ -598,7 +603,7 @@ class PolymorphicRoutesTest < ActionController::TestCase end end - # Tests for uncountable names + # Tests for uncountable names def test_uncountable_resource with_test_routes do @series.save @@ -622,7 +627,7 @@ class PolymorphicRoutesTest < ActionController::TestCase def with_namespaced_routes(name) with_routing do |set| set.draw do - scope(:module => name) do + scope(module: name) do resources :blogs do resources :posts do resources :faxes @@ -728,6 +733,54 @@ class PolymorphicPathRoutesTest < PolymorphicRoutesTest def assert_url(url, args) host = self.class.default_url_options[:host] - assert_equal url.sub(/http:\/\/#{host}/, ''), url_for(args) + assert_equal url.sub(/http:\/\/#{host}/, ""), url_for(args) + end +end + +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 + super + @category = Category.new("1") + @collection = Collection.new("2") + @product = Product.new("3") + @controller.singleton_class.include Routes.url_helpers + 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 |