Skip to content

Commit 3cf6378

Browse files
authored
Merge pull request rails#41584 from jonathanhefner/route-action-reuse-existing-namespace
Inject route with namespace into existing blocks
2 parents f77ec9a + 106e20d commit 3cf6378

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

railties/lib/rails/generators/actions.rb

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,25 @@ def rails_command(command, options = {})
268268
# route "root 'admin#index'", namespace: :admin
269269
def route(routing_code, namespace: nil)
270270
routing_code = Array(namespace).reverse.reduce(routing_code) do |code, ns|
271-
"namespace :#{ns} do\n#{indent(code, 2)}\nend"
271+
"namespace :#{ns} do\n#{optimize_indentation(code, 2)}end"
272272
end
273273

274274
log :route, routing_code
275-
sentinel = /\.routes\.draw do\s*\n/m
275+
276+
after_pattern = Array(namespace).each_with_index.reverse_each.reduce(nil) do |pattern, (ns, i)|
277+
margin = "\\#{i + 1}[ ]{2}"
278+
"(?:(?:^[ ]*\n|^#{margin}.*\n)*?^(#{margin})namespace :#{ns} do\n#{pattern})?"
279+
end.then do |pattern|
280+
/^([ ]*).+\.routes\.draw do[ ]*\n#{pattern}/
281+
end
276282

277283
in_root do
278-
inject_into_file "config/routes.rb", optimize_indentation(routing_code, 2), after: sentinel, verbose: false, force: false
284+
if existing = match_file("config/routes.rb", after_pattern)
285+
base_indent, *, prev_indent = existing.captures.compact.map(&:length)
286+
routing_code = optimize_indentation(routing_code, base_indent + 2).lines.grep_v(/^[ ]{,#{prev_indent}}\S/).join
287+
end
288+
289+
inject_into_file "config/routes.rb", routing_code, after: after_pattern, verbose: false, force: false
279290
end
280291
end
281292

@@ -360,6 +371,10 @@ def append_file_with_newline(path, str, options = {})
360371
match.end_with?("\n") ? "" : "\n#{str}\n"
361372
end
362373
end
374+
375+
def match_file(path, pattern)
376+
File.read(path).match(pattern) if File.exist?(path)
377+
end
363378
end
364379
end
365380
end

railties/test/generators/actions_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ def test_initializer_should_write_date_to_file_with_block_in_config_initializers
565565
test "route with namespace option should nest route" do
566566
run_generator
567567
action :route, "get 'foo'\nget 'bar'", namespace: :baz
568+
568569
assert_routes <<~ROUTING_CODE.chomp
569570
namespace :baz do
570571
get 'foo'
@@ -576,6 +577,7 @@ def test_initializer_should_write_date_to_file_with_block_in_config_initializers
576577
test "route with namespace option array should deeply nest route" do
577578
run_generator
578579
action :route, "get 'foo'\nget 'bar'", namespace: %w[baz qux]
580+
579581
assert_routes <<~ROUTING_CODE.chomp
580582
namespace :baz do
581583
namespace :qux do
@@ -586,6 +588,30 @@ def test_initializer_should_write_date_to_file_with_block_in_config_initializers
586588
ROUTING_CODE
587589
end
588590

591+
test "route with namespace option injects into existing namespace blocks" do
592+
run_generator
593+
action :route, "get 'foo1'\nget 'bar1'", namespace: %w[baz qux]
594+
action :route, "get 'foo2'\nget 'bar2'", namespace: %w[baz hoge]
595+
action :route, "get 'foo3'\nget 'bar3'", namespace: %w[baz qux hoge]
596+
597+
assert_routes <<~ROUTING_CODE.chomp
598+
namespace :baz do
599+
namespace :hoge do
600+
get 'foo2'
601+
get 'bar2'
602+
end
603+
namespace :qux do
604+
namespace :hoge do
605+
get 'foo3'
606+
get 'bar3'
607+
end
608+
get 'foo1'
609+
get 'bar1'
610+
end
611+
end
612+
ROUTING_CODE
613+
end
614+
589615
def test_readme
590616
run_generator
591617
assert_called(Rails::Generators::AppGenerator, :source_root, times: 2, returns: destination_root) do

0 commit comments

Comments
 (0)