Skip to content

Commit

Permalink
Use namespace if it's a mountable engine
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Sprenger committed Jun 7, 2011
1 parent 1a06530 commit f182831
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 12 deletions.
1 change: 1 addition & 0 deletions railties/lib/rails/engine/commands.rb
Expand Up @@ -15,6 +15,7 @@
case command case command
when 'generate', 'destroy' when 'generate', 'destroy'
require 'rails/generators' require 'rails/generators'
Rails::Generators.namespace = engine.railtie_namespace
engine.load_generators engine.load_generators
require "rails/commands/#{command}" require "rails/commands/#{command}"


Expand Down
10 changes: 10 additions & 0 deletions railties/lib/rails/generators.rb
Expand Up @@ -90,6 +90,16 @@ def self.options #:nodoc:
@options ||= DEFAULT_OPTIONS.dup @options ||= DEFAULT_OPTIONS.dup
end 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 # Hold configured generators fallbacks. If a plugin developer wants a
# generator group to fallback to another group in case of missing generators, # generator group to fallback to another group in case of missing generators,
# they can add a fallback. # they can add a fallback.
Expand Down
4 changes: 1 addition & 3 deletions railties/lib/rails/generators/named_base.rb
Expand Up @@ -63,9 +63,7 @@ def inside_template?
end end


def namespace def namespace
@namespace ||= if defined?(Rails) && Rails.application Rails::Generators.namespace
Rails.application.class.parents.detect { |n| n.respond_to?(:_railtie) }
end
end end


def namespaced? def namespaced?
Expand Down
5 changes: 5 additions & 0 deletions railties/lib/rails/railtie.rb
@@ -1,6 +1,7 @@
require 'rails/initializable' require 'rails/initializable'
require 'rails/configuration' require 'rails/configuration'
require 'active_support/inflector' require 'active_support/inflector'
require 'active_support/core_ext/module/introspection'


module Rails module Rails
# Railtie is the core of the Rails framework and provides several hooks to extend # Railtie is the core of the Rails framework and provides several hooks to extend
Expand Down Expand Up @@ -192,5 +193,9 @@ def load_tasks(app)
def load_generators(app) def load_generators(app)
self.class.generators.each { |block| block.call(app) } self.class.generators.each { |block| block.call(app) }
end end

def railtie_namespace
@railtie_namespace ||= self.class.parents.detect { |n| n.respond_to?(:_railtie) }
end
end end
end end
53 changes: 44 additions & 9 deletions railties/test/railties/generators_test.rb
Expand Up @@ -30,11 +30,13 @@ def rails(cmd)
`#{Gem.ruby} #{require_environment} #{RAILS_FRAMEWORK_ROOT}/bin/rails #{cmd}` `#{Gem.ruby} #{require_environment} #{RAILS_FRAMEWORK_ROOT}/bin/rails #{cmd}`
end end


def build_engine def build_engine(is_mountable=false)
FileUtils.mkdir_p(engine_path) FileUtils.mkdir_p(engine_path)
FileUtils.rm_r(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 Dir.chdir(engine_path) do
File.open("Gemfile", "w") do |f| File.open("Gemfile", "w") do |f|
Expand All @@ -52,32 +54,65 @@ def build_engine
end end
end end


def build_mountable_engine
build_engine(true)
end

def setup def setup
build_engine
end 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 Dir.chdir(engine_path) do
bundled_rails("g controller topics") 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" assert_no_file "app/controllers/topics_controller.rb"
end end
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 Dir.chdir(engine_path) do
bundled_rails("g model topic") 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" assert_no_file "app/models/topic.rb"
end end
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 Dir.chdir(engine_path) do
bundled_rails("g helper topics") 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" assert_no_file "app/helpers/topics_helper.rb"
end end
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
end end

0 comments on commit f182831

Please sign in to comment.