Skip to content
This repository
Browse code

Fixed generated whitespace in routes when using namespaced resource.

  • Loading branch information...
commit a3117335cce7797693c376f132e3bcc52307b9a9 1 parent 4fd0f90
Jeremy Walker iHiD authored
45 railties/lib/rails/generators/rails/resource_route/resource_route_generator.rb
... ... @@ -1,13 +1,50 @@
1 1 module Rails
2 2 module Generators
3 3 class ResourceRouteGenerator < NamedBase
  4 +
  5 + # Properly nests namespaces passed into a generator
  6 + #
  7 + # $ rails generate resource admin/users/products
  8 + #
  9 + # should give you
  10 + #
  11 + # namespace :admin do
  12 + # namespace :users
  13 + # resources :products
  14 + # end
  15 + # end
4 16 def add_resource_route
5 17 return if options[:actions].present?
6   - route_config = regular_class_path.collect{ |namespace| "namespace :#{namespace} do " }.join(" ")
7   - route_config << "resources :#{file_name.pluralize}"
8   - route_config << " end" * regular_class_path.size
9   - route route_config
  18 +
  19 + # iterates over all namespaces and opens up blocks
  20 + regular_class_path.each_with_index do |namespace, index|
  21 + write("namespace :#{namespace} do", index + 1)
  22 + end
  23 +
  24 + # inserts the primary resource
  25 + write("resources :#{file_name.pluralize}", route_length + 1)
  26 +
  27 + # ends blocks
  28 + regular_class_path.each_index do |index|
  29 + write("end", route_length - index)
  30 + end
  31 +
  32 + # route prepends two spaces onto the front of the string that is passed, this corrects that
  33 + route route_string[2..-1]
10 34 end
  35 +
  36 + private
  37 + def route_string
  38 + @route_string ||= ""
  39 + end
  40 +
  41 + def write(str, indent)
  42 + route_string << "#{" " * indent}#{str}\n"
  43 + end
  44 +
  45 + def route_length
  46 + regular_class_path.length
  47 + end
11 48 end
12 49 end
13 50 end
76 railties/test/generators/namespaced_generators_test.rb
@@ -304,7 +304,7 @@ def test_scaffold_with_namespace_on_invoke
304 304
305 305 # Route
306 306 assert_file "config/routes.rb" do |route|
307   - assert_match(/namespace :admin do resources :roles end$/, route)
  307 + assert_match(/^ namespace :admin do\n resources :roles\n end$/, route)
308 308 end
309 309
310 310 # Controller
@@ -346,7 +346,7 @@ def test_scaffold_with_namespace_on_revoke
346 346
347 347 # Route
348 348 assert_file "config/routes.rb" do |route|
349   - assert_no_match(/namespace :admin do resources :roles end$/, route)
  349 + assert_no_match(/^ namespace :admin do\n resources :roles\n end$$/, route)
350 350 end
351 351
352 352 # Controller
@@ -364,4 +364,76 @@ def test_scaffold_with_namespace_on_revoke
364 364 # Stylesheets (should not be removed)
365 365 assert_file "app/assets/stylesheets/scaffold.css"
366 366 end
  367 +
  368 + def test_scaffold_with_nested_namespace_on_invoke
  369 + run_generator [ "admin/user/special/role", "name:string", "description:string" ]
  370 +
  371 + # Model
  372 + assert_file "app/models/test_app/admin/user/special.rb", /module TestApp\n module Admin/
  373 + assert_file "app/models/test_app/admin/user/special/role.rb", /module TestApp\n class Admin::User::Special::Role < ActiveRecord::Base/
  374 + assert_file "test/unit/test_app/admin/user/special/role_test.rb", /module TestApp\n class Admin::User::Special::RoleTest < ActiveSupport::TestCase/
  375 + assert_file "test/fixtures/test_app/admin/user/special/roles.yml"
  376 + assert_migration "db/migrate/create_test_app_admin_user_special_roles.rb"
  377 +
  378 + # Route
  379 + assert_file "config/routes.rb" do |route|
  380 + assert_match(/^ namespace :admin do\n namespace :user do\n namespace :special do\n resources :roles\n end\n end\n end$/, route)
  381 + end
  382 +
  383 + # Controller
  384 + assert_file "app/controllers/test_app/admin/user/special/roles_controller.rb" do |content|
  385 + assert_match(/module TestApp\n class Admin::User::Special::RolesController < ApplicationController/, content)
  386 + end
  387 +
  388 + assert_file "test/functional/test_app/admin/user/special/roles_controller_test.rb",
  389 + /module TestApp\n class Admin::User::Special::RolesControllerTest < ActionController::TestCase/
  390 +
  391 + # Views
  392 + %w(
  393 + index
  394 + edit
  395 + new
  396 + show
  397 + _form
  398 + ).each { |view| assert_file "app/views/test_app/admin/user/special/roles/#{view}.html.erb" }
  399 + assert_no_file "app/views/layouts/admin/user/special/roles.html.erb"
  400 +
  401 + # Helpers
  402 + assert_file "app/helpers/test_app/admin/user/special/roles_helper.rb"
  403 + assert_file "test/unit/helpers/test_app/admin/user/special/roles_helper_test.rb"
  404 +
  405 + # Stylesheets
  406 + assert_file "app/assets/stylesheets/scaffold.css"
  407 + end
  408 +
  409 + def test_scaffold_with_nested_namespace_on_revoke
  410 + run_generator [ "admin/user/special/role", "name:string", "description:string" ]
  411 + run_generator [ "admin/user/special/role" ], :behavior => :revoke
  412 +
  413 + # Model
  414 + assert_file "app/models/test_app/admin/user/special.rb" # ( should not be remove )
  415 + assert_no_file "app/models/test_app/admin/user/special/role.rb"
  416 + assert_no_file "test/unit/test_app/admin/user/special/role_test.rb"
  417 + assert_no_file "test/fixtures/test_app/admin/user/special/roles.yml"
  418 + assert_no_migration "db/migrate/create_test_app_admin_user_special_roles.rb"
  419 +
  420 + # Route
  421 + assert_file "config/routes.rb" do |route|
  422 + assert_no_match(/^ namespace :admin do\n namespace :user do\n namespace :special do\n resources :roles\n end\n end\n end$/, route)
  423 + end
  424 +
  425 + # Controller
  426 + assert_no_file "app/controllers/test_app/admin/user/special/roles_controller.rb"
  427 + assert_no_file "test/functional/test_app/admin/user/special/roles_controller_test.rb"
  428 +
  429 + # Views
  430 + assert_no_file "app/views/test_app/admin/user/special/roles"
  431 +
  432 + # Helpers
  433 + assert_no_file "app/helpers/test_app/admin/user/special/roles_helper.rb"
  434 + assert_no_file "test/unit/helpers/test_app/admin/user/special/roles_helper_test.rb"
  435 +
  436 + # Stylesheets (should not be removed)
  437 + assert_file "app/assets/stylesheets/scaffold.css"
  438 + end
367 439 end

0 comments on commit a311733

Please sign in to comment.
Something went wrong with that request. Please try again.