module ActionController module Routing class RouteSet class Mapper # Modifies your route set to produce TML-compatible routes. Also connects the default # TML request (/index.tml) to the options you provide, if any. # # Unlike the rest of Rails routing, if you call #connect_rtml multiple times with different # options, the /index.tml path will be updated to reflect the new options. # # To use, just add the following to your /config/routes.rb file: # map.connect_rtml :controller => "start_page" # # Note that you should do this for each RTML controller in your application. To prevent # overriding the /index.tml page, pass :no_index => true: # map.connect_rtml :controller => "another_page", :no_index => true # def connect_rtml(options = {}) no_index = options.delete(:no_index) make_rtml_connection('/rtml/:action.:format', '/rtml/index.rtml', :controller => 'rtml') make_rtml_connection('/rtml/:action.:id.:format', '/rtml/index.1.rtml', :controller => 'rtml') #make_rtml_connection('/:controller/:action/:id', '/rtml/index/1') unless options.empty? || no_index connect '/index.tml', options.reverse_merge(:format => 'rtml') end if options[:controller] controller = options[:controller] make_rtml_connection("/#{controller}/:action.:format", "/#{controller}/index.rtml", :controller => controller) make_rtml_connection("/#{controller}/:action.:id.:format", "/#{controller}/index.1.rtml", :controller => controller) end end private def route_exists?(path) path = "/#{path}" unless path[0] == ?/ ActionController::Routing::Routes.routes.each do |route| segs = route.segments.to_s return true if segs =~ /^#{Regexp::escape path}(\/|)$/ end false end def make_rtml_connection(connection, example, options = {}) connect connection, options unless route_exists?(example) end end end end end