SomeSchmo
SomeSchmo

Reputation: 665

Rails 5: Encapsulating route files

I'm refactoring my companies routes file following this SO post to look like the following.

config/application.rb

module YourProject class Application < Rails::Application config.autoload_paths += %W(#{config.root}/config/routes) end end

config/routes/admin_routes.rb

module AdminRoutes def self.extended(router) router.instance_exec do namespace :admin do resources :articles root to: "dashboard#index" end end end end

config/routes.rb

Rails.application.routes.draw do extend AdminRoutes end

However much of our newer RoR code we put into appsules, which are self contained little pieces of the application that contain their own controllers, models, serializers, etc, and someone mentioned how nice it would be if they also contained their own routes. The path to that would look like the following

/appsules/#{appsule_name}/routes.rb

But when I look at the config.paths in my application.rb I don't see any paths pertaining to the appsules directory. Is this possible to read in routes files in that fashion?

Upvotes: 0

Views: 262

Answers (1)

Daniel Westendorf
Daniel Westendorf

Reputation: 3465

Are you updating the autoload path to use the new folder structures? Something like:

module YourProject
  class Application < Rails::Application
    config.autoload_paths += %W(
      #{config.root}/config/routes
      #{config.root}/appsules/appsule1_name/routes.rb
      #{config.root}/appsules/appsule2_name/routes.rb
    )
  end
end

If you want them added dynamically, you should be able to iterate the appsule directory and add these files dynamically to the autoload path.

Upvotes: 1

Related Questions