amatsuda/active_decorator — pull request #8 by ronen (2012-05-29 14:25:27+00:00)
Title: Decorate associations of a decorated object

Hi, I just started using active_decorator, it seems great, thanks for making it!

But I quickly came across a feature that I needed: the ability to access an association's decorations. That is, considering the `Book` and `Author` classes in `spec/fake_app` -- I wanted to create a method in `BookDecoration` like:

def attribution
"#{title}, by #{author.capitalized_name}"
end

which uses `AuthorDecoration#capitalized_name`. But typically although `this` is decorated, `this.author` is not decorated so the method fails.

The solution I came up with is, after decorating an object, to introspect on all associations and override their methods in the object's singleton class, to call super() and return the decorated result. This approach seems to work, and has the feature of being "lazy" in that the associated objects only get decorated if/when they're accessed. I don't think it will conflict with anything else, though I'll admit I haven't put very deep thought into it.

Cheers