diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2018-09-27 16:36:49 -0400 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2018-09-27 16:36:49 -0400 |
commit | 6a73faa5c4065a77483c1136290f57a4dc3b485a (patch) | |
tree | 647b6e3b4ac25e3ecd88b149fa8efbf96c811f46 | |
parent | d0d1cd3d45f14e8423ee8ee3f4c19e999a69b96c (diff) | |
parent | d043920eb903d32a1e3540690d3b08d98d1b6d88 (diff) | |
download | rails-6a73faa5c4065a77483c1136290f57a4dc3b485a.tar.gz rails-6a73faa5c4065a77483c1136290f57a4dc3b485a.tar.bz2 rails-6a73faa5c4065a77483c1136290f57a4dc3b485a.zip |
Merge pull request #34004 from simonc/constraints-call-matches-edge-case
Fixing an edge case when using objects as constraints
-rw-r--r-- | actionpack/lib/action_dispatch/routing/mapper.rb | 14 | ||||
-rw-r--r-- | actionpack/test/dispatch/routing_test.rb | 15 |
2 files changed, 28 insertions, 1 deletions
diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index b618b9c400..8386cb9689 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -50,7 +50,19 @@ module ActionDispatch private def constraint_args(constraint, request) - constraint.arity == 1 ? [request] : [request.path_parameters, request] + arity = if constraint.respond_to?(:arity) + constraint.arity + else + constraint.method(:call).arity + end + + if arity < 1 + [] + elsif arity == 1 + [request] + else + [request.path_parameters, request] + end end end diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index 5efbe5b553..ee87791538 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -115,6 +115,21 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest assert_equal 301, status end + def test_accepts_a_constraint_object_responding_to_call + constraint = Class.new do + def call(*); true; end + def matches?(*); false; end + end + + draw do + get "/", to: "home#show", constraints: constraint.new + end + + assert_nothing_raised do + get "/" + end + end + def test_namespace_with_controller_segment assert_raise(ArgumentError) do draw do |