Archive for January, 2008

Talking about “Advanced RESTful Rails”

Thursday, January 31st, 2008

So I know one of the ongoing concerns with conferences in general (and Railsconf in particular) is that people often don’t know what to expect from the presentations - they go in expecting an in-depth study of a subject they know well, and end up seeing an quick and dirty introduction to the stuff they’ve been working with for months. In an attempt to avoid that for my session, I thought I’d go into a bit more depth here so people can make an informed decision.

There are two main parts to the talk. The first deals with RESTful domain modeling - in particular, how you identify resources that don’t map onto your models. I think the most common instance of this across applications is search, with the possible exception of some aspects of authentication.

The second part is specifically aimed at problems with the implementation of REST in Rails. For instance, I’ll be talking about some issues that arise from the _method hack, and possible solutions for them.

What I’d really like to do, however, is make this session useful for as many people as possible. If you have any questions about modeling complicated domains with resources, or about REST support in Rails specifically, please leave a comment here or email me at rest@turrean.com. I’ll answer questions as they come in, so you won’t have to wait to Railsconf.

Also, if there’s anything else you’d like to know about the session, please ask - I think we all (as both speakers and attendees) have a vested interest in being as informed as possible about the various talks, so I’m happy to clarify anything that needs it.

Rails Goodies: ActiveSupport::OrderedHash

Tuesday, January 29th, 2008

Rails is well-known for providing little things here and there that make developers’ lives easier. Sometimes they’re obvious, like the various inflection methods added to String (pluralize, titleize, etc.) - but just as often, these useful tidbits are hidden in the Rails internals. Just because they’re usually limited to the internals of the framework, though, doesn’t mean that these features can’t be useful to the everyday Rails coder, though, so I thought it’d be interesting to expose some of them.

The first of these Rails goodies I’m going to talk about is ActiveSupport::OrderedHash. This class is defined in activesupport/lib/active_support/ordered_options.rb, and is a subclass of Array. Here’s the definition (from edge):

module ActiveSupport
# Hash is ordered in Ruby 1.9!
if RUBY_VERSION >= '1.9'
OrderedHash = ::Hash
else
class OrderedHash < Array #:nodoc:
def []=(key, value)
if pair = assoc(key)
pair.pop
pair << value
else
self << [key, value]
end
end
 
def [](key)
pair = assoc(key)
pair ? pair.last : nil
end
 
def keys
collect { |key, value| key }
end
 
def values
collect { |key, value| value }
end
end
end
end

The first thing to note is that this class is not defined if you’re running Ruby 1.9 - these compatibility changes have been appearing recently in edge. Assuming you’re on 1.8.x, though, you will have the class defined. Basically, OrderedHash flattens a hash into an array of arrays. It doesn’t automatically sort the resulting structure, but it does preserve the order in which the elements were added, and you can manually sort it later:

>> hash = {:a => 1, :d => 4, :c => 3, :b => 2}
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> ohash = ActiveSupport::OrderedHash.new
=> []
>> hash.inject(ohash) do |all, row|
?> all[row.first] = row.last
>> all
>> end
=> [[:b, 2], [:c, 3], [:d, 4], [:a, 1]]
>> ohash.sort! {|x, y| x.first.to_s <=> y.first.to_s}
=> [[:a, 1], [:b, 2], [:c, 3], [:d, 4]]

In addition, OrderedHashes preserve the means of hash access, as well as the keys and values methods:

>> ohash[:a]
=> 1
>> ohash[:c]
=> 3
>> ohash.keys
=> [:a, :b, :c, :d]
>> ohash.values
=> [1, 2, 3, 4]

OrderedHash isn’t used much in the Rails internals - in fact, the only place it shows up is in the ActiveRecord::Calculations module, where it’s responsible for the output of calculations that include a :group parameter:

>> Line.count(:id, :group => :year)
=> [[nil, 25], [2004, 1], [2005, 3], [2006, 17], [2007, 7], [2008, 2]]

It can be useful in the code you write, however - so if you ever have the need to enforce some sorting on a hash, keep it in mind!

Note: Unless, of course, you’re running Ruby 1.9, in which case this is all handled for you automatically.

Yes sir, that’s my baby

Saturday, January 26th, 2008

Lacie and I went for a third-trimester ultrasound on Thursday, and we came out of it with both a new appreciation for the technology involved and a couple of neat, three-dimensional pictures of our baby’s face. This one’s my favorite.

little miss scofield

Less than two months to the due date now!

Railsconf 2008 update

Wednesday, January 23rd, 2008

Looks like I’m going to Portland for Railsconf again, woohoo! My session on Advanced RESTful Rails was accepted, so at some point between the end of May and the first of June I’ll be talking about domain modeling and the specific implementation of REST in Rails in front of a bunch of very smart people.

Decisions are still being made, apparently, so there’s also a chance that the tutorial proposal (on resourceful plugins) I submitted with my co-worker Clinton will be accepted - but even if it’s not, I’m over the moon. Railsconf, here I come (again)!

… now, on to the proposals for OSCON and Railsconf Europe.

Getting back to the fun

Sunday, January 20th, 2008

You know that feeling where you’re working on things you love so much that it becomes kind of tiresome? Where you still enjoy what you’re doing, but it’s just on the edge of feeling like work? I’ve been there for a little while, but I’m happy to say that I had an experience a few days ago that recentered me.

A friend and I were discussing mashups over breakfast - sites like Flickrvision, ChicagoCrime, and the like - and somehow, we also started talking about LOLcats. Jokingly, we started talking about a site that would translate your friends’ tweets from Twitter into LOLspeak and insert them into appropriate images from Flickr automatically. I didn’t think much more of it until I got back to my hotel room that evening, but in a fit of … something, I decided to give it a try. Without further ado, then, here are the fruits of my labor. I give you … LOLTwitter!

LOLTwitter #1

LOLTwitter #2