I want to split the routing definition file config / routes.rb into multiple files. Specifically, I have a file called lib / my_app / routes.rb and want to load it.
The 2015 article https://y-yagi.tumblr.com/post/118514473965/ is a hit, but it doesn't work as it is. The definition and execution of draw works well, but it fails because the mechanism for reloading seems to be old.
Rails6 has a pretty organized way of loading routes so you don't have to reload it yourself. A class called Rails :: Application :: RoutesReloader is defined with the correct parameters. If you can give it, it will be completed. If you want to decompose routes.rb under config / routes /, the method described in the article below seems to be good.
However, I couldn't read any file with this, so I chose another method.
The routing is loaded at ʻadd_routing_paths` in initializers. So I thought that I should set it so that path is added before the initializers.
Specifically, the following settings were added to config / application.rb.
initializer('add_my_app_routing_paths', before: 'add_routing_paths') do |app| routing_path = root.join('lib/my_app/routes.rb') app.routes_reloader.paths.prepend(routing_path) end
Now, as expected, the routes defined in lib / my_app / routes.rb are loaded. I didn't seem to have the side effect of not being reloaded, so I decided to go with this.
If you look at the unreleased master branch source code, it looks like below written.
initializer :add_routing_paths do |app| routing_paths = paths["config/routes.rb"].existent external_paths = self.paths["config/routes"].paths routes.draw_paths.concat(external_paths) if routes? || routing_paths.any? app.routes_reloader.paths.unshift(*routing_paths) app.routes_reloader.route_sets << routes app.routes_reloader.external_routes.unshift(*external_paths) end end
When I set a variable called external_paths, I feel that if I can pass the path I want to read, I can set it better.