Skip to content

Commit b0f8355

Browse files
committed
Fix generators to help with ambiguous ApplicationController issue
In development mode, dependencies are loaded dynamically at runtime, using `const_missing`. Because of that, when one of the constants is already loaded and `const_missing` is not triggered, user can end up with unexpected results. Given such file in an Engine: ```ruby module Blog class PostsController < ApplicationController end end ``` If you load it first, before loading any application files, it will correctly load `Blog::ApplicationController`, because second line will hit `const_missing`. However if you load `ApplicationController` first, the constant will be loaded already, `const_missing` hook will not be fired and in result `PostsController` will inherit from `ApplicationController` instead of `Blog::ApplicationController`. Since it can't be fixed in `AS::Dependencies`, the easiest fix is to just explicitly load application controller. closes rails#6413
1 parent 68a454c commit b0f8355

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

railties/lib/rails/generators/named_base.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ def regular_class_path
8282
@class_path
8383
end
8484

85+
def namespaced_file_path
86+
@namespaced_file_path ||= namespaced_class_path.join("/")
87+
end
88+
8589
def namespaced_class_path
8690
@namespaced_class_path ||= begin
8791
namespace_path = namespace.name.split("::").map {|m| m.underscore }

railties/lib/rails/generators/rails/controller/templates/controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<% if namespaced? -%>
2+
require "<%= namespaced_file_path %>/application_controller"
3+
<% end -%>
4+
15
<% module_namespacing do -%>
26
class <%= class_name %>Controller < ApplicationController
37
<% actions.each do |action| -%>

railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<% if namespaced? -%>
2+
require "<%= namespaced_file_path %>/application_controller"
3+
<% end -%>
4+
15
<% module_namespacing do -%>
26
class <%= controller_class_name %>Controller < ApplicationController
37
# GET <%= route_url %>

railties/test/generators/namespaced_generators_test.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@ class NamespacedControllerGeneratorTest < NamespacedGeneratorTestCase
2020

2121
def test_namespaced_controller_skeleton_is_created
2222
run_generator
23-
assert_file "app/controllers/test_app/account_controller.rb", /module TestApp/, / class AccountController < ApplicationController/
24-
assert_file "test/functional/test_app/account_controller_test.rb", /module TestApp/, / class AccountControllerTest/
23+
assert_file "app/controllers/test_app/account_controller.rb",
24+
/require "test_app\/application_controller"/,
25+
/module TestApp/,
26+
/ class AccountController < ApplicationController/
27+
28+
assert_file "test/functional/test_app/account_controller_test.rb",
29+
/module TestApp/,
30+
/ class AccountControllerTest/
2531
end
2632

2733
def test_skipping_namespace
@@ -222,9 +228,10 @@ def test_scaffold_on_invoke
222228
end
223229

224230
# Controller
225-
assert_file "app/controllers/test_app/product_lines_controller.rb" do |content|
226-
assert_match(/module TestApp\n class ProductLinesController < ApplicationController/, content)
227-
end
231+
assert_file "app/controllers/test_app/product_lines_controller.rb",
232+
/require "test_app\/application_controller"/,
233+
/module TestApp/,
234+
/class ProductLinesController < ApplicationController/
228235

229236
assert_file "test/functional/test_app/product_lines_controller_test.rb",
230237
/module TestApp\n class ProductLinesControllerTest < ActionController::TestCase/

0 commit comments

Comments
 (0)