jcasimir/draper — Issue #78: Feature proposal: Decorate collection proxy
Author: pfeiffer
Created: 2011-11-07 09:28:55+00:00
State: open

I sometimes find it very handy to add decoration methods to the collection object, as well as the individual objects of the collection.

As an example, lets say I have a shop and want to display a list of orders received recently in a table view. I also want to display a summary in the bottom of the table displaying total order value of the visible orders. Typically I would iterate through the order collection and output the individual order as a row, while summing up the `amount`'s into some instance variable.

We can do better - a proposed example for controller and view code:

**Controller:**

```
@orders = OrderCollectionDecorator.new(@orders)
```

**View:**

```
%table
%tbody
- @orders.each do |order|
%tr
%td= order.customer
%td= order.amount
%tfoot
%tr
%td "Total value of orders:"
%td= @orders.amount
```

In this case the last `amount`, summarizing the total order amount of the orders, is a decorated method on the collection, rather than on the individual models in the collection. The simplicity offered gets even greater when the need arises for more summarizing methods eg. average order value, total number of items in orders etc.

At the moment there's no easy way to implement decoration methods on the collection.

What do you think of this idiom? Is there any way we can achieve it with Draper?

A quick and easy step in the right direction would be to extract the methods of `Draper::DecoratedEnumerableProxy` into a module, so it as least could be included in the collection decorators. I can certainly do this and open a pull request, but I'd like to get your inputs before hand.