diff options
author | Jeremy Walker <jeremy@meducation.net> | 2012-09-29 16:24:38 +0100 |
---|---|---|
committer | Jeremy Walker <jeremy@meducation.net> | 2012-09-29 16:39:27 +0100 |
commit | a3117335cce7797693c376f132e3bcc52307b9a9 (patch) | |
tree | 5371bc78414153c8728c30bd79eab71b75760454 /railties/lib | |
parent | 4fd0f90549fb91be1c5446472790082ab2f72eae (diff) | |
download | rails-a3117335cce7797693c376f132e3bcc52307b9a9.tar.gz rails-a3117335cce7797693c376f132e3bcc52307b9a9.tar.bz2 rails-a3117335cce7797693c376f132e3bcc52307b9a9.zip |
Fixed generated whitespace in routes when using namespaced resource.
Diffstat (limited to 'railties/lib')
-rw-r--r-- | railties/lib/rails/generators/rails/resource_route/resource_route_generator.rb | 45 |
1 files changed, 41 insertions, 4 deletions
diff --git a/railties/lib/rails/generators/rails/resource_route/resource_route_generator.rb b/railties/lib/rails/generators/rails/resource_route/resource_route_generator.rb index 6a5d62803c..f33d56b564 100644 --- a/railties/lib/rails/generators/rails/resource_route/resource_route_generator.rb +++ b/railties/lib/rails/generators/rails/resource_route/resource_route_generator.rb @@ -1,13 +1,50 @@ module Rails module Generators class ResourceRouteGenerator < NamedBase + + # Properly nests namespaces passed into a generator + # + # $ rails generate resource admin/users/products + # + # should give you + # + # namespace :admin do + # namespace :users + # resources :products + # end + # end def add_resource_route return if options[:actions].present? - route_config = regular_class_path.collect{ |namespace| "namespace :#{namespace} do " }.join(" ") - route_config << "resources :#{file_name.pluralize}" - route_config << " end" * regular_class_path.size - route route_config + + # iterates over all namespaces and opens up blocks + regular_class_path.each_with_index do |namespace, index| + write("namespace :#{namespace} do", index + 1) + end + + # inserts the primary resource + write("resources :#{file_name.pluralize}", route_length + 1) + + # ends blocks + regular_class_path.each_index do |index| + write("end", route_length - index) + end + + # route prepends two spaces onto the front of the string that is passed, this corrects that + route route_string[2..-1] end + + private + def route_string + @route_string ||= "" + end + + def write(str, indent) + route_string << "#{" " * indent}#{str}\n" + end + + def route_length + regular_class_path.length + end end end end |