From 7c95be54b4c3f8ad2273eea39afa233f8f8b31c1 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Sun, 20 May 2012 16:19:11 -0700 Subject: 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 #6413 --- railties/lib/rails/generators/named_base.rb | 4 ++++ .../lib/rails/generators/rails/controller/templates/controller.rb | 4 ++++ .../generators/rails/scaffold_controller/templates/controller.rb | 4 ++++ 3 files changed, 12 insertions(+) (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb index e85d1b8fa2..63703176de 100644 --- a/railties/lib/rails/generators/named_base.rb +++ b/railties/lib/rails/generators/named_base.rb @@ -79,6 +79,10 @@ module Rails @class_path end + def namespaced_file_path + @namespaced_file_path ||= namespaced_class_path.join("/") + end + def namespaced_class_path @namespaced_class_path ||= begin namespace_path = namespace.name.split("::").map {|m| m.underscore } diff --git a/railties/lib/rails/generators/rails/controller/templates/controller.rb b/railties/lib/rails/generators/rails/controller/templates/controller.rb index 52243f4a2f..9b04192126 100644 --- a/railties/lib/rails/generators/rails/controller/templates/controller.rb +++ b/railties/lib/rails/generators/rails/controller/templates/controller.rb @@ -1,3 +1,7 @@ +<% if namespaced? -%> +require "<%= namespaced_file_path %>/application_controller" +<% end -%> + <% module_namespacing do -%> class <%= class_name %>Controller < ApplicationController <% actions.each do |action| -%> diff --git a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb index b95aea5f19..5cd31bf2ee 100644 --- a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +++ b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb @@ -1,3 +1,7 @@ +<% if namespaced? -%> +require "<%= namespaced_file_path %>/application_controller" +<% end -%> + <% module_namespacing do -%> class <%= controller_class_name %>Controller < ApplicationController # GET <%= route_url %> -- cgit v1.2.3