Using method_missing to cleanup your models

April 23, 2009

I’ve inherited an Entity-Attribute-Value (EAV) database setup at work.  I’ve never really seen this used at the scale that it was used.  EAV’s are great for rapidly changing database fields, until now I’ve seen them mostly for select box’s and configurations.

In my instance, I have a main table for items and the rest of the attributes can be looked up.  EAV’s in Rails actually is easier than it was implemented in PHP.  However, the ever changing attributes would be better setup using meta-programing.

In my model, I define an attributes method, like so:

def attributes
@attribute ||= {}
end

This makes an attribute instance available for my method_missing call.  In particular, I’ll need to make a call to the database, so this caches the result for me.

Now for the real magic.  Typically, the item record has an id, itemcode, product_type, and title.  So I can call “item.title” and get the item’s title.  Other attributes like the “subtitle”, “price”, and “image” are not.  Using method_missing I can get at them as if they were part of the item model.


def method_missing(method)
begin
super
rescue
return attributes[method.to_sym] if attributes[method.to_sym]
attributes[method.to_sym] = Attribute::get(itemcode, method.to_s) unless method.nil? || method.blank?
end
end

The begin… rescue blocks test method missing and rescue block is the code needed access the attributes.  Calling super, keeps method_missing functionality in place.  If that fails, it checks the attributes hash, then calls the Attribute::get method.

With over 330 possible attributes (and counting), I’ll be able to access them all like they are part of the item!


Everything You Know About CSS is Wrong!

February 3, 2009
Everything You Know about CSS is wrong! Everything You Know About CSS is Wrong!

by Rachel Andrew, and Kevin Yank

To hone my skills as an application developer, where visual design is a weakness, I was intrigued by this title. A rather thin title around 100 pages and color illustrations, it looked promising.  I was hoping for a silver bullet, but instead was left disappointed.

The book first outlines the problem of CSS, in general its lack of support for a grid layout with CSS in IE7 and below.  Many past hacks have been implemented to enable this with CSS, which reminds me exactly why I dislike CSS.  Otherwise, grid layouts are easily handled by tables, but they were never intended for complicated layouts that developers have thrown together.  This book argues that IE8 will change all that and solve grid layout problems.

I believe its primary purpose is to give developers a road map to start implementing CSS tables for design layout.  Transitioning to use CSS tables will be a short 2-3 years as a majority of IE users upgrade.  It will be a sacrifice of either time or the final layout for older browsers.  If we need to support a pixel perfect design then it will take time to design specifically for older browsers.  If the design does not need to be the same across browsers, then several techniques are discussed that can be more vertical than a grid.

This is why CSS is so painful and why I don’t want to focus on CSS design, normally I’d stick to html tables if anything was too complicated.  In my controlled environment, it is easier for me to transition to using CSS tables.  For others, this can help make the transition quicker.

CSS has been a slow moving train and the authors point out that it is both the browser’s and designer’s fault.  We need to keep up to date with features that are implemented and utilize them early.  We as developers tend to complain about the lack of standards, however we don’t implement the new features that we have available.  Otherwise, browsers creators have no reason to implement standards.  I think it comes down to a triangle of browsers updating and designers updating their web layouts, which will follow with users upgrading their browsers more often.


Hello world!

January 25, 2009

This is my first post and I’m really excited to start blogging.  I’m wanted to get started, but I’ve been too lazy.

I’ve been a lone ranger too long.  My typical experience has been I can figure out things on my own, so I don’t need anyone.  I’m not sure if others feel that way, but I’ve accomplished a lot in the past few years, including running a marathon, learning new programming languages, and raising two girls with my lovely wife Caroline.

I couldn’t be happier, but the reality is that we need other people and we also need to contribute back.  As a person, I am very detailed.  And I like to help others with programming Rails.  Also as a professional, I want to share and grow.

Coming next, I promised ChicagoRuby.org to do two book reports on CSS and Ruby.