Class: Konmari::Routes::Loader
- Inherits:
-
Object
- Object
- Konmari::Routes::Loader
- Defined in:
- lib/konmari/routes/loader.rb
Overview
Given a routes directory, recursively load all routes.
Recursively loads all routes in a directory following this algorithm:
-
Load the files in each directory that match PRIORITY_FILES in order listed
-
Load each directory, opening a *new namespace* matching the directory name, and start back at step 1
-
Load all other files in alphabetical order
Be aware that any routes will be respected in the order they are loaded Routes files must use extension .routes
Example file structure:
|- routes/
| |- index.routes
| |- comments.routes
| |- users/
| |- index.routes
| |- friends.routes
Analogous routes definitions
application.routes.draw do
# all index routes
# all comments routes (likely a resource, *must* have first code line of `resource/namespace :comments`)
namespace :users do
# all index routes from `index.routes` file in `users/`
# all friends routes from `friends.routes` in `users/`
end
end
The friends.routes file could then be as simple as:
# routes/users/friends.routes
# NOTE: the resource matches the filename
resources :friends, only: [:index, :create, :delete], path: :my_friends # or any other options passed to resource(s)
Point being, declaring the namespace is unnecessary. This gives us the huge advantage of being able to have our routes files exactly match the file heirarchy of our controllers. We also have the flexibility, through the prioritized files, to add any other routes we might need without being restrained by the filename constraint.
Constant Summary collapse
- PRIORITY_FILES =
The list of filenames which are exempt from the resource name matching rule, and are always loaded first in any directory they are seen in.
[ :priority, :redirects, :index ].freeze
Instance Method Summary collapse
-
#initialize(config) ⇒ Loader
constructor
A new instance of Loader.
Constructor Details
#initialize(config) ⇒ Loader
Returns a new instance of Loader.
56 57 58 59 60 61 |
# File 'lib/konmari/routes/loader.rb', line 56 def initialize(config) @app = config.application @routes_folder = config.routes_path @logger = config.logger build_routes end |