franklin stine
franklin stine

Reputation: 1110

How to programmatically list all controllers and actions in RoR engines?

This question is sort of an extension of what was brought up in this discussion: How to programmatically list all controllers in Rails

It seems most of the solutions for listing out an application's controllers and actions makes use of importing and parsing #{RAILS_ROOT}/app/controllers.

I've been building and making use of RoR Engines which are in #{RAILS_ROOT}/vendor/plugins/

How could these be included to list out every engine's controllers and actions?

Upvotes: 1

Views: 803

Answers (2)

franklin stine
franklin stine

Reputation: 1110

This is pretty old, and rails 3 has changed a lot of things. I've had to go through about 3 different ways of doing this as things have changed. Currently on rails 3.2.2 this has been my best solution:

Rails.application.reload_routes!
all_routes = Rails.application.routes.routes

require 'rails/application/route_inspector'
inspector = Rails::Application::RouteInspector.new
for routeRule in inspector.format(all_routes, ENV['CONTROLLER'])
    # Parse routeRule to get your values
end

Upvotes: 0

franklin stine
franklin stine

Reputation: 1110

Weird how just writing a question out can help you figure it out. I was able to get this working by simply including the engine's controllers by running:

Find.find(File.join(RAILS_ROOT, 'vendor/plugins/'))  { |name|  
    require_dependency(name) if /_controller\.rb$/ =~ name
}

Upvotes: 2

Related Questions