In routes.rb of gitlab, I found something like draw: api
and found it very useful, so I will summarize it.
draw
If you write something like draw: api
, it will read the routes described in config/routes/api.rb
.
For example, you can make it easier to understand by writing the routing in a separate file for each namespace.
It is not prepared by default, but it was prepared independently in the source code of gitlab, so some preparation is required.
Create a draw_routes.rb
file under config/initializers
and describe the following contents.
It's almost the same as the gitlab source code.
module DrawRoute
RoutesNotFound = Class.new(StandardError)
def draw(routes_name)
drawn_any = draw_route(routes_name)
drawn_any || raise(RoutesNotFound, "Cannot find #{routes_name}")
end
def route_path(routes_name)
Rails.root.join(routes_name)
end
def draw_route(routes_name)
path = route_path("config/routes/#{routes_name}.rb")
if File.exist?(path)
instance_eval(File.read(path))
true
else
false
end
end
end
ActionDispatch::Routing::Mapper.prepend DrawRoute
Cut out the routes of the management screen
Create admin.rb under config/routes and describe as below
namespace :admin do
resources :users
end
in routes.rb
Rails.application.routes.draw do
draw :admin
end
By describing, routes will be created as shown below.
admin_users GET /admin/users(.:format) admin/users#index
POST /admin/users(.:format) admin/users#create
new_admin_user GET /admin/users/new(.:format) admin/users#new
edit_admin_user GET /admin/users/:id/edit(.:format) admin/users#edit
admin_user GET /admin/users/:id(.:format) admin/users#show
PATCH /admin/users/:id(.:format) admin/users#update
PUT /admin/users/:id(.:format) admin/users#update
DELETE /admin/users/:id(.:format) admin/users#destroy
Cut out the routes of the larger model.
For example, if you have users_controller.rb
and users/licenses_controller.rb
users/activates_controller.rb
, group them into folders for each user.
config/routes/user.rb
resources :users do
scope module: :users do
resource :activates, only: %i[update destroy]
resources :licences
end
end
And by reading in routes.rb
Rails.application.routes.draw do
draw :user
end
The following routing will be created.
user_activates PATCH /users/:user_id/activates(.:format) users/activates#update
PUT /users/:user_id/activates(.:format) users/activates#update
DELETE /users/:user_id/activates(.:format) users/activates#destroy
licences GET /users/licences(.:format) users/licences#index
POST /users/licences(.:format) users/licences#create
new_licence GET /users/licences/new(.:format) users/licences#new
edit_licence GET /users/licences/:id/edit(.:format) users/licences#edit
licence GET /users/licences/:id(.:format) users/licences#show
PATCH /users/licences/:id(.:format) users/licences#update
PUT /users/licences/:id(.:format) users/licences#update
DELETE /users/licences/:id(.:format) users/licences#destroy
users GET /users(.:format) users#index
As the app grows, routes.rb can become chaotic, so using draw to nicely separate the files seems to be neat.
Recommended Posts