Skip to content

Commit

Permalink
Ensure the constraints block is only applied to the correct route
Browse files Browse the repository at this point in the history
addresses issue rails#1907 - any routes that follow a route with a constraints
  block are inheriting the previous route's constraints.
  • Loading branch information
davidtrogers committed Jul 25, 2011
1 parent 02691d3 commit 4dc42f5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
11 changes: 5 additions & 6 deletions actionpack/lib/action_dispatch/routing/mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,12 @@ def default_controller_and_action(to_shorthand=nil)
end

def blocks
block = @scope[:blocks] || []

if @options[:constraints].present? && !@options[:constraints].is_a?(Hash)
block << @options[:constraints]
constraints = @options[:constraints]
if constraints.present? && !constraints.is_a?(Hash)
[constraints]
else
@scope[:blocks] || []
end

block
end

def constraints
Expand Down
20 changes: 20 additions & 0 deletions actionpack/test/dispatch/routing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,12 @@ def self.call(params, request)
match '/countries/:country/(*other)', :to => redirect{ |params, req| params[:other] ? "/countries/all/#{params[:other]}" : '/countries/all' }

match '/:locale/*file.:format', :to => 'files#show', :file => /path\/to\/existing\/file/

scope '/italians' do
match '/writers', :to => 'italians#writers', :constraints => ::TestRoutingMapper::IpRestrictor
match '/sculptors', :to => 'italians#sculptors'
match '/painters/:painter', :to => 'italians#painters', :constraints => {:painter => /michelangelo/}
end
end
end

Expand Down Expand Up @@ -2229,6 +2235,20 @@ def test_symbolized_path_parameters_is_not_stale
verify_redirect 'http://www.example.com/countries/all/cities'
end

def test_constraints_block_not_carried_to_following_routes
get '/italians/writers'
assert_equal 'Not Found', @response.body

get '/italians/sculptors'
assert_equal 'italians#sculptors', @response.body

get '/italians/painters/botticelli'
assert_equal 'Not Found', @response.body

get '/italians/painters/michelangelo'
assert_equal 'italians#painters', @response.body
end

def test_custom_resource_actions_defined_using_string
get '/customers/inactive'
assert_equal 'customers#inactive', @response.body
Expand Down

0 comments on commit 4dc42f5

Please sign in to comment.