Skip to content

Commit

Permalink
Fix requirements regexp for path segments
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#1772 state:committed]
  • Loading branch information
pixeltrix authored and NZKoz committed Feb 22, 2009
1 parent f7a0a39 commit 3248553
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
28 changes: 12 additions & 16 deletions actionpack/lib/action_controller/routing/segments.rb
Expand Up @@ -191,23 +191,19 @@ def value_regexp
end

def regexp_chunk
if regexp
if regexp_has_modifiers?
"(#{regexp.to_s})"
else
"(#{regexp.source})"
end
else
"([^#{Routing::SEPARATORS.join}]+)"
end
regexp ? regexp_string : default_regexp_chunk
end

def regexp_string
regexp_has_modifiers? ? "(#{regexp.to_s})" : "(#{regexp.source})"
end

def default_regexp_chunk
"([^#{Routing::SEPARATORS.join}]+)"
end

def number_of_captures
if regexp
regexp.number_of_captures + 1
else
1
end
regexp ? regexp.number_of_captures + 1 : 1
end

def build_pattern(pattern)
Expand Down Expand Up @@ -285,8 +281,8 @@ def match_extraction(next_capture)
"params[:#{key}] = PathSegment::Result.new_escaped((match[#{next_capture}]#{" || " + default.inspect if default}).split('/'))#{" if match[" + next_capture + "]" if !default}"
end

def regexp_chunk
regexp || "(.*)"
def default_regexp_chunk
"(.*)"
end

def number_of_captures
Expand Down
24 changes: 24 additions & 0 deletions actionpack/test/controller/routing_test.rb
Expand Up @@ -340,6 +340,30 @@ def test_regexp_should_only_match_possible_controllers
end
end

class PathSegmentTest < Test::Unit::TestCase
def segment(options = {})
unless @segment
@segment = ROUTING::PathSegment.new(:path, options)
end
@segment
end

def test_regexp_chunk_should_return_string
segment = segment(:regexp => /[a-z]+/)
assert_kind_of String, segment.regexp_chunk
end

def test_regexp_chunk_should_be_wrapped_with_parenthesis
segment = segment(:regexp => /[a-z]+/)
assert_equal "([a-z]+)", segment.regexp_chunk
end

def test_regexp_chunk_should_respect_options
segment = segment(:regexp => /[a-z]+/i)
assert_equal "((?i-mx:[a-z]+))", segment.regexp_chunk
end
end

class RouteBuilderTest < Test::Unit::TestCase
def builder
@builder ||= ROUTING::RouteBuilder.new
Expand Down

0 comments on commit 3248553

Please sign in to comment.