#329 √ duplicate
Rich Cavanaugh

Add an API for plugins to register routes

Reported by Rich Cavanaugh | June 4th, 2008 @ 02:26 AM | in 2.x

This change allows plugins to register routes while maintaining control for the developer using the plugin.

This is how a plugin registers it's routes:

config.draw_for do |map|
  map.connect ...
  map.resources ...
end

# or

config.draw_for :my_plugin_admin do |map|
  map.connect ...
  map.resources ...
end

This is how a developer would install the plugin routes:

ActionController::Routing::Routes.draw do |map|
  # Install every registered plugin route.
  map.plugin_routes
  
  # Same as above.
  map.plugin_routes :all
  
  # Install the listed plugin routes.
  map.plugin_routes :hobo, :active_scaffold, :restful_authentication
  
  # The above approach could also be used multiple times
  # to handle order sensitive routes.
  map.plugin_routes :hobo
  map.connect '/something', ...
  map.plugin_routes :active_scaffold, :restful_authentication
  
  # Install every registered plugin route execept the listed plugins.
  map.plugin_routes :except => [:hobo, :something]
end

There are currently no tests for these changes. I will add tests soon. This patch includes the suggestions from DHH and technoweenie on #rails-contrib

Comments and changes to this ticket

  • Rich Cavanaugh

    Rich Cavanaugh June 4th, 2008 @ 02:27 AM

    Attaching the patch would probably help it make it into Rails.

  • rick

    rick June 4th, 2008 @ 02:29 AM

    • → State changed from “new” to “open”
    • → Assigned user changed from “” to “rick”
  • rick

    rick June 4th, 2008 @ 03:36 AM

    Just a nitpick, but config.draw_for with no arguments looks weird. That's why I suggested config.plugin_routes or something to that effect in #rails-contrib. It's something that looks fine with or without a given plugin name.

  • Rich Cavanaugh
  • Rich Cavanaugh

    Rich Cavanaugh June 4th, 2008 @ 07:27 PM

    You're absolutely right, I should have caught that. I've changed the method name to plugin_routes but, I also setup an alias for draw_for so you could still do:

    config.draw_for :plugin_admin do |map|
    end
    

    The unadorned approach is now:

    config.plugin_routes do |map|
    end
    
  • Rich Cavanaugh

    Rich Cavanaugh August 12th, 2008 @ 09:06 AM

    • → Tag changed from “” to “actionpack edge enhancement patch railties routing”

    I've attached the finalized patch updated for edge and added tests.

  • voxdolo

    voxdolo August 13th, 2008 @ 01:57 AM

    This looks like a great enhancement. I've wanted this functionality as a plugin developer several times. +1 here.

  • Bruno Miranda

    Bruno Miranda August 14th, 2008 @ 08:26 PM

    This would be awesome to have. +1

  • RSL

    RSL August 15th, 2008 @ 10:34 PM

    Question: is it really a good idea for routing information to be spread all over the rails app? and potentially conflicted between plugins? I'm much more comfortable with rake tasks and generators adding code to the routes.rb where you can easily see what's what. I'm all for this if someone could assuage those fears.

  • Rich Cavanaugh

    Rich Cavanaugh August 15th, 2008 @ 11:09 PM

    This change simply allows plugins to define their routes but does not automatically inject them into the actual routing.

    They are then inserted by the developers into config/routes.rb using code like:

    
      map.plugin_routes :hobo
      # or more automated
      map.plugin_routes :all
    

    So control remains entirely in the rails app developer's hands. They can control the precise order that all of the plugin routes are injected to avoid conflicts.

    At that point it's just a matter of the developer knowing what the plugins they're using are doing. If they don't, conflicts could happen in many places between plugins, not just routes.

  • azimux

    azimux August 16th, 2008 @ 10:26 AM

    I don't know if anybody has looked at the engines plugin, but it uses the syntax:

    map.from_plugin :some_plugin

    Then the plugin has a routes.rb in it's root directory and the routes in this file are evaled by the above call

  • RSL

    RSL August 16th, 2008 @ 05:17 PM

    rich, i misread developer to mean the plugin developer [my fault for reading too quickly]. great patch. +1

  • Ramon Salvadó
  • Michael Koziarski

    Michael Koziarski September 13th, 2008 @ 12:23 PM

    • → Milestone changed from “” to “2.x”

    I like this, or something like it, for the 2.3 stream.

    But for now I'm just moving it off the radar for 2.2

  • James Adam

    James Adam September 14th, 2008 @ 11:31 PM

    The engines plugin does indeed implement this, but in a much simpler, low-tech way. If this is going into core, it probably needs to work nicely with things like namespaces.

    Additionally, is there any way to avoid the 'currently_loading_plugin' attribute? It's a bit inelegant.

  • DHH

    DHH November 26th, 2008 @ 07:38 PM

    • → State changed from “open” to “duplicate”

    I've added auto-loading of config/routes.rb from all plugins now.

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Source available from github

Repository is at http://github.com/rails/rails

Check out the development master (Edge Rails):

git clone git://github.com/rails/rails.git

Creating or reviewing a patch

See the contributor guide.

Creating a feature request

Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.

Creating a bug report

When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.

Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.

Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too"..

Shared Ticket Bins