Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Draper with InheritedResources or ActiveAdmin #54

Closed
jcarlson opened this issue Oct 19, 2011 · 5 comments
Closed

Using Draper with InheritedResources or ActiveAdmin #54

jcarlson opened this issue Oct 19, 2011 · 5 comments

Comments

@jcarlson
Copy link

I would love to use Draper with a back-end helper like ActiveAdmin... the trouble is that InheritedResources and ActiveAdmin depend a lot on scopes, and thus the ActiveRecord::Relation class.

For example:

>> posts = Post.published   # a scope on the Post model
=> [...some Post instances]
>> posts.class
=> ActiveRecord::Relation
>> posts = PostDecorator.decorate(posts)
=> [...some PostDecorator instances]
>> posts.class
=> Array

After decorating the scoped array of Posts, we lose access to the chainable methods that ActiveAdmin is looking for...

Any thoughts?

@jcasimir
Copy link
Member

Can you try it with the edge code in the repo? I'm thinking that the just-merged Decorated Collection Proxy will help with this issue.

@jcasimir
Copy link
Member

Yes! I just did some tests that indicate this is solved in 0.9.2. I created an ActiveRecord::Relation, then decorated it, then was able to call further AREL methods modifying the collection.

Please comment/reopen if you're still running into issues.

@jcarlson
Copy link
Author

Well this certainly helps.

What this does not do, however, is decorate items in the collection when chaining methods on the collection:

>> posts = PostDecorator.decorate Post.scoped
  Post Load (0.1ms)  SELECT "posts".* FROM "posts" 
=> #<Draper::DecoratedEnumerableProxy:0x1057a44e0 @klass=PostDecorator, @context={}, @wrapped_collection=[#<Post id: 1, title: "My first post", content: "# This is an H1\r\n\r\n## This is an H2\r\n\r\n### This is ...", publish_at: "2011-10-22 18:07:00", slug: "", created_at: "2011-10-18 23:05:54", updated_at: "2011-10-22 19:00:08", public: true>]>
>> posts.where(:public => true).first
  Post Load (0.2ms)  SELECT "posts".* FROM "posts" WHERE "posts"."public" = 't' LIMIT 1
=> #<Post id: 1, title: "My first post", content: "# This is an H1\r\n\r\n## This is an H2\r\n\r\n### This is ...", publish_at: "2011-10-22 18:07:00", slug: "", created_at: "2011-10-18 23:05:54", updated_at: "2011-10-22 19:00:08", public: true>

Notice that the Post returned from the call to #first is not decorated!

However, I'm satisfied with what I've got working right now, and I think it's fair to call this little nuance a totally separate issue...

Thanks!

@jcasimir
Copy link
Member

Hmmm, now that is interesting! I don't, without more research, know much about how the ARel scopes are added together. If you look into it at all, please report back.

@amiel
Copy link
Contributor

amiel commented May 21, 2012

FYI: I'm working on integrating draper support in ActiveAdmin here: activeadmin/activeadmin#1117

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants