draper icon indicating copy to clipboard operation
draper copied to clipboard

Add better support for activerecord relations and mongoid criteria

Open donaldpiret opened this issue 9 years ago • 7 comments

Cleaned up implementation of #610 with Mongoid support

donaldpiret avatar Mar 28 '15 09:03 donaldpiret

I think a valuable question here is if we want to explicitly continue support for Mongoid.

What does everyone think about this? I think a good discussion about it either way would be valuable, arguments for and arguments against.

mikedao avatar Apr 03 '15 21:04 mikedao

I was working with your branch and it works well but I noticed two small issues.

  1. edit my own fault
  2. RelationDecorator doesn't work with render collection: Model.decorated_associaton. It won't error but it doesn't render any of the partials so it seems unable to find the models in RelationDecorator.

The ActionView partial renderer calls to_ary on the object passed the collection parameter. I was able to resolve number two by defining this method. There may be a better way already in draper.

# lib/draper/relation_decorator.rb
def to_ary
   relation.map(&:decorate)
end

colinpetruno avatar May 31 '15 05:05 colinpetruno

Hi @colinpetruno, awesome, thanks for reporting these. I'll try to get issue 2 resolved over the next few days or this weekend at the latest.

donaldpiret avatar Jun 02 '15 06:06 donaldpiret

:+1: can you delegate_all from relation_decorator? I would imagine yes, and perhaps it is implicit, similar to SimpleDelegator's method missing I imagine, right? Otherwise what's the point of distinguishing it as a relation?

saneshark avatar Jun 02 '15 22:06 saneshark

README documentation needed before merge?

saneshark avatar Jun 02 '15 22:06 saneshark

Hi @saneshark, not sure I understand your question regarding the delegate_all. If your decorator implements delegate_all, the decorated model objects returned by accessing a member of the relation will respect this as well. The point of distinguishing it as a relations rather than turning it into a collection is to enable further chaining of scopes down the line, so your .decorate call can be called at any time, not necessarily at the end. It also supports pagination out of the box by delegating the pagination methods to the relation directly and handling the decoration of the returned model instances.

For example you can now do:

User.decorate.page(2).per(1).first and this will return a decorated user instance.

I'm happy to write up documentation for this if there is a real intention of getting this branch merged. Let me know if this is the case and I'll write up some proper docs.

donaldpiret avatar Jun 06 '15 06:06 donaldpiret