text_field and currency values 

Filed under: Blogroll on Sunday, September 16th, 2007 by jeremy | Comments Off

I’ve asked around about this issue and nobody has been able to give me a straightforward solution. It is for that reason that I’m not afraid to share my hack. If there’s a better way to do this in Rails, let me know.

The problem: you’re using some sort Numeric field for storing monetary values. But you can’t guarantee how those values will be formatted in the view. It looks best when a field containing a dollar amount is formatted like “2.00″ instead of “2″ or “2.0″ or, god forbid, “2.000″. But you can’t do formatting of the Numeric type (you should be using BigDecimal, btw) and there’s no way to get between the view’s conversion of the number to a string. At least, not without a custom getter / setter in the model.

My solution keeps formatting code in the view, where it belongs. I essentially create a helper method for that view called “monetary_field” that does the string conversion when the text_field tag is rendered. In my template:

<%= monetary_field 'contribution', 'amount', @contribution.amount %>

And my helper method:

def monetary_field(objname, method, value)
text_field "#{objname}", "#{method}", :value => ("%0.2f" % value), :size => 6
end

It works very well for my purposes, though you could make it prettier and more universally applicable. Unfortunately, my deadline is tomorrow, so none of that. But hopefully if anybody else has this problem they’ll find this super simple way to get between the model and the view easily.

Quick Rails Logging Tip 

Filed under: Blogroll on Friday, September 14th, 2007 by jeremy | Comments Off

It’s not difficult to alter how Rails logs requests. Just look at ActionController::Base#log_processing (see code here), and you can see that’s it’s basically just printing request.inspect. So if you override that method in your ApplicationController, you can change how requests are logged. This was important for me, because I’m using gsub to wipe credit card numbers from the log. Works like a charm!

I can take your money 

Filed under: Blogroll on Thursday, September 13th, 2007 by jeremy | Comments Off

Or, rather, the Rails web app I’ve been working on for the past month can. After a very frustrating week of slack-ass-edness from Wachovia, we finally got stuff hooked up right for credit card donations to this new political action committee. So the big hurdle has officially been crossed - though at the cost of much wailing and gnashing of teeth.

And the application is really coming together. The app’s not very complex; it just accepts donations on behalf of others. What has truly impressed me this time around is the agility with which the framework handled the demands I put on it.

At some very inconvenient junctures, I’ve done one major domain model redesign on top of constant controller and view changes, I haven’t yet done something that I feel is technically dubious. Everything has been pretty straight forward and in line with MVC philosophy. I’ve engaged in some “science experiments”, trying to test out better ways of doing certain things I already knew how to do but suspected were accomplished better, but I was never stuck without a tool to get the job done if I needed it.

Rails has enabled me to create a very elegant app, due in no small part to my recent in-depth studies of Ruby. I’ve also been able to get the project done only slightly off schedule in a fraction of the normal development time. I don’t know of any other programming experience where I’ve ever felt I’ve done as clean and thorough a job. And I really think that has a lot more to do with Rails than with me.

Question on migrations 

Filed under: Blogroll on Sunday, September 9th, 2007 by jeremy | Comments Off

OK. So you have been using migrations to iteratively modify your database and populate it with information as you flesh out your application. Awesome. Now, what happens when you change the model definitions and try to rollback and then roll forward?

This is the problem I’m encountering. For example, I’ve made changes to my models through the lifecycle of the app, mostly in adding new fields and deleting others. The model code validates the presence of the most recent set of fields. But in those earlier migrations, I populated the database with model objects that don’t supply that field, or supply different fields, based on the model definition at that time. So if I ever roll back to those earlier migrations, they are apt to fail because model objects are being created based on an outdated model definition.

What is the proper Rails method of dealing with the mismatch between the current model code and legacy migrations that use out-of-date model definitions? I haven’t been able to find any guidance on this matter, which leads me to conclude that I’m asking the wrong question here.

Basic Authentication with Apache 2.2.*, mod_proxy_balancer, and Mongrel 

Filed under: Blogroll on Thursday, September 6th, 2007 by jeremy | Comments Off

Clients frequently like to keep applications under development behind some kind of basic authentication. Apache makes this easy, but mod_proxy_balancer complicates things slightly. While you normally put your AllowOverride, AuthType, etc. directives under the correct Directory definition, if you try doing this alone with a Rails app running on a mongrel cluster, you’ll see it doesn’t work. Instead, you’ll get an authentication dialog for each resource on the page (stylesheet, pictures, etc.) and it will still allow you through to the code! Probably has something to do with the URL rewriting going on to make the cluster proxying possible (which doesn’t happen in the Directory clause).

The solution was found here: put your authentication directives in the proxy definition itself:

<Proxy balancer://my_awesome_cluster>
BalancerMember http://127.0.0.1:9000
BalancerMember http://127.0.0.1:9001
BalancerMember http://127.0.0.1:9002
BalancerMember http://127.0.0.1:9003
AllowOverride AuthConfig
AuthType Basic
AuthName "Top Secret, biotch"
AuthUserFile /etc/apache2/authorized_users.passwd
Require valid-user
</Proxy>

That’ll put the kibosh on broke ass mahfuckahz. Hope this helps somebody.

A Slice of Life 

Filed under: Blogroll on Friday, August 24th, 2007 by jeremy | Comments Off

Yeah, so not a lot of interesting thoughts coming out of my blog these days. Well, right now I’ve got four jobs going on at once, so my free time is pretty expensive lately. The most important one right now is a custom Rails application that I’ve pledged to have beta-ready in less than two weeks. So far, I’m doing pretty good - mostly fleshing out the data model into MVC fanciness and setting up the Subversion repository and a development hosting environment. Yes, I bought another Ubuntu Dapper slice.

With the likely demands of this application, I’ll need full license to tune this thing, so SliceHost is a good choice. Though they’re not the most helpful guys out there, they at least don’t get in your way. Since this is the second time I’ve configured a slice, I thought I’d drop some hints on what got me here. I can’t claim that I followed all these instructions to the letter (in fact, I took some major liberties), but they did give me guidance on setting up my machine:

  • Start with PickledOnion’s Ubuntu setup instructions, as there’s a great introduction to securing the server from intruders.
  • Since I need to run Apache 2.2 (in order to run mod_proxy_balancer with Mongrel), I can build Apache on my own or upgrade to Feisty and pull 2.2.3 off the repository. Follow the instructions here but if you need SSL support be aware that apache_ssl_certificate is broken.
  • Next step is to get your Subversion repository up and running. I recommend these instructions but couldn’t get SSL or WebSVN working with my self-signed fake-o certificate, so feel free to remove the SSL stuff and just run on port 80 with basic authentication. Oh well.
  • Toolman Tim has a great script (which I simply followed manually) for starting a new edge Rails project with Subversion, Piston, and Capistrano.

That should get you all the way to where I am, which is actually writing code now. By the way, this multiple selects form plugin looks awesome (found it here but the repository address listed doesn’t work. I found a working one on Google Code). It’s a way to tie drop-down select boxes who have a foreign key relationship together. Make a selection in the parent drop-down, and it filters by the selection in the child drop-down. And I thought I was gonna have to get all RJS on it.

Congratulations to Megan and Jim! 

Filed under: Blogroll on Sunday, August 19th, 2007 by jeremy | Comments Off

What a wonderful day everybody had yesterday at the marriage of Jim and Megan. Yesterday at the wedding, Megan’s maid of honor spoke on how the sincerity and warmness of Jim and Megan’s relationship so infectiously spreads among their friends and even catalyzes new friendships in the wake of their love. I really couldn’t have said it better (indeed, her version of the idea was much less tortured sounding!). I’m so proud of and happy for them and the awesome, beautiful life they’re going to have together!

Monday Roundup 

Filed under: Blogroll on Monday, August 13th, 2007 by jeremy | Comments Off

Welp, I’m back from the Hoedown and it was a blast. Thanks to Jon, Jim, and Patrick for the company on the near-3 hour drive. There were some wonderful presentations, including one on MERB, tuning Ruby using C, and some reflections on the state of the Ruby community. These coming weeks I’m going to need to knuckle down and blast out code, but being able to see the exciting stuff people are thinking about and doing in the community is awesome. The conference was superbly organized and at $100 a killer value. Looking forward to next year!

BTW, check out this hilarious poster we saw at the gas station we stopped at on the way to and from the conference. Mecklenburg County reprazent!

G’s up, Hoedown 

Filed under: Blogroll on Thursday, August 9th, 2007 by jeremy | Comments Off

Welp, here’s the update on my professional life. I’m working on three contracts, so money is coming in. Hopefully, this money will make the search for a new employer less urgent and allow me to be picky. Additionally, one of these gigs is going to be a fast-paced, agilely developed Rails application for a new political action committee (what’s the emoticon for irony?). I’m excited.

Of course, in the next month or two I’d like to find a full-time job. My ideal position is a small, agile organization where people play roles when necessary instead of clinging to titles. I’d like to play more of a lead developer role - it’s high time for me to stop simply taking orders and start participating in a greater diversity of architectural, business, and strategic decisions in the software biz. For a long time, I’ve felt I’ve had too much yet to learn to take on a great deal of responsibility. No more - I’m ready.

If Tasha and I were better positioned financially, I might go it my own, but it’s just as well - I could stand a bit more experience. And I still intend to continue freelancing on the side, so I can take the move to self-employment gradually. But it’s been absolutely exhilarating to see how much opportunity is out there, and anyway I was getting too complacent for my own good. Only risks yield the big rewards in the market.

Tomorrow I’m headed down to Raleigh with my buds from the Central Virginia Ruby Enthusiasts Group (site’s down) for the Ruby Hoedown where I hope to meet some interesting people and learn about non-Rails applications of Ruby. I’ve already been using Ruby to write scripts for a friend’s business, and it’s a blast. I’m particularly hoping to talk to somebody about cross-platform GUI libraries for desktop app development with Ruby. If you’re there, holla atcha boy.

In the twinkling of an eye 

Filed under: Blogroll on Friday, August 3rd, 2007 by jeremy | Comments Off

Looking back at the beginning of this week, I marvel at the relatively secure position in life I enjoyed - content but static. There wasn’t much on the horizon to indicate that I would be making any giant strides forward, but I could comfortably tread water indefinitely.

Now at the end of the week, the world has turned upside down. I’m suddenly unemployed, the future is wide open, and I’m more excited and energized about life than I’ve been in years. I found a freelance gig within hours of being laid off that couldn’t have been more tailored to my situation, and beyond that I have the freedom to head in any direction I choose.

Life’s weird… and wonderful.