jcasimir/draper — Issue #82: Feature proposal: Decorate associations
Author: tarasevich
Created: 2011-11-15 12:15:37+00:00
State: open

Consider we have models User and Post with decorators and a user `has_many :posts`.
In a controller we expose a user decorator to the view:

@user_decorator = UserDecorator.find params[:id]

In the view or in the controller it would be nice to access PostDecorators with a concise syntax, like this:

`@user_decorator.posts.each do |post_decorator|`

In decorator it would be nice to have the following syntax, or something similar:

class UserDecorator < ApplicationDecorator
decorates :user
decorates_associations :posts
...

Basically the proposal has two parts:

1. classes Base and DecoratedEnumerableProxy should support lazy decorating of associations ( by analogy with relations/scopes)
2. define class methods that declare association decorator accessors like this: `decorates_associations :posts`

With 1st point the problem is here:

base.rb-95- def self.decorate(input, context = {})
base.rb:96: input.respond_to?(:each) ? Draper::DecoratedEnumerableProxy.new(input, self, context) : new(input, context)

ActiveRecord::Associations::CollectionProxy does not have `each` method declared. So the code does not work as desired.