Skip to content

Commit

Permalink
allow shorthand routes with nested optional parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
dcrec1 committed Oct 10, 2011
1 parent fcb70f3 commit 8f86374
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 3 additions & 3 deletions actionpack/lib/action_dispatch/routing/mapper.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ 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] IGNORE_OPTIONS = [:to, :as, :via, :on, :constraints, :defaults, :only, :except, :anchor, :shallow, :shallow_path, :shallow_prefix]
ANCHOR_CHARACTERS_REGEX = %r{\A(\\A|\^)|(\\Z|\\z|\$)\Z} ANCHOR_CHARACTERS_REGEX = %r{\A(\\A|\^)|(\\Z|\\z|\$)\Z}
SHORTHAND_REGEX = %r{^/[\w/]+$} SHORTHAND_REGEX = %r{/[\w/]+$}
WILDCARD_PATH = %r{\*([^/\)]+)\)?$} WILDCARD_PATH = %r{\*([^/\)]+)\)?$}


def initialize(set, scope, path, options) def initialize(set, scope, path, options)
Expand All @@ -70,7 +70,7 @@ def normalize_options!


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


@options.merge!(default_controller_and_action(to_shorthand)) @options.merge!(default_controller_and_action(to_shorthand))
Expand All @@ -90,7 +90,7 @@ def normalize_options!


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


def normalize_path(path) def normalize_path(path)
Expand Down
11 changes: 11 additions & 0 deletions actionpack/test/dispatch/routing_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ def self.call(params, request)
end end


scope '(:locale)', :locale => /en|pl/ do scope '(:locale)', :locale => /en|pl/ do
get "registrations/new"
resources :descriptions resources :descriptions
root :to => 'projects#index' root :to => 'projects#index'
end end
Expand Down Expand Up @@ -1471,6 +1472,16 @@ def test_nested_optional_scoped_path
end end
end end


def test_nested_optional_path_shorthand
with_test_routes do
get '/registrations/new'
assert @request.params[:locale].nil?

get '/en/registrations/new'
assert 'en', @request.params[:locale]
end
end

def test_default_params def test_default_params
with_test_routes do with_test_routes do
get '/inline_pages' get '/inline_pages'
Expand Down

0 comments on commit 8f86374

Please sign in to comment.