-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Support automatic use of decorators #1117
Conversation
Any word on getting this merged in? Would love to see this as part of AA. |
I've rebased this back to master to ensure a clean merge. |
@amiel Thanks for rebasing it. It does not pass on Travis-CI. http://travis-ci.org/#!/gregbell/active_admin/builds/1234486 |
:( It looks like I'm using some ruby 1.9 syntax, which is why it broken in |
Ok, it looks like the I've removed the ruby 1.9 specific syntax and test pass for me in
|
Also I rebased on master again |
--- ## Background See discussion at #1079. Includes brand new shiny specs for `ActiveAdmin::Views::Pages::Index` and `ActiveAdmin::Views::Pages::Show`. All of this is consistant with the jcasimir/draper api, but does not specifically require draper. It is also fully backwards compatible for folks that do not use decorators. ## Current functionality ### index If `#{active_admin_config.resource_class}Decorator` is defined, the view will get the collection returned by `#{active_admin_config.resource_class}Decorator.decorate(collection)` Because of the pagination and scoping in ActiveAdmin, this requires that `#decorate` return a collection proxy and not just an array of decorators. This is the case in jcasimir/draper starting at v0.9.3. A different decorator class can be passed with the decorator option. Examples ```ruby index decorator: AwesomeDecorator do; end index decorator: nil; end # To disable the use of decorators ``` ### Show The show version works a little differently. If the resource object `responds_to?(:decorator)` then that is what will be used. It would be possible to configure the decorator class the same way as `Index` for consistency, but for now you can override `#decorator` on the model.
Hi @amiel, Thanks for the pull request, this is an awesome feature. Definitely something that I want to see merged in. The issue with the current implementation (as I see it) is that the view components are building the decorator objects. It should not be the responsibility of the view to know about decorators, rather it should just receive the decorated resource or collection. To do this, we should override the #collection and #resource methods that are provided by the Inherited Resources. There is reference code in a ticket on the Draper project: https://github.com/jcasimir/draper/issues/99 Then, we could add a configuration to resource like so: ActiveAdmin.register Post do
decorate_with PostDecorator
end Is this a refactoring you have some time to take on? |
Also, the above example, for the collection it should use: PostDecorator.decorate(collection) And for a single instance it should use: PostDecorator.new(single_instance) This will keep the API simple and will allow users to create decorators that don't depend on Draper. |
@gregbell thanks for the feedback. I'll try to make the changes you've requested during my next available open-source-time. |
@gregbell What would be the preferred place to implement It's fine if you don't have a preference, I'll find somewhere's to put it. |
To add a method to the DSL, simple add an instance method to Basically |
Thanks, that's really helpful. |
Not a problem. Let me know if you have any other questions. |
Hmm, I'm a little confused because the travis-ci failures have nothing to do with what I'm changing, and all the tests pass locally. Travisbot tells me it's merged, but when I do the same merge, and run the tests again, they all still pass. I'm gonna keep on truckin' and hope this issue might be solved in a future rebase... |
@gregbell -- I think I'm getting pretty close here. Just a couple of notes while I'm thinking about them:
Anyway, that's my status. BTW, as I start to wrap this up, what's your opinion on git history with the change of direction. Do you want the original history of this feature? or would you like me to rebase to remove the process of writing the original implementation? |
This last failure is baffling me. |
Thanks @amiel! This is really coming along.
Thanks for your work on this! This is going to be a very useful feature. In another couple of apps, I'm already wishing this was in place to use :) |
@gregbell Thanks. As I said before, I'll try to get working on this again next week. For now, a couple of thoughts.
|
@gregbell Ok, I've added a cucumber test, cleaned up a few files, and squashed all this stuff into one commit. I've tested this in a couple of apps I've written for work that use ActiveAdmin and Draper. My next step is to write documentation. My first thought was to write in the README so that my changes would be part of the pull-request, but after reading through what's there, it doesn't really make sense to write there. Where should I write documentation? Should I start writing in the wiki? Should I add a page to the docs directory? |
I merged in the pull request to Arbre (activeadmin/arbre#2). I'll release a new version and bump the version in AA shortly. |
Sweet. Thanks guys, I'm looking forward to this being a part of Active Admin. Travis is still failing only for The failure is:
which I completely don't understand. All of the other scenarios pass so I'm not sure how to deal with that. Is it possible to run the tests again? Maybe it was a fluke in the universe? Thoughts? |
ok guys, I've rebased again to try to keep up. What's weird is that I'm getting failing tests locally that pass on travis-ci.
which is strange since the Post model has a title and it works in every other environment. |
Indeed, that is an odd test failure... |
@jpmckinney Any ideas? do you want me to rebase again? |
Worth a try! |
Hmmm, it looks like something changed in the rebase. I'm going to have to look at this tomorrow. |
I'm very interested in this request. Can I help? |
@dapi This feature is pretty much finished. There are still two things left to do:
Thanks for your interest, I'm hopeful that we can get this merged in to master soon. |
ok. I get it |
@dapi Please let me know if you find any issues with your testing. |
It look's like a bug in rails_apps_composer with rvm usages. Fixing.. |
@dapi Wow, thanks for looking into this. I look forward to seeing your changes. |
@dapi Wow, thanks so much for figuring this out, I was baffled. @jpmckinney @gregbell @pcreux Would you like me to rebase this on master again, or can you take it from here? |
Cool, I'll close this issue then, and review #1647. |
I'm trying to find out how to make use of this functionality without using draper, as I'm already using non-draper decorators. I found this page on the wiki, which still says this functionality is a work in progress: https://github.com/gregbell/active_admin/wiki/Implement-a-custom-decorator Anyone know how to do this? |
I believe all the code is implemented in this file: https://github.com/gregbell/active_admin/blob/master/lib/active_admin/resource_controller/decorators.rb The ActiveAdmin.register Post do
decorate_with PostDecorator
end As long as your decorator responds to |
@macfanatic The only tricky part is making sure collections are handled correctly. For example, it must handle being filtered with scopes, pagination, filters, etc., and still return the decorated collection. The integration tests use this: |
Thanks guys. I got as far as trying to implement #decorate_collection before I posed my question, but I could not figure out how to do it, as a I kept bumping into new errors. Thanks for the hint about the code in the integration tests, @amiel, I'll see if I can make that work! |
Background
See discussion at #1079.
All of this is consistant with the jcasimir/draper api, but does not specifically require draper.
It is also fully backwards compatible for folks that do not use decorators.
Notes
Because of the pagination and scoping in ActiveAdmin, this requires that
#decorate
return acollection proxy and not just an array of decorators. This is the case in jcasimir/draper starting at v0.9.3.