rails + emacs + windoze = less fail

Posted on August 10th, 2009 in rails | No Comments »

Have spent the last few days working in Vista rather than OS X and it was looking fairly bleak until I spent some time updating my favorite editor. I have never liked full blown IDE’s (like Eclipse) for rails work. My first editor and still favorite is emacs. Textmate keeps my fingers humming on OS X but emacs is also running somewhere too. On windows, emacs is the main tool but I never really invested the time to update it (was running 21.x and 22.x versions which is quite old). So I spent some time this weekend to update it to 23.1.50 and add the ruby and rails modes. After some missteps, I finally have a fairly nice environment with pretty nice rails support. It even has snippets and quick navigation and refactoring (quickly create partials or helpers for example). I even have the ‘railscast’ theme so it looks pretty nice (at least what I’m used to on my Mac).

I can’t wait to go back to the Mac but at least I can be more productive while I’m slumming in Vista. The LONG delay on starting ruby processes (like tests, console, etc…) is sapping my will to live tho…

The info on rails and emacs on the web was pretty good but if someone wants my formula, I’ll be glad to post it.

Bitten by HABTM Again

Posted on July 23rd, 2009 in rails | No Comments »

I while back needed to add an audit trail to an older project. I found the acts as audited plugin and put it into play and it is great. I added my models to it and added a way to review the audit trail online – all good – until…

I started added audits for my model associations. This went well until I wanted to audit a has and belongs to many (HABTM) association which as you know, doesn’t have to have a backing model (it simply relies on a join table that is named a particular way). Sigh. On a few HABTM as sociations, I actually had a model but our smarted myself (and followed an older web recipe) and created the model without id’s.  These associations do not work with this plugin.

No way to get there from here exept to convert HABTM to has many through. Further evidence why HABTM is shortcut that only leads to pain later if you need to extend the associations (like adding audit trails).

Avoid HABTM and your life will be better…. just imagine having to convert 10 or more association tables to models on a production system that has 1000’s of parent objects. You see any fun in that? The superstitious would call how HABTM handles join objects as ‘magic’ which isn’t really very accurate. It is simply a conventional shortcut. My big problem with it is the lack of flexibility it gives you – it is one of those box-canyon paradigms that is hard to get out of.

Oh and another note, if you add acts_as_audited to your project late in the game, you may run into problems with migrations if any migrations load data. The reason is a fresh checkout will bring the audit code which will perform audits even on migrated data and since early migrations don’t have the audit table loaded yet (it is a later migration), you can’t migrate data. Sure you can just do a rake db:schema:load but that will leave you without some of your migration data.

An easy solution is to simply rename the acts as audited migration to be your first migration (mung the date in the file name). This tricks the migration to run first and all should be fine… Note, this even works if you have previously migrated and then do an update to get this new ‘old’ migration – the migration still runs even though it is out of sequence thanks to the schema_migrations table which keeps up with what migrations have been run.

Silly Simple Git Initialization for your Rails Projects

Posted on July 6th, 2009 in rails | No Comments »

It is such a joy to have such great technologies at our fingertips. Ruby on Rails can be a fantastic productivity booster for projects but I often find I get huge benefits from it for simply exploring ideas or working up a quick functional prototype. By adding on things like customized generators that suit your client’s configuration and customizable menus, you can get to a workable prototype in record time (minutes to hours).

Part of the benefit of working with rails projects is the speed with which you can refactor and scaffold your way into different configurations. This can sometimes get you into trouble if an experiment or plugin doesn’t work. The best solution I’ve found for this is to ALWAYS initialized your fresh rails project into a git repository. This is like having infinite idea or skill mulligan’s for your project – a great safety net that let’s you go fast without fear.

Ryan Bates’s excellent screen cast series had a nice review of using git with rails and one of the commenters (Pete Yandell) offered a script to simplify life. This script sets up a standard rails project into git with the proper ignores. I modified the original script slightly to avoid placing .gitignore files into the empty directories within the .git directory. This is a harmless change but it seems cleaner to me. Here is my version of it:


#!/bin/sh
cat < .gitignore
log/*.log
tmp/**/*
db/*.sqlite3
coverage
doc/app/*
EOF
find . -type d -empty -exec touch {}/.gitignore \;
git init
echo "TODO - provide project README" > README
git add .
git commit -a -m "Initial import."

(here’s the gist)

This script works great (on OS X anyway) for setting up a project in git. This allows me to extend the ‘prototype’ from hours to multiple days if I need to without losing control or risking breaking the whole thing with a laps of concentration. I can create branches for experimental ideas and quickly switch back and forth. As long as my migrations are current and especially if I’m using data generators like populator, I can switch between branches to show my client a few different ideas or behaviors within seconds.

It is so great to have a (revisable) history of what I’ve done on a project. I can also quickly stash an idea away to work on it later. No matter what I do on the prototype, I can always recover back to some previous spot without much fuss or concern.

This technique works so well because it is all self contained within your rails directory and because it is such a low-overhead and fast approach.

The approach works like this:

  1. rails some_new_project
  2. cd some_new_project
  3. git-rails
  4. work on project for minutes or weeks with frequent checkins
  5. create branches, rebase, squash commits, what ever – repeat previous steps
  6. if want to keep it, move it to a public repo, if not simply rm -rf the entire directory…

Rails and git are really two powerful tools that have greatly revolutionized my personal work flow. I get to move quickly and with the assurance that I can undo any bad ideas. Infinite idea mulligans. I also get to practice using git even if my client is stuck on subversion (or worse). I highly recommend ALWAYS setting up your rails projects this way.

Powerful Results with Google Charts

Posted on July 15th, 2008 in agile | No Comments »

I was working on a scrum team that wanted to make our burn down charts available on our wiki. We tried the movable type chart plugins and they worked ok but we lacked the control we needed. I had heard about google charts and after looking into them I realized that we might be able to coax these to produce the charts we were missing. To my surprise this was quite easy to prototype but a little harder to do in practice. The nice thing about the google charts, is they are essentially image tags with specifically built URL’s. Constructing the URL can be a challenge but in general the performance we observed was great and the price (free) can’t be beat. You can read more about it here.

Read the rest of this entry »

Continuous Integration Too Simple to Ignore

Posted on April 15th, 2008 in rails | No Comments »

I’ve always been a big fan of continuous integration. There is something empowering about having a tireless little robot patiently waiting for you to check your code in. When it sees your latest commit, it quickly downloads it and then runs all your tests. When that’s over, it prints a nice web summary of what it did. It can even update your main team portal pages with updated content (javadocs, rdocs, test coverage, daily metrics, whatever). It is like having a groupie fanbot who loves your code.

Until you check in something that breaks (fails a test or worse yet, doesn’t compile). Then your little groupie is more like a stalker. Not only does it display a nasty web page (in shades of red) about the build YOU broke but it probably sent out a raft of emails about it. Pretty embarrassing.

Read the rest of this entry »

Boost Team Productivity and Consistency By Developing and Using Rails Generators

Posted on March 1st, 2008 in rails | 1 Comment »

If you have worked with Ruby on Rails for any time at all, you are very comfortable with the speed at which you can create an application or prototype. Using scaffold generators and by following the conventions of rails, you can easily get started on a prototype app in a few minutes.  As you start to incorporate your client’s styling, menus or other markup/design aspects the power of generators and scaffolds seems to diminished.  If you find yourself in a position of creating the same menu or stylistic elements over and over for multiple sites, you should consider packaging them generically as a generator gem.

In some recent projects, we were creating sites with a consistent  ‘intranet’ look and feel (mandated by the client’s web guidelines).  Doing this each time was a pain because of the need to rework the style sheets, layouts and copy in css images. Lots of  moving parts and hard for the some of the less experienced team members to get right without lots of help. I kept thinking, it would be great if there was some way to generate the boilerplate web menus for our apps so you can get up and running in minutes.  This is where taking a little time to create a generator and package it as an internal gem that each team member can install and use.

Read the rest of this entry »