I was doing a little Ruby on Rails hacking and I needed an easy way to automatically format ActiveRecord string columns. I was surprised that this wasn’t built into Rails already, and even more when I couldn’t find a 3rd party plugin that did that. I guess the open source community was calling on me to write my own!
I’m glad to introduce acts_as_formatted 0.1. Here’s an example of how it works. As you can see, it is pretty straightforward:
class Person < ActiveRecord::Base acts_as_formatted :case => :capitalize, :except => :suffix end
This snippet formats all string fields by Capitalizing the first letter of a string, and by stripping leading and trailing white spaces. No more custom setters. acts_as_formatted can also change the case to UPPERCASE, lowercase or sWAP cASE. Check out the README for more details on usage and installation instructions. Comments are welcome, of course!
November 10th, 2006 at 12:02 am
I’m an extreme newbie, but isnt this built in to the Ruby string class itself?
November 10th, 2006 at 12:56 am
The formatting is indeed built in the String class, but this plugin adds automatic formatting to ActiveRecord string columns, which is different. There was no way to automate this before.