Skip to content

Commit

Permalink
Fix generator command for nested (namespaced) rails engine
Browse files Browse the repository at this point in the history
If we create nested (namespaced) rails engine such like bukkits-admin,
`bin/rails g scaffold User name:string age:integer`
will create
`bukkits-admin/app/controllers/bukkits/users_controller.rb`
but it should create
`bukkits-admin/app/controllers/bukkits/admin/users_controller.rb`.

In rails#6643, we changed `namespaced_path` as root path
because we supposed application_controller is always in root
but nested rails engine's application_controller will not.
  • Loading branch information
mtsmfm committed Jan 3, 2017
1 parent 33e6051 commit 085546d
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def file_name # :doc:

def application_mailer_file_name
@_application_mailer_file_name ||= if mountable_engine?
"app/mailers/#{namespaced_path}/application_mailer.rb"
File.join("app/mailers", namespaced_path, "application_mailer.rb")
else
"app/mailers/application_mailer.rb"
end
Expand Down
2 changes: 1 addition & 1 deletion activejob/lib/rails/generators/job/job_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def create_job_file
private
def application_job_file_name
@application_job_file_name ||= if mountable_engine?
"app/jobs/#{namespaced_path}/application_job.rb"
File.join("app/jobs", namespaced_path, "application_job.rb")
else
"app/jobs/application_job.rb"
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def application_record_exist?

def application_record_file_name
@application_record_file_name ||= if mountable_engine?
"app/models/#{namespaced_path}/application_record.rb"
File.join("app/models", namespaced_path, "application_record.rb")
else
"app/models/application_record.rb"
end
Expand Down
4 changes: 2 additions & 2 deletions railties/lib/rails/generators/named_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ def regular_class_path # :doc:
end

def namespaced_class_path # :doc:
@namespaced_class_path ||= [namespaced_path] + @class_path
@namespaced_class_path ||= namespaced_path + @class_path
end

def namespaced_path # :doc:
@namespaced_path ||= namespace.name.split("::").first.underscore
@namespaced_path ||= namespace.name.split("::").map(&:underscore)
end

def class_name # :doc:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% if namespaced? -%>
require_dependency "<%= namespaced_path %>/application_controller"
require_dependency "<%= File.join(namespaced_path) %>/application_controller"
<% end -%>
<% module_namespacing do -%>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% if namespaced? -%>
require_dependency "<%= namespaced_path %>/application_controller"
require_dependency "<%= File.join(namespaced_path) %>/application_controller"
<% end -%>
<% module_namespacing do -%>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% if namespaced? -%>
require_dependency "<%= namespaced_path %>/application_controller"
require_dependency "<%= File.join(namespaced_path) %>/application_controller"
<% end -%>
<% module_namespacing do -%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def create_test_files
def fixture_name
@fixture_name ||=
if mountable_engine?
"%s_%s" % [namespaced_path, table_name]
"%s_%s" % [namespaced_path.join("_"), table_name]
else
table_name
end
Expand Down
20 changes: 20 additions & 0 deletions railties/test/generators/scaffold_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,26 @@ def test_scaffold_tests_pass_by_default_inside_mountable_engine
end
end

def test_scaffold_tests_pass_by_default_inside_namespaced_mountable_engine
Dir.chdir(destination_root) { `bundle exec rails plugin new bukkits-admin --mountable` }

engine_path = File.join(destination_root, "bukkits-admin")

Dir.chdir(engine_path) do
quietly do
`bin/rails g scaffold User name:string age:integer;
bin/rails db:migrate`
end

assert_file "bukkits-admin/app/controllers/bukkits/admin/users_controller.rb" do |content|
assert_match(/module Bukkits::Admin/, content)
assert_match(/class UsersController < ApplicationController/, content)
end

assert_match(/8 runs, 10 assertions, 0 failures, 0 errors/, `bin/rails test 2>&1`)
end
end

def test_scaffold_tests_pass_by_default_inside_full_engine
Dir.chdir(destination_root) { `bundle exec rails plugin new bukkits --full` }

Expand Down

0 comments on commit 085546d

Please sign in to comment.