Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

determine the match shorthand target early. #9361

Merged
merged 1 commit into from Feb 21, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions actionpack/CHANGELOG.md
@@ -1,5 +1,19 @@
## Rails 4.0.0 (unreleased) ## ## Rails 4.0.0 (unreleased) ##


* Determine the controller#action from only the matched path when using the
shorthand syntax. Previously the complete path was used, which led
to problems with nesting (scopes and namespaces).
Fixes #7554.

Example:

# this will route to questions#new
scope ':locale' do
get 'questions/new'
end

*Yves Senn*

* Remove support for parsing XML parameters from request. If you still want to parse XML * Remove support for parsing XML parameters from request. If you still want to parse XML
parameters, please install `actionpack-xml_parser' gem. parameters, please install `actionpack-xml_parser' gem.


Expand Down
26 changes: 12 additions & 14 deletions actionpack/lib/action_dispatch/routing/mapper.rb
Expand Up @@ -50,7 +50,6 @@ def constraint_args(constraint, request)
class Mapping #:nodoc: class Mapping #:nodoc:
IGNORE_OPTIONS = [:to, :as, :via, :on, :constraints, :defaults, :only, :except, :anchor, :shallow, :shallow_path, :shallow_prefix, :format] IGNORE_OPTIONS = [:to, :as, :via, :on, :constraints, :defaults, :only, :except, :anchor, :shallow, :shallow_path, :shallow_prefix, :format]
ANCHOR_CHARACTERS_REGEX = %r{\A(\\A|\^)|(\\Z|\\z|\$)\Z} ANCHOR_CHARACTERS_REGEX = %r{\A(\\A|\^)|(\\Z|\\z|\$)\Z}
SHORTHAND_REGEX = %r{/[\w/]+$}
WILDCARD_PATH = %r{\*([^/\)]+)\)?$} WILDCARD_PATH = %r{\*([^/\)]+)\)?$}


attr_reader :scope, :path, :options, :requirements, :conditions, :defaults attr_reader :scope, :path, :options, :requirements, :conditions, :defaults
Expand Down Expand Up @@ -111,17 +110,7 @@ def normalize_options!
@options[:controller] ||= /.+?/ @options[:controller] ||= /.+?/
end end


if using_match_shorthand?(path_without_format, @options) @options.merge!(default_controller_and_action)
to_shorthand = @options[:to].blank?
@options[:to] ||= path_without_format.gsub(/\(.*\)/, "")[1..-1].sub(%r{/([^/]*)$}, '#\1')
end

@options.merge!(default_controller_and_action(to_shorthand))
end

# match "account/overview"
def using_match_shorthand?(path, options)
path && (options[:to] || options[:action]).nil? && path =~ SHORTHAND_REGEX
end end


def normalize_format! def normalize_format!
Expand Down Expand Up @@ -214,7 +203,7 @@ def app
Constraints.new(endpoint, blocks, @set.request_class) Constraints.new(endpoint, blocks, @set.request_class)
end end


def default_controller_and_action(to_shorthand=nil) def default_controller_and_action
if to.respond_to?(:call) if to.respond_to?(:call)
{ } { }
else else
Expand All @@ -227,7 +216,7 @@ def default_controller_and_action(to_shorthand=nil)
controller ||= default_controller controller ||= default_controller
action ||= default_action action ||= default_action


unless controller.is_a?(Regexp) || to_shorthand unless controller.is_a?(Regexp)
controller = [@scope[:module], controller].compact.join("/").presence controller = [@scope[:module], controller].compact.join("/").presence
end end


Expand Down Expand Up @@ -1383,6 +1372,11 @@ def match(path, *rest)
paths = [path] + rest paths = [path] + rest
end 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')
end

options[:anchor] = true unless options.key?(:anchor) options[:anchor] = true unless options.key?(:anchor)


if options[:on] && !VALID_ON_OPTIONS.include?(options[:on]) if options[:on] && !VALID_ON_OPTIONS.include?(options[:on])
Expand All @@ -1393,6 +1387,10 @@ def match(path, *rest)
self self
end end


def using_match_shorthand?(path, options)
path && (options[:to] || options[:action]).nil? && path =~ %r{/[\w/]+$}
end

def decomposed_match(path, options) # :nodoc: def decomposed_match(path, options) # :nodoc:
if on = options.delete(:on) if on = options.delete(:on)
send(on) { decomposed_match(path, options) } send(on) { decomposed_match(path, options) }
Expand Down
27 changes: 27 additions & 0 deletions actionpack/test/dispatch/routing_test.rb
Expand Up @@ -1146,6 +1146,33 @@ def test_match_shorthand_inside_namespace_with_controller
assert_equal 'api/products#list', @response.body assert_equal 'api/products#list', @response.body
end end


def test_match_shorthand_inside_scope_with_variables_with_controller
draw do
scope ':locale' do
match 'questions/new', via: [:get]
end
end

get '/de/questions/new'
assert_equal 'questions#new', @response.body
assert_equal 'de', @request.params[:locale]
end

def test_match_shorthand_inside_nested_namespaces_and_scopes_with_controller
draw do
namespace :api do
namespace :v3 do
scope ':locale' do
get "products/list"
end
end
end
end

get '/api/v3/en/products/list'
assert_equal 'api/v3/products#list', @response.body
end

def test_dynamically_generated_helpers_on_collection_do_not_clobber_resources_url_helper def test_dynamically_generated_helpers_on_collection_do_not_clobber_resources_url_helper
draw do draw do
resources :replies do resources :replies do
Expand Down