Archive for January, 2007

Tuesday, January 23rd, 2007

rid.onkul.us returns

One of the first Rails projects I ever coded (then abandoned) has returned.

rid.onkul.us is kind of like a “tumble-log” but more ridiculous. Kronenberg, my constant partner in hilarity, is co-captaining the ship of absurdities, and we’ve been off to a good start.

The main use of ridonk is to have a place to store all the daily crap that Kron and I amuse ourselves with.

There’s also a feed (of course) and future plans for comments and anything else we can cram into this pretty crude blogging platform I built and rebuilt in a couple days.

Monday, January 15th, 2007

Replacing the ‘rails’ command

I always forget about the ’svn copy’ command, and Josh Susser has reminded me of its awesome usefulness. In this case, its usefulness as a way to start projects.

I took his advice a step further. Rolling on the idea of an ‘Image’ of a basic Rails project with its own repository, on top of setting the svn:ignore properties for log/ and tmp/ I also installed a couple things I use in almost every project:

  1. acts_as_authenticated
  2. ar_fixtures
  3. Capistrano rake and deploy tasks

I also generated an authenticated controller/model and added an initial user to my migration, so I’m all set to login. Before importing my ‘basicauth’ Rails image to subversion, I created my svn best practices directories (branches/ tags/ trunk/) and put the rails code into trunk.

After importing, I’m all set to start as many rails projects as I want. Why not use bash scripting to make it even easier.

#!/bin/bash
echo 'Creating new rails app'
cd ~/Sites/
echo 'Creating repository'
svn copy svn+ssh://myuser@myrepos.com/svn/rails/basicauth svn+ssh:/myuser@myrepos.com/svn/rails/$1
echo 'Checking out code'
svn co svn+ssh://myuser@myrepos.com/svn/rails/$1/trunk $1
cd $1
mate .

If I save this, chmod +x it, and copy it to /usr/local/bin/ - Now all I have to do to start a new rails app is:

myrails anewapp

It will copy the repository, checkout the code, and even open it in TextMate.

Friday, January 5th, 2007

RSS should be big

I’ve become a bit of a feed evangelist over the past year. I want RSS to succeed. It has so much potential not only as a tool, but as a medium. RSS isn’t just for podcasting, or even just for blogs. I am invested in this. I’ve spent more then a year, with RadioTail, discovering the nuances of the feed and finding ways to optimize and use it in interesting ways.

If you haven’t already, get a feed reader.

If you publish feeds, here are some ideas.

First make sure you make use of XHTML auto-discovery tags. In the <head> of your document you should place something like

<link href="FEED_URL" rel="alternate" title="RSS" type="application/rss+xml" />

This will make browsers like Safari (> 2.0), Firefox, and even the new Internet Explorer (7.0) show a little link in the address bar to subscribe or view the feed. It’s very useful to place on pages where users may not expect a feed. It gives them an extra little hint and direction to discover more and maybe even subscribe.

No matter what make sure you include a very visible and easy to access link to your feeds. Though not necessary, its helpful to use the universal feed icon. If you’re not a fan of the orange and white, FeedIcons.com has a great collection of editable versions of the icon so you can make something like the icon I have at right. It incorporates the feed icon (a nice and large one) puts it in a prominent place, and even gives the text clue as well.

The main lesson is please don’t hide your feeds. Unfortunately, the default WordPress template, puts the only link to the feed at the very bottom of the page in a tiny text link.

If I had it my way, all pages would have feed links, and they would all
  • incorporate the universal icon
  • be before the fold
  • and be bigger then 16px

I’m not blind, I just want everyone to promote their feeds, thus promoting the feed.

Thursday, January 4th, 2007

F– up Fast

“We did all kinds of dumb, stupid things. But our unofficial slogan was, “F– up fast.” Make mistakes rapidly, learn from them, and move past them.”

Caterina Fake, Flickr Co-Founder. (via BrainSparks)

I love this mentality. One of the most beautiful parts of building web apps is the ability to constantly update and fix and change. To constantly release.

I’ve seen a lot of great ideas crumble up and die, unreleased apps of genius that will never see the light of day. What better way to learn “what works” then by letting your users tell you. I love the Flickr interface, and you can tell its a constantly shifting landscape. Its fun to find new things to do every time you visit. It’s better to make mistakes and learn to shift and jive then retain an expectation that your app will ever be perfect.

I do think, before I eat my words, that a distinction has to be made between making mistakes and making buggy untested crap. Test, prod, poke, finesse and fix your code. Don’t release something that doesn’t work, but don’t wait to release something until the 100000 features you have planned are built. Don’t be afraid to do something stupid, the fact is - we all do something stupid sometimes. Assume somewhere near the worst, and get over it.

Wednesday, January 3rd, 2007

Calendaring with Ruby’s Time class

Part of preparing one of my ongoing projects for release was getting some calendaring functionality built. The Calendar Helper is a great resource but has limited functionality for printing items within dates. Specifically, I wanted to work with time within dates, e.g. “Weds. Jan 3, 2007 at 10:30AM” instead of just “1/3/07”. I have had a lot of fun working with Ruby’s Time class and find it a bit more flexible then the Date or DateTime classes.

In order to make a calendar, I needed to be able to iterate through dates and print or manipulate as I go. Finally, an opportunity to make use of what I’ve been learning about blocks! For a number you would do something like this to iterate through integers.

1.upto(5) { |n| puts n+1 } #=> 2 3 4 5 6

Why not the same for Time? In this case, though I wanted to iterate through Time as days, not seconds or microseconds. I added a method to Time:

class Time
  def to_dt
    Time.local(self.year,self.month,self.day,0,0,1)
  end
end

>> Time.now.to_dt
#=> Wed Jan 03 00:00:01 EST 2007

Now I can use time.to_dt as a date representation of a Time. Meaning, if something is calendared for Jan 3, 2007 at 00:00:01 I know its calendared for that day in general instead of for a specific time. Now to iterate with some block action.

class Time
  def span_times_as_days(to, &block) 
     cur = self.to_dt
     while cur <= to.to_dt
       yield cur
       yes = cur
       cur += 1.day
       if yes.dst? != cur.dst?
         if cur.mon > 6
           cur += 1.hour 
         else
           cur -= 1.hour
         end
       end
     end
  end
end

Pretty straight forward. Its an equivalent of num.upto for the Time class. The only thing thats a little confusing is the last little bit, that has to add or subtract hours based on Daylight Savings. This is necessary because we’re moving through the block by adding hours (actually seconds) instead of days. The block returns a Time object in the form of to_dt.

>> Time.now.span_times_as_days(Time.now + 1.week) { |day| puts day }
# Wed Jan 03 00:00:01 EST 2007
# Thu Jan 04 00:00:01 EST 2007
# Fri Jan 05 00:00:01 EST 2007
# Sat Jan 06 00:00:01 EST 2007
# Sun Jan 07 00:00:01 EST 2007
# Mon Jan 08 00:00:01 EST 2007
# Tue Jan 09 00:00:01 EST 2007
# Wed Jan 10 00:00:01 EST 2007

I’ve used this method to block through the dates and print it out, similar to the calendar helper. Heres another helpful function using span_times_as_days:

def bind_array_to_calendar(items,time_attribute,since = false)
  calendar = []
  first_date = since
  first_date = items.first.attributes[time_attribute].to_dt unless first_date
  last_date = items.last.attributes[time_attribute].to_dt
  first_date.span_times_as_days(last_date) do |dt|
    calendar_day = {:date => dt, :items => []}
    while !items.empty? && items.first.attributes[time_attribute].to_dt == dt
      item = items.shift
      calendar_day[:items] << item
    end
    calendar << calendar_day
  end
  calendar
end

Give this method an array of ActiveRecord (or ActiveRecord-like) items that have a specific attribute (or method) that returns a Time object. For example, I have a class called Assignment, that had a ‘datetime’ attribute ‘due_at’.

@assignments = Assignment.find(:all,:order => 'due_at ASC')
# Assignment 1 - due_at Sat Jan 06 10:00:00 EST 2007
# Assignment 2 - due_at Sun Jan 07 00:00:01 EST 2007
@calendared = bind_array_to_calendar(@assignments,:due_at)
#=> [{:date => Sat Jan 06 00:00:01 EST 2007, :items => [Assignment 1]},
#    {:date => Sun Jan 07 00:00:01 EST 2007, :items => [Assignment 2]}]

Its been fun playing with Time. Let me know if you see something that can be slickified.