drapergem/draper — pull request #324 by haines (2012-11-01 02:15:15+00:00)
Title: Refactoring

I went through the `CollectionDecorator` specs that I had previously just cut-pasted from `Decorator` and tidied them up.

Along the way I refactored a bunch of things, the important (/breaking) changes are

`Decorator`:
- Used `source` rather than `model` throughout `Decorator`, since the source is not necessarily a model. This makes it consistent with `CollectionDecorator`. Retained `#model` as an alias.
- Dropped `#wrapped_object`. We already have `source`, `to_source` and `model`, it seems like that should be plenty of aliases!

`CollectionDecorator`:
- Changed the constructor from `.new(collection, SomeDecorator)` to `.new(collection, with: SomeDecorator)`, so that it infers the decorator when it is omitted.
- `.decorate` then becomes a simple alias of `.new`. Previously the option was called `class` but I think `ProductsDecorator.decorate(collection, with: WidgetDecorator)` reads better.
- Dropped `.decorates`, which was an alias for `.decorate` but didn't really make sense.
- Raise an error if we try to infer the decorator for a `CollectionDecorator` with a singular name (`CollectionDecorator` itself being an obvious example), otherwise we try to decorate each item with the collection decorator.
- `#find` no longer proxies to the class of the first item in the collection, but instead goes to the decorator (i.e. `product.pets.find(params[:id])` will call `ProductDecorator.find`). This means that the return value is decorated, and that it works for empty collections.