Revert "Allow loading external route files from the router"
This reverts commit 6acebb3.
Usage of this feature did not reveal any improvement in existing apps.
Conflicts:
actionpack/lib/action_dispatch/routing/mapper.rb
guides/source/routing.textile
railties/lib/rails/engine.rb
railties/lib/rails/paths.rb
railties/test/paths_test.rb
This will create routing helpers such as +magazine_periodical_ads_url+ and +edit_magazine_periodical_ad_path+.
h3. Breaking Up a Large Route File
If you have a large route file that you would like to break up into multiple files, you can use the +#draw+ method in your router:
<ruby>
draw :admin
</ruby>
Then, create a file called +config/routes/admin.rb+. Name the file the same as the symbol passed to the +draw+ method. You can then use the normal routing DSL inside that file:
<ruby>
# in config/routes/admin.rb
namespace :admin do
resources :posts
end
</ruby>
h3. Inspecting and Testing Routes
Rails offers facilities for inspecting and testing your routes.
@josevalim What do you mean by "Usage of this feature did not reveal any improvement in existing apps."? Compared to doing it manually, without special framework support?
I've found it useful to split routes into multiple files on larger projects, but IIRC it was easy enough with just multiple My::Application.routes.draw or instance_eval or something.
The point of 'splitting up routes' was to make the routes file easier to read. routing_concerns will (hopefully) DRY up your routes so that you don't need to split them up anymore.
Are there any examples of this new approach to defining multiple routes? Rails 4 docs make it look like concerns can only be used to modify existing routes...?
Rails 4 docs make it look like concerns can only be used to modify existing routes...?
Actually concern routes are provided for DRY concerns, this is not really an alternative to the reverted patch here. I guess there is no equivalent at the time being but I may be wrong. It looks like this is not so hard to implement such feature inside your application.
I tried it out in practice and found that it made things more opaque, not clearer like I expected. So not something we want to encourage. If you need it, it's very easy to do on your own.
On Mar 30, 2014, at 10:39, Robin Dupret ***@***.*** wrote:
Rails 4 docs make it look like concerns can only be used to modify existing routes...?
Actually concern routes are provided for DRY concerns, this is not really an alternative to the reverted patch here. I guess there is no equivalent at the time being but I may be wrong. It looks like this is not so hard to implement such feature inside your application.
—
Reply to this email directly or view it on GitHub.
I find this really useful, so I followed the gist linked to here to get the basic functionality working.
The problem I'm running into now is the lack of autoloader support. I see in this commit that a bunch of integration points had to be made to autoload the files in config/routes/*.rb. Does Rails 4 have some cool API I don't know about that will allow me to affect the same changes without monkeypatching?
Splitting a large routes.rb file in smaller parts can be very helpful, specially if you have totally independent routes. A classical example would be an app with many different user types (admin, members, guests, etc). Concerns could even be shared between all of them. Anyways, concerns helps a lot on code reusability, but not necessarily on code readability. Anyway the need for native support depends on the eye of the beholder
From my point of view you removed a really great feature.. Concerns are nice, but if you really want to seperate big route sets (like for components: apis, backend, frontend1, frontend2) you are now lost. I've tried the instance_eval solution, but this doesn't reload my routes in development..
I would be really happy if you could bring back this functionality.
I had reload issue similar to @lichtamberg .
And I re-implemented Rails::Application#routes_reloader to solve this issue.
It works nice so far.
module YourProject
class Application < Rails::Application
def routes_reloader
@routes_reloader ||= RoutesReloader.new.tap do |routes_reloader|
routes_reloader.paths.push(*Pathname.glob(Rails.root.join('config', 'routes') + '*.rb'))
end
end
end
end
with the new direct directive on routes.rb this feature definitely should be back. for now I will be using the GitLab solution described by @tomaadland.
@tomaadland I know some time went by, but right now I have this issue up again. Is your mentioned solution from gitlab not very imperformant? Loading the route files every request looks not very optimal for me.
Right now I couldn't find any solution to make it work with routing concerns, so I really miss the built-in feature to split up routes.
This comment has been minimized.
@josevalim What do you mean by "Usage of this feature did not reveal any improvement in existing apps."? Compared to doing it manually, without special framework support?
I've found it useful to split routes into multiple files on larger projects, but IIRC it was easy enough with just multiple
My::Application.routes.draw
orinstance_eval
or something.This comment has been minimized.
This was reverted in favor of https://github.com/rails/routing_concerns, that we think is a better approach.
This comment has been minimized.
@rafaelfranca Thanks! I don't see from the README how
routing_concerns
does anything to help split up routes, though – how would you go about that?This comment has been minimized.
The point of 'splitting up routes' was to make the routes file easier to read.
routing_concerns
will (hopefully) DRY up your routes so that you don't need to split them up anymore.This comment has been minimized.
@steveklabnik Alright, thanks!
This comment has been minimized.
But Rails makes possible to split much: locales, migrations... It's useful for spliting-up-by-components very much!
This comment has been minimized.
@steveklabnik amn't I right (see previous message)?
This comment has been minimized.
I don't know if you're right or not. Personally, I never tried this feature. @dhh didn't find it very helpful.
This comment has been minimized.
Are there any examples of this new approach to defining multiple routes? Rails 4 docs make it look like concerns can only be used to modify existing routes...?
This comment has been minimized.
Actually concern routes are provided for DRY concerns, this is not really an alternative to the reverted patch here. I guess there is no equivalent at the time being but I may be wrong. It looks like this is not so hard to implement such feature inside your application.
This comment has been minimized.
This comment has been minimized.
Agreed, especially if the language already supports it, no need to try and over-systemize it. I think I've got something figured out.
This comment has been minimized.
I find this really useful, so I followed the gist linked to here to get the basic functionality working.
The problem I'm running into now is the lack of autoloader support. I see in this commit that a bunch of integration points had to be made to autoload the files in
config/routes/*.rb
. Does Rails 4 have some cool API I don't know about that will allow me to affect the same changes without monkeypatching?Thanks!
This comment has been minimized.
Splitting a large routes.rb file in smaller parts can be very helpful, specially if you have totally independent routes. A classical example would be an app with many different user types (admin, members, guests, etc). Concerns could even be shared between all of them. Anyways, concerns helps a lot on code reusability, but not necessarily on code readability. Anyway the need for native support depends on the eye of the beholder
This comment has been minimized.
From my point of view you removed a really great feature.. Concerns are nice, but if you really want to seperate big route sets (like for components: apis, backend, frontend1, frontend2) you are now lost. I've tried the
instance_eval
solution, but this doesn't reload my routes in development..I would be really happy if you could bring back this functionality.
Edit:
I'm now going with this approach, but it's still not reloading in my dev environment if I change something (see http://stackoverflow.com/a/34798146/74264):
This comment has been minimized.
@lichtamberg did you try the answer below ? This is how its been done in Gitlab (see https://gitlab.com/gitlab-org/gitlab-ce/blob/master/config/initializers/routing_draw.rb) works like charm :)
This comment has been minimized.
I had reload issue similar to @lichtamberg .
And I re-implemented
Rails::Application#routes_reloader
to solve this issue.It works nice so far.
This comment has been minimized.
with the new
direct
directive onroutes.rb
this feature definitely should be back. for now I will be using the GitLab solution described by @tomaadland.This comment has been minimized.
@tomaadland I know some time went by, but right now I have this issue up again. Is your mentioned solution from gitlab not very imperformant? Loading the route files every request looks not very optimal for me.
Right now I couldn't find any solution to make it work with routing concerns, so I really miss the built-in feature to split up routes.
This comment has been minimized.
This feature exists on Rails master already #37892