Skip to content
This repository
Browse code

Do not overwrite _railtie method on namespace while creating isolated…

… engine or application.

In order to run Engine as standalone application, you will need
Rails::Application instance in the same namespace that engine
one. It's very important to leave _railtie bound to whatever
used "namespace" method first.
  • Loading branch information...
commit 74598fe7e9097484c4029e2731e462c6c82836f9 1 parent 69f97f4
Piotr Sarnacki drogus authored
25 railties/lib/rails/engine.rb
@@ -333,21 +333,22 @@ def endpoint(endpoint = nil)
333 333 def namespace(mod)
334 334 engine_name(generate_railtie_name(mod))
335 335
336   - _railtie = self
337 336 name = engine_name
338   - mod.singleton_class.instance_eval do
339   - define_method(:_railtie) do
340   - _railtie
341   - end
342   -
343   - define_method(:table_name_prefix) do
344   - "#{name}_"
345   - end
346   - end
347   -
348 337 self.routes.default_scope = {:module => name}
349   -
350 338 self.namespaced = true
  339 +
  340 + unless mod.respond_to?(:_railtie)
  341 + _railtie = self
  342 + mod.singleton_class.instance_eval do
  343 + define_method(:_railtie) do
  344 + _railtie
  345 + end
  346 +
  347 + define_method(:table_name_prefix) do
  348 + "#{name}_"
  349 + end
  350 + end
  351 + end
351 352 end
352 353
353 354 def namespaced?
20 railties/test/railties/engine_test.rb
@@ -643,5 +643,25 @@ class Engine < ::Rails::Engine
643 643 Bukkits::Engine.load_seed
644 644 assert Bukkits::Engine.config.bukkits_seeds_loaded
645 645 end
  646 +
  647 + test "using namespace more than once on one module should not overwrite _railtie method" do
  648 + @plugin.write "lib/bukkits.rb", <<-RUBY
  649 + module AppTemplate
  650 + class Engine < ::Rails::Engine
  651 + namespace(AppTemplate)
  652 + end
  653 + end
  654 + RUBY
  655 +
  656 + add_to_config "namespace AppTemplate"
  657 +
  658 + app_file "config/routes.rb", <<-RUBY
  659 + AppTemplate::Application.routes.draw do end
  660 + RUBY
  661 +
  662 + boot_rails
  663 +
  664 + assert_equal AppTemplate._railtie, AppTemplate::Engine
  665 + end
646 666 end
647 667 end

0 comments on commit 74598fe

Please sign in to comment.
Something went wrong with that request. Please try again.