Extending Routing

I am working on a CMS project and would like to extend how routing
works, however I have not done such deep magic in the past.
Specifically I would like to map the /*path/ glob to a function that
will return the controller, action and id based on a database lookup.
I have it working now, but I am using render_component to get it to
work.

Any thoughts?

The current routing table:

ActionController::Routing::Routes.draw do |map|
  map.connect ':controller/service.wsdl', :action => 'wsdl'
  map.connect ':controller/:action/:id'
  map.connect '*path', :controller => 'application', :action =>
'parse_path'
end

The current application controller:

class ApplicationController < ActionController::Base
  model :domain
  attr_writer :root_node
  attr_reader :root_node
  before_filter { |c| c.root_node = Domain.find_by_name(c.request.host)
if c.root_node.nil? }

  def parse_path

    # Set the current object to the root node for the current domain
    obj = root_node

    # Set the default action to index
    action = 'render_node'

    # Loop through each path element
    for path_item in params[:path]

      # Find the new path element in the children of the current node
      new_obj = obj.children.find_by_name(path_item)

      # If the new path element is not found
      if new_obj.nil?
        # Then set the current path element to the action
        action = path_item
        # and exit the loop
        break
      end

      # Make the child the current node
      obj = new_obj
    end

    @obj = obj
    logger.info( "obj: " + @obj.to_s )

    node_controller = obj.class.to_s.underscore
    logger.info( "node controller :" + node_controller )

    # Render the node and action
    render_component :controller => node_controller,
                      :action => action, :id => obj.id,
                      :obj => obj, :params => params
  end
end

While it is not pretty and I fear will not scale real well, it works.

I had a look at Radiant CMS recently and was somewhat disappointed about the way they implemented everything. They just had to rewrite most of the parts concerning template and layout rendering. They even have their own “controllers” (which they call behaviours). I think it’s a reasonable solution on its own, but there isn’t much to borrow if you want to build some small and cute CMS.

Anyway, take a look at Radiant’s implementation. Maybe you’ll find some interesting points to start from.

If you are really about to extend rails routing, take a look at Jamis Buck’s articles on it:

http://weblog.jamisbuck.org/2006/10/2/under-the-hood-rails-routing-dsl

http://weblog.jamisbuck.org/2006/10/4/under-the-hood-route-recognition-in-rails

That's similar to what I'm doing in Mephisto, without the
render_component overhead (I just call a dispatch_* action in the same
controller). With page caching, the performance hit doesn't really
matter.