My Octopress Blog

A blogging framework for hackers.

Using Ruby to Cleanup

I had a problem with editors on one of my sites not conforming to the standards for url path formatting (all lowercase alphanumeric, hyphen instead of spaces) so I added a little validation to the page model:

validates_format_of :slug,
    :with => /\A[a-z0-9\-]+\Z/,
    :message => 'can only contain lowercase letters, numbers and hyphens'

Now, I had to update all of the pages that had been written to fit the new standard, but the real problem was updating inter-page links that had already been written. Luckily, script console came to the rescue:

Page.find(:all).each { |p|

Page.find(:all).each do |p|
    p.body.gsub(/\/c\/([a-z0-9\_\-]+)/i) { |m|

I love Ruby. This would have been a pain in the ass in any other language.