Forum: Ruby on Rails Adding routes from plugin

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9976060bfb26220dd042340394d06b31?d=identicon&s=25 Mihael Konjevic (retro)
on 2006-06-09 13:09
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
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-06-09 18:30
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.
0727907ae68db2e8ebc1ea1b01f00d69?d=identicon&s=25 Dan Webb (Guest)
on 2006-06-09 19:07
(Received via mailing list)
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
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2006-06-09 19:24
(Received via mailing list)
Just curious, did you apply the patch in that changeset?
0727907ae68db2e8ebc1ea1b01f00d69?d=identicon&s=25 Dan Webb (Guest)
on 2006-06-09 19:30
(Received via mailing list)
It's in Rails 1.1 already.  It was applied 11/10/05.
05d703f649ef1d07e78d7b479fb4c4ac?d=identicon&s=25 James Adam (Guest)
on 2006-06-10 11:55
(Received via mailing list)
>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 Buck 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.
0727907ae68db2e8ebc1ea1b01f00d69?d=identicon&s=25 Dan Webb (Guest)
on 2006-06-10 16:17
(Received via mailing list)
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....
6076c22b65b36f5d75c30bdcfb2fda85?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-06-10 21:21
(Received via mailing list)
Go Dan, go! ;-)

-Ezra
05d703f649ef1d07e78d7b479fb4c4ac?d=identicon&s=25 James Adam (Guest)
on 2006-06-11 19:10
(Received via mailing list)
You can do it, put your hack into it.

ahem.

- james
0727907ae68db2e8ebc1ea1b01f00d69?d=identicon&s=25 Dan Webb (Guest)
on 2006-06-11 21:09
(Received via mailing list)
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?
05d703f649ef1d07e78d7b479fb4c4ac?d=identicon&s=25 James Adam (Guest)
on 2006-06-11 23:42
(Received via mailing list)
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
0727907ae68db2e8ebc1ea1b01f00d69?d=identicon&s=25 Dan Webb (Guest)
on 2006-06-12 01:02
(Received via mailing list)
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.
This topic is locked and can not be replied to.