提交 f1828319 编写于 作者: S Stefan Sprenger

Use namespace if it's a mountable engine

上级 1a06530a
......@@ -15,6 +15,7 @@
case command
when 'generate', 'destroy'
require 'rails/generators'
Rails::Generators.namespace = engine.railtie_namespace
engine.load_generators
require "rails/commands/#{command}"
......
......@@ -90,6 +90,16 @@ def self.options #:nodoc:
@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.
......
......@@ -63,9 +63,7 @@ def inside_template?
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?
......
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 @@ def load_tasks(app)
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
......@@ -30,11 +30,13 @@ def rails(cmd)
`#{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 @@ def build_engine
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册