mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #41584 from jonathanhefner/route-action-reuse-existing-namespace
Inject route with namespace into existing blocks
This commit is contained in:
commit
3cf6378a5b
2 changed files with 44 additions and 3 deletions
|
@ -268,14 +268,25 @@ module Rails
|
|||
# route "root 'admin#index'", namespace: :admin
|
||||
def route(routing_code, namespace: nil)
|
||||
routing_code = Array(namespace).reverse.reduce(routing_code) do |code, ns|
|
||||
"namespace :#{ns} do\n#{indent(code, 2)}\nend"
|
||||
"namespace :#{ns} do\n#{optimize_indentation(code, 2)}end"
|
||||
end
|
||||
|
||||
log :route, routing_code
|
||||
sentinel = /\.routes\.draw do\s*\n/m
|
||||
|
||||
after_pattern = Array(namespace).each_with_index.reverse_each.reduce(nil) do |pattern, (ns, i)|
|
||||
margin = "\\#{i + 1}[ ]{2}"
|
||||
"(?:(?:^[ ]*\n|^#{margin}.*\n)*?^(#{margin})namespace :#{ns} do\n#{pattern})?"
|
||||
end.then do |pattern|
|
||||
/^([ ]*).+\.routes\.draw do[ ]*\n#{pattern}/
|
||||
end
|
||||
|
||||
in_root do
|
||||
inject_into_file "config/routes.rb", optimize_indentation(routing_code, 2), after: sentinel, verbose: false, force: false
|
||||
if existing = match_file("config/routes.rb", after_pattern)
|
||||
base_indent, *, prev_indent = existing.captures.compact.map(&:length)
|
||||
routing_code = optimize_indentation(routing_code, base_indent + 2).lines.grep_v(/^[ ]{,#{prev_indent}}\S/).join
|
||||
end
|
||||
|
||||
inject_into_file "config/routes.rb", routing_code, after: after_pattern, verbose: false, force: false
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -360,6 +371,10 @@ module Rails
|
|||
match.end_with?("\n") ? "" : "\n#{str}\n"
|
||||
end
|
||||
end
|
||||
|
||||
def match_file(path, pattern)
|
||||
File.read(path).match(pattern) if File.exist?(path)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -565,6 +565,7 @@ class ActionsTest < Rails::Generators::TestCase
|
|||
test "route with namespace option should nest route" do
|
||||
run_generator
|
||||
action :route, "get 'foo'\nget 'bar'", namespace: :baz
|
||||
|
||||
assert_routes <<~ROUTING_CODE.chomp
|
||||
namespace :baz do
|
||||
get 'foo'
|
||||
|
@ -576,6 +577,7 @@ class ActionsTest < Rails::Generators::TestCase
|
|||
test "route with namespace option array should deeply nest route" do
|
||||
run_generator
|
||||
action :route, "get 'foo'\nget 'bar'", namespace: %w[baz qux]
|
||||
|
||||
assert_routes <<~ROUTING_CODE.chomp
|
||||
namespace :baz do
|
||||
namespace :qux do
|
||||
|
@ -586,6 +588,30 @@ class ActionsTest < Rails::Generators::TestCase
|
|||
ROUTING_CODE
|
||||
end
|
||||
|
||||
test "route with namespace option injects into existing namespace blocks" do
|
||||
run_generator
|
||||
action :route, "get 'foo1'\nget 'bar1'", namespace: %w[baz qux]
|
||||
action :route, "get 'foo2'\nget 'bar2'", namespace: %w[baz hoge]
|
||||
action :route, "get 'foo3'\nget 'bar3'", namespace: %w[baz qux hoge]
|
||||
|
||||
assert_routes <<~ROUTING_CODE.chomp
|
||||
namespace :baz do
|
||||
namespace :hoge do
|
||||
get 'foo2'
|
||||
get 'bar2'
|
||||
end
|
||||
namespace :qux do
|
||||
namespace :hoge do
|
||||
get 'foo3'
|
||||
get 'bar3'
|
||||
end
|
||||
get 'foo1'
|
||||
get 'bar1'
|
||||
end
|
||||
end
|
||||
ROUTING_CODE
|
||||
end
|
||||
|
||||
def test_readme
|
||||
run_generator
|
||||
assert_called(Rails::Generators::AppGenerator, :source_root, times: 2, returns: destination_root) do
|
||||
|
|
Loading…
Reference in a new issue