Rails3 Custom Password Validators

As I was writing validators for the User class of a Rails 3 app, I wanted to make sure that people wouldn't use their names, usernames, or email addresses as passwords.
Unfortunately I couldn't find a way to accomplish this with the built-in validators. Fortunately Rails 3 makes it easy to write your own custom validators.

Here's an extract of my User class

The "password => true" tells Rails to call my custom validator which, in this case, has to be called password_format.rb.

I keep my custom validators in /lib/validators, so I need to add the following to my config/application.rb file:

And finally the validator itself:

(Don't forget to write the specs to test this! :-)

Rails3 Mind Map

I find mind maps useful for many purposes. The process of clearing your screen, fullscreening your mind mapping tool, and immersing yourself in a topic of interested is a great brainstorming exercise.

Today, my goal wasn't creativity, it was to build a map of main components of Rails 3. You'll likely find the PDF more useful as its nodes are clickable and refer back to the Rails API (and github in a couple cases where I found documentation to be more useful).

This isn't a comprehensive map. Let me know what I've missed.

(This map was created with MindNode Pro, an easy to use, cheap, and high quality OS X app, proving that sometimes you can have your cake and eat it :-)

No More Excuses! Using RVM to Play with Rails 3

Now that Rails 3.0 is out, it's high time to start using it. But what if you want to keep Rails 2.x around for your current projects? Fortunately, on OS X, there's a simple solution: RVM.

Once you've installed RVM, you'll need to install a version of ruby compatible with Rails 3. There are two choices: 1.8.7 and 1.9.2. Given its new features and speed improvements, 1.9.2 is the one to choose, unless you have particular dependencies on 1.8.7.

Installing 1.9.2 is simple: rvm install 1.9.2. This will download, compile, and install 1.9.2 to a .rvm folder in your home directory.

Once that's done, type rvm 1.9.2 to switch over and rvm info to confirm that you're now running 1.9.2. Note: this will only apply to the current terminal window, here's how to make it the default.

Type gem list and you should see just two gems: rake and bundler.

Now go ahead and install Rails 3: gem install rails. Confirm by way of rails --version and gem list.

That's it, you're done... Now have fun!

What to go back to your previous version of ruby? Just type: rvm system and you'll revert back to your standard ruby installation and the gems that went with it.

RailsConf 2006

I attended RailsConf in Chicago about 10 days ago. It was a great way to find out what was happening in arguably the most vibrant part of the web development community. I met a lot of people over those three days and would always ask them: "So, are you doing rails by day, by night, or both?". Most of the answers were "by night", with a few "just starting by day", and one "both". From my unstatistical sample, it seems like rails clearly had people's mindshare but getting it into their "workshare" is a different story.

A quick search on google or technorati will turn up a lot of info on the conference. Here are some of my favorite moments (in no particular order):

  • Martin Fowler's keynote: he spoke for over an hour about ruby, rails, frameworks & their (dis)advantages, etc. All pretty much ad lib. I have a number of his books and it was great hearing him speak live. Very interesting.
  • Why the lucky stiff's concert / monologue / animations extraordinaire. A glance at his site will tell you that all is not as it should be in Why's brain, which made his concert that much more fun.
  • Mike Clark's intro to Capistrano. This is the utility I really want to play with.
  • Justin Gehtland's overview of Ajax and RJS with Rails, and esp. the sneak peak at the new version of scaffolding he and his colleagues are working on called Streamlined.
  • One of the pleasures of attending a conference is discovering great new speakers. Out of the railsconf crew, my pick goes to Matt Biddulph. Matt's presentation was not only engaging, enlightening, and full of cool annecdotes but also focused on a BBC-related project. Having grown up in London, the beeb is still near and dear to my heart. I've found lots of interesting topics on Matt's blog, such as this great example of putting the Wikipedia and Yahoo APIs to use. Cheers Matt!
  • DHH's keynote started off with me thinking "CRUD? Why is he talking to us about CRUD?" and ended with the thoughts "Wow! This is a cool way of looking at things, must try it out!".
  • Last but not least, hooking up with a long lost friend and, as it turns out, rails and OSX fan Steve Chanin.

Next year's conference is in Portland and will be co-organized by O'Reilly. They're clearly pursuing rails as strong alpha (and dare I say beta?) geek territory. Lots of fun in store!

SQL Server Adapter Fixed (sort of)

Many thanks to Ryan Tomayko for fixing the MS SQL adapter bug I posted a little while back.

Things aren't all roses, as Ryan states:

Quick Note: the SQL parsing regexp stuff in this adapter is bound to fail in many edge cases. This patch improves the accuracy of the regexen in many places but the basic parsing logic is flawed and could probably use a complete overhaul that accounted for things like habtm and eager loading issues from the beginning.

But it's still cool to see the process working and bugs getting fixed Cheers Ryan!

ActiveRecord and SQL Server 2005 not ready for primetime

We use a lot of Microsoft technologies at work, so I was curious to see how well ActiveRecord would work with SQL Server 2005 (I tested against the CTP edition).

Why not stick with MySQL? Well, though I use MySQL personally for some applications, SQL Server 2005 is significantly more sophisticated. If it works well with AR, then I get the best of both worlds...

The feedback is mixed. On the plus side, getting up and running was painless. You won't be able to do windows integrated auth but SQL Server auth works fine and once the user is created, you're up and running.

On the downside, the SQL Server adapter is broken. I logged a bug in the Rails tracker (see link for more details). In some cases, the adapter can confuse data in the query for its instructions, and end up running the mangling the query. We either need a much more sophisticated set of regexs or (preferrably) a way for an adapter to get the information it needs without resorting to parsing the query.

Interestingly the MySQL adapter doesn't need to resort to regexps at all, nor do most (all?) of the other adapters. For the moment, I'm sticking with Rails on MySQL!

Sam Ruby on Rails

Amusing quote by Sam Ruby speaking to a group of Java developers:

  • Rails is the 80/20 rule applied twice
  • 64% of the function for 4% of the complexity
  • What about the "other" 36%?
  • If anybody here doesn't believe that J2EE has 36% fat, I'm talking to the wrong audience.