h2. Overview
A customer of ours has a splendidly restrictive environment which I have the pleasure to work within. Not only is there no Rails, there's no PHP and presumably no database to hook into even if there were dynamic languages. This is probably complete crap but it led to an interesting problem solving situation regardless.
What do you do with a client that wants hundreds of pages to be redesigned when you don't have access to handy timesaving tools like Rails or PHP?
Long before I started research I told my boss to continue with the site anyways; I thought that caching would work exactly the way I needed. This turned out to be completely wrong but the site was well underway so I had to figure something out.
After trolling the net for a long time I stumbled on a website that took ASP pages and made static pages out of them by spidering; basically wget tweaked a little bit and resold for far too much money.
So I endeavored to do exactly the same for Rails.
h2. Routing
In order to make this work I knew that I needed to do be able to represent pages by their name; something like activities.html would pull up the page with the title 'Activities'. Once I had the idea it was a short jump to the following routing rules:
map.connect '', :controller => 'page', :action => 'splash_page'
map.connect ':id', :controller => 'page', :action => 'index'
map.connect ':controller/:action/:id'
def index
# If there's a number in the id assumed that it's the id of a page.
if params[:id] =~ /^\d+$/
@page = Page.find(params[:id])
# If there's a .html in the id assume we want the page with that title.
elsif params[:id] =~ /^.*\.html$/
# This allows users to use any case for the link name.
# Otherwise exporting to windows/mac might break from
# multiple pages being named the same. For instance
# Index.html != index.html in Linux but does in Windows/Mac.
# Therefore sending both versions will break a
# lot of archiving utilities in Win/Mac.
if params[:id] =~ /[A-Z]/
redirect_to :action => :index, :id => params[:id].downcase
end
# Otherwise strip the .html and the _ then query the database.
@page = Page.find(:first,
:conditions => ['title = ?', html_to_title(params[:id])])
# Finally if theres simply a name we should redirect to
# the .html version like we did above.
elsif params[:id]
redirect_to(:action => :index,
:id => title_to_html(params[:id])
end
# If the page somehow makes it through that battery
# and the routing rules above just redirect to the
# homepage keeping the flash intact.
if @page.nil?
flash[:notice] = flash[:notice]
redirect_to :action => :splash_page
end
end
def title_to_html(title)
title.downcase.tr('\ \\\/', '_') + '.html'
end
def html_to_title(title)
title.gsub(/\.html/,'').humanize
end
wget -m -nH www.example.com