From 190455ded6c50550067e99393ac6c57d39041a3c Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 28 Apr 2011 16:27:09 -0400 Subject: [PATCH] support for options in namespaces --- lib/routes_upgrader.rb | 20 +++++++++++++------- test/routes_upgrader_test.rb | 10 +++++++++- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/routes_upgrader.rb b/lib/routes_upgrader.rb index 033c082..d474b1f 100644 --- a/lib/routes_upgrader.rb +++ b/lib/routes_upgrader.rb @@ -139,9 +139,9 @@ module Rails end end - def namespace(name) + def namespace(name, options = {}) debug "mapping namespace #{name}" - namespace = FakeNamespace.new(name) + namespace = FakeNamespace.new(name, options) namespace = stack(namespace) do yield(self) @@ -200,16 +200,22 @@ module Rails end class FakeNamespace < RouteObject - attr_accessor :routes, :name + attr_accessor :routes, :name, :options - def initialize(name) + def initialize(name, options = {}) @routes = [] - @name = name + @name, @options = name, options @indent = RouteRedrawer.indent end def to_route_code - lines = ["namespace :#{@name} do", @routes.map {|r| r.to_route_code}, "end"] + if !@options.empty? + options = ', ' + opts_to_string(@options) + else + options = '' + end + + lines = ["namespace :#{@name}#{options} do", @routes.map {|r| r.to_route_code}, "end"] indent_lines(lines) end @@ -357,4 +363,4 @@ module Rails end end end -end \ No newline at end of file +end diff --git a/test/routes_upgrader_test.rb b/test/routes_upgrader_test.rb index fad5843..f2f2c15 100644 --- a/test/routes_upgrader_test.rb +++ b/test/routes_upgrader_test.rb @@ -74,6 +74,14 @@ end assert_equal "namespace :static do\nmatch '/about' => 'static#about'\nend\n", ns.to_route_code end + def test_generates_code_for_namespace_with_options + ns = Rails::Upgrading::FakeNamespace.new("static", { :path_prefix => 'prefix' }) + # Add a route to the namespace + ns << Rails::Upgrading::FakeRoute.new("/about", {:controller => 'static', :action => 'about'}) + + assert_equal "namespace :static, :path_prefix => 'prefix' do\nmatch '/about' => 'static#about'\nend\n", ns.to_route_code + end + def test_generates_code_for_resources route = Rails::Upgrading::FakeResourceRoute.new("hats") assert_equal "resources :hats", route.to_route_code @@ -173,4 +181,4 @@ end upgrader.routes_code = routes_code assert_equal new_routes_code.strip, upgrader.generate_new_routes.strip end -end \ No newline at end of file +end