1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Only accept symbols and strings for Mapper#direct

This commit is contained in:
Andrew White 2017-02-20 13:54:06 +00:00
parent 47a27e8950
commit e96da0a7be
2 changed files with 23 additions and 1 deletions

View file

@ -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

View file

@ -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