Announcement: New Rails Plugin

Posted by trevor Tue, 25 Oct 2005 20:33:00 GMT

I’m pleased to announce the release of a new rails plugin for ActiveRecord, chock full o’goodness from:

  • acts_as_enumerated
  • has_enumerated
  • ActiveRecord::VirtualEnumerations

What is it?

As part of the work I’ve been doing at Site5.com (who have very graciously allowed me to release this code into the wild) I put together an extension to treat an ActiveRecord model as an enumeration. I.e.:


class BookingStatus < ActiveRecord::Base
  acts_as_enumerated, :order => 'position ASC'
end

booking.status = BookingStatus[:confirmed]

Values for acts_as_enumerated models are cached (reducing hits to the database) and you can define ‘virtual’ acts_as_enumeration classes, eliminating the need to clutter your models directory with classes that do nothing more than acts_as_enumerated.

The has_enumerated macro allows your models to refer to acts_as_enumerated instances without resorting to using belongs_to (which can require you to jump through hoops to avoid the database hit when retrieving the enumeration value).

I should point out that this isn’t the first time something like this has been done. Michael Shuerig sent a mail to the rails list a while back (when I was first looking at this feature) outlining how he was adding enumerations to his Boilerplate library.

Boilerplate’s enumerations differ from this plugin in some pretty significant ways but Michael’s technique of hooking into Module.const_missing was the missing piece of the puzzle I needed to eliminate clutter in the models directory – I’m very grateful for that.

How is it installed?

Well, the new rails ‘plugins’ feature makes packaging and releasing extensions a lot more straight forward.

First of all, you need to be running a plugin-compatible version of rails (0.14 and above). Then you simply:


cd vendor/plugins
svn export http://svn.protocool.com/rails/plugins/enumerations_mixin/trunk \
   enumerations_mixin

The README_ENUMERATIONS file should be enough to get you started – when I manage to scrape together some spare time I’ll insert some (hopefully) more helpful rdoc.

What’s missing?

Well, proper documentation, for a start. Apart from that it’s fully functional.

Enjoy!

Update

Years later and this plugin is now hosted on github

Posted in code

Site5 Teaser

Posted by trevor Tue, 25 Oct 2005 20:29:39 GMT

The day is nearly upon us. A little teaser perhaps?

Getting off the ground with Flock

Posted by trevor Fri, 21 Oct 2005 21:33:35 GMT

Apologies for the title.  Not funny, I know.

I just had to try out Flock to see what the fuss was about.  I guess my biggest complaint so far is that the interface looks like a web page so I expect it to behave like one.

Having to double-click certain page elements just doesn’t naturally occur to me.

And when I say “save as draft”, that’s kind of what I meant to happen.  Publishing this entry was never my intention so it’s a bit disappointing that you’re reading this don’t you think?

And be really careful when clicking ‘delete this post’.

Okay, it is beta, no arguments there.  It sure does feel like a beta.

Anyhow, thanks to Tom Wilcoxen for sharing his method for getting flock to talk to typo.

A Little Timesaver

Posted by trevor Sat, 17 Sep 2005 04:04:42 GMT

It seems to happen every single time I run ./script/generate model

I open the newly minted fixtures file and go “damn, what are the database columns again?” And of course, even if I could accurately remember them, why should I have to type them all in by hand?

Well, no more. ActiveRecord knows what the columns are so ActiveRecord can just darn-well tell me.


./script/runner "puts MyModel.new().to_yaml" \ 
   >> test/fixtures/my_model.yml

It gives me a lovely list of attributes to play with. Joy.

confess "stupidity" if self.plank?

Posted by trevor Thu, 15 Sep 2005 19:52:50 GMT

Here’s something learned – the hard way.

There is a reason you aliased rm to rm -i. Automatically adding -f to every damn call kind of defeats the purpose, doesn’t it?

What a great way to flush hours worth of work down the bloody drain.

The hidden social toll of Deer Gangs

Posted by trevor Mon, 22 Aug 2005 16:40:00 GMT

It seems that one of the local Deer Gangs was initiating a new member on Saturday night.

These initiations normally involve the young buck proving his bravery by trying to steal the manufacturer’s Marque from the front of a car.

Often the initiation passes without incident: it usually doesn’t take long for the buck to realize, in the eerie sodium-light of a car park at 2AM, that he doesn’t have opposable thumbs and he will never have his prize.

Of course, for the older Deer in the gang, this is the whole point of the initiation – the big joke.

On Saturday night however, an initiation took a tragic turn for the worse.

A local buck-initiate, as stupid as he was brave, decided to impress the rest of the gang by trying this stunt on my wife’s car.

It would have been a lot less messy if he would have waited for the car to come to a complete stop first.

Back on VI - Oh Happy Day

Posted by trevor Wed, 03 Aug 2005 18:00:05 GMT

I’ve been back home on Vancouver Island for less than a week now. I was in the UK visiting family and friends during July and I have to say that I’m really glad to be home now.

Why? Well, it’s the little things. Okay it’s also the big things like we don’t tend to worry about being blown up or anything. But more-so it’s the little things like how you’re treated when you go into a shop.

Good god the people in the UK service industry seem to loath their jobs.

Here on my little island people are doing the same, seemingly pointless, things to earn money. Manning the checkout, stocking shelves, answering the same questions over and over again on the telephone.

And yet, they generally do it with a smile on their face and with a touch of personality. They seem determined to take an otherwise mundane job and add something special to it.

It’s as though everybody on the island was given a copy of Finding Flow after graduating high school or something.

First Rails Wiki Entry

Posted by trevor Tue, 07 Jun 2005 23:36:00 GMT

So it’s been two weeks since I (thought I) had anything to say.

I completely forgot that a few days back I put my first entry into the Ruby On Rails Wiki. That’s got to count for something right?

So, if you’ve ever wondered how to do validations outside of DB-bound models, this is a good starting point.

Rails eager loading bug

Posted by trevor Tue, 24 May 2005 17:29:51 GMT

If you’ve been caught out by the Rails ActiveRecord bug where eager load mysteriously deletes records you might be interested to know that, for me at least, it only happens when you have ”:dependent => true” set on the association.

Half-Baked Epiphany

Posted by trevor Wed, 18 May 2005 18:41:23 GMT

Browsing when I should be working, I learned something when I came across this little essay. Garrett’s ideas are interesting but some of the comments really struck a chord.

One comment in particular talks about “cogs”. To paraphrase – a designer starts from how something looks, building cogs to support it, while a programmer starts from a cog and drapes a skin over it to act as an interface.

What’s interesting is “cogs”. That’s pretty visual isn’t it?

It’s also nothing like how I operate as a programmer. I don’t imagine discrete machine parts turning in concert, I imagine discrete entities talking to each other. Nouns and verbs.

When I express a problem in my head I don’t see the solution unfold, I hear it.

Think fast: Observer or Listener? Granted, they aren’t exactly the same thing but they address similar needs. What do you instinctively reach for? Do you imagine your objects are watching for changes or listening for changes?

Does it even matter? Maybe.

It may be why starting with the interface feels a little unnatural to me. Of course it makes sense – starting with the interface helps to ensure early on that your priorities are aligned with the priorities of the person paying the bills.

I think why I’ve felt it unnatural is because an interface is something you see (it’s not called a ‘display’ for nothing) and something you touch. It passively waits for you to poke and prod. It’s a viewpoint that’s habit more than anything else – I spent too long writing code and then grafting on the interface when the designers were done.

The “half-baked epiphany” is that I’ve probably sold myself short. Not to insult your intelligence (dear reader) by stating the obvious, but “the visual” speaks too. I need to spend more time giving the users an appropriate vocabulary to talk to my code.

Older posts: 1 2 3 4