aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_dispatch/routing/mapper.rb7
-rw-r--r--actionpack/test/dispatch/routing/custom_url_helpers_test.rb17
2 files changed, 23 insertions, 1 deletions
diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb
index 474856b23a..fceadc7039 100644
--- a/actionpack/lib/action_dispatch/routing/mapper.rb
+++ b/actionpack/lib/action_dispatch/routing/mapper.rb
@@ -2056,7 +2056,12 @@ module ActionDispatch
# NOTE: It is the url helper's responsibility to return the correct
# set of options to be passed to the `url_for` call.
def direct(name, options = {}, &block)
- @set.add_url_helper(name, options, &block)
+ case name
+ when String, Symbol
+ @set.add_url_helper(name, options, &block)
+ else
+ raise ArgumentError, "The direct method only accepts a string or symbol"
+ end
end
end
diff --git a/actionpack/test/dispatch/routing/custom_url_helpers_test.rb b/actionpack/test/dispatch/routing/custom_url_helpers_test.rb
index a55ee8e9b0..bf4b323cf0 100644
--- a/actionpack/test/dispatch/routing/custom_url_helpers_test.rb
+++ b/actionpack/test/dispatch/routing/custom_url_helpers_test.rb
@@ -31,6 +31,7 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest
end
direct(:website) { "http://www.rubyonrails.org" }
+ direct("string") { "http://www.rubyonrails.org" }
direct(:linkable) { |linkable| [:"#{linkable.linkable_type}", { id: linkable.id }] }
direct(:params) { |params| params }
direct(:symbol) { :basket }
@@ -61,6 +62,9 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest
assert_equal "http://www.rubyonrails.org", website_path
assert_equal "http://www.rubyonrails.org", Routes.url_helpers.website_path
+ assert_equal "http://www.rubyonrails.org", string_path
+ assert_equal "http://www.rubyonrails.org", Routes.url_helpers.string_path
+
assert_equal "/categories/1", linkable_path(@category)
assert_equal "/categories/1", Routes.url_helpers.linkable_path(@category)
assert_equal "/collections/2", linkable_path(@collection)
@@ -92,6 +96,9 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest
assert_equal "http://www.rubyonrails.org", website_url
assert_equal "http://www.rubyonrails.org", Routes.url_helpers.website_url
+ assert_equal "http://www.rubyonrails.org", string_url
+ assert_equal "http://www.rubyonrails.org", Routes.url_helpers.string_url
+
assert_equal "http://www.example.com/categories/1", linkable_url(@category)
assert_equal "http://www.example.com/categories/1", Routes.url_helpers.linkable_url(@category)
assert_equal "http://www.example.com/collections/2", linkable_url(@collection)
@@ -118,4 +125,14 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest
assert_equal "http://www.example.com/products?size=20", defaults_url(size: 20)
assert_equal "http://www.example.com/products?size=20", Routes.url_helpers.defaults_url(size: 20)
end
+
+ def test_raises_argument_error
+ routes = ActionDispatch::Routing::RouteSet.new
+
+ assert_raises ArgumentError do
+ routes.draw do
+ direct(1) { "http://www.rubyonrails.org" }
+ end
+ end
+ end
end