Usually we have this:
module MyApp
class Application < Rails::Application
config...
end
end
But we want this:
# this would be in a seperate gem
module Other
class Application < Rails::Application
config...
end
end
module MyApp
class Application < Other::Application
end
end
module MyOtherApp
class Application < Other::Application
end
end
However because Rails sets Rails.application
when Rails::Application is first inherited from it means that Rails.application
becomes an instance of Other::Application
,
not MyApp::Application
. This in turn means routes do not get loaded correctly, since they are MyApp::Application.routes.draw
and thus Rails.application.routes
is empty and no routes work.
The relevant Rails source is:
railties/lib/rails/application.rb
def inherited(base)
super
Rails.app_class = base
add_lib_to_load_path!(find_root(base.called_from))
end
and
railties/lib/rails.rb
def application
@application ||= (app_class.instance if app_class)
end
The fix is this:
module MyApp
class Application < Other::Application
end
end
Rails.application = MyApp::Application.instance
Is this a good idea, who knows?