1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Correct indent-accounting in controller route generation

Fixes #27447

[Matthew Draper & Yuuji Yaginuma]
This commit is contained in:
Matthew Draper 2016-12-25 16:44:39 +10:30
parent 019cc5960d
commit f27edc84df
2 changed files with 15 additions and 12 deletions

View file

@ -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

View file

@ -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