From 7915c8df4070cb0c385627e1eaa32ded3030ff0c Mon Sep 17 00:00:00 2001 From: Neil Cook Date: Fri, 11 Jun 2010 23:19:42 +0800 Subject: [PATCH] Second fix to route upgrade bug for :requirements Regexp conversion. Also handles string values, adds additional test and tests pass. --- lib/routes_upgrader.rb | 12 +++++++++++- test/routes_upgrader_test.rb | 5 +++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/routes_upgrader.rb b/lib/routes_upgrader.rb index a47f024..82dea89 100644 --- a/lib/routes_upgrader.rb +++ b/lib/routes_upgrader.rb @@ -185,7 +185,17 @@ module Rails end def opts_to_string(opts) - opts.is_a?(Hash) ? opts.to_a.map {|o, v| ":#{o} => '#{v}'"}.join(", ") : nil + opts.is_a?(Hash) ? opts.map {|k, v| + ":#{k} => " + (v.is_a?(Hash) ? ('{ ' + opts_to_string(v) + ' }') : "#{value_to_string(v)}") + }.join(", ") : opts.to_s + end + + def value_to_string(value) + case value + when Regexp then value.inspect + when String then "'" + value.to_s + "'" + else value.to_s + end end end diff --git a/test/routes_upgrader_test.rb b/test/routes_upgrader_test.rb index e9b7ca9..3d2c6a3 100644 --- a/test/routes_upgrader_test.rb +++ b/test/routes_upgrader_test.rb @@ -94,6 +94,11 @@ end assert_equal "match '/about' => 'static#about', :something => 'extra'", route.to_route_code end + def test_generates_code_for_route_with_requirements + route = Rails::Upgrading::FakeRoute.new("/foo", {:controller => 'foo', :action => 'bar', :requirements => {:digit => /%d/}}) + assert_equal "match '/foo' => 'foo#bar', :constraints => { :digit => /%d/ }", route.to_route_code + end + def test_generates_code_for_root routes_code = " ActionController::Routing::Routes.draw do |map|