administrate icon indicating copy to clipboard operation
administrate copied to clipboard

WIP: Action-specific models

Open pablobm opened this issue 4 years ago • 6 comments

This is an experiment to see what would be the simplest way to implement https://github.com/thoughtbot/administrate/issues/278 The idea here is to use database views to render index pages, allowing us to display and sort records in any way we please.

For the moment, I managed to make it work while only having to change Administrate's ApplicationController. In this change, new hooks are introduced to allow controller actions to use specific resource classes. Therefore the index action can use a model based off a view, while other actions keep using the default model based off a table.

As a result, this code can do three things:

  1. Order by a field in a belongs_to association. In the products index, the title of the product meta tag is shown, and it's possible to sort by it.
  2. Order by the result of an aggregate function. In the customers index, it's possible to sort customers by lifetime value.
  3. Eliminate the N+1 queries provoked by aggregate functions. In the customers index, the lifetime value is rendered from the result of the view. This is a bit more hacky as it involves a new definition for lifetime_value.

This is not quite there yet. For one, I managed to make index actions to work this way, but not show actions. There are new things that break and I need to look into.

Making the show action work would allow us to remove the lifetime_value methods from Customer and Customer::Index. The first is redundant with the view, and the second is only there to work around the first and avoid N+1 queries.

I should also add some tests specific to the features listed above.

To do:

  • [ ] Add documentation
  • [ ] How does this play with authorization? If an action uses a model other than the "base" model, the "base" authorization rules won't apply. At the very least, this should be documented.

pablobm avatar Dec 23 '19 15:12 pablobm

@nickcharlton, would you be able to have a look at this and see if it's worthwhile, before I spend more time on it? I want to make sure I'm not deviating from Administrate's spirit, or some similarly fundamental problem.

pablobm avatar Jan 16 '20 12:01 pablobm

@pablobm, I think this sounds like a good approach.

Do you think you'd be able to add some draft documentation next so we can see how you might use this? (just a comment is fine, I think…)

nickcharlton avatar Apr 02 '20 10:04 nickcharlton

@nickcharlton I can help out with this, where do you suggest we put docs for this feature?

Was looking at /docs but wasn't sure if you'd like to add a new .md file or mention it elsewhere.

Edit: maybe another option would be putting a section in customizing_dashboards.md

cabe56 avatar Sep 05 '20 22:09 cabe56

Thank you for your interest @cabe56. I had parked this PR while I worked on something else.

I had envisioned two ways of documenting this feature:

  • Document public methods of Administrate::ApplicationController with YARD, to make it easy for developers to find out what interfaces are available.

  • A new section in the Administrate documentation in a "how to" style. Here we could explain how to make Administrate work in the various use cases and configurations that we get asked about. Devise do this at https://github.com/heartcombo/devise/wiki/How-Tos.

Now, the problem is that Administrate doesn't yet have either! We haven't started documenting methods, and we don't have a "how to" section in the docs. I was thinking of doing that first so that this PR can get a place where to include its documentation.

From your comment, I understand that you have an interest in this feature? If you could provide help, like a review, testing in your own projects, or a how-to document, that would be great.

pablobm avatar Sep 12 '20 15:09 pablobm

@pablobm great ideas, I think that would help making Administrate more accessible. As you mention, these are not used in the project and I think incorporating them could be a barrier to getting this awesome feature shipped.

Focused (short-term) on how to get your change through this review, I re-read @nickcharlton's suggestion and I think he meant just adding a quick snippet/comment/tl;dr on how to use this feature.

I'll post back here once I try this PR out on my project. Thanks for taking the time to build this!

cabe56 avatar Sep 28 '20 01:09 cabe56

I don't remember why I closed this...? Re-opening for now, although I'm still a bit far from being able to work on it.

pablobm avatar Dec 16 '21 09:12 pablobm