Skip to content

Instantly share code, notes, and snippets.

@croaky
Created August 8, 2009 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save croaky/164536 to your computer and use it in GitHub Desktop.
Save croaky/164536 to your computer and use it in GitHub Desktop.
def test_routes_with_format_requirements
ActionController::Routing.with_controllers(['pages']) do
set.draw do |map|
map.public_view ':id.:format',
:controller => 'pages',
:action => 'show',
:requirements => { :method =>'get', :format =>/html/i}
map.public_view ':id.:format',
:controller => 'pages',
:action => 'old_show',
:requirements => { :method =>'get', :format =>/htm/i}
end
assert_equal({:controller => 'pages', :action => 'show', :id => '10',
:method => 'get', :format => 'html'},
set.recognize_path('/10.html'))
assert_equal({:controller => 'pages', :action => 'old_show', :id => '10',
:method => 'get', :format => 'htm'},
set.recognize_path('/10.htm'))
end
end
# actionpack/lib/action_controller/routing/builder.rb
def segment_for(string)
segment =
case string
when /\A\.(:format)?\//
OptionalFormatSegment.new
# actionpack/lib/action_controller/routing/segments.rb
# The OptionalFormatSegment allows for any resource route to have an optional
# :format, which decreases the amount of routes created by 50%.
class OptionalFormatSegment < DynamicSegment
def initialize(key = nil, options = {})
super(:format, {:optional => true}.merge(options))
end
def interpolation_chunk
"." + super
end
def regexp_chunk
'/|(\.[^/?\.]+)?'
end
def to_s
'(.:format)?'
end
# actionpack/lib/action_controller/routing/recognition_optimisation.rb
def to_plain_segments(str)
str = str.dup
str.sub!(/^\/+/,'')
str.sub!(/\/+$/,'')
segments = str.split(/\.[^\/]+\/+|\/+|\.[^\/]+\Z/) # cut off ".format" also
segments << nil
segments
end
1) Failure:
test_routes_with_format_requirements(RouteSetTest)
[./test/controller/routing_test.rb:1859:in `test_routes_with_format_requirements'
./test/../lib/action_controller/routing.rb:290:in `with_controllers'
./test/controller/routing_test.rb:1843:in `test_routes_with_format_requirements'
/Library/Ruby/Gems/1.8/gems/mocha-0.9.7/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb:19:in `__send__'
/Library/Ruby/Gems/1.8/gems/mocha-0.9.7/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb:19:in `run']:
<{:action=>"old_show",
:format=>"htm",
:method=>"get",
:controller=>"pages",
:id=>"10"}> expected but was
<{:action=>"show",
:format=>"htm",
:method=>"get",
:controller=>"pages",
:id=>"10"}>.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment