I'm looking for a better approach... then what I'm currently doing.
I'm working on a generator that will help me setup my base projects with many of my common defaults that I use internally. Now I know that generators have the route_resources method that allows a person to create a resource in the routes file, but is there a way to create the map.root :controller => 'name' entry as well?
Here is what I've done. I customized the route_resources method to be something like this:
def add_root_route sentinel = 'ActionController::Routing::Routes.draw do |map|' gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match| "#{match}\n map.root :controller => \"welcome\"\n" end end
And then just copied the gsub_file method to my generator (it was a protected method and I couldn't think of another way to execute it - thoughts?).
I then have a bit that does:
action = File.basename($0) case action when "generate" add_root_route when "destroy" puts "------------------------------------------------------------" puts "Make sure you remove the map.root from the /config/ routes.rb" puts "------------------------------------------------------------" end
Which makes sure it only runs when they are running generate.
My question is this... Is there an better way to be doing this? It seems like a lot of insanity just to add a root route.