Skip to content

Commit 6e5e8ba

Browse files
authored
Merge pull request rails#27605 from mtsmfm/fix-generator-command-for-nested-rails-engine-take-2
Fix generator command for nested (namespaced) rails engine (take 2)
2 parents e1a1585 + 4459a18 commit 6e5e8ba

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

railties/lib/rails/generators/named_base.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ def namespaced? # :doc:
8282
!options[:skip_namespace] && namespace
8383
end
8484

85+
def namespace_dirs
86+
@namespace_dirs ||= namespace.name.split("::").map(&:underscore)
87+
end
88+
8589
def file_path # :doc:
8690
@file_path ||= (class_path + [file_name]).join("/")
8791
end
@@ -95,11 +99,11 @@ def regular_class_path # :doc:
9599
end
96100

97101
def namespaced_class_path # :doc:
98-
@namespaced_class_path ||= [namespaced_path] + @class_path
102+
@namespaced_class_path ||= namespace_dirs + @class_path
99103
end
100104

101105
def namespaced_path # :doc:
102-
@namespaced_path ||= namespace.name.split("::").first.underscore
106+
@namespaced_path ||= namespace_dirs.join("/")
103107
end
104108

105109
def class_name # :doc:

railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def create_test_files
2222
def fixture_name
2323
@fixture_name ||=
2424
if mountable_engine?
25-
"%s_%s" % [namespaced_path, table_name]
25+
(namespace_dirs + [table_name]).join("_")
2626
else
2727
table_name
2828
end

railties/test/generators/scaffold_generator_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,26 @@ def test_scaffold_tests_pass_by_default_inside_mountable_engine
492492
end
493493
end
494494

495+
def test_scaffold_tests_pass_by_default_inside_namespaced_mountable_engine
496+
Dir.chdir(destination_root) { `bundle exec rails plugin new bukkits-admin --mountable` }
497+
498+
engine_path = File.join(destination_root, "bukkits-admin")
499+
500+
Dir.chdir(engine_path) do
501+
quietly do
502+
`bin/rails g scaffold User name:string age:integer;
503+
bin/rails db:migrate`
504+
end
505+
506+
assert_file "bukkits-admin/app/controllers/bukkits/admin/users_controller.rb" do |content|
507+
assert_match(/module Bukkits::Admin/, content)
508+
assert_match(/class UsersController < ApplicationController/, content)
509+
end
510+
511+
assert_match(/8 runs, 10 assertions, 0 failures, 0 errors/, `bin/rails test 2>&1`)
512+
end
513+
end
514+
495515
def test_scaffold_tests_pass_by_default_inside_full_engine
496516
Dir.chdir(destination_root) { `bundle exec rails plugin new bukkits --full` }
497517

0 commit comments

Comments
 (0)