How to get the list of all engines in Rails 3 app

落花浮王杯 提交于 2019-12-20 23:32:00

问题


According to Rails engines extending functionality in Rails 2.x one could do

Rails::Initializer.new(Rails.configuration).plugin_loader.engines

This code is not working in Rails 3

ActionController::RoutingError (undefined method `new' for Rails::Initializer:Module):
  config/application.rb:12:in `require_or_load'

What do I need to do in Rails 3 to get such list of engines?

This is needed for Extending controllers of a Rails 3 Engine in the main app


回答1:


As of 5/10/2011 and Rails 3.1 beta, it's now

Rails::Application::Railties.engines



回答2:


This has changed with Rails 4.1. The accepted answer is deprecated and the new way to get the installed Engines for a Rails application is now:

::Rails::Engine.subclasses.map(&:instance)

Here's a reference to the commit in github making the change (and also showing how it was implemented after initial deprecation...)

If you need to use the previous solution from Rails 4.1:

module Rails
    class Engine
        class Railties
            def self.engines
                @engines ||= Rails::Engine.subclasses.map(&:instance)
            end
        end
    end
end



回答3:


Try:

Rails::Application.railties.engines



回答4:


For Rails 4 the best way is:

Rails.application.railties


来源:https://stackoverflow.com/questions/5049640/how-to-get-the-list-of-all-engines-in-rails-3-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!