decorators for ActiveRecord model attributes

Something I’ve always felt was missing from ActiveRecord is the ability to abstract out presentation logic for attributes. For example, you may want to be able to get at an attribute called full_name, but store the constituent parts, first_name and last_name, in your relational database.

Thankfully, I am not alone. There’s a ticket on this and in the meantime an associated plugin up on github.

I think this is a logical evolution from the discussion of the presenter pattern. Although I liked CachingPresenter and ActivePresenter based on the idea, I need to decorate methods on the way through JSON serialization for an ExtJS app and there’s no logical place for one of the aforementioned presenter plugins to make an appearance without monkeying with resource_controller’s default RESTful actions.

Ideally, I can simply do

@person.to_json

And I’ll magically have my full_name attribute.

Of course, you can format data on a Web client with JavaScript, but I also generate PDF files which need identical formatting.

Update. It could also be that I want to use composed_of, but had no idea of its existence until now.

Post a Comment

You must be logged in to post a comment.