Adding routes via a plugin


#1

I have a plugin that I am working on that will as part of its
distribution
contain a controller. I would like to be able to add a route for it in
the
plugin’s init.rb so that when the plugin is loaded, the route will
automatically be registered without having to force the user to edit
routes.rb or even make a rake task to do it for them.

I saw the following code in one of the SVN rails/plugin modules:

ActionController::Routing::RouteSet.send :include, RestfulRoutes

, but it just includes a module with a method to add named routes.

I would like to actually add a route to the routeset automatically. I
tried
using variations on the above to directly call named_route from my
plugin’s
init.rb, but I have not been able to get it to work.

Also I notice that there is a reload method in the routing.rb file that
will
clear out the current named routes and read in the routes.rb file again.
Does this get called internally somewhere by Rails? I guess I am
wondering
that even if I figure out a way to add my route dynamically, Rails might
call the reload method after my plugin is loaded and my route woud go
away
anyway.

Should I just pursue a rake task as part of the install to add the route
into the routes.rb file, or can what I want to do be accomplished with
Rails
1.0?

Thanks,
-Steve
http://www.stevelongdo.com


#2

Steve L. wrote:

I have a plugin that I am working on that will as part of its
distribution
contain a controller. I would like to be able to add a route for it in
the
plugin’s init.rb so that when the plugin is loaded, the route will
automatically be registered without having to force the user to edit
routes.rb or even make a rake task to do it for them.

How would you know if your route will break other routes?
Should it be first in the list or last?
Does it conflict with existing routes?

These seem like good reasons to make users define one manually.

_Kevin


#3

Ideally I would be using a namespace to prevent just that. Similiar to
the
Recommendation to not use the ActiveRecord::Acts namespace for
developing
plugins, I placed my module extensions in a Longdo::Acts namespace.
Also
named routes are designed to overcome this very problem. That said I
still
would probably use ‘longdo/:controller/:id/:value’ to prevent conflicts.
Similar in a sense to Java packages.

That said, I am more interested in the technical feasability of dynamic
routing versus simple namespace issues. Any other thoughts?