Cookin' with Ruby on Rails - May

by Bill Walton

Editor's Note: This series continues with Cookin' With Ruby on Rails: Designing for Testability.

NOTE TO READER: This tutorial is intended for use with Rails 1.2.6. It has not yet been updated to support Rails 2.x.

It's getting close to lunch and CB is thinking, "That sure was some good pizza Boss brought by the other day.  I wish I'd thought to write down the name of the place.  Jeez, I can almost smell it just thinking about it.  Hey, wait a minute.  I think I DO smell it!"  And in walks Boss carrying a fresh pie, all smiles and looking like all's right with the world.

CB:  Hi, Boss.  Whatcha got there?

Boss:  Hey, CB.  I just thought I'd drop by and thank you for getting me out of that jam.  I really appreciate it.

 No problem, Boss.  I was glad to get the chance to show you Ruby on Rails.  I've been wondering how your preso went.  I haven't seen you in a bit.

Boss:  The demo went great, CB!  The reason I haven't been by is that I walked out of the meeting with a whole stack of action items. You're in there, but there were some others I needed to handle before coming back to you.  Like "breaking the ice" for you with the Ops guys.  I don't have time right now to get into the details, but let me bring you up to speed on the project overall.

The little app you coded up for me the other day over lunch was a proof-of-concept, so to speak. My boss and I have floated an idea for a new company-sponsored website.  The problem is that the VPs needed some convincing when we said that we wanted our IT department to develop and maintain it.  Our company, the business our group supports, is in the cookware business.  Our group's day-to-day work is normally focused on Accounting, Finance, and Manufacturing apps, you know.  Not web-based systems.  So they needed a little demonstration to show them we could deliver. And, with your help, we did.  So they've OK'd us moving forward. Slowly, but forward.  The picture from 50,000 feet is this...

The company is going to put up a cookbook site that will let our customers enter their recipes into the general collection, search for recipes, and collect recipes into their own personal cookbooks. That's just the basics.  I expect us to be able to do some really cool stuff along the way, but we're going to have to take it a step at a time.  What I got them to agree to was that we'd only work on stuff that was requested by customers using the site's "feedback" link.   Or stuff requested by the VPs, of course ;-)

CB:  Cool!  So, aside from taking that pizza off your hands (he says with a grin, reaching for the box), what can I do for you?  I mean, I'd really love to be able to work in Rails, but I've already got a lot on my plate.  And there's more to developing and deploying a website than just coding.  You got some additional resources in mind?

Boss:  Of course, CB.  In fact, Paul should be stopping by any minute now.  And I'll be shifting some of your current workload to other members of the team.  The level of funding I've been able to get commitments for so far won't support full-time work on it for you guys, but I'll make sure you get at least half-day blocks of time, not half-hours here and there.  Ah, here's Paul now.  Well, I'm going to take off and leave the pie to you two.  I'd appreciate it if you could scratch out a high-level plan over lunch.  What do you think?

CB:  No sweat, boss.  You got the pizza.  We'll get the plan.  Heck, I'll bet we even get some work done on the app!  Pull up a chair, Paul.  Grab some napkins, will you?

Paul:  Got it, CB.  You still got your Rails development environment set up?

CB:  Absolutely, Paul.  Let me grab a piece of that pie, and I'll fire it up!  

Paul:   Great.  But before we jump in, what did you mean when you said "get some work done on the app"?  I'm not sure we have enough info to start coding.

CB:  You're right, Paul. We're going to want Boss or somebody he designates to act as our Customer, our single source for directions.  They're going to tell us stories about how they want to use the system, we'll code that up, they'll use it and see where we are.  Then we'll go 'round again.  Since we have no stories today, we write no code today.  No application code, that is.  There's some infrastructure we're going to need no matter where the application goes from here: unit tests and migrations.  Given that we have Customer go-ahead on the project, we know that we need to put those in place to move forward.  It's a small enough piece of work that it doesn't warrant going back to Boss to get approval.

Paul:  Got it.  That'll work.  I guess.  I grep "unit tests."  What's a "migration"?

CB:  Migrations are a Rails mechanism for managing the database component of our project.  They give us the ability to manage our schema with Ruby scripts instead of SQL.  They also give us a good level of database independence.   The same migration script will generate the appropriate commands for MySQL, PostgreSQL, SQLite, SQL Server, Sybase, or Oracle (all supported databases except DB2).  And they give us the ability to easily preserve the existing data in a live database.  There's an excellent screencast by David Heinemer-Hanson, the original creator of Rails, that you should take a look at.  And you should take a look at the Rails documentation too.

 OK.  So where do you want to start?  Migrations?  Unit tests?  Or planning?

CB:  Let's spend just a couple of minutes on planning.  What I'd like to do first is settle on some initial role definitions.  Right now, there's three of us: you, me, and Boss.  The way I see it, there are also three roles:  Developer, Customer, and Tester.  So we can start out, if it's OK with you, with Boss playing the Customer role, you playing the Tester role, and me playing the Developer role.  The Tester role is really a subrole for both the Developer and the Customer roles, but since, in our case, playing the role is going to involve developing technical expertise with a set of tools, I think it makes sense to break it out--to give you a chance to come up to speed one step at a time.  What do you think?

Paul:  That sounds OK as long as you're not thinking of pigeon-holing me as your tester.  I mean, I've been doing architecture and project management work here for a while now.  

CB:  Not at all.  Just a way for us to step off.  We'll be doing a lot of role swapping/sharing before it's over.  It just seems like a good way to bring you up to speed on Ruby and Rails.  And since the Tester role has both Developer and Customer pieces to it, and since your normal architect and project manager roles put you in contact with Boss a lot more than me in the course of a typical day, it seems like a natural fit on that front, too.  What do you think?

Paul:  OK.  Let's run with that.  What else?

CB:  Well, we haven't been given any info on budget or due date, so I really don't see any value in spending any more time on planning at this point.  Do you?

Paul:  Nope.  So what now?  Migrations or unit tests?

CB:  Well, we could do them in either order, but if we do the migrations first, we'll get some help on the unit test side.

Paul:  Help is good ;-)  

CB:  Exactly ;-)  So to start, let's make sure we've still got a working app.  I've fired up the copy of Instant Rails I brought in.  Let's start mongrel and see what we've got.  Why don't you take the keyboard?  Typing the commands myself really helps me internalize the learning a lot faster than just watching someone else do it.

Paul:  Me too.  Thanks.  If I remember, I need to open a command window,  change to the rails application directory, and enter:

mongrel_rails start

Starting mongrel
Figure 1.

CB:  Good memory!

Paul:  Not that good.  I can't remember the URL we used.

CB:  No problem.  Just remember that all Rails URLs map to methods in controllers.  Easiest thing to do when that happens is just check that app/controllers directory and see what's in there.  I'll just open another command window and...

app/controllers directory contents
Figure 2.

Paul:  There we go.  Now I remember.


cookbook2 opening recipe page
Figure 3.

CB:  Why don't you poke around a bit and make sure it's still working OK?  I can't think of any reason it wouldn't be, but let's make sure. Meanwhile, I think I'll grab another piece of that pizza.

Paul:  Yeah, sure.  I test.  You eat.  Where's the justice in that?

CB:  Think of it as motivation to master the tools of your new role, Grasshopper ;-)

Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow