Skip to content

Commit

Permalink
Correct indent-accounting in controller route generation
Browse files Browse the repository at this point in the history
Fixes #27447

[Matthew Draper & Yuuji Yaginuma]
  • Loading branch information
matthewd committed Dec 25, 2016
1 parent 019cc59 commit f27edc8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
Expand Up @@ -16,7 +16,7 @@ def add_routes
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
Expand All @@ -34,27 +34,30 @@ def add_routes
# 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
Expand Down
2 changes: 1 addition & 1 deletion railties/test/generators/controller_generator_test.rb
Expand Up @@ -65,7 +65,7 @@ def test_invokes_default_template_engine

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
Expand Down

0 comments on commit f27edc8

Please sign in to comment.