Adding routes from plugin


#1

Can anyone tell me how to add routes from plugin? I know it is possible
because of this changeset: http://dev.rubyonrails.org/changeset/2967 but
i didn’t find way how to do it.

retro


#2

Mihael Konjevic wrote:

Can anyone tell me how to add routes from plugin? I know it is possible
because of this changeset: http://dev.rubyonrails.org/changeset/2967 but
i didn’t find way how to do it.

retro

Remember in Ruby, all clases remain open regardless of the file they are
called from. So you should be able to simply mimic the structure of the
routes.rb file.

#vendor/plugin/foo/lib/foo.rb
ActionController::Routing::Routes.draw do |map|
map.my_route ‘foo/:id’, :controller => ‘foo’
end

Never done it but it should work, in theory.


#3

Good guess but…No, it doesn’t work.

I’m looking for a way of doing this as well. I’ve been poking around
in the routes code to work out whats going on as yet. It seems alot
easier in edge rails.

Can anyone shed any light on this?

Cheers,

Dan


#4

Just curious, did you apply the patch in that changeset?


#5

It’s in Rails 1.1 already. It was applied 11/10/05.


#6

From my own investigations earlier in the year, despite the fact that
plugins are loaded before the routing is set up this doesn’t allow the
plugins to alter the routing in any trivial fashion i.e. outside of
the block passed to Routes.draw.

I have seen a plugin add routes by injecting some new functionality
directly into the routing mechanism, but this requires a thorough
understanding of the routing code, which is in turn hard to obtain
since much of it is generated on the fly. Jamis B. has been
discussing a rewrite of this aspect of Rails on the core list
recently.

So, in a nutshell: it’s possible (because nothing is impossible), but
very very difficult.


#7

That sounds like a challenge! Right then of to the source we go.

The annoying thing is that it’s going to be two seperate solutions to
the problem. One for 1.1 and one for the rewritten routes in edge
rails (although the new routes are much easier to work with)

I reckon I can do it though…


#8

You can do it, put your hack into it.

ahem.

  • james

#9

Go Dan, go! :wink:

-Ezra


#10

Ha ha, excellent pun action.

Been looking at it this weekend and was thinking of a patch(or plugin
or whatever) that allows a routes.rb to be specified in a plugin
directory. When reload! is called it will load the plugin routes,
then load the main routes…what do you all reckon? Is that a good
approach?


#11

I think the key issue when you’re considering routing with plugins
(beyond actually making it work) is precidence. Because the plugin
load order is currently at the whim and mercy of the filesystem, it
may be problematic ensuring that the routes you define within plugin X
aren’t going to have a lower precidence that some which
conincidentally match from plugin Y.

I have in mind a patch to control plugin loading order because it’s
something I know that I would find useful, but even with this routing
would still be a bit swampy - you’d have to inspect each plugin for
potentially nasty routes and double-check the precidence yourself
(hard enough when they’re all in a single file, sometimes).

That said, I’d still like it to be possible. When you’re developing
plugins/engines for internal use only, these kind of issues are more
managable, and the benefits might outweigh the hassle.

Dan - If you’re at the LRUG meeting tomorrow, we can try and bash
something out.

  • james

#12

Okay, yeah, I’ll come along tomorrow. I know what you mean about
precidence, it’s a big problem. I’ve got a very specific use case for
what I’m trying to do at the moment but to generalise it will be
difficult.

Anyway, speak to you tomorrow.