Subvert legacy table schemas with alias_column

Earlier this week I was writing a model class for a table whose schema broke Rails coding convention. Some column names were written in CamelCase, some were way too short, and the naming scheme overall was inconsistent. Here’s some sample code that gives you an idea of what I’m talking about:

<%= text_field 'order', 'BillingAddressLine1' -%>
<%= text_field 'order', 'order_recipient' -%>

Okay, easily taken care of. I’ll just rename the table columns in the database; they were due for a makeover anyways. But what if you can’t? What if there are shell scripts or other web apps that depend on the table definition as-is, and you don’t have the time/budget to change all of them?

And so alias_column was born. It’s a single function that takes a hash of column aliases. I wrapped it up as a plugin because, hey, I’d never built a proper one before.

Here’s our new Order model definition:

class Order
  alias_column :billing1 => :BillingAddressLine1,
               :billing2 => :BillingAddressLine2,
               :credit_card_number => :ccNum

  validates_presence_of :billing1
end

Now you can access those fields just as you would the old ones. Validators, form helpers, and group setters all work fine.

$ ruby script/console
$ Loading development environment.
>> order = Order.new :billing1 => '123 Main St.', :billing2 => 'Unit 101'
=> <Order:...>
>> order.billing1
=> '123 Main St.'
>> order.credit_card_number
=> nil

There’s nothing fancy going on here: alias_column just creates read/write accessors for your column attributes. It does not introduce a second layer of abstraction between ActiveRecord and your table. For example, Order.columns will still return me the original columns and their db-specific connections.

To install the plugin, use the following:

ruby script/plugin install svn://www.benlog.org/pub/plugins/alias_column

Edit: Okay, it turns out this has been done before. But if it’s of any consolation, those solutions don’t make use of that nice hash statement.

Happy Holidays, and things to come

Happy Holidays everyone. It’s been nearly two weeks since I released Reddish, and there’s been nary a peep on this blog. Busy month. I’m really pleased to see that people are downloading the theme and giving it a try. I’ve been sneaking around, checking out people’s sites, and I’ve noticed a number of outstanding issues w/ Reddish that I’d like to fix asap.

  • Modifying the default column widths breaks the shadows
  • Numbered lists don’t have proper margins
  • There’s no place for a blog sub-title
  • Articles with expired comments still show ‘Post a comment’
  • ‘Continue Reading …’ appears in both the article body and excerpt

Whoops. If you’ve found any others, please let me know!

Introducing Reddish, a Mephisto theme

Reddish is a simple, extendable Mephisto theme – and my former blog design!

Download

You can download the latest tarball (.tar.gz), or visit the project page on GitHub.

Installation

You’ll want to unpack this in /yourblog/themes/site-1/reddish, then enable the theme through Mephisto’s admin interface.

More

Reddish is a re-skinned version of Scribbish, an excellent blogging theme designed by packagethief and ported to Mephisto by DeLynn Berry. The CSS is almost totally redone (only the syntax highlighting remains), but the XHTML/Liquid templates are 80-90% the same. Really, hats off to those guys for making Scribbish so well organized and easy to work with.

Some other details, off the top of my head.

Reddish was last updated May 7th, 2007.

I plan to revise Reddish on a regular basis, so comments are always welcome.

New site design nearly there

Things have been a little slow as far as content goes, but that’s because I’ve been cooking up this new theme for the past week. It’s not 100% done – there’s still some scribbish code hanging around (like the green on black code blocks), and I’m still deciding between a few link styles and effects, but it’s getting close.

I’ve learned that putting a theme together isn’t trivial, so I can understand why there’s dozens of scribbish-themed blogs out there. When it’s all said and done, I’d like to package this up and distribute it with a creative commons license. There’s only a handful of public Mephisto themes available (4-5 by my count), the last of which was released months ago, so hopefully someone will find it useful.

Anyways, if anyone has any comments, I’d love to hear them.