diff options
author | Matthew Draper <matthew@trebex.net> | 2016-12-25 16:44:39 +1030 |
---|---|---|
committer | Matthew Draper <matthew@trebex.net> | 2016-12-25 16:49:23 +1030 |
commit | f27edc84dfe714869a1702206c5c6e57a0810cfc (patch) | |
tree | 34f34ab2d12e19f8a267a5ffa1de6324a7f83439 /railties | |
parent | 019cc5960d25582966733d2bba54553869e67496 (diff) | |
download | rails-f27edc84dfe714869a1702206c5c6e57a0810cfc.tar.gz rails-f27edc84dfe714869a1702206c5c6e57a0810cfc.tar.bz2 rails-f27edc84dfe714869a1702206c5c6e57a0810cfc.zip |
Correct indent-accounting in controller route generation
Fixes #27447
[Matthew Draper & Yuuji Yaginuma]
Diffstat (limited to 'railties')
-rw-r--r-- | railties/lib/rails/generators/rails/controller/controller_generator.rb | 25 | ||||
-rw-r--r-- | railties/test/generators/controller_generator_test.rb | 2 |
2 files changed, 15 insertions, 12 deletions
diff --git a/railties/lib/rails/generators/rails/controller/controller_generator.rb b/railties/lib/rails/generators/rails/controller/controller_generator.rb index 01214dc919..06bdb8b5ce 100644 --- a/railties/lib/rails/generators/rails/controller/controller_generator.rb +++ b/railties/lib/rails/generators/rails/controller/controller_generator.rb @@ -16,7 +16,7 @@ module Rails unless options[:skip_routes] actions.reverse_each do |action| # route prepends two spaces onto the front of the string that is passed, this corrects that. - route generate_routing_code(action) + route indent(generate_routing_code(action), 2)[2..-1] end end end @@ -34,27 +34,30 @@ module Rails # end # end def generate_routing_code(action) - depth = regular_class_path.length + depth = 0 + lines = [] + # Create 'namespace' ladder # namespace :foo do # namespace :bar do - namespace_ladder = regular_class_path.each_with_index.map do |ns, i| - indent(" namespace :#{ns} do\n", i * 2) - end.join[2..-1] + regular_class_path.each do |ns| + lines << indent("namespace :#{ns} do\n", depth * 2) + depth += 1 + end # Create route # get 'baz/index' - route = indent(%{ get '#{file_name}/#{action}'\n}, depth * 2) + lines << indent(%{get '#{file_name}/#{action}'\n}, depth * 2) # Create `end` ladder # end # end - end_ladder = (1..depth).reverse_each.map do |i| - indent("end\n", i * 2) - end.join + until depth.zero? + depth -= 1 + lines << indent("end\n", depth * 2) + end - # Combine the 3 parts to generate complete route entry - "#{namespace_ladder}#{route}#{end_ladder}" + lines.join end end end diff --git a/railties/test/generators/controller_generator_test.rb b/railties/test/generators/controller_generator_test.rb index 9b986a636a..af86a0136f 100644 --- a/railties/test/generators/controller_generator_test.rb +++ b/railties/test/generators/controller_generator_test.rb @@ -65,7 +65,7 @@ class ControllerGeneratorTest < Rails::Generators::TestCase def test_add_routes run_generator - assert_file "config/routes.rb", /get 'account\/foo'/, /get 'account\/bar'/ + assert_file "config/routes.rb", /^ get 'account\/foo'/, /^ get 'account\/bar'/ end def test_skip_routes |