Rewriting WordPress in Rails

Earlier when I wrote that I didn’t like the template language WordPress uses, I also made the joke about rewriting WP in Rails. Well, today I thought “Let’s give it a try”. Not that I seriously want to rewrite WP, but I was curious to see how Rails can deal with legacy database schemas.

I was up and running pretty quickly: Pointed database.yml to my WP’s database, created Post and Author models as well as a post_controller. It was doing what I inteded it to do after a couple of minutes, but I didn’t like the column names (resulting in the attributes of the objects). It would have been too easy to write a new pair of accessor and mutator with nice names for each ugly column name. But, after all, Rails promotes the DRY principle, so I was looking for a better means to map column names to nice attrbute names.

So I found Joshua Sierles posting “Rails aliasing of database column names”, However, I wasn’t able to get it to run from the project’s lib directory, so I created a Rails plugin for it. I works well for me, and it’s good Rails style to put extensions into plugins.

If you like the functionality, you can download the plugin (alias_column.zip) and unzip it into the vendor/plugins directory of your Rails project. The plugin works exactly as Joshua describes.

I found one caveat, however. It doesn’t seem possible to use dynamic finders with the alias_column. I map the post_status column of the wp_post table to status, so I can call

p = Post.find :first
p.status

which works fine. But using p.status instead of p.post_status doesn’t work for the find methods. I want to call

Post.find_all_by_status ‘publish’

but it just throws an error saying

undefined method `find_all_by_status’ for Post:Class

Please leave a comment or mail me if you have an idea how to get this to work.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>