Modular routing in rails and merb (acts_as_routing)

Here’s a proof of concept plugin that will monkeypatch Rails or Merb routing to allow you to define “acts as blocks” anywhere throughout your application (i.e. a plugin) and then use them in your routes file.
Imagine the plugin acts_as_commentable defines the following in its init.rb:

ActionController::Routing.routes_for_acts_as(:commentable) do |map|
  map.resources :comments
  map.best_comment '/best-comment', :controller => 'comments', :action => 'best'
end

If you added these :acts_as to your config/routes.rb:

ActionController::Routing::Routes.draw do |map|
  map.resources :people, :acts_as => [:commentable]
  map.resources :posts, :acts_as => [:commentable]
end

You could then use these routes throughout your application:

  <%=person_comments_path(Person.first)%>
  <%=post_comments_path(Post.first)%>
  <%=person_best_comment_path(Person.first)%>

This is equivalent to doing:

ActionController::Routing::Routes.draw do |map|
  map.resources :people do |people_map|
    people_map.resources :comments
    people_map.best_comment '/best-comment', :controller => 'comments', :action => 'best'
  end
  map.resources :posts do |posts_map|
    posts_map.resources :comments
    posts_map.best_comment '/best-comment', :controller => 'comments', :action => 'best'
  end
end

The plugin is available on github: http://github.com/hungrymachine/acts_as_routing/tree/master

P.S. At some point, I’ll submit patches to Rails and Merb so this functionality is native rather than provided via a monkeypatching plugin.

9 Comments

  1. IdioniHix says:

    Hello,

    as i’m here for the first time, i wanna say hi to all you guys!

    cheers,
    ______________
    IdioniHix
    acheter achat starflam piste

  2. Breeniuraerom says:

    New here… Found this site for searching for criminal/dwi/dui attorneys on Long Island, New York. My cousin came accross the one he hired here and was very pleased with the outcome.

    The website is “http://www.licriminalattorney.com”

    Again that address is that is http://www.licriminalattorney.com

    I Hope this is advantageous information to all.

  3. What’s up all? I just finished building a new website called Watch Dexter Online, and was hoping that I could get a little help from those of you that are familiar with Wordpress. I wanted some suggestions for plugins that will help with SEO. Please post your recommendations when you get a chance.

  4. What a great idea and explanation. I would have seldom thought to do this, but then again, that

  5. Formerly your next shindy or http://teakrefinishing.blogspot.com – restore teak furniture special companionship arrives, we make amends move aside your teak furniture look like new. Runabout was in fresh water throughout seven years and has been in brackish drinking-water looking for six years. But where you start depends on your budget and your tastes.

  6. Before your next party or http://teakrefinishing.blogspot.com – restore teak furniture festive company arrives, we move your teak belongings look like new. Runabout was in fresh latin aqua for seven years and has been in brackish sea water on six years. But where you start depends on your budget and your tastes.

Leave a Reply