Skip to content

Commit 02480a8

Browse files
pixeltrixjosevalim
authored andcommitted
Move implicit nested call before options handling so that nested constraints work [rails#5513 state:resolved]
Signed-off-by: José Valim <jose.valim@gmail.com>
1 parent dbf8255 commit 02480a8

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

actionpack/lib/action_dispatch/routing/mapper.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,11 @@ def apply_common_behavior_for(method, resources, options, &block)
909909
return true
910910
end
911911

912+
if resource_scope?
913+
nested { send(method, resources.pop, options, &block) }
914+
return true
915+
end
916+
912917
options.keys.each do |k|
913918
(options[:constraints] ||= {})[k] = options.delete(k) if options[k].is_a?(Regexp)
914919
end
@@ -925,13 +930,6 @@ def apply_common_behavior_for(method, resources, options, &block)
925930
options.merge!(scope_action_options) if scope_action_options?
926931
end
927932

928-
if resource_scope?
929-
nested do
930-
send(method, resources.pop, options, &block)
931-
end
932-
return true
933-
end
934-
935933
false
936934
end
937935

@@ -1017,11 +1015,11 @@ def nested_options
10171015
end
10181016

10191017
def id_constraint?
1020-
@scope[:id].is_a?(Regexp) || (@scope[:constraints] && @scope[:constraints][:id].is_a?(Regexp))
1018+
@scope[:constraints] && @scope[:constraints][:id].is_a?(Regexp)
10211019
end
10221020

10231021
def id_constraint
1024-
@scope[:id] || @scope[:constraints][:id]
1022+
@scope[:constraints][:id]
10251023
end
10261024

10271025
def canonical_action?(action, flag)

actionpack/test/dispatch/routing_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,10 @@ def self.matches?(request)
448448
:filename => /(.+)/,
449449
:as => :purchase
450450

451+
resources :lists, :id => /([A-Za-z0-9]{25})|default/ do
452+
resources :todos, :id => /\d+/
453+
end
454+
451455
scope '/countries/:country', :constraints => lambda { |params, req| %[all France].include?(params[:country]) } do
452456
match '/', :to => 'countries#index'
453457
match '/cities', :to => 'countries#cities'
@@ -2110,6 +2114,20 @@ def test_named_character_classes_in_regexp_constraints
21102114
assert_equal '/purchases/315004be7e/Ruby_on_Rails_3.pdf', purchase_path(:token => '315004be7e', :filename => 'Ruby_on_Rails_3.pdf')
21112115
end
21122116

2117+
def test_nested_resource_constraints
2118+
get '/lists/01234012340123401234fffff'
2119+
assert_equal 'lists#show', @response.body
2120+
assert_equal '/lists/01234012340123401234fffff', list_path(:id => '01234012340123401234fffff')
2121+
2122+
get '/lists/01234012340123401234fffff/todos/1'
2123+
assert_equal 'todos#show', @response.body
2124+
assert_equal '/lists/01234012340123401234fffff/todos/1', list_todo_path(:list_id => '01234012340123401234fffff', :id => '1')
2125+
2126+
get '/lists/2/todos/1'
2127+
assert_equal 'Not Found', @response.body
2128+
assert_raises(ActionController::RoutingError){ list_todo_path(:list_id => '2', :id => '1') }
2129+
end
2130+
21132131
private
21142132
def with_test_routes
21152133
yield

0 commit comments

Comments
 (0)