Search code examples
ruby-on-rails-3routesrails-routing

Load routes from lib folder in full rails engine


I made a full engine and converted some plugins to work together with the engine ( I put them in lib/ ) and load them in an initializer engine.rb

This is the structure :

  • app
    • config
      • routes.rb
  • lib
    • plugin
      • config
        • routes.rb

In the routes in app/config/routes.rb I have :

Rails.application.routes.draw do
  match 'help', :to => 'help#index', :as => 'help'
  match 'login', :to => 'sessions#new', :as => 'login'
  match 'logout', :to => 'sessions#destroy', :as => 'logout'
  match 'loadtest', :to => 'loadtests#index', :as => 'loadtest'
end

In the second routes.rb file in ( lib/plugin/config/routes.rb ) I have this :

Rails.application.routes.draw do
   match '/mailchimp/callback', :to => 'mailchimp#callback', :as => 'mailchimp_unsubscribe'
end

In my engine.rb in config/initializers/ I put :

require "#{File.dirname(__FILE__)}/../../lib/plugin/config/routes"

Now when I run rake app:routes I get this as output :

    help  /help(.:format)     help#index
   login  /login(.:format)    sessions#new
  logout  /logout(.:format)   sessions#destroy
loadtest  /loadtest(.:format) loadtests#index

How can I add the routes from the plugin to the engine routes?


Solution

  • Edit config/application.rb and add this line:

    config.paths["config/routes"] << Rails.root.join('lib/plugin/config/routes.rb')
    

    It should works.