Skip to content

Commit

Permalink
Fix shorthand routes where controller and action are in the scope
Browse files Browse the repository at this point in the history
Merge `:action` from routing scope and assign endpoint if both `:controller`
and `:action` are present. The endpoint assignment only occurs if there is
no `:to` present in the options hash so should only affect routes using the
shorthand syntax (i.e. endpoint is inferred from the the path).

Fixes #9856

Backport of 37b4276
  • Loading branch information
pixeltrix committed Jun 25, 2013
1 parent ca23e6d commit 622e4ab
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
9 changes: 9 additions & 0 deletions actionpack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
## unreleased ##

* Merge `:action` from routing scope and assign endpoint if both `:controller`
and `:action` are present. The endpoint assignment only occurs if there is
no `:to` present in the options hash so should only affect routes using the
shorthand syntax (i.e. endpoint is inferred from the the path).

Fixes #9856

*Yves Senn*, *Andrew White*

* Always escape the result of `link_to_unless` method.

Before:
Expand Down
8 changes: 8 additions & 0 deletions actionpack/lib/action_dispatch/routing/mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,10 @@ def merge_controller_scope(parent, child) #:nodoc:
child
end

def merge_action_scope(parent, child) #:nodoc:
child
end

def merge_path_names_scope(parent, child) #:nodoc:
merge_options_scope(parent, child)
end
Expand Down Expand Up @@ -1253,6 +1257,10 @@ def match(path, *rest)
paths = [path] + rest
end

if @scope[:controller] && @scope[:action]
options[:to] ||= "#{@scope[:controller]}##{@scope[:action]}"
end

path_without_format = path.to_s.sub(/\(\.:format\)$/, '')
if using_match_shorthand?(path_without_format, options)
options[:to] ||= path_without_format.gsub(%r{^/}, "").sub(%r{/([^/]*)$}, '#\1')
Expand Down
13 changes: 13 additions & 0 deletions actionpack/test/dispatch/routing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,12 @@ def self.call(params, request)
end
end
end

scope '/job', controller: 'job' do
scope ':id', action: 'manage_applicant' do
get "/active"
end
end
end
end

Expand Down Expand Up @@ -1444,6 +1450,13 @@ def test_match_shorthand_inside_nested_namespaces_and_scopes_with_controller
end
end

def test_controller_option_with_nesting_and_leading_slash
with_test_routes do
get '/job/5/active'
assert_equal 'job#manage_applicant', @response.body
end
end

def test_dynamically_generated_helpers_on_collection_do_not_clobber_resources_url_helper
with_test_routes do
assert_equal '/replies', replies_path
Expand Down

0 comments on commit 622e4ab

Please sign in to comment.