Things tagged 'web-technical'

Truncating URLs w/PermalinkFu

I use PermalinkFu for generating nice slugs for URLs here, at Artlog and in the Backlight CMS that runs a number of websites for friends like Marlene Marino and A Crime So Monstrous.

In any case, it’s a great plugin, but it’s begun to bother me that it can generate some massively long permalinks if left unchecked. So for the new Artlog site I am going to truncate the permalinks into some limited number of words.

For example, I’ve set the note model to generate the permalinks (saved in a “permalink” column in the DB) this way:

class Note < ActiveRecord::Base
  def to_param
    "#{id}-#{permalink}" 
  end
  before_save :create_or_modify_permalink
  def create_or_modify_permalink
    self.permalink = PermalinkFu.escape(("#{name}").to_s)
  end
end

That’s fine in most situations, but if the note’s name is long you run into issues.

>> n = Note.new
=> #<Note id: nil, name: nil, permalink: nil, created_at: nil, updated_at: nil>
>> n.name = "I just flew back from Chicago and boy are my arms tired" 
=> "I just flew back from Chicago and boy are my arms tired" 
>> n.save!
=> true
>> n.permalink = "I-just-flew-back-from-Chicago-and-boy-are-my-arms-tired" 

That’s awkward – particularly if you are RESTing it up and your edit URL ends up looking like

/notes/1-I-just-flew-back-from-Chicago-and-boy-are-my-arms-tired/edit

So I am going to go ahead and create an evil twin plugin to generate more sane slugs and have the new method available across all models (rather than having the fix only available in the note model).

In vendor/plugins create a directory called “permalink_fu_hacks” or something and create an init.rb file in that directory (/vendor/plugins/permalink_fu_hacks/init.rb) that looks like the following:

PermalinkFu.module_eval do
  class << self    
    # truncate the permalinks to x number of words (specified in length below)
    def truncated_escape(str)
      length = 6
      PermalinkFu.escape(("#{str.split()[0..(length-1)].join(' ')}").to_s)
    end
  end
end

You can change the length we specify to whatever you’d like. Six may seem like to many or too few words to you, but it doesn’t really matter; you’re largely just setting an upper limit to prevent URLs from getting way too long.

You can now change your create_or_modify_permalink callback in the note model to this

def create_or_modify_permalink
  self.permalink = PermalinkFu.truncated_escape(("#{name}").to_s)
end

and you’ll end up with a better permalink:

>> n.save!
=> true
>> n.permalink = "I-just-flew-back-from-Chicago" 

Amazon S3 outages

Amazon S3 went down for about 8 hours yesterday, effecting a number of our in-house projects including this site, Artlog, and all sites powered by Backlight. Considerably larger sites that also use S3 for media storage like Twitter and Tumblr were similarly hamstrung.

S3 is an amazing service despite this and other recent outages. Fortunately, Amazon recently setup a page providing status updates for their web services which went a long ways toward effectively keeping customers informed of progress. It’s an example other companies providing web-services should strive toward.

Top / © 2007-2008 I Am Still Alive. All rights reserved. Information