From f18283194bfb43fc020be37d6fa7cd3149fa8b5e Mon Sep 17 00:00:00 2001 From: Stefan Sprenger Date: Tue, 7 Jun 2011 11:21:38 +0200 Subject: Use namespace if it's a mountable engine --- railties/lib/rails/engine/commands.rb | 1 + railties/lib/rails/generators.rb | 10 ++++++ railties/lib/rails/generators/named_base.rb | 4 +-- railties/lib/rails/railtie.rb | 5 +++ railties/test/railties/generators_test.rb | 53 ++++++++++++++++++++++++----- 5 files changed, 61 insertions(+), 12 deletions(-) (limited to 'railties') diff --git a/railties/lib/rails/engine/commands.rb b/railties/lib/rails/engine/commands.rb index 59b7c4d67f..3b0920e213 100644 --- a/railties/lib/rails/engine/commands.rb +++ b/railties/lib/rails/engine/commands.rb @@ -15,6 +15,7 @@ engine = ::Rails::Engine.find(ENGINE_ROOT) case command when 'generate', 'destroy' require 'rails/generators' + Rails::Generators.namespace = engine.railtie_namespace engine.load_generators require "rails/commands/#{command}" diff --git a/railties/lib/rails/generators.rb b/railties/lib/rails/generators.rb index 09e505a75b..508d5359ba 100644 --- a/railties/lib/rails/generators.rb +++ b/railties/lib/rails/generators.rb @@ -90,6 +90,16 @@ module Rails @options ||= DEFAULT_OPTIONS.dup end + def self.namespace + @namespace ||= if defined?(Rails) && Rails.application + Rails.application.class.parents.detect { |n| n.respond_to?(:_railtie) } + end + end + + def self.namespace=(namespace) + @namespace ||= namespace + end + # Hold configured generators fallbacks. If a plugin developer wants a # generator group to fallback to another group in case of missing generators, # they can add a fallback. diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb index 7e7f8d2d08..c6c0392f43 100644 --- a/railties/lib/rails/generators/named_base.rb +++ b/railties/lib/rails/generators/named_base.rb @@ -63,9 +63,7 @@ module Rails end def namespace - @namespace ||= if defined?(Rails) && Rails.application - Rails.application.class.parents.detect { |n| n.respond_to?(:_railtie) } - end + Rails::Generators.namespace end def namespaced? diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index 65c567d72f..5f50943626 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -1,6 +1,7 @@ require 'rails/initializable' require 'rails/configuration' require 'active_support/inflector' +require 'active_support/core_ext/module/introspection' module Rails # Railtie is the core of the Rails framework and provides several hooks to extend @@ -192,5 +193,9 @@ module Rails def load_generators(app) self.class.generators.each { |block| block.call(app) } end + + def railtie_namespace + @railtie_namespace ||= self.class.parents.detect { |n| n.respond_to?(:_railtie) } + end end end diff --git a/railties/test/railties/generators_test.rb b/railties/test/railties/generators_test.rb index 69ba03673e..4f96e4d9ef 100644 --- a/railties/test/railties/generators_test.rb +++ b/railties/test/railties/generators_test.rb @@ -30,11 +30,13 @@ module RailtiesTests `#{Gem.ruby} #{require_environment} #{RAILS_FRAMEWORK_ROOT}/bin/rails #{cmd}` end - def build_engine + def build_engine(is_mountable=false) FileUtils.mkdir_p(engine_path) FileUtils.rm_r(engine_path) - rails("plugin new #{engine_path} --full --mountable") + mountable = is_mountable ? "--mountable" : "" + + rails("plugin new #{engine_path} --full #{mountable}") Dir.chdir(engine_path) do File.open("Gemfile", "w") do |f| @@ -52,32 +54,65 @@ module RailtiesTests end end + def build_mountable_engine + build_engine(true) + end + def setup - build_engine end - def test_controllers_are_correctly_namespaced + def test_controllers_are_correctly_namespaced_when_engine_is_mountable + build_mountable_engine Dir.chdir(engine_path) do bundled_rails("g controller topics") - assert_file "app/controllers/foo_bar/topics_controller.rb", /FooBar::TopicsController/ + assert_file "app/controllers/foo_bar/topics_controller.rb", /module FooBar\n class TopicsController/ assert_no_file "app/controllers/topics_controller.rb" end end - def test_models_are_correctly_namespaced + def test_models_are_correctly_namespaced_when_engine_is_mountable + build_mountable_engine Dir.chdir(engine_path) do bundled_rails("g model topic") - assert_file "app/models/foo_bar/topic.rb", /FooBar::Topic/ + assert_file "app/models/foo_bar/topic.rb", /module FooBar\n class Topic/ assert_no_file "app/models/topic.rb" end end - def test_helpers_are_correctly_namespaced + def test_helpers_are_correctly_namespaced_when_engine_is_mountable + build_mountable_engine Dir.chdir(engine_path) do bundled_rails("g helper topics") - assert_file "app/helpers/foo_bar/topics_helper.rb", /FooBar::TopicsHelper/ + assert_file "app/helpers/foo_bar/topics_helper.rb", /module FooBar\n module TopicsHelper/ assert_no_file "app/helpers/topics_helper.rb" end end + + def test_controllers_are_not_namespaced_when_engine_is_not_mountable + build_engine + Dir.chdir(engine_path) do + bundled_rails("g controller topics") + assert_file "app/controllers/topics_controller.rb", /class TopicsController/ + assert_no_file "app/controllers/foo_bar/topics_controller.rb" + end + end + + def test_models_are_not_namespaced_when_engine_is_not_mountable + build_engine + Dir.chdir(engine_path) do + bundled_rails("g model topic") + assert_file "app/models/topic.rb", /class Topic/ + assert_no_file "app/models/foo_bar/topic.rb" + end + end + + def test_helpers_are_not_namespaced_when_engine_is_not_mountable + build_engine + Dir.chdir(engine_path) do + bundled_rails("g helper topics") + assert_file "app/helpers/topics_helper.rb", /module TopicsHelper/ + assert_no_file "app/helpers/foo_bar/topics_helper.rb" + end + end end end -- cgit v1.2.3